PNF WF post instantiation configuration
[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.put;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
29 import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareAssertions.assertThat;
30
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.UUID;
34 import org.camunda.bpm.engine.runtime.Execution;
35 import org.camunda.bpm.engine.runtime.ProcessInstance;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.so.BaseBPMNTest;
39 import org.onap.so.GrpcNettyServer;
40 import org.onap.so.bpmn.mock.FileUtil;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44
45
46 /**
47  * Basic Integration test for createVcpeResCustService_Simplified.bpmn workflow.
48  */
49 public class CreateVcpeResCustServiceSimplifiedTest extends BaseBPMNTest {
50
51     private Logger logger = LoggerFactory.getLogger(getClass());
52
53     private static final String TEST_PROCESSINSTANCE_KEY = "CreateVcpeResCustService_simplified";
54
55     private String testBusinessKey;
56     private String requestObject;
57     private String responseObject;
58
59     @Autowired
60     private GrpcNettyServer grpcNettyServer;
61
62     @Before
63     public void setUp() throws IOException {
64
65         requestObject = FileUtil.readResourceFile("request/" + getClass().getSimpleName() + ".json");
66         responseObject = FileUtil.readResourceFile("response/" + getClass().getSimpleName() + ".json");
67
68         variables.put("bpmnRequest", requestObject);
69
70         /**
71          * This variable indicates that the flow was invoked asynchronously.
72          * It's injected by {@link WorkflowProcessor}.
73          */
74         variables.put("isAsyncProcess", "true");
75
76         /**
77          * Temporary solution to add pnfCorrelationId to context.
78          * this value is getting from the request to SO api handler and then convert to CamudaInput
79          */
80         variables.put("pnfCorrelationId", "PNFDemo");
81
82         /**
83          * Create mso-request-id.
84          */
85         String msoRequestId = UUID.randomUUID().toString();
86
87         variables.put("mso-request-id", msoRequestId);
88
89         /**
90          * Create Business key for the process instance
91          */
92         testBusinessKey = UUID.randomUUID().toString();
93
94         logger.info("Test the process instance: {} with business key: {}", TEST_PROCESSINSTANCE_KEY, testBusinessKey);
95
96     }
97
98     @Test
99     public void workflow_validInput_expectedOuput() {
100
101         mockCatalogDb();
102         mockAai();
103         mockDmaapForPnf();
104
105         ProcessInstance pi = runtimeService
106             .startProcessInstanceByKey(TEST_PROCESSINSTANCE_KEY, testBusinessKey, variables);
107         assertThat(pi).isNotNull();
108
109         Execution execution = runtimeService.createExecutionQuery().processDefinitionKey("CreateAndActivatePnfResource")
110             .activityId("WaitForDmaapPnfReadyNotification").singleResult();
111
112         if (!execution.isSuspended() && !execution.isEnded()) {
113             try {
114
115                 runtimeService.signal(execution.getId());
116             } catch (Exception e) {
117                 logger.info(e.getMessage(), e);
118             }
119         }
120
121         assertThat(pi).isStarted().hasPassedInOrder(
122             "createVCPE_startEvent",
123             "preProcessRequest_ScriptTask",
124             "sendSyncAckResponse_ScriptTask",
125             "ScriptTask_0cdtchu",
126             "DecomposeService",
127             "ScriptTask_0lpv2da",
128             "ScriptTask_1y241p8",
129             "CallActivity_1vc4jeh",
130             "ScriptTask_1y5lvl7",
131             "GeneratePnfUuid",
132             "Task_14l19kv",
133             "Pnf_Con",
134             "setPONR_ScriptTask",
135             "postProcessAndCompletionRequest_ScriptTask",
136             "callCompleteMsoProcess_CallActivity",
137             "ScriptTask_2",
138             "CreateVCPE_EndEvent"
139         );
140
141         assertThat(pi).isEnded();
142
143         List<String> messagesDelivered = grpcNettyServer.getMessagesDelivered();
144         assertThat(messagesDelivered).containsSequence("config-assign", "config-deploy");
145     }
146
147     /**
148      * Mock the Dmaap Rest interface for Pnf topic.
149      */
150     private void mockDmaapForPnf() {
151
152         String pnfResponse = "[{\"correlationId\": \"PNFDemo\",\"key1\":\"value1\"}]";
153
154         /**
155          * Get the events from PNF topic
156          */
157         wireMockServer
158             .stubFor(get(urlPathMatching("/events/pnfReady/consumerGroup.*")).willReturn(okJson(pnfResponse)));
159     }
160
161     private void mockAai() {
162
163         String aaiResponse = "{\n"
164             + "  \"results\": [\n"
165             + "    {\n"
166             + "      \"resource-type\": \"service-instance\",\n"
167             + "      \"resource-link\": \"https://localhost:8443/aai/v15/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/key3\"\n"
168             + "    }\n"
169             + "  ]\n"
170             + "}";
171
172         String aaiPnfEntry = "{  \n"
173             + "   \"pnf-name\":\"PNFDemo\",\n"
174             + "   \"pnf-id\":\"testtest\",\n"
175             + "   \"in-maint\":true,\n"
176             + "   \"resource-version\":\"1541720264047\",\n"
177             + "   \"pnf-ipv4-address\":\"1.1.1.1\",\n"
178             + "   \"pnf-ipv6-address\":\"ipv6\"\n"
179             + "}";
180
181         /**
182          * Get the AAI entry for globalCustomerId as specified in the request file.
183          */
184         wireMockServer.stubFor(
185             get(urlPathMatching("/aai/v15/business/customers/customer/ADemoCustomerInCiti.*")).willReturn(ok()));
186
187         /**
188          * PUT the service to AAI with globalCustomerId, service type as specified in the request file.
189          * Service instance id is generated during runtime, REGEX is used to represent the information.
190          */
191         wireMockServer.stubFor(put(urlPathMatching(
192             "/aai/v15/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/.*")));
193
194         wireMockServer.stubFor(get(urlPathMatching(
195             "/aai/v15/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/.*"))
196             .willReturn(okJson(aaiResponse)));
197
198         /**
199          * Get the service from AAI
200          */
201         wireMockServer.stubFor(get(urlPathMatching("/aai/v15/nodes/service-instances/service-instance/.*"))
202             .willReturn(okJson(aaiResponse)));
203
204         /**
205          * Put the project as specified in the request file to AAI.
206          */
207         wireMockServer.stubFor(put(urlEqualTo("/aai/v15/business/projects/project/Project-Demonstration")));
208
209         /**
210          * GET the project as specified in the request file to AAI.
211          */
212         wireMockServer
213             .stubFor(get(urlPathMatching("/aai/v15/business/projects/project/Project-Demonstration")).willReturn(ok()));
214
215         /**
216          * PUT the PNF correlation ID to AAI.
217          */
218         wireMockServer.stubFor(put(urlEqualTo("/aai/v15/network/pnfs/pnf/PNFDemo")));
219
220         /**
221          * Get the PNF entry from AAI.
222          */
223         wireMockServer.stubFor(get(urlEqualTo("/aai/v15/network/pnfs/pnf/PNFDemo")).willReturn(okJson(aaiPnfEntry)));
224
225         /**
226          * Put the PNF relationship
227          */
228         wireMockServer.stubFor(
229             put(urlEqualTo("/aai/v15/business/projects/project/Project-Demonstration/relationship-list/relationship")));
230     }
231
232     /**
233      * Mock the catalobdb rest interface.
234      */
235     private void mockCatalogDb() {
236
237         String catalogdbClientResponse = FileUtil
238             .readResourceFile("response/" + getClass().getSimpleName() + "_catalogdb.json");
239
240         /**
241          * Return valid json for the model UUID in the request file.
242          */
243         wireMockServer
244             .stubFor(get(urlEqualTo("/v2/serviceResources?serviceModelUuid=f2daaac6-5017-4e1e-96c8-6a27dfbe1421"))
245                 .willReturn(okJson(responseObject)));
246
247         /**
248          * Return valid json for the service model InvariantUUID as specified in the request file.
249          */
250         wireMockServer.stubFor(
251             get(urlEqualTo("/v2/serviceResources?serviceModelInvariantUuid=539b7a2f-9524-4dbf-9eee-f2e05521df3f"))
252                 .willReturn(okJson(responseObject)));
253
254         /**
255          * Return valid spring data rest json for the service model UUID as specified in the request file.
256          */
257         wireMockServer.stubFor(get(urlEqualTo(
258             "/pnfResourceCustomization/search/findPnfResourceCustomizationByModelUuid?SERVICE_MODEL_UUID=f2daaac6-5017-4e1e-96c8-6a27dfbe1421"))
259             .willReturn(okJson(catalogdbClientResponse)));
260     }
261 }