diff --git a/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/BadRequestExceptionTest.java b/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/BadRequestExceptionTest.java new file mode 100644 index 0000000000..5b6740f8be --- /dev/null +++ b/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/BadRequestExceptionTest.java @@ -0,0 +1,44 @@ +package org.sagebionetworks.openchallenges.image.service.exception; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +public class BadRequestExceptionTest { + + @Test + public void BadRequestException_ShouldReturnType_WhenTypeKeyIsPassed() { + // 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.getType()).isEqualTo(ErrorConstants.BAD_REQUEST.getType()); + } + + @Test + public void BadRequestException_ShouldReturnStatus_WhenStatusKeyIsPassed() { + // 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()); + } + + @Test + public void BadRequestException_ShouldReturnDetail_WhenDetailKeyIsPassed() { + // 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.getDetail()).isEqualTo(detail); + } +} diff --git a/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/ControllerAdvisorTest.java b/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/ControllerAdvisorTest.java new file mode 100644 index 0000000000..f72594d556 --- /dev/null +++ b/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/ControllerAdvisorTest.java @@ -0,0 +1,48 @@ +package org.sagebionetworks.openchallenges.image.service.exception; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.sagebionetworks.openchallenges.image.service.model.dto.BasicErrorDto; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.BindException; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import org.springframework.web.context.request.WebRequest; + +public class ControllerAdvisorTest { + + @Test + public void HandleBindException_ShouldReturnStatusAndBody_WhenBothArePassed() { + // Create a sample BindException + BindException bindException = mock(BindException.class); + BindingResult bindingResult = mock(BindingResult.class); + FieldError fieldError = + new FieldError( + "objectName", "fieldName", "rejectedValue", false, null, null, "error message"); + List fieldErrors = new ArrayList<>(); + fieldErrors.add(fieldError); + + // Mock the behavior of the BindException and BindingResult + when(bindException.getBindingResult()).thenReturn(bindingResult); + when(bindingResult.getFieldErrors()).thenReturn(fieldErrors); + + // Create the ControllerAdvisor instance + ControllerAdvisor controllerAdvisor = new ControllerAdvisor(); + + // Call handleBindException + ResponseEntity responseEntity = + controllerAdvisor.handleBindException( + bindException, new HttpHeaders(), HttpStatus.BAD_REQUEST, mock(WebRequest.class)); + + // Verify the response + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + BasicErrorDto errorDto = (BasicErrorDto) responseEntity.getBody(); + } +} diff --git a/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/ErrorConstantsTest.java b/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/ErrorConstantsTest.java new file mode 100644 index 0000000000..36f7bce5bc --- /dev/null +++ b/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/ErrorConstantsTest.java @@ -0,0 +1,24 @@ +package org.sagebionetworks.openchallenges.image.service.exception; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; + +@ExtendWith(MockitoExtension.class) +public class ErrorConstantsTest { + + @Test + public void ErrorConstants_ShouldReturnErrors_WhenErrorConstantsKeyIsPassed() { + + // Get the ImageHeightNotSpecified error constant + ErrorConstants constant = ErrorConstants.IMAGE_HEIGHT_NOT_SPECIFIED; + + // Verify the properties of the constant + assertThat(constant.getType()).isEqualTo("IMAGE-SERVICE-1000"); + assertThat(constant.getTitle()).isEqualTo("Image height not found"); + assertThat(constant.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST); + } +} diff --git a/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/exception/ImageHeightNotSpecifiedExceptionTest.java b/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/ImageHeightNotSpecifiedExceptionTest.java similarity index 82% rename from apps/openchallenges/image-service/src/test/java/org/sagebionetworks/exception/ImageHeightNotSpecifiedExceptionTest.java rename to apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/ImageHeightNotSpecifiedExceptionTest.java index edd24e7245..fc0e1b243f 100644 --- a/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/exception/ImageHeightNotSpecifiedExceptionTest.java +++ b/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/ImageHeightNotSpecifiedExceptionTest.java @@ -7,7 +7,7 @@ public class ImageHeightNotSpecifiedExceptionTest { @Test - public void ImageHeightNotSpecifiedException_ConstructorTypeShouldMatch() { + public void ImageHeightNotSpecifiedException_ShouldReturnType_WhenTypeKeyIsPassed() { // Define the exception detail String detail = "Image height is not specified"; @@ -19,7 +19,7 @@ public void ImageHeightNotSpecifiedException_ConstructorTypeShouldMatch() { } @Test - public void ImageHeightNotSpecifiedException_ConstructorTitle_Match() { + public void ImageHeightNotSpecifiedException_ShouldReturnTitle_WhenTitleKeyIsPassed() { // Define the exception detail String detail = "Image height is not specified"; @@ -32,7 +32,7 @@ public void ImageHeightNotSpecifiedException_ConstructorTitle_Match() { } @Test - public void ImageHeightNotSpecifiedException_ConstructorStatusMatch() { + public void ImageHeightNotSpecifiedException_ShouldReturnStatus_WhenStatusKeyIsPassed() { // Define the exception detail String detail = "Image height is not specified"; @@ -45,7 +45,7 @@ public void ImageHeightNotSpecifiedException_ConstructorStatusMatch() { } @Test - public void ImageHeightNotSpecifiedException_ConstructorDetailMatch() { + public void ImageHeightNotSpecifiedException_ShouldReturnException_WhenExceptionKeyIsPassed() { // Define the exception detail String detail = "Image height is not specified"; diff --git a/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/exception/SimpleChallengeGlobalExceptionTest.java b/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/SimpleChallengeGlobalExceptionTest.java similarity index 86% rename from apps/openchallenges/image-service/src/test/java/org/sagebionetworks/exception/SimpleChallengeGlobalExceptionTest.java rename to apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/SimpleChallengeGlobalExceptionTest.java index 6e33fa1a4c..47351a73d2 100644 --- a/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/exception/SimpleChallengeGlobalExceptionTest.java +++ b/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/exception/SimpleChallengeGlobalExceptionTest.java @@ -9,7 +9,7 @@ public class SimpleChallengeGlobalExceptionTest { @Test - public void SimpleChallengeGlobalException_ConstructorDetailsShouldMatch() { + public void SimpleChallengeGlobalException_ShouldReturnMessage_WhenMessageKeyIsPassed() { // Create an instance of SimpleChallengeGlobalException using the constructor with details String details = "Something went wrong"; SimpleChallengeGlobalException exception = new SimpleChallengeGlobalException(details); @@ -19,7 +19,7 @@ public void SimpleChallengeGlobalException_ConstructorDetailsShouldMatch() { } @Test - public void SimpleChallengeGlobalException_ConstructorTypeShouldMatch() { + public void SimpleChallengeGlobalException_ShouldReturnType_WhenTypeKeyIsPassed() { // Define the exception details String type = "ExceptionType"; String title = "Exception Title"; @@ -35,7 +35,7 @@ public void SimpleChallengeGlobalException_ConstructorTypeShouldMatch() { } @Test - public void SimpleChallengeGlobalException_ConstructorTitleShouldMatch() { + public void SimpleChallengeGlobalException_ShouldReturnTitle_WhenTitleKeyIsPassed() { // Define the exception details String type = "ExceptionType"; String title = "Exception Title"; @@ -51,7 +51,7 @@ public void SimpleChallengeGlobalException_ConstructorTitleShouldMatch() { } @Test - public void SimpleChallengeGlobalException_ConstructorStatusShouldMatch() { + public void SimpleChallengeGlobalException_ShouldReturnStatus_WhenStatusKeyIsPassed() { // Define the exception details String type = "ExceptionType"; String title = "Exception Title"; diff --git a/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/service/ImageServiceTest.java b/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/service/ImageServiceTest.java index fd48c45317..cd5a94d380 100644 --- a/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/service/ImageServiceTest.java +++ b/apps/openchallenges/image-service/src/test/java/org/sagebionetworks/openchallenges/image/service/service/ImageServiceTest.java @@ -26,7 +26,7 @@ class ImageServiceTest { @InjectMocks private ImageService imageService; @Test - void getImage_ShouldReturnImage_WhenObjectKeyIsPassed() { + void GetImage_ShouldReturnImage_WhenObjectKeyIsPassed() { String expectedUrl = "http://localhost:8082/img/S2_Nh1GysneL6qVEGuBdz5NK-wQ=/image.png"; // given