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.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNull;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
33 import org.apache.http.HttpResponse;
34 import org.apache.http.HttpStatus;
35 import org.apache.http.ProtocolVersion;
36 import org.apache.http.client.HttpClient;
37 import org.apache.http.client.methods.HttpPost;
38 import org.apache.http.entity.StringEntity;
39 import org.apache.http.message.BasicHttpResponse;
40 import org.apache.http.message.BasicStatusLine;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.so.apihandler.common.CamundaClient;
47 import org.onap.so.apihandler.common.CommonConstants;
48 import org.onap.so.apihandler.common.RequestClient;
49 import org.onap.so.apihandler.common.RequestClientFactory;
50 import org.springframework.mock.env.MockEnvironment;
52 import com.fasterxml.jackson.core.JsonGenerationException;
53 import com.fasterxml.jackson.databind.JsonMappingException;
57 * This class implements test methods of Camunda Beans.
61 public class CamundaClientTest{
66 private HttpClient mockHttpClient;
70 MockitoAnnotations.initMocks(this);
74 public void tesCamundaPost() throws JsonGenerationException,
75 JsonMappingException, IOException {
78 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}";
80 HttpResponse mockResponse = createResponse(200, responseBody);
81 mockHttpClient = Mockito.mock(HttpClient.class);
82 Mockito.when(mockHttpClient.execute(Mockito.any(HttpPost.class)))
83 .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");
92 RequestClientFactory reqClientFactory = new RequestClientFactory();
93 reqClientFactory.setEnv(environment);
94 RequestClient requestClient = reqClientFactory.getRequestClient(orchestrationURI);
96 requestClient.setClient(mockHttpClient);
97 HttpResponse response = requestClient.post(reqXML, "reqId", "timeout", "version", null, null);
100 int statusCode = response.getStatusLine().getStatusCode();
101 assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
102 assertEquals(statusCode, HttpStatus.SC_OK);
105 requestClient = reqClientFactory.getRequestClient(orchestrationURI);
106 requestClient.setClient(mockHttpClient);
107 response = requestClient.post(null, "reqId", null, null, null, null);
108 assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
109 assertEquals(statusCode, HttpStatus.SC_OK);
112 private HttpResponse createResponse(int respStatus,
114 HttpResponse response = new BasicHttpResponse(
116 new ProtocolVersion("HTTP", 1, 1), respStatus, ""));
117 response.setStatusCode(respStatus);
119 response.setEntity(new StringEntity(respBody));
120 response.setHeader("Content-Type", "application/json");
121 } catch (Exception e) {
127 public String inputStream(String JsonInput)throws IOException{
128 JsonInput = "src/test/resources/CamundaClientTest" + JsonInput;
129 String input = new String(Files.readAllBytes(Paths.get(JsonInput)));
134 public void wrapVIDRequestTest() throws IOException{
135 CamundaClient testClient = new CamundaClient();
136 testClient.setUrl("/mso/async/services/CreateGenericALaCarteServiceInstance");
138 String requestId = "f7ce78bb-423b-11e7-93f8-0050569a796";
139 boolean isBaseVfModule = true;
140 int recipeTimeout = 10000;
141 String requestAction = "createInstance";
142 String serviceInstanceId = "12345679";
143 String correlationId = "12345679";
144 String vnfId = "234567891";
145 String vfModuleId = "345678912";
146 String volumeGroupId = "456789123";
147 String networkId = "567891234";
148 String configurationId = "678912345";
149 String serviceType = "testService";
150 String vnfType = "testVnf";
151 String vfModuleType = "vfModuleType";
152 String networkType = "networkType";
153 String requestDetails = "{requestDetails: }";
154 String apiVersion = "6";
155 boolean aLaCarte = true;
156 String requestUri = "v7/serviceInstances/assign";
158 String testResult = testClient.wrapVIDRequest(requestId, isBaseVfModule, recipeTimeout, requestAction, serviceInstanceId, correlationId,
159 vnfId, vfModuleId, volumeGroupId, networkId, configurationId, serviceType,
160 vnfType, vfModuleType, networkType, requestDetails, apiVersion, aLaCarte, requestUri, "");
161 String expected = inputStream("/WrappedVIDRequest.json");
163 assertEquals(expected, testResult);
167 public void testPost() throws Exception{
168 CamundaClient testClient = new CamundaClient();
169 String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
170 MockEnvironment environment = new MockEnvironment();
172 environment.setProperty("mso.camundaUR", "yourValue1");
173 testClient.setProps(environment);
174 testClient.setClient(mockHttpClient);
176 testClient.setUrl(orchestrationURI);
178 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}";
179 assertNull(testClient.post(responseBody));