From cebbe0144bce9b2e9ee1b390dc02389e9ce015d1 Mon Sep 17 00:00:00 2001 From: ayalaben Date: Thu, 30 Aug 2018 13:44:51 +0300 Subject: Associate Artifact Test Change-Id: Ia45b1b1123a76b5642dda64a7bd75a31065de027 Issue-ID: SDC-1535 Signed-off-by: ayalaben --- .../workflow/api/ArtifactAssociationService.java | 33 ++++-- .../api/ArtifactAssociationHandlerTest.java | 126 +++++++++++++++++++++ .../src/test/resources/application-test.properties | 4 + 3 files changed, 151 insertions(+), 12 deletions(-) create mode 100644 workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/ArtifactAssociationHandlerTest.java create mode 100644 workflow-designer-be/src/test/resources/application-test.properties (limited to 'workflow-designer-be') diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/ArtifactAssociationService.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/ArtifactAssociationService.java index be327498..a837d700 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/ArtifactAssociationService.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/ArtifactAssociationService.java @@ -52,7 +52,8 @@ public class ArtifactAssociationService { private static final String USER_ID_HEADER = "USER_ID"; private static final String MD5_HEADER = "Content-MD5"; private static final String X_ECOMP_INSTANCE_ID_HEADER = "X-ECOMP-InstanceID"; - private static final String INIT_ERROR_MSG = "Failed while attaching workflow artifact to Operation in SDC. Parameters were not initialized: %s"; + private static final String INIT_ERROR_MSG = + "Failed while attaching workflow artifact to Operation in SDC. Parameters were not initialized: %s"; private static final Logger LOGGER = LoggerFactory.getLogger(ArtifactAssociationService.class); @Value("${sdc.be.endpoint}") private String sdcBeEndpoint; @@ -63,19 +64,26 @@ public class ArtifactAssociationService { @Value("${sdc.be.external.password}") private String sdcPassword; - private final RestTemplate restClient; + private RestTemplate restClient; @Autowired public ArtifactAssociationService(RestTemplateBuilder builder) { this.restClient = builder.build(); } + void setRestClient(RestTemplate restClient) { + this.restClient = restClient; + } + + void setSdcBeEndpoint(String value) { + this.sdcBeEndpoint = value; + } ResponseEntity execute(String userId, ArtifactDeliveriesRequestDto deliveriesRequestDto, ArtifactEntity artifactEntity) { Optional initializationState = parametersInitializationState(); - if(initializationState.isPresent()){ + if(initializationState.isPresent()) { LOGGER.error(String.format(INIT_ERROR_MSG,initializationState.get())); return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(String.format(INIT_ERROR_MSG,initializationState.get())); } @@ -90,29 +98,30 @@ public class ArtifactAssociationService { HttpEntity request = new HttpEntity<>(formattedArtifact, createHeaders(userId,formattedArtifact)); - return restClient.exchange(sdcBeProtocol +"://" + sdcBeEndpoint + "/" + deliveriesRequestDto.getEndpoint(), + return restClient.exchange(sdcBeProtocol + "://" + sdcBeEndpoint + "/" + deliveriesRequestDto.getEndpoint(), HttpMethod.valueOf(deliveriesRequestDto.getMethod()), request, String.class); } - private Optional parametersInitializationState() { - ArrayList result=new ArrayList(); - if (sdcBeEndpoint.equals("")) { + Optional parametersInitializationState() { + ArrayList result = new ArrayList<>(); + if (sdcBeEndpoint == null || sdcBeEndpoint.equals("")) { result.add("SDC_ENDPOINT"); } - if (sdcBeProtocol.equals("")) { + if (sdcBeProtocol == null || sdcBeProtocol.equals("")) { result.add("SDC_PROTOCOL"); } - if (sdcUser.equals("")) { + if (sdcUser == null || sdcUser.equals("")) { result.add("SDC_USER"); } - if (sdcPassword.equals("")) { + if (sdcPassword == null || sdcPassword.equals("")) { result.add("SDC_PASSWORD"); } - if(result.isEmpty()) { + if (result.isEmpty()) { return Optional.empty(); + } else { + return Optional.of(result.toString()); } - else return Optional.of(result.toString()); } diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/ArtifactAssociationHandlerTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/ArtifactAssociationHandlerTest.java new file mode 100644 index 00000000..b4fe7d39 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/ArtifactAssociationHandlerTest.java @@ -0,0 +1,126 @@ +/* + * Copyright © 2018 European Support Limited + * + * 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. + */ + +package org.onap.sdc.workflow.api; + +import static junit.framework.TestCase.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.io.InputStream; +import org.apache.commons.io.IOUtils; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.onap.sdc.workflow.api.types.dto.ArtifactDeliveriesRequestDto; +import org.onap.sdc.workflow.persistence.types.ArtifactEntity; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.client.RestTemplate; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + classes = {ArtifactAssociationHandlerTest.RestBuilderMockProvider.class, ArtifactAssociationService.class}) +@TestPropertySource(locations = "classpath:application-test.properties") +@Component("ArtifactAssociationHandlerTest") +public class ArtifactAssociationHandlerTest { + + @Configuration + static class RestBuilderMockProvider { + + @Bean + public RestTemplateBuilder templateBuilder() { + restTemplateBuilderMock = Mockito.mock(RestTemplateBuilder.class); + return restTemplateBuilderMock; + } + + @Bean + public RestTemplate restTemplate() { + restClientMock = Mockito.mock(RestTemplate.class); + return restClientMock; + } + } + + private static final String FILE_NAME = "fileName.txt"; + private static final String USER_ID = "cs0008"; + private static final String END_POINT = + "sdc/v1/catalog/resources/46434d20-40f6-4a5f-a0c4-8c1da6791bdb/interfaces/137a0264-47a5-4dab-b79d-cfdd8cd9a9a1/artifacts/ef82dec9-cb99-48a3-aaba-5ae832417dc5"; + private final String EROR_MSG = + "Failed while attaching workflow artifact to Operation in SDC. Parameters were not initialized: [SDC_ENDPOINT]"; + private InputStream inputStreamMock; + private ArtifactEntity artifactMock; + private ArtifactDeliveriesRequestDto requestDto; + @Value("${sdc.be.endpoint}") + private String sdcBeEndpoint; + @Value("${sdc.be.protocol}") + private String sdcBeProtocol; + @Value("${sdc.be.external.user}") + private String sdcUser; + @Value("${sdc.be.external.password}") + private String sdcPassword; + + private static RestTemplate restClientMock; + + private static RestTemplateBuilder restTemplateBuilderMock; + + @Autowired + private ArtifactAssociationService associationService; + + @Before + public void setUp() throws IOException { + inputStreamMock = IOUtils.toInputStream("some test data for my input stream", "UTF-8"); + artifactMock = new ArtifactEntity(FILE_NAME, inputStreamMock); + requestDto = new ArtifactDeliveriesRequestDto("POST", END_POINT); + associationService.setRestClient(restClientMock); + } + + + @Test + public void shouldGetResponseStatusOk() { + ResponseEntity responseEntity = new ResponseEntity(HttpStatus.OK); + when(restClientMock.exchange(eq(sdcBeProtocol + "://" + sdcBeEndpoint + "/" + requestDto.getEndpoint()), + eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class))).thenReturn(responseEntity); + + ResponseEntity response = associationService.execute(USER_ID, requestDto, artifactMock); + assertEquals(200, response.getStatusCode().value()); + + } + + + @Test + public void shouldReturnStatusFailWhenNoParametersInitialized() { + associationService.setSdcBeEndpoint(null); + ResponseEntity response = associationService.execute(USER_ID, requestDto, artifactMock); + assertEquals(417, response.getStatusCode().value()); + assertEquals(EROR_MSG, response.getBody()); + } + +} diff --git a/workflow-designer-be/src/test/resources/application-test.properties b/workflow-designer-be/src/test/resources/application-test.properties new file mode 100644 index 00000000..4d3e874b --- /dev/null +++ b/workflow-designer-be/src/test/resources/application-test.properties @@ -0,0 +1,4 @@ +sdc.be.protocol=${SDC_PROTOCOL:http} +sdc.be.endpoint=${SDC_ENDPOINT:localhost:8080} +sdc.be.external.user=${SDC_USER:cs0008} +sdc.be.external.password=${SDC_PASSWORD:Aa123456} \ No newline at end of file -- cgit 1.2.3-korg