-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add unit tests for
exception
classes (#246)
feat: Add unit tests for exception classes Signed-off-by: Oleg Kopysov <[email protected]>
- Loading branch information
Showing
6 changed files
with
193 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/java/com/lpvs/exception/LoginFailedExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/test/java/com/lpvs/exception/PageControllerAdviceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ErrorResponse> 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<ErrorResponse> 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<ErrorResponse> 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 | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/java/com/lpvs/exception/WrongAccessExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |