-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(openchallenges): add more unit tests to the image service except…
…ion to increase coverage to > 50% (#2319) * additional image service unit tests * new unit test for image service * move folders update naming * add unit test for handleBindException * update controller advisor test formatting * update bindException * formatting update * update * add controller test unit testingfile * format controller advisor * update name of test method * update names of tests
- Loading branch information
Showing
6 changed files
with
125 additions
and
9 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...a/org/sagebionetworks/openchallenges/image/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,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); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...ava/org/sagebionetworks/openchallenges/image/service/exception/ControllerAdvisorTest.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,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<FieldError> 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<Object> 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(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...t/java/org/sagebionetworks/openchallenges/image/service/exception/ErrorConstantsTest.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,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); | ||
} | ||
} |
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