b0517ace3de227780111cb8aeac6d43fa8f39f48
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.process;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.get;
24 import static com.github.tomakehurst.wiremock.client.WireMock.ok;
25 import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
26 import static com.github.tomakehurst.wiremock.client.WireMock.post;
27 import static com.github.tomakehurst.wiremock.client.WireMock.put;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
29 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
30 import static org.assertj.core.api.Assertions.fail;
31 import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareAssertions.assertThat;
32 import com.google.protobuf.Struct;
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.UUID;
36 import org.camunda.bpm.engine.runtime.Execution;
37 import org.camunda.bpm.engine.runtime.ProcessInstance;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers;
41 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader;
42 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
43 import org.onap.so.BaseBPMNTest;
44 import org.onap.so.GrpcNettyServer;
45 import org.onap.so.bpmn.mock.FileUtil;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.beans.factory.annotation.Autowired;
49
50
51 /**
52  * Basic Integration test for createVcpeResCustService_Simplified.bpmn workflow.
53  */
54 public class CreateVcpeResCustServiceSimplifiedTest extends BaseBPMNTest {
55
56     private Logger logger = LoggerFactory.getLogger(getClass());
57
58     private static final String TEST_PROCESSINSTANCE_KEY = "CreateVcpeResCustService_simplified";
59
60     private String testBusinessKey;
61     private String requestObject;
62     private String responseObject;
63     private String msoRequestId;
64
65     @Autowired
66     private GrpcNettyServer grpcNettyServer;
67
68     @Before
69     public void setUp() throws IOException {
70
71         requestObject = FileUtil.readResourceFile("request/" + getClass().getSimpleName() + ".json");
72         responseObject = FileUtil.readResourceFile("response/" + getClass().getSimpleName() + ".json");
73
74         variables.put("bpmnRequest", requestObject);
75
76         /**
77          * This variable indicates that the flow was invoked asynchronously. It's injected by {@link WorkflowProcessor}.
78          */
79         variables.put("isAsyncProcess", "true");
80
81         /**
82          * Temporary solution to add pnfCorrelationId to context. this value is getting from the request to SO api
83          * handler and then convert to CamudaInput
84          */
85         variables.put("pnfCorrelationId", "PNFDemo");
86
87         /**
88          * Create mso-request-id.
89          */
90         msoRequestId = UUID.randomUUID().toString();
91
92         variables.put("mso-request-id", msoRequestId);
93
94         /**
95          * Create Business key for the process instance
96          */
97         testBusinessKey = UUID.randomUUID().toString();
98
99         logger.info("Test the process instance: {} with business key: {}", TEST_PROCESSINSTANCE_KEY, testBusinessKey);
100
101     }
102
103     @Test
104     public void workflow_validInput_expectedOuput() {
105
106         mockCatalogDb();
107         mockRequestDb();
108         mockAai();
109         mockDmaapForPnf();
110
111         ProcessInstance pi =
112                 runtimeService.startProcessInstanceByKey(TEST_PROCESSINSTANCE_KEY, testBusinessKey, variables);
113         assertThat(pi).isNotNull();
114
115         Execution execution = runtimeService.createExecutionQuery().processDefinitionKey("CreateAndActivatePnfResource")
116                 .activityId("WaitForDmaapPnfReadyNotification").singleResult();
117
118         if (!execution.isSuspended() && !execution.isEnded()) {
119             try {
120
121                 runtimeService.signal(execution.getId());
122             } catch (Exception e) {
123                 logger.info(e.getMessage(), e);
124             }
125         }
126
127         assertThat(pi).isStarted().hasPassedInOrder("createVCPE_startEvent", "preProcessRequest_ScriptTask",
128                 "sendSyncAckResponse_ScriptTask", "ScriptTask_0cdtchu", "DecomposeService", "ScriptTask_0lpv2da",
129                 "ScriptTask_1y241p8", "CallActivity_1vc4jeh", "ScriptTask_1y5lvl7", "GeneratePnfUuid", "Task_14l19kv",
130                 "Pnf_Con", "setPONR_ScriptTask", "postProcessAndCompletionRequest_ScriptTask",
131                 "callCompleteMsoProcess_CallActivity", "ScriptTask_2", "CreateVCPE_EndEvent");
132
133         assertThat(pi).isEnded();
134
135         List<ExecutionServiceInput> detailedMessages = grpcNettyServer.getDetailedMessages();
136         assertThat(detailedMessages).hasSize(2);
137         try {
138             checkConfigAssign(detailedMessages.get(0));
139             checkConfigDeploy(detailedMessages.get(1));
140         } catch (Exception e) {
141             e.printStackTrace();
142             fail("ConfigAssign/deploy request exception", e);
143         }
144     }
145
146     private void checkConfigAssign(ExecutionServiceInput executionServiceInput) {
147         ActionIdentifiers actionIdentifiers = executionServiceInput.getActionIdentifiers();
148
149         /**
150          * the fields of actionIdentifiers should match the one in the
151          * response/createVcpeResCustServiceSimplifiedTest_catalogdb.json.
152          */
153         assertThat(actionIdentifiers.getBlueprintName()).matches("test_configuration_restconf");
154         assertThat(actionIdentifiers.getBlueprintVersion()).matches("1.0.0");
155         assertThat(actionIdentifiers.getActionName()).matches("config-assign");
156         assertThat(actionIdentifiers.getMode()).matches("sync");
157
158         CommonHeader commonHeader = executionServiceInput.getCommonHeader();
159         assertThat(commonHeader.getOriginatorId()).matches("SO");
160         assertThat(commonHeader.getRequestId()).matches(msoRequestId);
161
162         Struct payload = executionServiceInput.getPayload();
163         Struct requeststruct = payload.getFieldsOrThrow("config-assign-request").getStructValue();
164
165         assertThat(requeststruct.getFieldsOrThrow("resolution-key").getStringValue()).matches("PNFDemo");
166         Struct propertiesStruct = requeststruct.getFieldsOrThrow("config-assign-properties").getStructValue();
167
168         assertThat(propertiesStruct.getFieldsOrThrow("pnf-name").getStringValue()).matches("PNFDemo");
169         assertThat(propertiesStruct.getFieldsOrThrow("service-model-uuid").getStringValue())
170                 .matches("f2daaac6-5017-4e1e-96c8-6a27dfbe1421");
171         assertThat(propertiesStruct.getFieldsOrThrow("pnf-customization-uuid").getStringValue())
172                 .matches("68dc9a92-214c-11e7-93ae-92361f002680");
173     }
174
175     private void checkConfigDeploy(ExecutionServiceInput executionServiceInput) {
176         ActionIdentifiers actionIdentifiers = executionServiceInput.getActionIdentifiers();
177
178         /**
179          * the fields of actionIdentifiers should match the one in the
180          * response/createVcpeResCustServiceSimplifiedTest_catalogdb.json.
181          */
182         assertThat(actionIdentifiers.getBlueprintName()).matches("test_configuration_restconf");
183         assertThat(actionIdentifiers.getBlueprintVersion()).matches("1.0.0");
184         assertThat(actionIdentifiers.getActionName()).matches("config-deploy");
185         assertThat(actionIdentifiers.getMode()).matches("async");
186
187         CommonHeader commonHeader = executionServiceInput.getCommonHeader();
188         assertThat(commonHeader.getOriginatorId()).matches("SO");
189         assertThat(commonHeader.getRequestId()).matches(msoRequestId);
190
191         Struct payload = executionServiceInput.getPayload();
192         Struct requeststruct = payload.getFieldsOrThrow("config-deploy-request").getStructValue();
193
194         assertThat(requeststruct.getFieldsOrThrow("resolution-key").getStringValue()).matches("PNFDemo");
195         Struct propertiesStruct = requeststruct.getFieldsOrThrow("config-deploy-properties").getStructValue();
196
197         assertThat(propertiesStruct.getFieldsOrThrow("pnf-name").getStringValue()).matches("PNFDemo");
198         assertThat(propertiesStruct.getFieldsOrThrow("service-model-uuid").getStringValue())
199                 .matches("f2daaac6-5017-4e1e-96c8-6a27dfbe1421");
200         assertThat(propertiesStruct.getFieldsOrThrow("pnf-customization-uuid").getStringValue())
201                 .matches("68dc9a92-214c-11e7-93ae-92361f002680");
202
203         /**
204          * IP addresses match the OAM ip addresses from AAI.
205          */
206         assertThat(propertiesStruct.getFieldsOrThrow("pnf-ipv4-address").getStringValue()).matches("1.1.1.1");
207         assertThat(propertiesStruct.getFieldsOrThrow("pnf-ipv6-address").getStringValue()).matches("::/128");
208     }
209
210     /**
211      * Mock the Dmaap Rest interface for Pnf topic.
212      */
213     private void mockDmaapForPnf() {
214
215         String pnfResponse = "[{\"correlationId\": \"PNFDemo\",\"key1\":\"value1\"}]";
216
217         /**
218          * Get the events from PNF topic
219          */
220         wireMockServer
221                 .stubFor(get(urlPathMatching("/events/pnfReady/consumerGroup.*")).willReturn(okJson(pnfResponse)));
222     }
223
224     private void mockAai() {
225
226         String aaiResponse = "{\n" + "  \"results\": [\n" + "    {\n"
227                 + "      \"resource-type\": \"service-instance\",\n"
228                 + "      \"resource-link\": \"https://localhost:8443/aai/v15/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/key3\"\n"
229                 + "    }\n" + "  ]\n" + "}";
230
231         String aaiPnfEntry = "{  \n" + "   \"pnf-name\":\"PNFDemo\",\n" + "   \"pnf-id\":\"testtest\",\n"
232                 + "   \"in-maint\":true,\n" + "   \"resource-version\":\"1541720264047\",\n"
233                 + "   \"ipaddress-v4-oam\":\"1.1.1.1\",\n" + "   \"ipaddress-v6-oam\":\"::/128\"\n" + "}";
234
235         /**
236          * Get the AAI entry for globalCustomerId as specified in the request file.
237          */
238         wireMockServer.stubFor(
239                 get(urlPathMatching("/aai/v15/business/customers/customer/ADemoCustomerInCiti.*")).willReturn(ok()));
240
241         /**
242          * PUT the service to AAI with globalCustomerId, service type as specified in the request file. Service instance
243          * id is generated during runtime, REGEX is used to represent the information.
244          */
245         wireMockServer.stubFor(put(urlPathMatching(
246                 "/aai/v15/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/.*")));
247
248         wireMockServer.stubFor(get(urlPathMatching(
249                 "/aai/v15/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/.*"))
250                         .willReturn(okJson(aaiResponse)));
251
252         /**
253          * Get the service from AAI
254          */
255         wireMockServer.stubFor(get(urlPathMatching("/aai/v15/nodes/service-instances/service-instance/.*"))
256                 .willReturn(okJson(aaiResponse)));
257
258         /**
259          * Put the project as specified in the request file to AAI.
260          */
261         wireMockServer.stubFor(put(urlEqualTo("/aai/v15/business/projects/project/Project-Demonstration")));
262
263         /**
264          * GET the project as specified in the request file to AAI.
265          */
266         wireMockServer.stubFor(
267                 get(urlPathMatching("/aai/v15/business/projects/project/Project-Demonstration")).willReturn(ok()));
268
269         /**
270          * PUT the PNF correlation ID to AAI.
271          */
272         wireMockServer.stubFor(put(urlEqualTo("/aai/v15/network/pnfs/pnf/PNFDemo")));
273
274         /**
275          * Get the PNF entry from AAI.
276          */
277         wireMockServer.stubFor(get(urlEqualTo("/aai/v15/network/pnfs/pnf/PNFDemo")).willReturn(okJson(aaiPnfEntry)));
278
279         /**
280          * Put the PNF relationship
281          */
282         wireMockServer.stubFor(put(
283                 urlEqualTo("/aai/v15/business/projects/project/Project-Demonstration/relationship-list/relationship")));
284     }
285
286     /**
287      * Mock the catalobdb rest interface.
288      */
289     private void mockCatalogDb() {
290
291         String catalogdbClientResponse =
292                 FileUtil.readResourceFile("response/" + getClass().getSimpleName() + "_catalogdb.json");
293
294         /**
295          * Return valid json for the model UUID in the request file.
296          */
297         wireMockServer
298                 .stubFor(get(urlEqualTo("/v2/serviceResources?serviceModelUuid=f2daaac6-5017-4e1e-96c8-6a27dfbe1421"))
299                         .willReturn(okJson(responseObject)));
300
301         /**
302          * Return valid json for the service model InvariantUUID as specified in the request file.
303          */
304         wireMockServer.stubFor(
305                 get(urlEqualTo("/v2/serviceResources?serviceModelInvariantUuid=539b7a2f-9524-4dbf-9eee-f2e05521df3f"))
306                         .willReturn(okJson(responseObject)));
307
308         /**
309          * Return valid spring data rest json for the service model UUID as specified in the request file.
310          */
311         wireMockServer.stubFor(get(urlEqualTo(
312                 "/pnfResourceCustomization/search/findPnfResourceCustomizationByModelUuid?SERVICE_MODEL_UUID=f2daaac6-5017-4e1e-96c8-6a27dfbe1421"))
313                         .willReturn(okJson(catalogdbClientResponse)));
314     }
315
316     private void mockRequestDb() {
317         wireMockServer.stubFor(post(urlEqualTo("/dbadapters/RequestsDbAdapter")).willReturn(ok()));
318     }
319
320 }