69927e212621154ec4c8efa267a455ee63fa29e5
[so.git] /
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.openecomp.mso.camunda.tests;
22
23
24 import static org.junit.Assert.assertEquals;
25
26 import java.io.IOException;
27
28 import org.apache.http.HttpResponse;
29 import org.apache.http.HttpStatus;
30 import org.apache.http.ProtocolVersion;
31 import org.apache.http.client.HttpClient;
32 import org.apache.http.client.methods.HttpPost;
33 import org.apache.http.entity.StringEntity;
34 import org.apache.http.message.BasicHttpResponse;
35 import org.apache.http.message.BasicStatusLine;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 import org.openecomp.mso.apihandler.common.CommonConstants;
42 import org.openecomp.mso.apihandler.common.RequestClient;
43 import org.openecomp.mso.apihandler.common.RequestClientFactory;
44 import org.openecomp.mso.apihandler.common.RequestClientParamater;
45 import org.openecomp.mso.properties.MsoJavaProperties;
46
47 /**
48  * This class implements test methods of Camunda Beans.
49  *
50  *
51  */
52 public class CamundaClientTest {
53
54
55
56     @Mock
57     private HttpClient mockHttpClient;
58
59     @Before
60     public void setUp() {
61         MockitoAnnotations.initMocks(this);
62     }
63
64     @Test
65     public void tesCamundaPost() throws IOException {
66         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}";
67
68         HttpResponse mockResponse = createResponse(200, responseBody);
69         mockHttpClient = Mockito.mock(HttpClient.class);
70         Mockito.when(mockHttpClient.execute(Mockito.any(HttpPost.class)))
71         .thenReturn(mockResponse);
72
73         String reqXML = "<xml>test</xml>";
74         String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
75
76         MsoJavaProperties props = new MsoJavaProperties();
77         props.setProperty(CommonConstants.CAMUNDA_URL, "http://localhost:8089");
78
79         RequestClient requestClient = RequestClientFactory.getRequestClient(orchestrationURI, props);
80         requestClient.setClient(mockHttpClient);
81         HttpResponse response = requestClient.post(reqXML, "reqId", "timeout", "version", null, null);
82
83
84         int statusCode = response.getStatusLine().getStatusCode();
85         assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
86         assertEquals(statusCode, HttpStatus.SC_OK);
87
88         props.setProperty (CommonConstants.CAMUNDA_AUTH, "ABCD1234");
89         requestClient = RequestClientFactory.getRequestClient(orchestrationURI, props);
90         requestClient.setClient(mockHttpClient);
91         response = requestClient.post(null, "reqId", null, null, null, null);
92         assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
93         assertEquals(statusCode, HttpStatus.SC_OK);
94     }
95
96     @Test
97     public void testCamundaPostJson() throws IOException {
98         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}";
99
100         HttpResponse mockResponse = createResponse(200, responseBody);
101         mockHttpClient = Mockito.mock(HttpClient.class);
102         Mockito.when(mockHttpClient.execute(Mockito.any(HttpPost.class)))
103                 .thenReturn(mockResponse);
104         String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
105
106         MsoJavaProperties props = new MsoJavaProperties();
107         props.setProperty(CommonConstants.CAMUNDA_URL, "http://localhost:8089");
108
109         RequestClient requestClient = RequestClientFactory.getRequestClient(orchestrationURI, props);
110         requestClient.setClient(mockHttpClient);
111         HttpResponse response = requestClient.post(createParams());
112         assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
113         assertEquals(response.getStatusLine().getStatusCode(), HttpStatus.SC_OK);
114     }
115
116     private HttpResponse createResponse(int respStatus,
117                                         String respBody) {
118         HttpResponse response = new BasicHttpResponse(
119                                                       new BasicStatusLine(
120                                                                           new ProtocolVersion("HTTP", 1, 1), respStatus, ""));
121         response.setStatusCode(respStatus);
122         try {
123             response.setEntity(new StringEntity(respBody));
124             response.setHeader("Content-Type", "application/json");
125         } catch (Exception e) {
126             e.printStackTrace();
127         }
128         return response;
129     }
130
131     private RequestClientParamater createParams(){
132         return new RequestClientParamater.Builder().setRequestId("mso-req-id").setBaseVfModule(false).
133                 setRecipeTimeout(180).setRequestAction("createInstance").setServiceInstanceId("svc-inst-id").
134                 setVnfId("vnf-id").setVfModuleId("vf-module-id").setVolumeGroupId("vg-id").setNetworkId("nw-id").
135                 setConfigurationId("conf-id").setServiceType("svc-type").setVnfType("vnf-type").
136                 setVfModuleType("vf-module-type").setNetworkType("nw-type").setRequestDetails("").
137                 setRecipeParamXsd("").build();
138     }
139
140
141
142
143
144 }