Skip to content

Commit

Permalink
feat: Add unit tests for exception classes (#246)
Browse files Browse the repository at this point in the history
feat: Add unit tests for exception classes

Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov authored Oct 16, 2023
1 parent e8a1f34 commit dc59815
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 18 deletions.
19 changes: 3 additions & 16 deletions src/main/java/com/lpvs/exception/ErrorResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

package com.lpvs.exception;

import lombok.Getter;

import java.time.LocalDateTime;

@Getter
public class ErrorResponse {

private LocalDateTime timestamp = LocalDateTime.now();
Expand All @@ -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;
}
}
2 changes: 0 additions & 2 deletions src/main/java/com/lpvs/exception/LoginFailedException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/lpvs/exception/ErrorResponseTest.java
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 src/test/java/com/lpvs/exception/LoginFailedExceptionTest.java
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 src/test/java/com/lpvs/exception/PageControllerAdviceTest.java
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 src/test/java/com/lpvs/exception/WrongAccessExceptionTest.java
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());
}
}

0 comments on commit dc59815

Please sign in to comment.