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