Containerization feature of SO
[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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.apihandler.common;
22
23
24 import static org.junit.Assert.assertEquals;
25
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29
30 import org.apache.http.HttpResponse;
31 import org.apache.http.HttpStatus;
32 import org.apache.http.ProtocolVersion;
33 import org.apache.http.client.HttpClient;
34 import org.apache.http.client.methods.HttpPost;
35 import org.apache.http.entity.StringEntity;
36 import org.apache.http.message.BasicHttpResponse;
37 import org.apache.http.message.BasicStatusLine;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Mock;
41 import org.mockito.Mockito;
42 import org.mockito.MockitoAnnotations;
43 import org.onap.so.apihandler.common.CamundaClient;
44 import org.onap.so.apihandler.common.CommonConstants;
45 import org.onap.so.apihandler.common.RequestClient;
46 import org.onap.so.apihandler.common.RequestClientFactory;
47 import org.springframework.mock.env.MockEnvironment;
48
49 import com.fasterxml.jackson.core.JsonGenerationException;
50 import com.fasterxml.jackson.databind.JsonMappingException;
51
52
53 /**
54  * This class implements test methods of Camunda Beans.
55  *
56  *
57  */
58 public class CamundaClientTest{
59
60
61
62     @Mock
63     private HttpClient mockHttpClient;
64
65     @Before
66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68     }
69
70     @Test
71     public void tesCamundaPost() throws JsonGenerationException,
72     JsonMappingException, IOException {
73
74
75         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}";
76
77         HttpResponse mockResponse = createResponse(200, responseBody);
78         mockHttpClient = Mockito.mock(HttpClient.class);
79         Mockito.when(mockHttpClient.execute(Mockito.any(HttpPost.class)))
80         .thenReturn(mockResponse);
81
82         String reqXML = "<xml>test</xml>";
83         String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
84         MockEnvironment environment = new MockEnvironment();
85         
86         environment.setProperty("mso.camundaUR", "yourValue1");
87  
88         
89         RequestClientFactory reqClientFactory = new RequestClientFactory();
90         reqClientFactory.setEnv(environment);
91         RequestClient requestClient = reqClientFactory.getRequestClient(orchestrationURI);
92         
93         requestClient.setClient(mockHttpClient);
94         HttpResponse response = requestClient.post(reqXML, "reqId", "timeout", "version", null, null);
95
96
97         int statusCode = response.getStatusLine().getStatusCode();
98         assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
99         assertEquals(statusCode, HttpStatus.SC_OK);
100
101   
102         requestClient = reqClientFactory.getRequestClient(orchestrationURI);
103         requestClient.setClient(mockHttpClient);
104         response = requestClient.post(null, "reqId", null, null, null, null);
105         assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
106         assertEquals(statusCode, HttpStatus.SC_OK);
107     }
108
109     private HttpResponse createResponse(int respStatus,
110                                         String respBody) {
111         HttpResponse response = new BasicHttpResponse(
112                                                       new BasicStatusLine(
113                                                                           new ProtocolVersion("HTTP", 1, 1), respStatus, ""));
114         response.setStatusCode(respStatus);
115         try {
116             response.setEntity(new StringEntity(respBody));
117             response.setHeader("Content-Type", "application/json");
118         } catch (Exception e) {
119             e.printStackTrace();
120         }
121         return response;
122     }
123     
124     public String inputStream(String JsonInput)throws IOException{
125                 JsonInput = "src/test/resources/CamundaClientTest" + JsonInput;
126                 String input = new String(Files.readAllBytes(Paths.get(JsonInput)));
127                 return input;
128         }
129     
130     @Test
131     public void wrapVIDRequestTest() throws IOException{
132         CamundaClient testClient = new CamundaClient();
133         testClient.setUrl("/mso/async/services/CreateGenericALaCarteServiceInstance");
134         
135         String requestId = "f7ce78bb-423b-11e7-93f8-0050569a796";
136         boolean isBaseVfModule = true;
137         int recipeTimeout = 10000;
138         String requestAction = "createInstance";
139         String serviceInstanceId = "12345679";
140         String correlationId = "12345679";
141         String vnfId = "234567891";
142         String vfModuleId = "345678912";
143         String volumeGroupId = "456789123";
144         String networkId = "567891234";
145         String configurationId = "678912345";
146         String serviceType = "testService";
147         String vnfType = "testVnf";
148         String vfModuleType = "vfModuleType";
149         String networkType = "networkType";
150         String requestDetails = "{requestDetails: }";
151         String apiVersion = "6";
152         boolean aLaCarte = true;
153         String requestUri = "v7/serviceInstances/assign";
154         
155         String testResult = testClient.wrapVIDRequest(requestId, isBaseVfModule, recipeTimeout, requestAction, serviceInstanceId, correlationId,
156                                                 vnfId, vfModuleId, volumeGroupId, networkId, configurationId, serviceType, 
157                                                 vnfType, vfModuleType, networkType, requestDetails, apiVersion, aLaCarte, requestUri, "");
158         String expected = inputStream("/WrappedVIDRequest.json");
159         
160         assertEquals(expected, testResult);
161     }
162
163
164
165
166 }