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