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