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 index 32aaf043eb..6695149d27 100644 --- 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 @@ -1,20 +1,12 @@ 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 org.sagebionetworks.openchallenges.image.service.exception.GlobalExceptionHandler; + +import java.util.ArrayList; +import java.util.List; import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.http.HttpStatus; -import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.validation.BindException; -import org.springframework.validation.BindingResult; -import org.springframework.validation.FieldError; import org.sagebionetworks.openchallenges.image.service.model.dto.BasicErrorDto; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -24,36 +16,33 @@ import org.springframework.validation.FieldError; import org.springframework.web.context.request.WebRequest; -import java.util.ArrayList; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - public class ControllerAdvisorTest { - @Test - public void testHandleBindException() { - // 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(); - } + @Test + public void testHandleBindException() { + // 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(); + } }