Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / java / org / onap / so / bpmn / infrastructure / pnf / delegate / PrepareConfigDeployDelegateTest.java
1 /*
2  * ============LICENSE_START======================================================= Copyright (C) 2019 Nordix
3  * Foundation. ================================================================================ Licensed under the
4  * Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
5  * obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
8  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
9  * either express or implied. See the License for the specific language governing permissions and limitations under the
10  * License.
11  *
12  * SPDX-License-Identifier: Apache-2.0 ============LICENSE_END=========================================================
13  */
14
15 package org.onap.so.bpmn.infrastructure.pnf.delegate;
16
17 import static org.assertj.core.api.Assertions.assertThat;
18 import static org.assertj.core.api.Assertions.assertThatThrownBy;
19 import static org.assertj.core.api.Assertions.fail;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.when;
22 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.EXECUTION_OBJECT;
23 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.MODEL_UUID;
24 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.MSO_REQUEST_ID;
25 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_CORRELATION_ID;
26 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_UUID;
27 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_BLUEPRINT_NAME;
28 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_BLUEPRINT_VERSION;
29 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_CUSTOMIZATION_UUID;
30 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_INSTANCE_NAME;
31 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.SERVICE_INSTANCE_ID;
32 import com.fasterxml.jackson.databind.JsonNode;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import java.io.IOException;
35 import java.util.Optional;
36 import org.camunda.bpm.engine.delegate.BpmnError;
37 import org.camunda.bpm.engine.delegate.DelegateExecution;
38 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.onap.aai.domain.yang.Pnf;
43 import org.onap.so.bpmn.core.WorkflowException;
44 import org.onap.so.bpmn.infrastructure.pnf.management.PnfManagement;
45 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
46 import org.onap.so.client.cds.beans.ConfigDeployPropertiesForPnf;
47 import org.onap.so.client.cds.beans.ConfigDeployRequestPnf;
48 import org.onap.so.client.exception.ExceptionBuilder;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.boot.test.mock.mockito.MockBean;
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, PrepareConfigDeployDelegate.class})
56 public class PrepareConfigDeployDelegateTest {
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     private static String TEST_IPV4_ADDRESS = "1.1.1.1";
69     private static String TEST_IPV6_ADDRESS = "::1/128";
70
71     @Autowired
72     private PrepareConfigDeployDelegate prepareConfigDeployDelegate;
73
74     @MockBean
75     private PnfManagement pnfManagement;
76
77     private DelegateExecution execution = new DelegateExecutionFake();
78
79     @Before
80     public void setUp() throws IOException {
81         execution.setVariable("testProcessKey", TEST_PROCESS_KEY);
82         execution.setVariable(PNF_CORRELATION_ID, TEST_PNF_CORRELATION_ID);
83         execution.setVariable(MODEL_UUID, TEST_MODEL_UUID);
84         execution.setVariable(SERVICE_INSTANCE_ID, TEST_SERVICE_INSTANCE_ID);
85         execution.setVariable(MSO_REQUEST_ID, TEST_MSO_REQUEST_ID);
86         execution.setVariable(PNF_UUID, TEST_PNF_UUID);
87         execution.setVariable(PRC_INSTANCE_NAME, TEST_PNF_RESOURCE_INSTANCE_NAME);
88         execution.setVariable(PRC_CUSTOMIZATION_UUID, TEST_PNF_RESOURCE_CUSTOMIZATION_UUID);
89         execution.setVariable(PRC_BLUEPRINT_NAME, TEST_PNF_RESOURCE_BLUEPRINT_NAME);
90         execution.setVariable(PRC_BLUEPRINT_VERSION, TEST_PNF_RESOURCE_BLUEPRINT_VERSION);
91         mockAai();
92     }
93
94     private void mockAai() throws IOException {
95         Pnf pnf = new Pnf();
96         pnf.setIpaddressV4Oam(TEST_IPV4_ADDRESS);
97         pnf.setIpaddressV6Oam(TEST_IPV6_ADDRESS);
98         when(pnfManagement.getEntryFor(TEST_PNF_CORRELATION_ID)).thenReturn(Optional.of(pnf));
99     }
100
101     @Test
102     public void testExecution_validPnf_executionObjectCreated() {
103         try {
104             prepareConfigDeployDelegate.execute(execution);
105             Object executionObject = execution.getVariable(EXECUTION_OBJECT);
106             assertThat(executionObject).isNotNull();
107             assertThat(executionObject).isInstanceOf(AbstractCDSPropertiesBean.class);
108             checkCDSPropertiesBean((AbstractCDSPropertiesBean) executionObject);
109         } catch (Exception e) {
110             e.printStackTrace();
111             fail("Exception thrown" + e.getMessage());
112         }
113     }
114
115     @Test
116     public void testExecution_failedAaiConnection_exceptionThrown() {
117         try {
118             /**
119              * Mock the IOException from AAI.
120              */
121             when(pnfManagement.getEntryFor(TEST_PNF_CORRELATION_ID)).thenThrow(new IOException("Connection failed"));
122         } catch (IOException e) {
123             e.printStackTrace();
124         }
125         assertThatThrownBy(() -> prepareConfigDeployDelegate.execute(execution)).isInstanceOf(BpmnError.class);
126         assertThat(execution.getVariable("WorkflowExceptionErrorMessage")).asString()
127                 .contains("Unable to fetch from AAI");
128         assertThat(execution.getVariable("WorkflowException")).isInstanceOf(WorkflowException.class);
129     }
130
131     @Test
132     public void testExecution_aaiEntryNotExist_exceptionThrown() {
133         try {
134             /**
135              * Mock the AAI without PNF.
136              */
137             when(pnfManagement.getEntryFor(TEST_PNF_CORRELATION_ID)).thenReturn(Optional.empty());
138         } catch (IOException e) {
139             e.printStackTrace();
140         }
141         assertThatThrownBy(() -> prepareConfigDeployDelegate.execute(execution)).isInstanceOf(BpmnError.class);
142         assertThat(execution.getVariable("WorkflowExceptionErrorMessage")).asString()
143                 .contains("AAI entry for PNF: " + TEST_PNF_CORRELATION_ID + " does not exist");
144         assertThat(execution.getVariable("WorkflowException")).isInstanceOf(WorkflowException.class);
145     }
146
147     private void checkCDSPropertiesBean(AbstractCDSPropertiesBean executionObject) {
148         assertThat(executionObject.getBlueprintName()).matches(TEST_PNF_RESOURCE_BLUEPRINT_NAME);
149         assertThat(executionObject.getBlueprintVersion()).matches(TEST_PNF_RESOURCE_BLUEPRINT_VERSION);
150         assertThat(executionObject.getRequestId()).matches(TEST_MSO_REQUEST_ID);
151         assertThat(executionObject.getSubRequestId()).matches(TEST_PNF_UUID);
152         assertThat(executionObject.getMode()).matches("async");
153         assertThat(executionObject.getActionName()).matches("config-deploy");
154         assertThat(executionObject.getOriginatorId()).matches("SO");
155
156         assertThat(executionObject.getRequestObject()).isNotNull();
157         String requestObject = executionObject.getRequestObject();
158
159         checkRequestJson(requestObject);
160     }
161
162     private void checkRequestJson(String requestObject) {
163         try {
164             ObjectMapper mapper = new ObjectMapper();
165             JsonNode tree = mapper.readTree(requestObject);
166             ConfigDeployRequestPnf configDeployRequestPnf =
167                     mapper.treeToValue(tree.at("/config-deploy-request"), ConfigDeployRequestPnf.class);
168             assertThat(configDeployRequestPnf.getResolutionKey()).matches(TEST_PNF_CORRELATION_ID);
169
170             ConfigDeployPropertiesForPnf properties = configDeployRequestPnf.getConfigDeployPropertiesForPnf();
171             assertThat(properties.getServiceInstanceId()).matches(TEST_SERVICE_INSTANCE_ID);
172             assertThat(properties.getPnfName()).matches(TEST_PNF_CORRELATION_ID);
173             assertThat(properties.getPnfId()).matches(TEST_PNF_UUID);
174             assertThat(properties.getPnfCustomizationUuid()).matches(TEST_PNF_RESOURCE_CUSTOMIZATION_UUID);
175             assertThat(properties.getServiceModelUuid()).matches(TEST_MODEL_UUID);
176             assertThat(properties.getPnfIpV4Address()).matches(TEST_IPV4_ADDRESS);
177             assertThat(properties.getPnfIpV6Address()).matches(TEST_IPV6_ADDRESS);
178         } catch (IOException e) {
179             e.printStackTrace();
180             fail("Check request body is json message" + e.getMessage());
181         }
182     }
183 }