Skip to content

Commit

Permalink
feat(openchallenges): add more unit tests to the image service except…
Browse files Browse the repository at this point in the history
…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
mdsage1 authored Nov 9, 2023
1 parent 1749d7f commit 7536af1
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 9 deletions.
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);
}
}
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();
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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";

Expand All @@ -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";

Expand All @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7536af1

Please sign in to comment.