2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2018 IBM.
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.so.apihandler.common;
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNull;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35 import org.apache.http.HttpResponse;
36 import org.apache.http.HttpStatus;
37 import org.apache.http.ProtocolVersion;
38 import org.apache.http.client.HttpClient;
39 import org.apache.http.client.methods.HttpPost;
40 import org.apache.http.entity.StringEntity;
41 import org.apache.http.message.BasicHttpResponse;
42 import org.apache.http.message.BasicStatusLine;
43 import org.junit.Assert;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.mockito.ArgumentCaptor;
47 import org.mockito.Mock;
48 import org.mockito.Mockito;
49 import org.mockito.MockitoAnnotations;
50 import org.springframework.mock.env.MockEnvironment;
51 import com.fasterxml.jackson.core.JsonGenerationException;
52 import com.fasterxml.jackson.databind.JsonMappingException;
56 * This class implements test methods of Camunda Beans.
60 public class CamundaClientTest {
65 private HttpClient mockHttpClient;
66 private static final String AUTHORIZATION_HEADER_NAME = "Authorization";
70 MockitoAnnotations.initMocks(this);
74 public void tesCamundaPost() throws JsonGenerationException, JsonMappingException, IOException {
78 "{\"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}";
80 HttpResponse mockResponse = createResponse(200, responseBody);
81 mockHttpClient = Mockito.mock(HttpClient.class);
82 ArgumentCaptor<HttpPost> httpPostCaptor = ArgumentCaptor.forClass(HttpPost.class);
83 Mockito.when(mockHttpClient.execute(Mockito.any(HttpPost.class))).thenReturn(mockResponse);
85 String reqXML = "<xml>test</xml>";
86 String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
87 MockEnvironment environment = new MockEnvironment();
89 environment.setProperty("mso.camundaUR", "yourValue1");
90 environment.setProperty("mso.camundaAuth",
91 "E8E19DD16CC90D2E458E8FF9A884CC0452F8F3EB8E321F96038DE38D5C1B0B02DFAE00B88E2CF6E2A4101AB2C011FC161212EE");
92 environment.setProperty("org.onap.so.adapters.network.encryptionKey", "aa3871669d893c7fb8abbcda31b88b4f");
95 RequestClientFactory reqClientFactory = new RequestClientFactory();
96 reqClientFactory.setEnv(environment);
97 RequestClient requestClient = reqClientFactory.getRequestClient(orchestrationURI);
99 requestClient.setClient(mockHttpClient);
100 HttpResponse response = requestClient.post(reqXML, "reqId", "timeout", "version", null, null);
103 int statusCode = response.getStatusLine().getStatusCode();
104 assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
105 assertEquals(statusCode, HttpStatus.SC_OK);
108 requestClient = reqClientFactory.getRequestClient(orchestrationURI);
109 requestClient.setClient(mockHttpClient);
110 response = requestClient.post(null, "reqId", null, null, null, null);
111 assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
112 assertEquals(statusCode, HttpStatus.SC_OK);
113 verify(mockHttpClient, times(2)).execute(httpPostCaptor.capture());
114 assertThat(httpPostCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)).isNotEmpty();
115 Assert.assertEquals("Basic YXBpaEJwbW46Y2FtdW5kYS1SMTUxMiE=",
116 httpPostCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)[0].getValue());
119 private HttpResponse createResponse(int respStatus, String respBody) {
120 HttpResponse response =
121 new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), respStatus, ""));
122 response.setStatusCode(respStatus);
124 response.setEntity(new StringEntity(respBody));
125 response.setHeader("Content-Type", "application/json");
126 } catch (Exception e) {
132 public String inputStream(String JsonInput) throws IOException {
133 JsonInput = "src/test/resources/CamundaClientTest" + JsonInput;
134 String input = new String(Files.readAllBytes(Paths.get(JsonInput)));
139 public void wrapVIDRequestTest() throws IOException {
140 CamundaClient testClient = new CamundaClient();
141 testClient.setUrl("/mso/async/services/CreateGenericALaCarteServiceInstance");
143 String requestId = "f7ce78bb-423b-11e7-93f8-0050569a796";
144 boolean isBaseVfModule = true;
145 int recipeTimeout = 10000;
146 String requestAction = "createInstance";
147 String serviceInstanceId = "12345679";
148 String pnfCorrelationId = "12345679";
149 String vnfId = "234567891";
150 String vfModuleId = "345678912";
151 String volumeGroupId = "456789123";
152 String networkId = "567891234";
153 String configurationId = "678912345";
154 String serviceType = "testService";
155 String vnfType = "testVnf";
156 String vfModuleType = "vfModuleType";
157 String networkType = "networkType";
158 String requestDetails = "{requestDetails: }";
159 String apiVersion = "6";
160 boolean aLaCarte = true;
161 String requestUri = "v7/serviceInstances/assign";
162 String instanceGroupId = "ff305d54-75b4-431b-adb2-eb6b9e5ff000";
164 String testResult = testClient.wrapVIDRequest(requestId, isBaseVfModule, recipeTimeout, requestAction,
165 serviceInstanceId, pnfCorrelationId, vnfId, vfModuleId, volumeGroupId, networkId, configurationId,
166 serviceType, vnfType, vfModuleType, networkType, requestDetails, apiVersion, aLaCarte, requestUri, "",
168 String expected = inputStream("/WrappedVIDRequest.json");
170 assertEquals(expected, testResult);
174 public void testPost() throws Exception {
175 CamundaClient testClient = new CamundaClient();
176 String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
177 MockEnvironment environment = new MockEnvironment();
179 environment.setProperty("mso.camundaUR", "yourValue1");
180 testClient.setProps(environment);
181 testClient.setClient(mockHttpClient);
183 testClient.setUrl(orchestrationURI);
185 String responseBody =
186 "{\"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}";
187 assertNull(testClient.post(responseBody));
192 public void testPostWithRequestClientParameter() throws Exception {
193 CamundaClient testClient = new CamundaClient();
194 String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
195 HttpResponse mockResponse = createResponse(200, "{}");
196 mockHttpClient = Mockito.mock(HttpClient.class);
197 Mockito.when(mockHttpClient.execute(Mockito.any(HttpPost.class))).thenReturn(mockResponse);
199 testClient.setClient(mockHttpClient);
200 testClient.setUrl(orchestrationURI);
202 HttpResponse response = testClient.post(new RequestClientParameter.Builder().build());
204 assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());