2 * Copyright © 2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.sdc.workflow.api;
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.when;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import org.apache.commons.io.IOUtils;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.mockito.Mockito;
30 import org.onap.sdc.workflow.api.types.dto.ArtifactDeliveriesRequestDto;
31 import org.onap.sdc.workflow.persistence.types.ArtifactEntity;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.boot.web.client.RestTemplateBuilder;
35 import org.springframework.context.annotation.Bean;
36 import org.springframework.context.annotation.Configuration;
37 import org.springframework.http.HttpEntity;
38 import org.springframework.http.HttpMethod;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.stereotype.Component;
42 import org.springframework.test.context.TestPropertySource;
43 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
44 import org.springframework.web.client.RestTemplate;
48 classes = {ArtifactAssociationHandlerTest.RestBuilderMockProvider.class, ArtifactAssociationService.class})
49 @TestPropertySource(locations = "classpath:application-test.properties")
50 @Component("ArtifactAssociationHandlerTest")
51 public class ArtifactAssociationHandlerTest {
54 static class RestBuilderMockProvider {
57 public RestTemplateBuilder templateBuilder() {
58 return Mockito.mock(RestTemplateBuilder.class);
62 public RestTemplate restTemplate() {
63 return Mockito.mock(RestTemplate.class);
67 private static final String FILE_NAME = "fileName.txt";
68 private static final String USER_ID = "cs0008";
69 private static final String END_POINT =
70 "sdc/v1/catalog/resources/46434d20-40f6-4a5f-a0c4-8c1da6791bdb/interfaces/137a0264-47a5-4dab-b79d-cfdd8cd9a9a1/artifacts/ef82dec9-cb99-48a3-aaba-5ae832417dc5";
71 private final String EROR_MSG =
72 "Failed while attaching workflow artifact to Operation in SDC. Parameters were not initialized: [SDC_ENDPOINT]";
73 private InputStream inputStreamMock;
74 private ArtifactEntity artifactMock;
75 private ArtifactDeliveriesRequestDto requestDto;
76 @Value("${sdc.be.endpoint}")
77 private String sdcBeEndpoint;
78 @Value("${sdc.be.protocol}")
79 private String sdcBeProtocol;
80 @Value("${sdc.be.external.user}")
81 private String sdcUser;
82 @Value("${sdc.be.external.password}")
83 private String sdcPassword;
86 private RestTemplate restClientMock;
90 private ArtifactAssociationService associationService;
93 public void setUp() throws IOException {
94 inputStreamMock = IOUtils.toInputStream("some test data for my input stream", "UTF-8");
95 artifactMock = new ArtifactEntity(FILE_NAME, inputStreamMock);
96 requestDto = new ArtifactDeliveriesRequestDto("POST", END_POINT);
97 associationService.setRestClient(restClientMock);
102 public void shouldGetResponseStatusOk() {
103 ResponseEntity<String> responseEntity = new ResponseEntity(HttpStatus.OK);
104 when(restClientMock.exchange(eq(sdcBeProtocol + "://" + sdcBeEndpoint + "/" + requestDto.getEndpoint()),
105 eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class))).thenReturn(responseEntity);
107 ResponseEntity<String> response = associationService.execute(USER_ID, requestDto, artifactMock);
108 assertEquals(200, response.getStatusCode().value());
114 public void shouldReturnStatusFailWhenNoParametersInitialized() {
115 associationService.setSdcBeEndpoint(null);
116 ResponseEntity<String> response = associationService.execute(USER_ID, requestDto, artifactMock);
117 assertEquals(417, response.getStatusCode().value());
118 assertEquals(EROR_MSG, response.getBody());