131e14beef82f600b0bdc57e8175b1ad69729319
[sdc/sdc-workflow-designer.git] /
1 /*
2  * Copyright © 2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.sdc.workflow.api;
18
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;
23
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;
45
46
47 @SpringJUnitConfig(
48         classes = {ArtifactAssociationHandlerTest.RestBuilderMockProvider.class, ArtifactAssociationService.class})
49 @TestPropertySource(locations = "classpath:application-test.properties")
50 @Component("ArtifactAssociationHandlerTest")
51 public class ArtifactAssociationHandlerTest {
52
53     @Configuration
54     static class RestBuilderMockProvider {
55
56         @Bean
57         public RestTemplateBuilder templateBuilder() {
58             return Mockito.mock(RestTemplateBuilder.class);
59         }
60
61         @Bean
62         public RestTemplate restTemplate() {
63             return Mockito.mock(RestTemplate.class);
64         }
65     }
66
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;
84
85     @Autowired
86     private RestTemplate restClientMock;
87
88
89     @Autowired
90     private ArtifactAssociationService associationService;
91
92     @BeforeEach
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);
98     }
99
100
101     @Test
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);
106
107         ResponseEntity<String> response = associationService.execute(USER_ID, requestDto, artifactMock);
108         assertEquals(200, response.getStatusCode().value());
109
110     }
111
112
113     @Test
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());
119     }
120
121 }