60c7e4e7692138a95cf63beace4146591441d7fa
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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.okJson;
25 import static com.github.tomakehurst.wiremock.client.WireMock.post;
26 import static com.github.tomakehurst.wiremock.client.WireMock.put;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
28 import static org.assertj.core.api.Assertions.fail;
29 import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareTests.assertThat;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertTrue;
32 import java.io.IOException;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.UUID;
37 import org.camunda.bpm.engine.runtime.ProcessInstance;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.aaiclient.client.aai.AAIVersion;
41 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers;
42 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader;
43 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
44 import org.onap.so.BaseBPMNTest;
45 import org.onap.so.GrpcNettyServer;
46 import org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames;
47 import org.onap.so.bpmn.mock.FileUtil;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import com.google.protobuf.Struct;
52
53 /**
54  * Basic Integration test for ServiceLevelUpgrade.bpmn workflow.
55  */
56 public class ServiceLevelUpgradeTest extends BaseBPMNTest {
57
58     private final Logger logger = LoggerFactory.getLogger(getClass());
59
60     private static final long WORKFLOW_WAIT_TIME = 1000L;
61
62     private static final String TEST_PROCESSINSTANCE_KEY = "ServiceLevelUpgrade";
63     private static final AAIVersion VERSION = AAIVersion.LATEST;
64     private static final Map<String, Object> executionVariables = new HashMap();
65     private static final String REQUEST_ID = "50ae41ad-049c-4fe2-9950-539f111120f5";
66     private static final String SERVICE_INSTANCE_ID = "5df8b6de-2083-11e7-93ae-92361f002676";
67     private final String[] actionNames = new String[10];
68     private final String[] pnfNames = new String[10];
69     private final String CLASSNAME = getClass().getSimpleName();
70     private String requestObject;
71     private String responseObject;
72
73     @Autowired
74     private GrpcNettyServer grpcNettyServer;
75
76     @Before
77     public void setUp() throws IOException {
78         actionNames[0] = "healthCheck";
79         actionNames[1] = "healthCheck";
80         actionNames[2] = "preCheck";
81         actionNames[3] = "downloadNESw";
82         actionNames[4] = "activateNESw";
83         actionNames[5] = "postCheck";
84         actionNames[6] = "preCheck";
85         actionNames[7] = "downloadNESw";
86         actionNames[8] = "activateNESw";
87         actionNames[9] = "postCheck";
88
89         pnfNames[0] = "PNFDemo";
90         pnfNames[1] = "PNFDemo1";
91         pnfNames[2] = "PNFDemo";
92         pnfNames[3] = "PNFDemo";
93         pnfNames[4] = "PNFDemo";
94         pnfNames[5] = "PNFDemo";
95         pnfNames[6] = "PNFDemo1";
96         pnfNames[7] = "PNFDemo1";
97         pnfNames[8] = "PNFDemo1";
98         pnfNames[9] = "PNFDemo1";
99
100         executionVariables.clear();
101
102         requestObject = FileUtil.readResourceFile("request/" + CLASSNAME + ".json");
103         responseObject = FileUtil.readResourceFile("response/" + CLASSNAME + ".json");
104
105         executionVariables.put("bpmnRequest", requestObject);
106         executionVariables.put("requestId", REQUEST_ID);
107         executionVariables.put("serviceInstanceId", SERVICE_INSTANCE_ID);
108
109
110         /**
111          * This variable indicates that the flow was invoked asynchronously. It's injected by {@link WorkflowProcessor}.
112          */
113         executionVariables.put("isAsyncProcess", "true");
114         executionVariables.put(ExecutionVariableNames.PRC_CUSTOMIZATION_UUID, "38dc9a92-214c-11e7-93ae-92361f002680");
115
116         /**
117          * Temporary solution to add pnfCorrelationId to context. this value is getting from the request to SO api
118          * handler and then convert to CamudaInput
119          */
120         executionVariables.put(ExecutionVariableNames.PNF_CORRELATION_ID, "PNFDemo");
121     }
122
123
124     @Test
125     public void workflow_validInput_expectedOutput() throws InterruptedException {
126
127         mockCatalogDb();
128         mockRequestDb();
129         mockAai();
130         grpcNettyServer.resetList();
131
132         final String msoRequestId = UUID.randomUUID().toString();
133         executionVariables.put(ExecutionVariableNames.MSO_REQUEST_ID, msoRequestId);
134
135         final String testBusinessKey = UUID.randomUUID().toString();
136         logger.info("Test the process instance: {} with business key: {}", TEST_PROCESSINSTANCE_KEY, testBusinessKey);
137
138         ProcessInstance pi =
139                 runtimeService.startProcessInstanceByKey(TEST_PROCESSINSTANCE_KEY, testBusinessKey, executionVariables);
140
141         int waitCount = 10;
142         while (!isProcessInstanceEnded() && waitCount >= 0) {
143             Thread.sleep(WORKFLOW_WAIT_TIME);
144             waitCount--;
145         }
146
147         // Layout is to reflect the bpmn visual layout
148         assertThat(pi).isEnded().hasPassedInOrder("Event_02mc8tr", "Activity_18vue7u", "Activity_09bqns0",
149                 "Activity_02vp5np", "Activity_0n17xou", "Gateway_1nr51kr", "Activity_0snmatn", "Activity_0e6w886",
150                 "Activity_1q4o9fx", "Gateway_02fectw", "Activity_1hp67qz", "Gateway_18ch73t", "Activity_0ft7fa2",
151                 "Gateway_1vq11i7", "Activity_0o2rrag", "Activity_1n4rk7m", "Activity_1lz38px", "Event_12983th");
152
153         List<ExecutionServiceInput> detailedMessages = grpcNettyServer.getDetailedMessages();
154         assertEquals(10, detailedMessages.size());
155         int count = 0;
156         String action = "";
157         try {
158             for (ExecutionServiceInput eSI : detailedMessages) {
159                 action = actionNames[count];
160                 if (action.equals(eSI.getActionIdentifiers().getActionName())
161                         && eSI.getCommonHeader().getRequestId().equals(msoRequestId)) {
162                     checkWithActionName(eSI, action, pnfNames[count]);
163                     count++;
164                 }
165             }
166         } catch (Exception e) {
167             e.printStackTrace();
168             fail("GenericPnfSoftwareUpgrade request exception", e);
169         }
170         assertTrue(count == actionNames.length);
171     }
172
173     private boolean isProcessInstanceEnded() {
174         return runtimeService.createProcessInstanceQuery().processDefinitionKey(TEST_PROCESSINSTANCE_KEY)
175                 .singleResult() == null;
176     }
177
178     private void checkWithActionName(final ExecutionServiceInput executionServiceInput, final String action,
179             final String pnfName) {
180
181         logger.info("Checking the " + action + " request");
182         ActionIdentifiers actionIdentifiers = executionServiceInput.getActionIdentifiers();
183
184         /**
185          * the fields of actionIdentifiers should match the one in the response/PnfHealthCheck_catalogdb.json.
186          */
187         assertEquals("test_pnf_software_upgrade_restconf", actionIdentifiers.getBlueprintName());
188         assertEquals("1.0.0", actionIdentifiers.getBlueprintVersion());
189         assertEquals(action, actionIdentifiers.getActionName());
190         assertEquals("async", actionIdentifiers.getMode());
191
192         CommonHeader commonHeader = executionServiceInput.getCommonHeader();
193         assertEquals("SO", commonHeader.getOriginatorId());
194
195         Struct payload = executionServiceInput.getPayload();
196         Struct requeststruct = payload.getFieldsOrThrow(action + "-request").getStructValue();
197
198         assertEquals(pnfName, requeststruct.getFieldsOrThrow("resolution-key").getStringValue());
199         Struct propertiesStruct = requeststruct.getFieldsOrThrow(action + "-properties").getStructValue();
200
201         assertEquals(pnfName, propertiesStruct.getFieldsOrThrow("pnf-name").getStringValue());
202         assertEquals("d88da85c-d9e8-4f73-b837-3a72a431622b",
203                 propertiesStruct.getFieldsOrThrow("service-model-uuid").getStringValue());
204         assertEquals("38dc9a92-214c-11e7-93ae-92361f002680",
205                 propertiesStruct.getFieldsOrThrow("pnf-customization-uuid").getStringValue());
206     }
207
208     private void mockAai() {
209
210         final String sIUrl =
211                 "/business/customers/customer/ETE_Customer_807c7a02-249c-4db8-9fa9-bee973fe08ce/service-subscriptions/service-subscription/pNF/service-instances/service-instance/5df8b6de-2083-11e7-93ae-92361f002676";
212         final String aaiPnfDemoEntry = FileUtil.readResourceFile("response/PnfDemo_aai.json");
213         final String aaiPnfDemo1Entry = FileUtil.readResourceFile("response/PnfDemo1_aai.json");
214         final String aaiServiceInstanceEntry = FileUtil.readResourceFile("response/Service_instance_aai.json");
215
216         /**
217          * PUT the PNF correlation ID PnfDemo to AAI.
218          */
219         wireMockServer.stubFor(put(urlEqualTo("/aai/" + VERSION + "/network/pnfs/pnf/PNFDemo")));
220
221         /**
222          * PUT the PNF correlation ID PnfDemo1 to AAI.
223          */
224         wireMockServer.stubFor(put(urlEqualTo("/aai/" + VERSION + "/network/pnfs/pnf/PNFDemo1")));
225
226         /**
227          * Get the PNF entry PnfDemo from AAI.
228          */
229         wireMockServer.stubFor(
230                 get(urlEqualTo("/aai/" + VERSION + "/network/pnfs/pnf/PNFDemo")).willReturn(okJson(aaiPnfDemoEntry)));
231
232         /**
233          * Get the PNF entry PnfDemo1 from AAI.
234          */
235         wireMockServer.stubFor(
236                 get(urlEqualTo("/aai/" + VERSION + "/network/pnfs/pnf/PNFDemo1")).willReturn(okJson(aaiPnfDemo1Entry)));
237
238         /**
239          * Post the pnf PnfDemo to AAI
240          */
241         wireMockServer.stubFor(post(urlEqualTo("/aai/" + VERSION + "/network/pnfs/pnf/PNFDemo")));
242
243         /**
244          * Post the pnf PnfDemo1 to AAI
245          */
246         wireMockServer.stubFor(post(urlEqualTo("/aai/" + VERSION + "/network/pnfs/pnf/PNFDemo1")));
247
248         /**
249          * Get the Service Instance to AAI.
250          */
251         wireMockServer.stubFor(get(urlEqualTo("/aai/" + VERSION + sIUrl)).willReturn(okJson(aaiServiceInstanceEntry)));
252
253         /**
254          * Post the Service Instance to AAI.
255          */
256         wireMockServer.stubFor(post(urlEqualTo("/aai/" + VERSION + sIUrl)));
257     }
258
259     private void mockRequestDb() {
260         /**
261          * Update Request DB
262          */
263         wireMockServer.stubFor(put(urlEqualTo("/infraActiveRequests/" + REQUEST_ID)));
264
265     }
266
267     /**
268      * Mock the catalobdb rest interface.
269      */
270     private void mockCatalogDb() {
271
272         String catalogdbClientResponse = FileUtil.readResourceFile("response/" + CLASSNAME + "_catalogdb.json");
273
274
275         /**
276          * Return valid json for the model UUID in the request file.
277          */
278         wireMockServer
279                 .stubFor(get(urlEqualTo("/v2/serviceResources?serviceModelUuid=d88da85c-d9e8-4f73-b837-3a72a431622b"))
280                         .willReturn(okJson(responseObject)));
281
282         /**
283          * Return valid json for the service model InvariantUUID as specified in the request file.
284          */
285         wireMockServer.stubFor(
286                 get(urlEqualTo("/v2/serviceResources?serviceModelInvariantUuid=fe41489e-1563-46a3-b90a-1db629e4375b"))
287                         .willReturn(okJson(responseObject)));
288
289         /**
290          * Return valid spring data rest json for the service model UUID as specified in the request file.
291          */
292         wireMockServer.stubFor(get(urlEqualTo(
293                 "/pnfResourceCustomization/search/findPnfResourceCustomizationByModelUuid?SERVICE_MODEL_UUID=d88da85c-d9e8-4f73-b837-3a72a431622b"))
294                         .willReturn(okJson(catalogdbClientResponse)));
295     }
296
297 }