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             return Mockito.mock(RestTemplateBuilder.class);
62         }
63
64         @Bean
65         public RestTemplate restTemplate() {
66             return Mockito.mock(RestTemplate.class);
67         }
68     }
69
70     private static final String FILE_NAME = "fileName.txt";
71     private static final String USER_ID = "cs0008";
72     private static final String END_POINT =
73             "sdc/v1/catalog/resources/46434d20-40f6-4a5f-a0c4-8c1da6791bdb/interfaces/137a0264-47a5-4dab-b79d-cfdd8cd9a9a1/artifacts/ef82dec9-cb99-48a3-aaba-5ae832417dc5";
74     private final String EROR_MSG =
75             "Failed while attaching workflow artifact to Operation in SDC. Parameters were not initialized: [SDC_ENDPOINT]";
76     private InputStream inputStreamMock;
77     private ArtifactEntity artifactMock;
78     private ArtifactDeliveriesRequestDto requestDto;
79     @Value("${sdc.be.endpoint}")
80     private String sdcBeEndpoint;
81     @Value("${sdc.be.protocol}")
82     private String sdcBeProtocol;
83     @Value("${sdc.be.external.user}")
84     private String sdcUser;
85     @Value("${sdc.be.external.password}")
86     private String sdcPassword;
87
88     @Autowired
89     private RestTemplate restClientMock;
90
91
92     @Autowired
93     private ArtifactAssociationService associationService;
94
95     @Before
96     public void setUp() throws IOException {
97         inputStreamMock = IOUtils.toInputStream("some test data for my input stream", "UTF-8");
98         artifactMock = new ArtifactEntity(FILE_NAME, inputStreamMock);
99         requestDto = new ArtifactDeliveriesRequestDto("POST", END_POINT);
100         associationService.setRestClient(restClientMock);
101     }
102
103
104     @Test
105     public void shouldGetResponseStatusOk() {
106         ResponseEntity<String> responseEntity = new ResponseEntity(HttpStatus.OK);
107         when(restClientMock.exchange(eq(sdcBeProtocol + "://" + sdcBeEndpoint + "/" + requestDto.getEndpoint()),
108                 eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class))).thenReturn(responseEntity);
109
110         ResponseEntity<String> response = associationService.execute(USER_ID, requestDto, artifactMock);
111         assertEquals(200, response.getStatusCode().value());
112
113     }
114
115
116     @Test
117     public void shouldReturnStatusFailWhenNoParametersInitialized() {
118         associationService.setSdcBeEndpoint(null);
119         ResponseEntity<String> response = associationService.execute(USER_ID, requestDto, artifactMock);
120         assertEquals(417, response.getStatusCode().value());
121         assertEquals(EROR_MSG, response.getBody());
122     }
123
124 }