aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-flows/src/test/java/org/onap/so/bpmn/infrastructure/process/CreateVcpeResCustServiceSimplifiedTest.java
blob: 2b1dc384a1c5e005314f953ccaa145e9a2ca1ea8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 *
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.so.bpmn.infrastructure.process;

import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.put;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static org.assertj.core.api.Assertions.fail;
import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareAssertions.assertThat;

import com.google.protobuf.Struct;
import com.google.protobuf.Value;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.camunda.bpm.engine.runtime.Execution;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.junit.Before;
import org.junit.Test;
import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers;
import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader;
import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
import org.onap.so.BaseBPMNTest;
import org.onap.so.GrpcNettyServer;
import org.onap.so.bpmn.mock.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * Basic Integration test for createVcpeResCustService_Simplified.bpmn workflow.
 */
public class CreateVcpeResCustServiceSimplifiedTest extends BaseBPMNTest {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private static final String TEST_PROCESSINSTANCE_KEY = "CreateVcpeResCustService_simplified";

    private String testBusinessKey;
    private String requestObject;
    private String responseObject;
    private String msoRequestId;

    @Autowired
    private GrpcNettyServer grpcNettyServer;

    @Before
    public void setUp() throws IOException {

        requestObject = FileUtil.readResourceFile("request/" + getClass().getSimpleName() + ".json");
        responseObject = FileUtil.readResourceFile("response/" + getClass().getSimpleName() + ".json");

        variables.put("bpmnRequest", requestObject);

        /**
         * This variable indicates that the flow was invoked asynchronously.
         * It's injected by {@link WorkflowProcessor}.
         */
        variables.put("isAsyncProcess", "true");

        /**
         * Temporary solution to add pnfCorrelationId to context.
         * this value is getting from the request to SO api handler and then convert to CamudaInput
         */
        variables.put("pnfCorrelationId", "PNFDemo");

        /**
         * Create mso-request-id.
         */
        msoRequestId = UUID.randomUUID().toString();

        variables.put("mso-request-id", msoRequestId);

        /**
         * Create Business key for the process instance
         */
        testBusinessKey = UUID.randomUUID().toString();

        logger.info("Test the process instance: {} with business key: {}", TEST_PROCESSINSTANCE_KEY, testBusinessKey);

    }

    @Test
    public void workflow_validInput_expectedOuput() {

        mockCatalogDb();
        mockRequestDb();
        mockAai();
        mockDmaapForPnf();

        ProcessInstance pi = runtimeService
            .startProcessInstanceByKey(TEST_PROCESSINSTANCE_KEY, testBusinessKey, variables);
        assertThat(pi).isNotNull();

        Execution execution = runtimeService.createExecutionQuery().processDefinitionKey("CreateAndActivatePnfResource")
            .activityId("WaitForDmaapPnfReadyNotification").singleResult();

        if (!execution.isSuspended() && !execution.isEnded()) {
            try {
                runtimeService.signal(execution.getId());
            } catch (Exception e) {
                logger.info(e.getMessage(), e);
            }
        }

        assertThat(pi).isStarted().hasPassedInOrder(
            "createVCPE_startEvent",
            "preProcessRequest_ScriptTask",
            "sendSyncAckResponse_ScriptTask",
            "ScriptTask_0cdtchu",
            "DecomposeService",
            "ScriptTask_0lpv2da",
            "ScriptTask_1y241p8",
            "CallActivity_1vc4jeh",
            "ScriptTask_1y5lvl7",
            "GeneratePnfUuid",
            "Task_14l19kv",
            "Pnf_Con",
            "setPONR_ScriptTask",
            "postProcessAndCompletionRequest_ScriptTask",
            "callCompleteMsoProcess_CallActivity",
            "ScriptTask_2",
            "CreateVCPE_EndEvent"
        );

        assertThat(pi).isEnded();

        List<ExecutionServiceInput> detailedMessages = grpcNettyServer.getDetailedMessages();
        assertThat(detailedMessages).hasSize(2);
        try {
            checkConfigAssign(detailedMessages.get(0));
            checkConfigDeploy(detailedMessages.get(1));
        } catch(Exception e){
            e.printStackTrace();
            fail("ConfigAssign/deploy request exception", e);
        }
    }

