5b095a9983983537a0fd04b726b7921e0d115fd6
[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.assertThat;
31 import static org.assertj.core.api.Assertions.fail;
32 import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareAssertions.assertThat;
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.UUID;
36 import org.camunda.bpm.engine.runtime.ProcessInstance;
37 import org.junit.Before;
38 import org.junit.Ignore;
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 import com.google.protobuf.Struct;
50
51
52 /**
53  * Basic Integration test for createVcpeResCustService_Simplified.bpmn workflow.
54  */
55 public class CreateVcpeResCustServiceSimplifiedTest extends BaseBPMNTest {
56
57     private Logger logger = LoggerFactory.getLogger(getClass());
58
59     private static final long WORKFLOW_WAIT_TIME = 1000L;
60     private static final int DMAAP_DELAY_TIME_MS = 2000;
61
62     private static final String TEST_PROCESSINSTANCE_KEY = "CreateVcpeResCustService_simplified";
63
64     private String testBusinessKey;
65     private String requestObject;
66     private String responseObject;
67     private String msoRequestId;
68
69     @Autowired
70     private GrpcNettyServer grpcNettyServer;
71
72     @Before
73     public void setUp() throws IOException {
74
75         requestObject = FileUtil.readResourceFile("request/" + getClass().getSimpleName() + ".json");
76         responseObject = FileUtil.readResourceFile("response/" + getClass().getSimpleName() + ".json");
77
78         variables.put("bpmnRequest", requestObject);
79
80         /**
81          * This variable indicates that the flow was invoked asynchronously. It's injected by {@link WorkflowProcessor}.
82          */
83         variables.put("isAsyncProcess", "true");
84
85         /**
86          * Temporary solution to add pnfCorrelationId to context. this value is getting from the request to SO api
87          * 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     @Ignore
108     @Test
109     public void workflow_validInput_expectedOuput() throws InterruptedException {
110
111         mockCatalogDb();
112         mockRequestDb();
113         mockAai();
114         mockDmaapForPnf();
115
116         ProcessInstance pi =
117                 runtimeService.startProcessInstanceByKey(TEST_PROCESSINSTANCE_KEY, testBusinessKey, variables);
118
119         int waitCount = 10;
120         while (!isProcessInstanceEnded() && waitCount >= 0) {
121             Thread.sleep(WORKFLOW_WAIT_TIME);
122             waitCount--;
123         }
124
125         assertThat(pi).isEnded().hasPassedInOrder("createVCPE_startEvent", "preProcessRequest_ScriptTask",
126                 "sendSyncAckResponse_ScriptTask", "ScriptTask_0cdtchu", "DecomposeService", "ScriptTask_0lpv2da",
127                 "ScriptTask_1y241p8", "CallActivity_1vc4jeh", "ScriptTask_1y5lvl7", "GeneratePnfUuid", "Task_14l19kv",
128                 "Pnf_Con", "setPONR_ScriptTask", "postProcessAndCompletionRequest_ScriptTask",
129                 "callCompleteMsoProcess_CallActivity", "ScriptTask_2", "CreateVCPE_EndEvent");
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 boolean isProcessInstanceEnded() {
143         return runtimeService.createProcessInstanceQuery().processDefinitionKey(TEST_PROCESSINSTANCE_KEY)
144                 .singleResult() == null;
145     }
146
147     private void checkConfigAssign(ExecutionServiceInput executionServiceInput) {
148
149         logger.info("Checking the configAssign request");
150         ActionIdentifiers actionIdentifiers = executionServiceInput.getActionIdentifiers();
151
152         /**
153          * the fields of actionIdentifiers should match the one in the
154          * response/createVcpeResCustServiceSimplifiedTest_catalogdb.json.
155          */
156         assertThat(actionIdentifiers.getBlueprintName()).isEqualTo("test_configuration_restconf");
157         assertThat(actionIdentifiers.getBlueprintVersion()).isEqualTo("1.0.0");
158         assertThat(actionIdentifiers.getActionName()).isEqualTo("config-assign");
159         assertThat(actionIdentifiers.getMode()).isEqualTo("sync");
160
161         CommonHeader commonHeader = executionServiceInput.getCommonHeader();
162         assertThat(commonHeader.getOriginatorId()).isEqualTo("SO");
163         assertThat(commonHeader.getRequestId()).isEqualTo(msoRequestId);
164
165         Struct payload = executionServiceInput.getPayload();
166         Struct requeststruct = payload.getFieldsOrThrow("config-assign-request").getStructValue();
167
168         assertThat(requeststruct.getFieldsOrThrow("resolution-key").getStringValue()).isEqualTo("PNFDemo");
169         Struct propertiesStruct = requeststruct.getFieldsOrThrow("config-assign-properties").getStructValue();
170
171         assertThat(propertiesStruct.getFieldsOrThrow("pnf-name").getStringValue()).isEqualTo("PNFDemo");
172         assertThat(propertiesStruct.getFieldsOrThrow("service-model-uuid").getStringValue())
173                 .isEqualTo("f2daaac6-5017-4e1e-96c8-6a27dfbe1421");
174         assertThat(propertiesStruct.getFieldsOrThrow("pnf-customization-uuid").getStringValue())
175                 .isEqualTo("68dc9a92-214c-11e7-93ae-92361f002680");
176     }
177
178     private void checkConfigDeploy(ExecutionServiceInput executionServiceInput) {
179
180         logger.info("Checking the configDeploy request");
181         ActionIdentifiers actionIdentifiers = executionServiceInput.getActionIdentifiers();
182
183         /**
184          * the fields of actionIdentifiers should match the one in the
185          * response/createVcpeResCustServiceSimplifiedTest_catalogdb.json.
186          */
187         assertThat(actionIdentifiers.getBlueprintName()).isEqualTo("test_configuration_restconf");
188         assertThat(actionIdentifiers.getBlueprintVersion()).isEqualTo("1.0.0");
189         assertThat(actionIdentifiers.getActionName()).isEqualTo("config-deploy");
190         assertThat(actionIdentifiers.getMode()).isEqualTo("async");
191
192         CommonHeader commonHeader = executionServiceInput.getCommonHeader();
193         assertThat(commonHeader.getOriginatorId()).isEqualTo("SO");
194         assertThat(commonHeader.getRequestId()).isEqualTo(msoRequestId);
195
196         Struct payload = executionServiceInput.getPayload();
197         Struct requeststruct = payload.getFieldsOrThrow("config-deploy-request").getStructValue();
198
199         assertThat(requeststruct.getFieldsOrThrow("resolution-key").getStringValue()).isEqualTo("PNFDemo");
200         Struct propertiesStruct = requeststruct.getFieldsOrThrow("config-deploy-properties").getStructValue();
201
202         assertThat(propertiesStruct.getFieldsOrThrow("pnf-name").getStringValue()).isEqualTo("PNFDemo");
203         assertThat(propertiesStruct.getFieldsOrThrow("service-model-uuid").getStringValue())
204                 .isEqualTo("f2daaac6-5017-4e1e-96c8-6a27dfbe1421");
205         assertThat(propertiesStruct.getFieldsOrThrow("pnf-customization-uuid").getStringValue())
206                 .isEqualTo("68dc9a92-214c-11e7-93ae-92361f002680");
207
208         /**
209          * IP addresses match the OAM ip addresses from AAI.
210          */
211         assertThat(propertiesStruct.getFieldsOrThrow("pnf-ipv4-address").getStringValue()).isEqualTo("1.1.1.1");
212         assertThat(propertiesStruct.getFieldsOrThrow("pnf-ipv6-address").getStringValue()).isEqualTo("::/128");
213     }
214
215     /**
216      * Mock the Dmaap Rest interface for Pnf topic.
217      */
218     private void mockDmaapForPnf() {
219
220         String pnfResponse = "[{\"correlationId\": \"PNFDemo\",\"key1\":\"value1\"}]";
221
222         /**
223          * Get the events from PNF topic
224          */
225         wireMockServer.stubFor(get(urlPathMatching("/events/pnfReady/consumerGroup.*"))
226                 .willReturn(okJson(pnfResponse).withFixedDelay(DMAAP_DELAY_TIME_MS)));
227     }
228
229     private void mockAai() {
230
231         String aaiResponse = "{\n" + "  \"results\": [\n" + "    {\n"
232                 + "      \"resource-type\": \"service-instance\",\n"
233                 + "      \"resource-link\": \"https://localhost:8443/aai/v15/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/key3\"\n"
234                 + "    }\n" + "  ]\n" + "}";
235
236         String aaiPnfEntry = "{  \n" + "   \"pnf-name\":\"PNFDemo\",\n" + "   \"pnf-id\":\"testtest\",\n"
237                 + "   \"in-maint\":true,\n" + "   \"resource-version\":\"1541720264047\",\n"
238                 + "   \"ipaddress-v4-oam\":\"1.1.1.1\",\n" + "   \"ipaddress-v6-oam\":\"::/128\"\n" + "}";
239
240         /**
241          * Get the AAI entry for globalCustomerId as specified in the request file.
242          */
243         wireMockServer.stubFor(
244                 get(urlPathMatching("/aai/v15/business/customers/customer/ADemoCustomerInCiti.*")).willReturn(ok()));
245
246         /**
247          * PUT the service to AAI with globalCustomerId, service type as specified in the request file. Service instance
248          * id is generated during runtime, REGEX is used to represent the information.
249          */
250         wireMockServer.stubFor(put(urlPathMatching(
251                 "/aai/v15/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/.*")));
252
253         wireMockServer.stubFor(get(urlPathMatching(
254                 "/aai/v15/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/.*"))
255                         .willReturn(okJson(aaiResponse)));
256
257         /**
258          * Get the service from AAI
259          */
260         wireMockServer.stubFor(get(urlPathMatching("/aai/v15/nodes/service-instances/service-instance/.*"))
261                 .willReturn(okJson(aaiResponse)));
262
263         /**
264          * Put the project as specified in the request file to AAI.
265          */
266         wireMockServer.stubFor(put(urlEqualTo("/aai/v15/business/projects/project/Project-Demonstration")));
267
268         /**
269          * GET the project as specified in the request file to AAI.
270          */
271         wireMockServer.stubFor(
272                 get(urlPathMatching("/aai/v15/business/projects/project/Project-Demonstration")).willReturn(ok()));
273
274         /**
275          * PUT the PNF correlation ID to AAI.
276          */
277         wireMockServer.stubFor(put(urlEqualTo("/aai/v15/network/pnfs/pnf/PNFDemo")));
278
279         /**
280          * Get the PNF entry from AAI.
281          */
282         wireMockServer.stubFor(get(urlEqualTo("/aai/v15/network/pnfs/pnf/PNFDemo")).willReturn(okJson(aaiPnfEntry)));
283
284         /**
285          * Put the PNF relationship
286          */
287         wireMockServer.stubFor(put(
288                 urlEqualTo("/aai/v15/business/projects/project/Project-Demonstration/relationship-list/relationship")));
289     }
290
291     /**
292      * Mock the catalobdb rest interface.
293      */
294     private void mockCatalogDb() {
295
296         String catalogdbClientResponse =
297                 FileUtil.readResourceFile("response/" + getClass().getSimpleName() + "_catalogdb.json");
298
299         /**
300          * Return valid json for the model UUID in the request file.
301          */
302         wireMockServer
303                 .stubFor(get(urlEqualTo("/v2/serviceResources?serviceModelUuid=f2daaac6-5017-4e1e-96c8-6a27dfbe1421"))
304                         .willReturn(okJson(responseObject)));
305
306         /**
307          * Return valid json for the service model InvariantUUID as specified in the request file.
308          */
309         wireMockServer.stubFor(
310                 get(urlEqualTo("/v2/serviceResources?serviceModelInvariantUuid=539b7a2f-9524-4dbf-9eee-f2e05521df3f"))
311                         .willReturn(okJson(responseObject)));
312
313         /**
314          * Return valid spring data rest json for the service model UUID as specified in the request file.
315          */
316         wireMockServer.stubFor(get(urlEqualTo(
317                 "/pnfResourceCustomization/search/findPnfResourceCustomizationByModelUuid?SERVICE_MODEL_UUID=f2daaac6-5017-4e1e-96c8-6a27dfbe1421"))
318                         .willReturn(okJson(catalogdbClientResponse)));
319     }
320
321     private void mockRequestDb() {
322         wireMockServer.stubFor(post(urlEqualTo("/dbadapters/RequestsDbAdapter")).willReturn(ok()));
323     }
324
325 }