Associate Artifact Test
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / test / java / org / onap / sdc / workflow / api / ArtifactAssociationHandlerTest.java
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 junit.framework.TestCase.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.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mockito;
31 import org.onap.sdc.workflow.api.types.dto.ArtifactDeliveriesRequestDto;
32 import org.onap.sdc.workflow.persistence.types.ArtifactEntity;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.boot.web.client.RestTemplateBuilder;
36 import org.springframework.context.annotation.Bean;
37 import org.springframework.context.annotation.Configuration;
38 import org.springframework.http.HttpEntity;
39 import org.springframework.http.HttpMethod;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.stereotype.Component;
43 import org.springframework.test.context.ContextConfiguration;
44 import org.springframework.test.context.TestPropertySource;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
46 import org.springframework.web.client.RestTemplate;
47
48
49 @RunWith(SpringJUnit4ClassRunner.class)
50 @ContextConfiguration(
51         classes = {ArtifactAssociationHandlerTest.RestBuilderMockProvider.class, ArtifactAssociationService.class})
52 @TestPropertySource(locations = "classpath:application-test.properties")
53 @Component("ArtifactAssociationHandlerTest")
54 public class ArtifactAssociationHandlerTest {
55
56     @Configuration
57     static class RestBuilderMockProvider {
58
59         @Bean
60         public RestTemplateBuilder templateBuilder() {
61             restTemplateBuilderMock = Mockito.mock(RestTemplateBuilder.class);
62             return restTemplateBuilderMock;
63         }
64
65         @Bean
66         public RestTemplate restTemplate() {
67             restClientMock = Mockito.mock(RestTemplate.class);
68             return restClientMock;
69         }
70     }
71
72     private static final String FILE_NAME = "fileName.txt";
73     private static final String USER_ID = "cs0008";
74     private static final String END_POINT =
75             "sdc/v1/catalog/resources/46434d20-40f6-4a5f-a0c4-8c1da6791bdb/interfaces/137a0264-47a5-4dab-b79d-cfdd8cd9a9a1/artifacts/ef82dec9-cb99-48a3-aaba-5ae832417dc5";
76     private final String EROR_MSG =
77             "Failed while attaching workflow artifact to Operation in SDC. Parameters were not initialized: [SDC_ENDPOINT]";
78     private InputStream inputStreamMock;
79     private ArtifactEntity artifactMock;
80     private ArtifactDeliveriesRequestDto requestDto;
81     @Value("${sdc.be.endpoint}")
82     private String sdcBeEndpoint;
83     @Value("${sdc.be.protocol}")
84     private String sdcBeProtocol;
85     @Value("${sdc.be.external.user}")
86     private String sdcUser;
87     @Value("${sdc.be.external.password}")
88     private String sdcPassword;
89
90     private static RestTemplate restClientMock;
91
92     private static RestTemplateBuilder restTemplateBuilderMock;
93
94     @Autowired
95     private ArtifactAssociationService associationService;
96
97     @Before
98     public void setUp() throws IOException {
99         inputStreamMock = IOUtils.toInputStream("some test data for my input stream", "UTF-8");
100         artifactMock = new ArtifactEntity(FILE_NAME, inputStreamMock);
101         requestDto = new ArtifactDeliveriesRequestDto("POST", END_POINT);
102         associationService.setRestClient(restClientMock);
103     }
104
105
106     @Test
107     public void shouldGetResponseStatusOk() {
108         ResponseEntity<String> responseEntity = new ResponseEntity(HttpStatus.OK);
109         when(restClientMock.exchange(eq(sdcBeProtocol + "://" + sdcBeEndpoint + "/" + requestDto.getEndpoint()),
110                 eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class))).thenReturn(responseEntity);
111
112         ResponseEntity<String> response = associationService.execute(USER_ID, requestDto, artifactMock);
113         assertEquals(200, response.getStatusCode().value());
114
115     }
116
117
118     @Test
119     public void shouldReturnStatusFailWhenNoParametersInitialized() {
120         associationService.setSdcBeEndpoint(null);
121         ResponseEntity<String> response = associationService.execute(USER_ID, requestDto, artifactMock);
122         assertEquals(417, response.getStatusCode().value());
123         assertEquals(EROR_MSG, response.getBody());
124     }
125
126 }