    private void checkConfigAssign(ExecutionServiceInput executionServiceInput) {
        ActionIdentifiers actionIdentifiers = executionServiceInput.getActionIdentifiers();

        /**
         * the fields of actionIdentifiers should match the one in the response/createVcpeResCustServiceSimplifiedTest_catalogdb.json.
         */
        assertThat(actionIdentifiers.getBlueprintName()).matches("test_configuration_restconf");
        assertThat(actionIdentifiers.getBlueprintVersion()).matches("1.0.0");
        assertThat(actionIdentifiers.getActionName()).matches("config-assign");
        assertThat(actionIdentifiers.getMode()).matches("sync");

        CommonHeader commonHeader = executionServiceInput.getCommonHeader();
        assertThat(commonHeader.getOriginatorId()).matches("SO");
        assertThat(commonHeader.getRequestId()).matches(msoRequestId);

        Struct payload = executionServiceInput.getPayload();
        Struct requeststruct = payload.getFieldsOrThrow("config-assign-request").getStructValue();

        assertThat(requeststruct.getFieldsOrThrow("resolution-key").getStringValue()).matches("PNFDemo");
        Struct propertiesStruct = requeststruct.getFieldsOrThrow("config-assign-properties").getStructValue();

        assertThat(propertiesStruct.getFieldsOrThrow("pnf-name").getStringValue()).matches("PNFDemo");
        assertThat(propertiesStruct.getFieldsOrThrow("service-model-uuid").getStringValue()).matches("f2daaac6-5017-4e1e-96c8-6a27dfbe1421");
        assertThat(propertiesStruct.getFieldsOrThrow("pnf-customization-uuid").getStringValue()).matches("68dc9a92-214c-11e7-93ae-92361f002680");
    }

    private void checkConfigDeploy(ExecutionServiceInput executionServiceInput) {
        ActionIdentifiers actionIdentifiers = executionServiceInput.getActionIdentifiers();

        /**
         * the fields of actionIdentifiers should match the one in the response/createVcpeResCustServiceSimplifiedTest_catalogdb.json.
         */
        assertThat(actionIdentifiers.getBlueprintName()).matches("test_configuration_restconf");
        assertThat(actionIdentifiers.getBlueprintVersion()).matches("1.0.0");
        assertThat(actionIdentifiers.getActionName()).matches("config-deploy");
        assertThat(actionIdentifiers.getMode()).matches("async");

        CommonHeader commonHeader = executionServiceInput.getCommonHeader();
        assertThat(commonHeader.getOriginatorId()).matches("SO");
        assertThat(commonHeader.getRequestId()).matches(msoRequestId);

        Struct payload = executionServiceInput.getPayload();
        Struct requeststruct = payload.getFieldsOrThrow("config-deploy-request").getStructValue();

        assertThat(requeststruct.getFieldsOrThrow("resolution-key").getStringValue()).matches("PNFDemo");
        Struct propertiesStruct = requeststruct.getFieldsOrThrow("config-deploy-properties").getStructValue();

        assertThat(propertiesStruct.getFieldsOrThrow("pnf-name").getStringValue()).matches("PNFDemo");
        assertThat(propertiesStruct.getFieldsOrThrow("service-model-uuid").getStringValue()).matches("f2daaac6-5017-4e1e-96c8-6a27dfbe1421");
        assertThat(propertiesStruct.getFieldsOrThrow("pnf-customization-uuid").getStringValue()).matches("68dc9a92-214c-11e7-93ae-92361f002680");

        /**
         * IP addresses match the OAM ip addresses from AAI.
         */
        assertThat(propertiesStruct.getFieldsOrThrow("pnf-ipv4-address").getStringValue()).matches("1.1.1.1");
        assertThat(propertiesStruct.getFieldsOrThrow("pnf-ipv6-address").getStringValue()).matches("::/128");
    }

    /**
     * Mock the Dmaap Rest interface for Pnf topic.
     */
    private void mockDmaapForPnf() {

        String pnfResponse = "[{\"correlationId\": \"PNFDemo\",\"key1\":\"value1\"}]";

        /**
         * Get the events from PNF topic
         */
        wireMockServer
            .stubFor(get(urlPathMatching("/events/pnfReady/consumerGroup.*")).willReturn(okJson(pnfResponse)));
    }

