7a4b587fd2c07cf1230e8e77d28106017254b08e
[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.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNull;
28
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32
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;
51
52 import com.fasterxml.jackson.core.JsonGenerationException;
53 import com.fasterxml.jackson.databind.JsonMappingException;
54
55
56 /**
57  * This class implements test methods of Camunda Beans.
58  *
59  *
60  */
61 public class CamundaClientTest{
62
63
64
65     @Mock
66     private HttpClient mockHttpClient;
67
68     @Before
69     public void setUp() {
70         MockitoAnnotations.initMocks(this);
71     }
72
73     @Test
74     public void tesCamundaPost() throws JsonGenerationException,
75     JsonMappingException, IOException {
76
77
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}";
79
80         HttpResponse mockResponse = createResponse(200, responseBody);
81         mockHttpClient = Mockito.mock(HttpClient.class);
82         Mockito.when(mockHttpClient.execute(Mockito.any(HttpPost.class)))
83         .thenReturn(mockResponse);
84
85         String reqXML = "<xml>test</xml>";
86         String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
87         MockEnvironment environment = new MockEnvironment();
88         
89         environment.setProperty("mso.camundaUR", "yourValue1");
90  
91         
92         RequestClientFactory reqClientFactory = new RequestClientFactory();
93         reqClientFactory.setEnv(environment);
94         RequestClient requestClient = reqClientFactory.getRequestClient(orchestrationURI);
95         
96         requestClient.setClient(mockHttpClient);
97         HttpResponse response = requestClient.post(reqXML, "reqId", "timeout", "version", null, null);
98
99
100         int statusCode = response.getStatusLine().getStatusCode();
101         assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
102         assertEquals(statusCode, HttpStatus.SC_OK);
103
104   
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);
110     }
111
112     private HttpResponse createResponse(int respStatus,
113                                         String respBody) {
114         HttpResponse response = new BasicHttpResponse(
115                                                       new BasicStatusLine(
116                                                                           new ProtocolVersion("HTTP", 1, 1), respStatus, ""));
117         response.setStatusCode(respStatus);
118         try {
119             response.setEntity(new StringEntity(respBody));
120             response.setHeader("Content-Type", "application/json");
121         } catch (Exception e) {
122             e.printStackTrace();
123         }
124         return response;
125     }
126     
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)));
130         return input;
131     }
132     
133     @Test
134     public void wrapVIDRequestTest() throws IOException{
135         CamundaClient testClient = new CamundaClient();
136         testClient.setUrl("/mso/async/services/CreateGenericALaCarteServiceInstance");
137         
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";
157         
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");
162         
163         assertEquals(expected, testResult);
164     }
165
166     @Test
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();
171         
172         environment.setProperty("mso.camundaUR", "yourValue1");
173         testClient.setProps(environment);
174         testClient.setClient(mockHttpClient);
175         
176         testClient.setUrl(orchestrationURI);
177         
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));
180         
181     }
182
183
184 }