Skip to content

Commit

Permalink
format controller advisor
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsage1 committed Nov 8, 2023
1 parent c2eb562 commit 71c99a1
Showing 1 changed file with 31 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<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();
}
@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<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();
}
}

0 comments on commit 71c99a1

Please sign in to comment.