forked from Sage-Bionetworks/sage-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(openchallenges): improve organization service exception unit tes…
…t coverage to 100% (Sage-Bionetworks#2348) * improve unit test coverage for exception to 100% * update unit test formatting * update format * Update apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/GlobalExceptionHandlerTest.java Co-authored-by: Thomas Schaffter <[email protected]> * add changes to condense tests * fix variable name * add back unit test * format * condense unit tests * update format * update tests * fix bad request * update test name * update test name * update tests * update test * update unit test * format update * add changes to unit tests * formatting * push changes again * update changes * use .isNull() * assert Response Entity for both exception types * fix test comments and name * remove whitespace * update assertions of the exception * update comments * update test name * fix format * changed message back to detail --------- Co-authored-by: Thomas Schaffter <[email protected]>
- Loading branch information
1 parent
9e037ca
commit 3355901
Showing
6 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...agebionetworks/openchallenges/organization/service/exception/BadRequestExceptionTest.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,23 @@ | ||
package org.sagebionetworks.openchallenges.organization.service.exception; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class BadRequestExceptionTest { | ||
|
||
@Test | ||
public void BadRequestException_ShouldReturnExceptionConstructors_WhenKeyIsPassed() { | ||
// Set up the input detail | ||
String detail = "Errordetail"; | ||
|
||
// Create an instance of BadRequestException | ||
BadRequestException exception = new BadRequestException(detail); | ||
|
||
// Verify the properties of the exception | ||
assertThat(exception.getStatus()).isEqualTo(ErrorConstants.BAD_REQUEST.getStatus()); | ||
assertThat(exception.getDetail()).isEqualTo(detail); | ||
assertThat(exception.getType()).isEqualTo(ErrorConstants.BAD_REQUEST.getType()); | ||
assertThat(exception.getTitle()).isEqualTo(ErrorConstants.BAD_REQUEST.getTitle()); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...bionetworks/openchallenges/organization/service/exception/GlobalExceptionHandlerTest.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,60 @@ | ||
package org.sagebionetworks.openchallenges.organization.service.exception; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Locale; | ||
import org.junit.jupiter.api.Test; | ||
import org.sagebionetworks.openchallenges.organization.service.model.dto.BasicErrorDto; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
public class GlobalExceptionHandlerTest { | ||
|
||
@Test | ||
public void | ||
GlobalExceptionHandler_ShouldReturnInternalServerErrorStatusCode_WhenExceptionAndLocalePassed() { | ||
|
||
// Create a sample Exception | ||
Exception exception = new Exception("An exception occurred"); | ||
Locale locale = Locale.getDefault(); | ||
|
||
// Create the GlobalExceptionHandler instance | ||
GlobalExceptionHandler exceptionHandler = new GlobalExceptionHandler(); | ||
|
||
// Call handleException | ||
ResponseEntity<BasicErrorDto> responseEntity = | ||
exceptionHandler.handleException(exception, locale); | ||
|
||
// Verify the Response Entity matches the Internal Server Error Code | ||
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
@Test | ||
public void | ||
GlobalExceptionHandler_ShouldReturnStatusCodeOfSimpleChallengeGlobalExceptionResponseEntityObject_WhenArgsPassedToSimpleChallengeGlobalException() { | ||
|
||
// Create a sample Exception | ||
Locale locale = Locale.getDefault(); | ||
|
||
// Define the exception details | ||
String type = "ExceptionType"; | ||
String title = "Exception Title"; | ||
HttpStatus status = HttpStatus.BAD_REQUEST; | ||
String detail = "Exception detail message"; | ||
|
||
// Create newSimpleChallengeGlobalException instance | ||
SimpleChallengeGlobalException exception = | ||
new SimpleChallengeGlobalException(type, title, status, detail); | ||
|
||
// Create the GlobalExceptionHandler instance | ||
GlobalExceptionHandler exceptionHandler = new GlobalExceptionHandler(); | ||
|
||
// Call handleGlobalException | ||
ResponseEntity<BasicErrorDto> responseEntity = | ||
exceptionHandler.handleGlobalException(exception, locale); | ||
|
||
// confirm that the status code that was set was retrieved and applied to the Response Entity | ||
// object | ||
assertThat(responseEntity.getStatusCode()).isEqualTo(exception.getStatus()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...works/openchallenges/organization/service/exception/InvalidOrganizationExceptionTest.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,23 @@ | ||
package org.sagebionetworks.openchallenges.organization.service.exception; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class InvalidOrganizationExceptionTest { | ||
|
||
@Test | ||
public void InvalidOrganizationException_ShouldReturnExceptionObject_WhenDetailKeyIsPassed() { | ||
// Set up the input detail | ||
String detail = "Errordetail"; | ||
|
||
// Create an instance of InvalidOrganizationException | ||
InvalidOrganizationException exception = new InvalidOrganizationException(detail); | ||
|
||
// Verify the properties of the exception | ||
assertThat(exception.getDetail()).isEqualTo(detail); | ||
assertThat(exception.getType()).isEqualTo(ErrorConstants.ENTITY_NOT_FOUND.getType()); | ||
assertThat(exception.getStatus()).isEqualTo(ErrorConstants.ENTITY_NOT_FOUND.getStatus()); | ||
assertThat(exception.getTitle()).isEqualTo(ErrorConstants.ENTITY_NOT_FOUND.getTitle()); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...openchallenges/organization/service/exception/OrganizationAlreadyExistsExceptionTest.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,20 @@ | ||
package org.sagebionetworks.openchallenges.organization.service.exception; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class OrganizationAlreadyExistsExceptionTest { | ||
|
||
@Test | ||
public void OrganizationAlreadyExistsException_ShouldReturnDetail_WhenDetailKeyIsPassed() { | ||
// Set up the input detail | ||
String detail = "Errordetail"; | ||
|
||
// Create an instance of OrganizationAlreadyExistsException | ||
OrganizationAlreadyExistsException exception = new OrganizationAlreadyExistsException(detail); | ||
|
||
// Verify the properties of the exception | ||
assertThat(exception.getDetail()).isEqualTo(detail); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...orks/openchallenges/organization/service/exception/OrganizationNotFoundExceptionTest.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,20 @@ | ||
package org.sagebionetworks.openchallenges.organization.service.exception; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class OrganizationNotFoundExceptionTest { | ||
|
||
@Test | ||
public void OrganizationNotFoundException_ShouldReturnDetail_WhenDetailKeyIsPassed() { | ||
// Set up the input detail | ||
String detail = "Errordetail"; | ||
|
||
// Create an instance of OrganizationNotFoundException | ||
OrganizationNotFoundException exception = new OrganizationNotFoundException(detail); | ||
|
||
// Verify the properties of the exception | ||
assertThat(exception.getDetail()).isEqualTo(detail); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...rks/openchallenges/organization/service/exception/SimpleChallengeGlobalExceptionTest.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,77 @@ | ||
package org.sagebionetworks.openchallenges.organization.service.exception; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.HttpStatus; | ||
|
||
public class SimpleChallengeGlobalExceptionTest { | ||
|
||
@Test | ||
public void SimpleChallengeGlobalException_ShouldReturnMessage_WhenMessageKeyIsPassed() { | ||
// Create an instance of SimpleChallengeGlobalException using the constructor with details | ||
String message = "Something went wrong"; | ||
SimpleChallengeGlobalException exception = new SimpleChallengeGlobalException(message); | ||
|
||
// Verify the exception details | ||
assertThat(exception.getMessage()).isEqualTo(message); | ||
} | ||
|
||
@Test | ||
public void SimpleChallengeGlobalException_ShouldReturnStatusTypeTitleDetail_WhenArgsPassed() { | ||
// Define the exception details | ||
String type = "ExceptionType"; | ||
String title = "Exception Title"; | ||
HttpStatus status = HttpStatus.BAD_REQUEST; | ||
String detail = "Exception detail message"; | ||
|
||
// Create an instance of SimpleChallengeGlobalException using the all-args constructor | ||
SimpleChallengeGlobalException exception = | ||
new SimpleChallengeGlobalException(type, title, status, detail); | ||
|
||
// Verify the exception details | ||
assertThat(exception.getStatus()).isEqualTo(status); | ||
assertThat(exception.getTitle()).isEqualTo(title); | ||
assertThat(exception.getType()).isEqualTo(type); | ||
assertThat(exception.getDetail()).isEqualTo(detail); | ||
} | ||
|
||
@Test | ||
public void | ||
SimpleChallengeGlobalException_ShouldReturnNullArgs_WhenCalledUsingNoArgsConstructor() { | ||
|
||
// Create an instance of SimpleChallengeGlobalException using the no-args constructor | ||
SimpleChallengeGlobalException exception = new SimpleChallengeGlobalException(); | ||
|
||
// confirm that not passing args leads to null arguments | ||
assertThat(exception.getTitle()).isNull(); | ||
assertThat(exception.getType()).isNull(); | ||
assertThat(exception.getStatus()).isNull(); | ||
assertThat(exception.getDetail()).isNull(); | ||
} | ||
|
||
@Test | ||
public void | ||
SimpleChallengeGlobalException_ShouldSetArgs_WhenNoArgsConstructorExceptionPassedArgsKeys() { | ||
// Define the exception details | ||
String type = "ExceptionType"; | ||
String title = "Exception Title"; | ||
HttpStatus status = HttpStatus.BAD_REQUEST; | ||
String detail = "Exception detail message"; | ||
|
||
// Create an instance of SimpleChallengeGlobalException using the no-args constructor | ||
SimpleChallengeGlobalException exception = new SimpleChallengeGlobalException(); | ||
|
||
// Create args for the exception | ||
exception.setType(type); | ||
exception.setTitle(title); | ||
exception.setStatus(status); | ||
exception.setDetail(detail); | ||
|
||
// confirm that the args were passed | ||
assertThat(exception.getType()).isEqualTo(type); | ||
assertThat(exception.getTitle()).isEqualTo(title); | ||
assertThat(exception.getStatus()).isEqualTo(status); | ||
assertThat(exception.getDetail()).isEqualTo(detail); | ||
} | ||
} |