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