2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.apihandler.common;
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;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
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;
53 import com.fasterxml.jackson.core.JsonGenerationException;
54 import com.fasterxml.jackson.databind.JsonMappingException;
58 * This class implements test methods of Camunda Beans.
62 public class CamundaClientTest{
67 private HttpClient mockHttpClient;
68 private static final String AUTHORIZATION_HEADER_NAME = "Authorization";
72 MockitoAnnotations.initMocks(this);
76 public void tesCamundaPost() throws JsonGenerationException,
77 JsonMappingException, IOException {
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}";
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);
88 String reqXML = "<xml>test</xml>";
89 String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
90 MockEnvironment environment = new MockEnvironment();
92 environment.setProperty("mso.camundaUR", "yourValue1");
93 environment.setProperty("mso.camundaAuth", "E8E19DD16CC90D2E458E8FF9A884CC0452F8F3EB8E321F96038DE38D5C1B0B02DFAE00B88E2CF6E2A4101AB2C011FC161212EE");
94 environment.setProperty("org.onap.so.adapters.network.encryptionKey", "aa3871669d893c7fb8abbcda31b88b4f");
97 RequestClientFactory reqClientFactory = new RequestClientFactory();
98 reqClientFactory.setEnv(environment);
99 RequestClient requestClient = reqClientFactory.getRequestClient(orchestrationURI);
101 requestClient.setClient(mockHttpClient);
102 HttpResponse response = requestClient.post(reqXML, "reqId", "timeout", "version", null, null);
105 int statusCode = response.getStatusLine().getStatusCode();
106 assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
107 assertEquals(statusCode, HttpStatus.SC_OK);
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());
120 private HttpResponse createResponse(int respStatus,
122 HttpResponse response = new BasicHttpResponse(
124 new ProtocolVersion("HTTP", 1, 1), respStatus, ""));
125 response.setStatusCode(respStatus);
127 response.setEntity(new StringEntity(respBody));
128 response.setHeader("Content-Type", "application/json");
129 } catch (Exception e) {
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)));
142 public void wrapVIDRequestTest() throws IOException{
143 CamundaClient testClient = new CamundaClient();
144 testClient.setUrl("/mso/async/services/CreateGenericALaCarteServiceInstance");
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";
166 String testResult = testClient.wrapVIDRequest(requestId, isBaseVfModule, recipeTimeout, requestAction, serviceInstanceId, correlationId,
167 vnfId, vfModuleId, volumeGroupId, networkId, configurationId, serviceType,
168 vnfType, vfModuleType, networkType, requestDetails, apiVersion, aLaCarte, requestUri, "");
169 String expected = inputStream("/WrappedVIDRequest.json");
171 assertEquals(expected, testResult);
175 public void testPost() throws Exception{
176 CamundaClient testClient = new CamundaClient();
177 String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
178 MockEnvironment environment = new MockEnvironment();
180 environment.setProperty("mso.camundaUR", "yourValue1");
181 testClient.setProps(environment);
182 testClient.setClient(mockHttpClient);
184 testClient.setUrl(orchestrationURI);
186 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}";
187 assertNull(testClient.post(responseBody));