    private void mockAai() {

        String aaiResponse = "{\n"
            + "  \"results\": [\n"
            + "    {\n"
            + "      \"resource-type\": \"service-instance\",\n"
            + "      \"resource-link\": \"https://localhost:8443/aai/v15/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/key3\"\n"
            + "    }\n"
            + "  ]\n"
            + "}";

        String aaiPnfEntry = "{  \n"
            + "   \"pnf-name\":\"PNFDemo\",\n"
            + "   \"pnf-id\":\"testtest\",\n"
            + "   \"in-maint\":true,\n"
            + "   \"resource-version\":\"1541720264047\",\n"
            + "   \"ipaddress-v4-oam\":\"1.1.1.1\",\n"
            + "   \"ipaddress-v6-oam\":\"::/128\"\n"
            + "}";

        /**
         * Get the AAI entry for globalCustomerId as specified in the request file.
         */
        wireMockServer.stubFor(
            get(urlPathMatching("/aai/v15/business/customers/customer/ADemoCustomerInCiti.*")).willReturn(ok()));

        /**
         * PUT the service to AAI with globalCustomerId, service type as specified in the request file.
         * Service instance id is generated during runtime, REGEX is used to represent the information.
         */
        wireMockServer.stubFor(put(urlPathMatching(
            "/aai/v15/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/.*")));

        wireMockServer.stubFor(get(urlPathMatching(
            "/aai/v15/business/customers/customer/ADemoCustomerInCiti/service-subscriptions/service-subscription/vCPE/service-instances/service-instance/.*"))
            .willReturn(okJson(aaiResponse)));

        /**
         * Get the service from AAI
         */
        wireMockServer.stubFor(get(urlPathMatching("/aai/v15/nodes/service-instances/service-instance/.*"))
            .willReturn(okJson(aaiResponse)));

        /**
         * Put the project as specified in the request file to AAI.
         */
        wireMockServer.stubFor(put(urlEqualTo("/aai/v15/business/projects/project/Project-Demonstration")));

        /**
         * GET the project as specified in the request file to AAI.
         */
        wireMockServer
            .stubFor(get(urlPathMatching("/aai/v15/business/projects/project/Project-Demonstration")).willReturn(ok()));

        /**
         * PUT the PNF correlation ID to AAI.
         */
        wireMockServer.stubFor(put(urlEqualTo("/aai/v15/network/pnfs/pnf/PNFDemo")));

        /**
         * Get the PNF entry from AAI.
         */
        wireMockServer.stubFor(get(urlEqualTo("/aai/v15/network/pnfs/pnf/PNFDemo")).willReturn(okJson(aaiPnfEntry)));

        /**
         * Put the PNF relationship
         */
        wireMockServer.stubFor(
            put(urlEqualTo("/aai/v15/business/projects/project/Project-Demonstration/relationship-list/relationship")));
    }

    /**
     * Mock the catalobdb rest interface.
     */
    private void mockCatalogDb() {

        String catalogdbClientResponse = FileUtil
            .readResourceFile("response/" + getClass().getSimpleName() + "_catalogdb.json");

        /**
         * Return valid json for the model UUID in the request file.
         */
        wireMockServer
            .stubFor(get(urlEqualTo("/v2/serviceResources?serviceModelUuid=f2daaac6-5017-4e1e-96c8-6a27dfbe1421"))
                .willReturn(okJson(responseObject)));

        /**
         * Return valid json for the service model InvariantUUID as specified in the request file.
         */
        wireMockServer.stubFor(
            get(urlEqualTo("/v2/serviceResources?serviceModelInvariantUuid=539b7a2f-9524-4dbf-9eee-f2e05521df3f"))
                .willReturn(okJson(responseObject)));

        /**
         * Return valid spring data rest json for the service model UUID as specified in the request file.
         */
        wireMockServer.stubFor(get(urlEqualTo(
            "/pnfResourceCustomization/search/findPnfResourceCustomizationByModelUuid?SERVICE_MODEL_UUID=f2daaac6-5017-4e1e-96c8-6a27dfbe1421"))
            .willReturn(okJson(catalogdbClientResponse)));
    }

    private void mockRequestDb() {
        wireMockServer.stubFor(post(urlEqualTo("/dbadapters/RequestsDbAdapter")).willReturn(ok()));
    }

}