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