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