diff --git a/src/main/java/com/lpvs/exception/ErrorResponse.java b/src/main/java/com/lpvs/exception/ErrorResponse.java index 9ebd5b33..1119244e 100644 --- a/src/main/java/com/lpvs/exception/ErrorResponse.java +++ b/src/main/java/com/lpvs/exception/ErrorResponse.java @@ -7,8 +7,11 @@ package com.lpvs.exception; +import lombok.Getter; + import java.time.LocalDateTime; +@Getter public class ErrorResponse { private LocalDateTime timestamp = LocalDateTime.now(); @@ -22,20 +25,4 @@ public ErrorResponse(String message, String code, int status) { this.code = code; this.status = status; } - - public LocalDateTime getTimestamp() { - return timestamp; - } - - public String getMessage() { - return message; - } - - public String getCode() { - return code; - } - - public int getStatus() { - return status; - } } diff --git a/src/main/java/com/lpvs/exception/LoginFailedException.java b/src/main/java/com/lpvs/exception/LoginFailedException.java index 67ce6071..9a44f2a4 100644 --- a/src/main/java/com/lpvs/exception/LoginFailedException.java +++ b/src/main/java/com/lpvs/exception/LoginFailedException.java @@ -4,8 +4,6 @@ * Use of this source code is governed by a MIT license that can be * found in the LICENSE file. */ - - package com.lpvs.exception; public class LoginFailedException extends RuntimeException { diff --git a/src/test/java/com/lpvs/exception/ErrorResponseTest.java b/src/test/java/com/lpvs/exception/ErrorResponseTest.java new file mode 100644 index 00000000..829c3ddd --- /dev/null +++ b/src/test/java/com/lpvs/exception/ErrorResponseTest.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2023, Samsung Electronics Co., Ltd. All rights reserved. + * + * Use of this source code is governed by a MIT license that can be + * found in the LICENSE file. + */ +package com.lpvs.exception; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; + +import static org.junit.jupiter.api.Assertions.*; + +public class ErrorResponseTest { + + @Test + public void testErrorResponseConstructor() { + String message = "Test Message"; + String code = "TEST_CODE"; + int status = 404; + ErrorResponse errorResponse = new ErrorResponse(message, code, status); + + // Check if the properties are set correctly + assertEquals(message, errorResponse.getMessage()); + assertEquals(code, errorResponse.getCode()); + assertEquals(status, errorResponse.getStatus()); + + // Check if the timestamp is not null + assertNotNull(errorResponse.getTimestamp()); + } + + @Test + public void testErrorResponseTimestamp() { + String message = "Another Test Message"; + String code = "ANOTHER_CODE"; + int status = 500; + ErrorResponse errorResponse = new ErrorResponse(message, code, status); + + // Check if the timestamp is not null + assertNotNull(errorResponse.getTimestamp()); + + // Check if the timestamp is close to the current time (within a few milliseconds) + LocalDateTime currentTime = LocalDateTime.now(); + LocalDateTime timestamp = errorResponse.getTimestamp(); + assertTrue(currentTime.isAfter(timestamp) || currentTime.isEqual(timestamp)); + } +} diff --git a/src/test/java/com/lpvs/exception/LoginFailedExceptionTest.java b/src/test/java/com/lpvs/exception/LoginFailedExceptionTest.java new file mode 100644 index 00000000..fa30e6af --- /dev/null +++ b/src/test/java/com/lpvs/exception/LoginFailedExceptionTest.java @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2023, Samsung Electronics Co., Ltd. All rights reserved. + * + * Use of this source code is governed by a MIT license that can be + * found in the LICENSE file. + */ +package com.lpvs.exception; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class LoginFailedExceptionTest { + + @Test + public void testLoginFailedExceptionConstructor() { + String message = "Test Message"; + LoginFailedException exception = new LoginFailedException(message); + + // Check if the exception message is set correctly + assertEquals(message, exception.getMessage()); + } + + @Test + public void testLoginFailedExceptionDefaultMessage() { + LoginFailedException exception = new LoginFailedException(null); + + // Check if the exception message is set to the default value + assertEquals(null, exception.getMessage()); + } +} diff --git a/src/test/java/com/lpvs/exception/PageControllerAdviceTest.java b/src/test/java/com/lpvs/exception/PageControllerAdviceTest.java new file mode 100644 index 00000000..3f5458db --- /dev/null +++ b/src/test/java/com/lpvs/exception/PageControllerAdviceTest.java @@ -0,0 +1,82 @@ +/** + * Copyright (c) 2023, Samsung Electronics Co., Ltd. All rights reserved. + * + * Use of this source code is governed by a MIT license that can be + * found in the LICENSE file. + */ +package com.lpvs.exception; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.time.LocalDateTime; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +public class PageControllerAdviceTest { + + @InjectMocks + private PageControllerAdvice pageControllerAdvice; + + @Mock + private ErrorResponse errorResponse; + + @BeforeEach + public void setup() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testLoginFailedHandle() { + String message = "Login failed message"; + LoginFailedException exception = new LoginFailedException(message); + when(errorResponse.getMessage()).thenReturn(message); + ResponseEntity responseEntity = pageControllerAdvice.loginFailedHandle(exception); + + // Check if the response has the correct status, message, and code + assertEquals(HttpStatus.UNAUTHORIZED, responseEntity.getStatusCode()); + assertEquals(message, responseEntity.getBody().getMessage()); + assertEquals(HttpStatus.UNAUTHORIZED.name(), responseEntity.getBody().getCode()); + assertEquals(HttpStatus.UNAUTHORIZED.value(), responseEntity.getBody().getStatus()); + assertNotNull(responseEntity.getBody().getTimestamp()); + assertTrue(responseEntity.getBody().getTimestamp().isBefore(LocalDateTime.now().plusSeconds(5))); // Adjust the time window as needed + } + + @Test + public void testWrongAccessHandle() { + String message = "Access denied message"; + WrongAccessException exception = new WrongAccessException(message); + when(errorResponse.getMessage()).thenReturn(message); + ResponseEntity responseEntity = pageControllerAdvice.wrongAccessHandle(exception); + + // Check if the response has the correct status, message, and code + assertEquals(HttpStatus.FORBIDDEN, responseEntity.getStatusCode()); + assertEquals(message, responseEntity.getBody().getMessage()); + assertEquals(HttpStatus.FORBIDDEN.name(), responseEntity.getBody().getCode()); + assertEquals(HttpStatus.FORBIDDEN.value(), responseEntity.getBody().getStatus()); + assertNotNull(responseEntity.getBody().getTimestamp()); + assertTrue(responseEntity.getBody().getTimestamp().isBefore(LocalDateTime.now().plusSeconds(5))); // Adjust the time window as needed + } + + @Test + public void testHandleSQLException() { + String message = "Conflict message"; + IllegalArgumentException exception = new IllegalArgumentException(message); + when(errorResponse.getMessage()).thenReturn(message); + ResponseEntity responseEntity = pageControllerAdvice.handleSQLException(exception); + + // Check if the response has the correct status, message, and code + assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode()); + assertEquals(message, responseEntity.getBody().getMessage()); + assertEquals(HttpStatus.CONFLICT.name(), responseEntity.getBody().getCode()); + assertEquals(HttpStatus.CONFLICT.value(), responseEntity.getBody().getStatus()); + assertNotNull(responseEntity.getBody().getTimestamp()); + assertTrue(responseEntity.getBody().getTimestamp().isBefore(LocalDateTime.now().plusSeconds(5))); // Adjust the time window as needed + } +} diff --git a/src/test/java/com/lpvs/exception/WrongAccessExceptionTest.java b/src/test/java/com/lpvs/exception/WrongAccessExceptionTest.java new file mode 100644 index 00000000..0efbbb94 --- /dev/null +++ b/src/test/java/com/lpvs/exception/WrongAccessExceptionTest.java @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2023, Samsung Electronics Co., Ltd. All rights reserved. + * + * Use of this source code is governed by a MIT license that can be + * found in the LICENSE file. + */ +package com.lpvs.exception; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class WrongAccessExceptionTest { + + @Test + public void testWrongAccessExceptionConstructor() { + String message = "Test Message"; + WrongAccessException exception = new WrongAccessException(message); + + // Check if the exception message is set correctly + assertEquals(message, exception.getMessage()); + } + + @Test + public void testWrongAccessExceptionDefaultMessage() { + WrongAccessException exception = new WrongAccessException(null); + + // Check if the exception message is set to the default value + assertEquals(null, exception.getMessage()); + } +}