From efbd6b79e61a62891f043926c7d3876ae945e50f Mon Sep 17 00:00:00 2001 From: Tomasz Gwozdecki Date: Thu, 4 Apr 2019 04:19:54 -0400 Subject: junits for ControllersUtils -Added new test to check Exception handling Change-Id: I4e634157ad942118221c07d7fe21ee33af68a9ef Issue-ID: VID-449 Signed-off-by: Tomasz Gwozdecki --- .../onap/vid/controller/ControllersUtilsTest.java | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/ControllersUtilsTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/ControllersUtilsTest.java index f084b3dec..80836d155 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controller/ControllersUtilsTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controller/ControllersUtilsTest.java @@ -21,16 +21,25 @@ package org.onap.vid.controller; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.mock; import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; +import org.apache.commons.lang.exception.ExceptionUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Answers; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.model.ExceptionResponse; import org.onap.vid.utils.SystemPropertiesWrapper; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; @RunWith(MockitoJUnitRunner.class) public class ControllersUtilsTest { @@ -110,4 +119,26 @@ public class ControllersUtilsTest { // THEN assertThat(loginId).isEmpty(); } + + @Test + public void shouldCreateResponseEntity_fromGivenException() { + // GIVEN + EELFLoggerDelegate eelfLoggerDelegate = mock(EELFLoggerDelegate.class); + Response response = mock(Response.class); + given(response.getStatus()).willReturn(HttpStatus.OK.value()); + WebApplicationException webApplicationException = new WebApplicationException("ErrorMessage", response); + + // WHEN + ResponseEntity responseEntity = ControllersUtils + .handleWebApplicationException(webApplicationException, eelfLoggerDelegate); + + // THEN + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(responseEntity.getBody()) + .isInstanceOfSatisfying(ExceptionResponse.class, + er -> assertThat(er.getMessage()).isEqualTo("ErrorMessage (Request id: null)")); + then(eelfLoggerDelegate).should() + .error(EELFLoggerDelegate.errorLogger, "{}: {}", "handleWebApplicationException", + ExceptionUtils.getMessage(webApplicationException), webApplicationException); + } } \ No newline at end of file -- cgit 1.2.3-korg