diff options
3 files changed, 150 insertions, 31 deletions
diff --git a/bpmn/MSOCommonBPMN/pom.xml b/bpmn/MSOCommonBPMN/pom.xml index 4a7cb5900a..523ee403d0 100644 --- a/bpmn/MSOCommonBPMN/pom.xml +++ b/bpmn/MSOCommonBPMN/pom.xml @@ -501,5 +501,16 @@ <artifactId>guava</artifactId> <version>22.0</version> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>3.9.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>pl.pragmatists</groupId> + <artifactId>JUnitParams</artifactId> + <version>1.1.1</version> + </dependency> </dependencies> </project> diff --git a/bpmn/MSOCommonBPMN/src/test/groovy/org/openecomp/mso/bpmn/common/scripts/CompleteMsoProcessTest.groovy b/bpmn/MSOCommonBPMN/src/test/groovy/org/openecomp/mso/bpmn/common/scripts/CompleteMsoProcessTest.groovy index ab7ee7a689..5949c3af53 100644 --- a/bpmn/MSOCommonBPMN/src/test/groovy/org/openecomp/mso/bpmn/common/scripts/CompleteMsoProcessTest.groovy +++ b/bpmn/MSOCommonBPMN/src/test/groovy/org/openecomp/mso/bpmn/common/scripts/CompleteMsoProcessTest.groovy @@ -16,23 +16,24 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= - */ + */ package org.openecomp.mso.bpmn.common.scripts -import org.junit.runner.RunWith; -import static org.junit.Assert.* -import static org.mockito.Mockito.* - import org.camunda.bpm.engine.delegate.BpmnError import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity import org.junit.Before import org.junit.Test import org.junit.runner.RunWith +import org.mockito.ArgumentCaptor import org.mockito.MockitoAnnotations import org.mockito.runners.MockitoJUnitRunner import org.openecomp.mso.bpmn.core.WorkflowException +import static org.assertj.core.api.Assertions.assertThat +import static org.assertj.core.api.Assertions.assertThatThrownBy +import static org.mockito.Matchers.eq +import static org.mockito.Mockito.* @RunWith(MockitoJUnitRunner.class) class CompleteMsoProcessTest { @@ -128,7 +129,7 @@ class CompleteMsoProcessTest { when(mockExecution.getVariable("CMSO_mso-bpel-name")).thenReturn("BPEL") when(mockExecution.getVariable("URN_mso_adapters_db_auth")).thenReturn("757A94191D685FD2092AC1490730A4FC"); when(mockExecution.getVariable("URN_mso_msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7"); - + CompleteMsoProcess completeMsoProcess = new CompleteMsoProcess() completeMsoProcess.setUpdateDBstatustoSuccessPayload(mockExecution) @@ -145,29 +146,25 @@ class CompleteMsoProcessTest { </sdncadapterworkflow:MsoCompletionResponse>""" */ @Test - public void testbuildDataError(){ - - boolean thrown = false; - String msg = "Some-Message"; - - ExecutionEntity mockExecution = mock(ExecutionEntity.class) - when(mockExecution.getVariable("CMSO_mso-bpel-name")).thenReturn("BPEL-NAME") - when(mockExecution.getVariable("testProcessKey")).thenReturn("CompleteMsoProcess") - - WorkflowException exception = new WorkflowException("CompleteMsoProcess", 500, msg); - - try{ - CompleteMsoProcess completeMsoProcess = new CompleteMsoProcess() - completeMsoProcess.buildDataError(mockExecution, msg) - } - catch (BpmnError e){ - thrown = true; - } - - - verify(mockExecution).setVariable("CompleteMsoProcessResponse",msoCompletionResponse) - // Can't seem to figure out how to verify the exception and have spent way too much time on fixing this test case! - //verify(mockExecution).setVariable("WorkflowException",exception) - assertTrue(thrown); - } + void testBuildDataError() { + // given + def message = "Some-Message" + + def mockExecution = mock ExecutionEntity.class + when mockExecution.getVariable("CMSO_mso-bpel-name") thenReturn "BPEL-NAME" + when mockExecution.getVariable("testProcessKey") thenReturn "CompleteMsoProcess" + + def completeMsoProcess = new CompleteMsoProcess() + // when + assertThatThrownBy { completeMsoProcess.buildDataError(mockExecution, message) } isInstanceOf BpmnError + // then + verify mockExecution setVariable("CompleteMsoProcessResponse", msoCompletionResponse) + def argumentCaptor = ArgumentCaptor.forClass WorkflowException.class + verify mockExecution setVariable(eq("WorkflowException"), argumentCaptor.capture()) + def capturedException = argumentCaptor.value + + assertThat capturedException.processKey isEqualTo "CompleteMsoProcess" + assertThat capturedException.errorCode isEqualTo 500 + assertThat capturedException.errorMessage isEqualTo message + } }
\ No newline at end of file diff --git a/bpmn/MSOCommonBPMN/src/test/java/org/openecomp/mso/client/ResponseExceptionMapperImplTest.java b/bpmn/MSOCommonBPMN/src/test/java/org/openecomp/mso/client/ResponseExceptionMapperImplTest.java new file mode 100644 index 0000000000..8943014ad0 --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/test/java/org/openecomp/mso/client/ResponseExceptionMapperImplTest.java @@ -0,0 +1,111 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.mso.client; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.google.common.base.Charsets; +import javax.ws.rs.BadRequestException; +import javax.ws.rs.ForbiddenException; +import javax.ws.rs.InternalServerErrorException; +import javax.ws.rs.NotAcceptableException; +import javax.ws.rs.NotAllowedException; +import javax.ws.rs.NotAuthorizedException; +import javax.ws.rs.NotFoundException; +import javax.ws.rs.NotSupportedException; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.ClientResponseContext; +import javax.ws.rs.core.Response.Status; +import junitparams.JUnitParamsRunner; +import junitparams.Parameters; +import org.apache.commons.io.IOUtils; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(JUnitParamsRunner.class) +public class ResponseExceptionMapperImplTest { + + private static final ResponseExceptionMapperImpl mapper = new ResponseExceptionMapperImpl(); + + public static Object[][] statusesAndCorrespondingExceptions() { + return new Object[][]{ + {Status.BAD_REQUEST, BadRequestException.class}, + {Status.UNAUTHORIZED, NotAuthorizedException.class}, + {Status.FORBIDDEN, ForbiddenException.class}, + {Status.NOT_FOUND, NotFoundException.class}, + {Status.METHOD_NOT_ALLOWED, NotAllowedException.class}, + {Status.NOT_ACCEPTABLE, NotAcceptableException.class}, + {Status.PRECONDITION_FAILED, PreconditionFailedException.class}, + {Status.UNSUPPORTED_MEDIA_TYPE, NotSupportedException.class}, + {Status.INTERNAL_SERVER_ERROR, InternalServerErrorException.class}, + {Status.SERVICE_UNAVAILABLE, WebApplicationException.class}, + {Status.BAD_GATEWAY, WebApplicationException.class}, + }; + } + + @Test + @Parameters(method = "statusesAndCorrespondingExceptions") + public void shouldThrowExceptionWhenStatusIsNotOk(Status status, Class<Exception> expectedException) { + // given + ClientResponseContext responseContext = createMockResponseContext(status); + // when, then + assertThatThrownBy(() -> mapper.filter(null, responseContext)).isInstanceOf(expectedException); + } + + @Test + public void shouldNotThrowExceptionWhenStatusIsOk() { + // given + ClientResponseContext responseContext = createMockResponseContext(Status.OK); + // when, then + assertThatCode(() -> mapper.filter(null, responseContext)).doesNotThrowAnyException(); + } + + @Test + public void shouldThrowExceptionWithCustomMessageWhenResponseHasEntity() { + // given + ClientResponseContext responseContext = createMockResponseContext(Status.BAD_REQUEST); + when(responseContext.hasEntity()).thenReturn(true); + when(responseContext.getEntityStream()).thenReturn(IOUtils.toInputStream("test message", Charsets.UTF_8)); + // when, then + assertThatThrownBy(() -> mapper.filter(null, responseContext)).isInstanceOf(BadRequestException.class) + .hasMessage("test message"); + } + + @Test + public void shouldThrowExceptionWithDefaultMessageWhenResponseHasNoEntity() { + // given + ClientResponseContext responseContext = createMockResponseContext(Status.BAD_REQUEST); + when(responseContext.hasEntity()).thenReturn(false); + // when, then + assertThatThrownBy(() -> mapper.filter(null, responseContext)).isInstanceOf(BadRequestException.class) + .hasMessage("empty message"); + } + + private static ClientResponseContext createMockResponseContext(Status status) { + ClientResponseContext responseContext = mock(ClientResponseContext.class); + when(responseContext.getStatusInfo()).thenReturn(status); + when(responseContext.getStatus()).thenReturn(status.getStatusCode()); + return responseContext; + } +}
\ No newline at end of file |