7a03ce0542ed5fc0103fedba401d7ec7c2537de1
[so.git] / mso-api-handlers / mso-api-handler-common / src / test / java / org / onap / so / apihandler / common / CamundaClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.apihandler.common;
24
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNull;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35
36 import org.apache.http.HttpResponse;
37 import org.apache.http.HttpStatus;
38 import org.apache.http.ProtocolVersion;
39 import org.apache.http.client.HttpClient;
40 import org.apache.http.client.methods.HttpPost;
41 import org.apache.http.entity.StringEntity;
42 import org.apache.http.message.BasicHttpResponse;
43 import org.apache.http.message.BasicStatusLine;
44 import org.junit.Assert;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.mockito.ArgumentCaptor;
48 import org.mockito.Mock;
49 import org.mockito.Mockito;
50 import org.mockito.MockitoAnnotations;
51 import org.springframework.mock.env.MockEnvironment;
52
53 import com.fasterxml.jackson.core.JsonGenerationException;
54 import com.fasterxml.jackson.databind.JsonMappingException;
55
56
57 /**
58  * This class implements test methods of Camunda Beans.
59  *
60  *
61  */
62 public class CamundaClientTest{
63
64
65
66     @Mock
67     private HttpClient mockHttpClient;
68     private static final String AUTHORIZATION_HEADER_NAME = "Authorization";
69
70     @Before
71     public void setUp() {
72         MockitoAnnotations.initMocks(this);
73     }
74
75     @Test
76     public void tesCamundaPost() throws JsonGenerationException,
77     JsonMappingException, IOException {
78
79
80         String responseBody ="{\"links\":[{\"method\":\"GET\",\"href\":\"http://localhost:9080/engine-rest/process-instance/2047c658-37ae-11e5-9505-7a1020524153\",\"rel\":\"self\"}],\"id\":\"2047c658-37ae-11e5-9505-7a1020524153\",\"definitionId\":\"dummy:10:73298961-37ad-11e5-9505-7a1020524153\",\"businessKey\":null,\"caseInstanceId\":null,\"ended\":true,\"suspended\":false}";
81
82         HttpResponse mockResponse = createResponse(200, responseBody);
83         mockHttpClient = Mockito.mock(HttpClient.class);
84         ArgumentCaptor<HttpPost> httpPostCaptor = ArgumentCaptor.forClass(HttpPost.class);
85         Mockito.when(mockHttpClient.execute(Mockito.any(HttpPost.class)))
86         .thenReturn(mockResponse);
87
88         String reqXML = "<xml>test</xml>";
89         String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
90         MockEnvironment environment = new MockEnvironment();
91         
92         environment.setProperty("mso.camundaUR", "yourValue1");
93         environment.setProperty("mso.camundaAuth", "E8E19DD16CC90D2E458E8FF9A884CC0452F8F3EB8E321F96038DE38D5C1B0B02DFAE00B88E2CF6E2A4101AB2C011FC161212EE");
94         environment.setProperty("org.onap.so.adapters.network.encryptionKey", "aa3871669d893c7fb8abbcda31b88b4f");
95  
96         
97         RequestClientFactory reqClientFactory = new RequestClientFactory();
98         reqClientFactory.setEnv(environment);
99         RequestClient requestClient = reqClientFactory.getRequestClient(orchestrationURI);
100         
101         requestClient.setClient(mockHttpClient);
102         HttpResponse response = requestClient.post(reqXML, "reqId", "timeout", "version", null, null);
103
104
105         int statusCode = response.getStatusLine().getStatusCode();
106         assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
107         assertEquals(statusCode, HttpStatus.SC_OK);
108
109   
110         requestClient = reqClientFactory.getRequestClient(orchestrationURI);
111         requestClient.setClient(mockHttpClient);
112         response = requestClient.post(null, "reqId", null, null, null, null);
113         assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
114         assertEquals(statusCode, HttpStatus.SC_OK);
115         verify(mockHttpClient,times(2)).execute(httpPostCaptor.capture());
116         assertThat(httpPostCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)).isNotEmpty();
117         Assert.assertEquals("Basic YXBpaEJwbW46Y2FtdW5kYS1SMTUxMiE=",httpPostCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)[0].getValue());
118     }
119
120     private HttpResponse createResponse(int respStatus,
121                                         String respBody) {
122         HttpResponse response = new BasicHttpResponse(
123                                                       new BasicStatusLine(
124                                                                           new ProtocolVersion("HTTP", 1, 1), respStatus, ""));
125         response.setStatusCode(respStatus);
126         try {
127             response.setEntity(new StringEntity(respBody));
128             response.setHeader("Content-Type", "application/json");
129         } catch (Exception e) {
130             e.printStackTrace();
131         }
132         return response;
133     }
134     
135     public String inputStream(String JsonInput)throws IOException{
136         JsonInput = "src/test/resources/CamundaClientTest" + JsonInput;
137         String input = new String(Files.readAllBytes(Paths.get(JsonInput)));
138         return input;
139     }
140     
141     @Test
142     public void wrapVIDRequestTest() throws IOException{
143         CamundaClient testClient = new CamundaClient();
144         testClient.setUrl("/mso/async/services/CreateGenericALaCarteServiceInstance");
145
146         String requestId = "f7ce78bb-423b-11e7-93f8-0050569a796";
147         boolean isBaseVfModule = true;
148         int recipeTimeout = 10000;
149         String requestAction = "createInstance";
150         String serviceInstanceId = "12345679";
151         String correlationId = "12345679";
152         String vnfId = "234567891";
153         String vfModuleId = "345678912";
154         String volumeGroupId = "456789123";
155         String networkId = "567891234";
156         String configurationId = "678912345";
157         String serviceType = "testService";
158         String vnfType = "testVnf";
159         String vfModuleType = "vfModuleType";
160         String networkType = "networkType";
161         String requestDetails = "{requestDetails: }";
162         String apiVersion = "6";
163         boolean aLaCarte = true;
164         String requestUri = "v7/serviceInstances/assign";
165         String instanceGroupId = "ff305d54-75b4-431b-adb2-eb6b9e5ff000";
166         
167         String testResult = testClient.wrapVIDRequest(requestId, isBaseVfModule, recipeTimeout, requestAction, serviceInstanceId, correlationId,
168                                                 vnfId, vfModuleId, volumeGroupId, networkId, configurationId, serviceType, 
169                                                 vnfType, vfModuleType, networkType, requestDetails, apiVersion, aLaCarte, requestUri, "", instanceGroupId);
170         String expected = inputStream("/WrappedVIDRequest.json");
171         
172         assertEquals(expected, testResult);
173     }
174
175     @Test
176     public void testPost() throws Exception{
177         CamundaClient testClient = new CamundaClient();
178         String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
179         MockEnvironment environment = new MockEnvironment();
180         
181         environment.setProperty("mso.camundaUR", "yourValue1");
182         testClient.setProps(environment);
183         testClient.setClient(mockHttpClient);
184         
185         testClient.setUrl(orchestrationURI);
186         
187         String responseBody ="{\"links\":[{\"method\":\"GET\",\"href\":\"http://localhost:9080/engine-rest/process-instance/2047c658-37ae-11e5-9505-7a1020524153\",\"rel\":\"self\"}],\"id\":\"2047c658-37ae-11e5-9505-7a1020524153\",\"definitionId\":\"dummy:10:73298961-37ad-11e5-9505-7a1020524153\",\"businessKey\":null,\"caseInstanceId\":null,\"ended\":true,\"suspended\":false}";
188         assertNull(testClient.post(responseBody));
189         
190     }
191
192
193 }