8d817e5bca3992cb52f840cee1d29257f0a87c45
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / java / org / onap / so / bpmn / infrastructure / pnf / delegate / PrepareConfigAssignDelegateTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.so.bpmn.infrastructure.pnf.delegate;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.assertj.core.api.Assertions.fail;
25 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.EXECUTION_OBJECT;
26 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.MODEL_UUID;
27 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.MSO_REQUEST_ID;
28 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_CORRELATION_ID;
29 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_UUID;
30 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_BLUEPRINT_NAME;
31 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_BLUEPRINT_VERSION;
32 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_CUSTOMIZATION_UUID;
33 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_INSTANCE_NAME;
34 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.SERVICE_INSTANCE_ID;
35
36 import com.fasterxml.jackson.databind.JsonNode;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import java.io.IOException;
39 import org.camunda.bpm.engine.delegate.BpmnError;
40 import org.camunda.bpm.engine.delegate.DelegateExecution;
41 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.onap.so.bpmn.core.WorkflowException;
46 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
47 import org.onap.so.client.cds.beans.ConfigAssignPropertiesForPnf;
48 import org.onap.so.client.cds.beans.ConfigAssignRequestPnf;
49 import org.onap.so.client.exception.ExceptionBuilder;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.test.context.ContextConfiguration;
52 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
53
54 @RunWith(SpringJUnit4ClassRunner.class)
55 @ContextConfiguration(classes = {ExceptionBuilder.class, PrepareConfigAssignDelegate.class})
56 public class PrepareConfigAssignDelegateTest {
57
58     private static String TEST_MODEL_UUID = "6bc0b04d-1873-4721-b53d-6615225b2a28";
59     private static String TEST_SERVICE_INSTANCE_ID = "test_service_id";
60     private static String TEST_PROCESS_KEY = "processKey1";
61     private static String TEST_PNF_RESOURCE_INSTANCE_NAME = "PNF_demo_resource";
62     private static String TEST_PNF_CORRELATION_ID = "PNFDemo";
63     private static String TEST_PNF_RESOURCE_BLUEPRINT_NAME = "blueprintOnap";
64     private static String TEST_PNF_RESOURCE_BLUEPRINT_VERSION = "1.0.1";
65     private static String TEST_PNF_RESOURCE_CUSTOMIZATION_UUID = "9acb3a83-8a52-412c-9a45-901764938144";
66     private static String TEST_MSO_REQUEST_ID = "ff874603-4222-11e7-9252-005056850d2e";
67     private static String TEST_PNF_UUID = "5df8b6de-2083-11e7-93ae-92361f002671";
68
69     @Autowired
70     private PrepareConfigAssignDelegate prepareConfigAssignDelegate;
71
72     private DelegateExecution execution = new DelegateExecutionFake();
73
74     @Before
75     public void setUp(){
76         execution.setVariable("testProcessKey", TEST_PROCESS_KEY);
77         execution.setVariable(PNF_CORRELATION_ID, TEST_PNF_CORRELATION_ID);
78         execution.setVariable(MODEL_UUID, TEST_MODEL_UUID);
79         execution.setVariable(SERVICE_INSTANCE_ID, TEST_SERVICE_INSTANCE_ID);
80         execution.setVariable(MSO_REQUEST_ID, TEST_MSO_REQUEST_ID);
81         execution.setVariable(PNF_UUID, TEST_PNF_UUID);
82         execution.setVariable(PRC_INSTANCE_NAME, TEST_PNF_RESOURCE_INSTANCE_NAME);
83         execution.setVariable(PRC_CUSTOMIZATION_UUID, TEST_PNF_RESOURCE_CUSTOMIZATION_UUID);
84         execution.setVariable(PRC_BLUEPRINT_NAME, TEST_PNF_RESOURCE_BLUEPRINT_NAME);
85         execution.setVariable(PRC_BLUEPRINT_VERSION, TEST_PNF_RESOURCE_BLUEPRINT_VERSION);
86     }
87
88     @Test
89     public void testExecution_validPnf_executionObjectCreated() {
90         try {
91             prepareConfigAssignDelegate.execute(execution);
92             Object executionObject = execution.getVariable(EXECUTION_OBJECT);
93             assertThat(executionObject).isNotNull();
94             assertThat(executionObject).isInstanceOf(AbstractCDSPropertiesBean.class);
95             checkCDSPropertiesBean((AbstractCDSPropertiesBean) executionObject);
96         } catch (Exception e) {
97             e.printStackTrace();
98             fail("Exception thrown" + e.getMessage());
99         }
100     }
101
102     private void checkCDSPropertiesBean(AbstractCDSPropertiesBean executionObject) {
103         assertThat(executionObject.getBlueprintName()).matches(TEST_PNF_RESOURCE_BLUEPRINT_NAME);
104         assertThat(executionObject.getBlueprintVersion()).matches(TEST_PNF_RESOURCE_BLUEPRINT_VERSION);
105         assertThat(executionObject.getRequestId()).matches(TEST_MSO_REQUEST_ID);
106         assertThat(executionObject.getSubRequestId()).matches(TEST_PNF_UUID);
107         assertThat(executionObject.getMode()).matches("sync");
108         assertThat(executionObject.getActionName()).matches("config-assign");
109         assertThat(executionObject.getOriginatorId()).matches("SO");
110
111         assertThat(executionObject.getRequestObject()).isNotNull();
112         String requestObject = executionObject.getRequestObject();
113
114         checkRequestJson(requestObject);
115     }
116
117     private void checkRequestJson(String requestObject) {
118         try {
119             ObjectMapper mapper = new ObjectMapper();
120             JsonNode tree = mapper.readTree(requestObject);
121             ConfigAssignRequestPnf configAssignRequestPnf = mapper
122                 .treeToValue(tree.at("/config-assign-request"), ConfigAssignRequestPnf.class);
123             assertThat(configAssignRequestPnf.getResolutionKey()).matches(TEST_PNF_CORRELATION_ID);
124
125             ConfigAssignPropertiesForPnf properties = configAssignRequestPnf.getConfigAssignPropertiesForPnf();
126             assertThat(properties.getServiceInstanceId()).matches(TEST_SERVICE_INSTANCE_ID);
127             assertThat(properties.getPnfName()).matches(TEST_PNF_CORRELATION_ID);
128             assertThat(properties.getPnfId()).matches(TEST_PNF_UUID);
129             assertThat(properties.getPnfCustomizationUuid()).matches(TEST_PNF_RESOURCE_CUSTOMIZATION_UUID);
130             assertThat(properties.getServiceModelUuid()).matches(TEST_MODEL_UUID);
131         } catch (IOException e) {
132             e.printStackTrace();
133             fail("Check request body is json message" + e.getMessage());
134         }
135     }
136 }