diff options
author | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-17 15:34:21 -0400 |
---|---|---|
committer | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-17 15:34:21 -0400 |
commit | 0333def352ed9e9159b391ecbca60ffe358cd793 (patch) | |
tree | 5ab15f9fb8dcb8b30f557aaddc0a4b5adaaef053 | |
parent | 5d79657cc37d33133c73621d290efeac566bf1b7 (diff) | |
download | JellySolutions-0333def352ed9e9159b391ecbca60ffe358cd793.tar.gz JellySolutions-0333def352ed9e9159b391ecbca60ffe358cd793.tar.bz2 JellySolutions-0333def352ed9e9159b391ecbca60ffe358cd793.zip |
Created authController tests
-rw-r--r-- | ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java | 114 |
1 files changed, 114 insertions, 0 deletions
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<String, String> 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<String> 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<String> 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<String> 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<String> 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<String> response = authController.login(authMap); + + // Analyze + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } +} |