From 335590136b67e529b6405bc971c0771ae8338f36 Mon Sep 17 00:00:00 2001 From: mdsage1 <122999770+mdsage1@users.noreply.github.com> Date: Mon, 4 Dec 2023 09:08:04 -0800 Subject: [PATCH] test(openchallenges): improve organization service exception unit test coverage to 100% (#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 * 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 --- .../exception/BadRequestExceptionTest.java | 23 ++++++ .../exception/GlobalExceptionHandlerTest.java | 60 +++++++++++++++ .../InvalidOrganizationExceptionTest.java | 23 ++++++ ...rganizationAlreadyExistsExceptionTest.java | 20 +++++ .../OrganizationNotFoundExceptionTest.java | 20 +++++ .../SimpleChallengeGlobalExceptionTest.java | 77 +++++++++++++++++++ 6 files changed, 223 insertions(+) create mode 100644 apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/BadRequestExceptionTest.java create mode 100644 apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/GlobalExceptionHandlerTest.java create mode 100644 apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/InvalidOrganizationExceptionTest.java create mode 100644 apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/OrganizationAlreadyExistsExceptionTest.java create mode 100644 apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/OrganizationNotFoundExceptionTest.java create mode 100644 apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/SimpleChallengeGlobalExceptionTest.java diff --git a/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/BadRequestExceptionTest.java b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/BadRequestExceptionTest.java new file mode 100644 index 0000000000..885d3edddb --- /dev/null +++ b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/BadRequestExceptionTest.java @@ -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()); + } +} diff --git a/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/GlobalExceptionHandlerTest.java b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/GlobalExceptionHandlerTest.java new file mode 100644 index 0000000000..7357f98429 --- /dev/null +++ b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/GlobalExceptionHandlerTest.java @@ -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 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 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()); + } +} diff --git a/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/InvalidOrganizationExceptionTest.java b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/InvalidOrganizationExceptionTest.java new file mode 100644 index 0000000000..f358586181 --- /dev/null +++ b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/InvalidOrganizationExceptionTest.java @@ -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()); + } +} diff --git a/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/OrganizationAlreadyExistsExceptionTest.java b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/OrganizationAlreadyExistsExceptionTest.java new file mode 100644 index 0000000000..56e11ca0a7 --- /dev/null +++ b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/OrganizationAlreadyExistsExceptionTest.java @@ -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); + } +} diff --git a/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/OrganizationNotFoundExceptionTest.java b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/OrganizationNotFoundExceptionTest.java new file mode 100644 index 0000000000..e9a20ef375 --- /dev/null +++ b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/OrganizationNotFoundExceptionTest.java @@ -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); + } +} diff --git a/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/SimpleChallengeGlobalExceptionTest.java b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/SimpleChallengeGlobalExceptionTest.java new file mode 100644 index 0000000000..623c1432d5 --- /dev/null +++ b/apps/openchallenges/organization-service/src/test/java/org/sagebionetworks/openchallenges/organization/service/exception/SimpleChallengeGlobalExceptionTest.java @@ -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); + } +}