From 0333def352ed9e9159b391ecbca60ffe358cd793 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 17 Mar 2025 15:34:21 -0400 Subject: Created authController tests --- .../ufundapi/controller/AuthControllerTest.java | 114 +++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java') diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java new file mode 100644 index 0000000..85af481 --- /dev/null +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java @@ -0,0 +1,114 @@ +package com.ufund.api.ufundapi.controller; + +import java.io.IOException; +import java.util.Map; +import static java.util.Map.entry; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.ufund.api.ufundapi.service.AuthService; + +@RestController +@RequestMapping("auth") +public class AuthControllerTest { + + private AuthController authController; + private AuthService mockAuthService; + private Map authMap; + + @BeforeEach + private void setupAuthController() { + mockAuthService = mock(AuthService.class); + authController = new AuthController(mockAuthService); + + authMap = Map.ofEntries( + entry("Bob", "123") + ); + } + + @Test + private void testLogin() throws IllegalAccessException, IOException { + // Setup + String key = "123"; + + // Mock + when(mockAuthService.login(any(), any())).thenReturn(key); + + // Invoke + ResponseEntity response = authController.login(authMap); + + // Analyze + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(key, response.getBody()); + } + + @Test + private void testLoginUnauthorized() throws IllegalAccessException, IOException { + // Setup + String key = "123"; + + // Mock + when(mockAuthService.login(any(), any())).thenThrow(IllegalAccessException.class); + + // Invoke + ResponseEntity response = authController.login(authMap); + + // Analyze + assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); + } + + @Test + private void testLoginIOException() throws IllegalAccessException, IOException { + // Setup + String key = "123"; + + // Mock + when(mockAuthService.login(any(), any())).thenThrow(IOException.class); + + // Invoke + ResponseEntity response = authController.login(authMap); + + // Analyze + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + + @Test + void testLogout() throws IllegalAccessException, IOException { + // Setup + String key = "123"; + + // Mock + when(mockAuthService.login(any(), any())).thenReturn(key); + + // Invoke + ResponseEntity response = authController.login(authMap); + + // Analyze + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(key, response.getBody()); + } + + @Test + void testLogoutIOException() throws IllegalAccessException, IOException { + // Setup + String key = "123"; + + // Mock + when(mockAuthService.login(any(), any())).thenThrow(IOException.class); + + // Invoke + ResponseEntity response = authController.login(authMap); + + // Analyze + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } +} -- cgit v1.2.3 From 4d9fe6c96f487d75a03e3a680cc80fa3f2ad5e4f Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 17 Mar 2025 15:53:44 -0400 Subject: Fixed broken tests --- .../ufundapi/controller/AuthControllerTest.java | 30 ++++++++-------------- 1 file changed, 10 insertions(+), 20 deletions(-) (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java') diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java index 85af481..3d4637d 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java @@ -8,16 +8,16 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.mockito.ArgumentMatchers.any; +import org.mockito.Mockito; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; import com.ufund.api.ufundapi.service.AuthService; -@RestController @RequestMapping("auth") public class AuthControllerTest { @@ -36,7 +36,7 @@ public class AuthControllerTest { } @Test - private void testLogin() throws IllegalAccessException, IOException { + public void testLogin() throws IllegalAccessException, IOException { // Setup String key = "123"; @@ -52,10 +52,7 @@ public class AuthControllerTest { } @Test - private void testLoginUnauthorized() throws IllegalAccessException, IOException { - // Setup - String key = "123"; - + public void testLoginUnauthorized() throws IllegalAccessException, IOException { // Mock when(mockAuthService.login(any(), any())).thenThrow(IllegalAccessException.class); @@ -67,10 +64,7 @@ public class AuthControllerTest { } @Test - private void testLoginIOException() throws IllegalAccessException, IOException { - // Setup - String key = "123"; - + public void testLoginIOException() throws IllegalAccessException, IOException { // Mock when(mockAuthService.login(any(), any())).thenThrow(IOException.class); @@ -82,31 +76,27 @@ public class AuthControllerTest { } @Test - void testLogout() throws IllegalAccessException, IOException { + public void testLogout() throws IllegalAccessException, IOException { // Setup String key = "123"; - // Mock - when(mockAuthService.login(any(), any())).thenReturn(key); - // Invoke - ResponseEntity response = authController.login(authMap); + ResponseEntity response = authController.logout(key); // Analyze assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(key, response.getBody()); } @Test - void testLogoutIOException() throws IllegalAccessException, IOException { + public void testLogoutIOException() throws IllegalAccessException, IOException { // Setup String key = "123"; // Mock - when(mockAuthService.login(any(), any())).thenThrow(IOException.class); + doThrow(new IOException()).when(mockAuthService).logout(key); // Invoke - ResponseEntity response = authController.login(authMap); + ResponseEntity response = authController.logout(key); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); -- cgit v1.2.3