PNF WF post instantiation configuration
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / java / org / onap / so / bpmn / infrastructure / pnf / delegate / PrepareConfigDeployDelegateTest.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.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.EXECUTION_OBJECT;
28 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.MODEL_UUID;
29 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.MSO_REQUEST_ID;
30 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_CORRELATION_ID;
31 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_UUID;
32 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_BLUEPRINT_NAME;
33 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_BLUEPRINT_VERSION;
34 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_CUSTOMIZATION_UUID;
35 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_INSTANCE_NAME;
36 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.SERVICE_INSTANCE_ID;
37
38 import com.fasterxml.jackson.databind.JsonNode;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import java.io.IOException;
41 import java.util.Optional;
42 import org.camunda.bpm.engine.delegate.BpmnError;
43 import org.camunda.bpm.engine.delegate.DelegateExecution;
44 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.onap.aai.domain.yang.Pnf;
49 import org.onap.so.bpmn.core.WorkflowException;
50 import org.onap.so.bpmn.infrastructure.pnf.management.PnfManagement;
51 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
52 import org.onap.so.client.cds.beans.ConfigDeployPropertiesForPnf;
53 import org.onap.so.client.cds.beans.ConfigDeployRequestPnf;
54 import org.onap.so.client.exception.ExceptionBuilder;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.boot.test.mock.mockito.MockBean;
57 import org.springframework.test.context.ContextConfiguration;
58 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
59
60 @RunWith(SpringJUnit4ClassRunner.class)
61 @ContextConfiguration(classes = {ExceptionBuilder.class, PrepareConfigDeployDelegate.class})
62 public class PrepareConfigDeployDelegateTest {
63
64     private static String TEST_MODEL_UUID = "6bc0b04d-1873-4721-b53d-6615225b2a28";
65     private static String TEST_SERVICE_INSTANCE_ID = "test_service_id";
66     private static String TEST_PROCESS_KEY = "processKey1";
67     private static String TEST_PNF_RESOURCE_INSTANCE_NAME = "PNF_demo_resource";
68     private static String TEST_PNF_CORRELATION_ID = "PNFDemo";
69     private static String TEST_PNF_RESOURCE_BLUEPRINT_NAME = "blueprintOnap";
70     private static String TEST_PNF_RESOURCE_BLUEPRINT_VERSION = "1.0.1";
71     private static String TEST_PNF_RESOURCE_CUSTOMIZATION_UUID = "9acb3a83-8a52-412c-9a45-901764938144";
72     private static String TEST_MSO_REQUEST_ID = "ff874603-4222-11e7-9252-005056850d2e";
73     private static String TEST_PNF_UUID = "5df8b6de-2083-11e7-93ae-92361f002671";
74     private static String TEST_IPV4_ADDRESS = "1.1.1.1";
75     private static String TEST_IPV6_ADDRESS = "::1/128";
76
77     @Autowired
78     private PrepareConfigDeployDelegate prepareConfigDeployDelegate;
79
80     @MockBean
81     private PnfManagement pnfManagement;
82
83     private DelegateExecution execution = new DelegateExecutionFake();
84
85     @Before
86     public void setUp() throws IOException {
87         execution.setVariable("testProcessKey", TEST_PROCESS_KEY);
88         execution.setVariable(PNF_CORRELATION_ID, TEST_PNF_CORRELATION_ID);
89         execution.setVariable(MODEL_UUID, TEST_MODEL_UUID);
90         execution.setVariable(SERVICE_INSTANCE_ID, TEST_SERVICE_INSTANCE_ID);
91         execution.setVariable(MSO_REQUEST_ID, TEST_MSO_REQUEST_ID);
92         execution.setVariable(PNF_UUID, TEST_PNF_UUID);
93         execution.setVariable(PRC_INSTANCE_NAME, TEST_PNF_RESOURCE_INSTANCE_NAME);
94         execution.setVariable(PRC_CUSTOMIZATION_UUID, TEST_PNF_RESOURCE_CUSTOMIZATION_UUID);
95         execution.setVariable(PRC_BLUEPRINT_NAME, TEST_PNF_RESOURCE_BLUEPRINT_NAME);
96         execution.setVariable(PRC_BLUEPRINT_VERSION, TEST_PNF_RESOURCE_BLUEPRINT_VERSION);
97         mockAai();
98     }
99
100     private void mockAai() throws IOException {
101         Pnf pnf = new Pnf();
102         pnf.setPnfIpv4Address(TEST_IPV4_ADDRESS);
103         pnf.setPnfIpv6Address(TEST_IPV6_ADDRESS);
104         when(pnfManagement.getEntryFor(TEST_PNF_CORRELATION_ID)).thenReturn(Optional.of(pnf));
105     }
106
107     @Test
108     public void testExecution_validPnf_executionObjectCreated() {
109         try {
110             prepareConfigDeployDelegate.execute(execution);
111             Object executionObject = execution.getVariable(EXECUTION_OBJECT);
112             assertThat(executionObject).isNotNull();
113             assertThat(executionObject).isInstanceOf(AbstractCDSPropertiesBean.class);
114             checkCDSPropertiesBean((AbstractCDSPropertiesBean) executionObject);
115         } catch (Exception e) {
116             e.printStackTrace();
117             fail("Exception thrown" + e.getMessage());
118         }
119     }
120
121     @Test
122     public void testExecution_failedAaiConnection_exceptionThrown(){
123         try {
124             /**
125              * Mock the IOException from AAI.
126              */
127             when(pnfManagement.getEntryFor(TEST_PNF_CORRELATION_ID)).thenThrow(new IOException("Connection failed"));
128         } catch (IOException e) {
129             e.printStackTrace();
130         }
131         assertThatThrownBy(()->prepareConfigDeployDelegate.execute(execution)).isInstanceOf(BpmnError.class);
132         assertThat(execution.getVariable("WorkflowExceptionErrorMessage")).asString().contains("Unable to fetch from AAI");
133         assertThat(execution.getVariable("WorkflowException")).isInstanceOf(WorkflowException.class);
134     }
135
136     @Test
137     public void testExecution_aaiEntryNotExist_exceptionThrown(){
138         try {
139             /**
140              * Mock the AAI without PNF.
141              */
142             when(pnfManagement.getEntryFor(TEST_PNF_CORRELATION_ID)).thenReturn(Optional.empty());
143         } catch (IOException e) {
144             e.printStackTrace();
145         }
146         assertThatThrownBy(()->prepareConfigDeployDelegate.execute(execution)).isInstanceOf(BpmnError.class);
147         assertThat(execution.getVariable("WorkflowExceptionErrorMessage")).asString().contains("AAI entry for PNF: " + TEST_PNF_CORRELATION_ID + " does not exist");
148         assertThat(execution.getVariable("WorkflowException")).isInstanceOf(WorkflowException.class);
149     }
150
151     private void checkCDSPropertiesBean(AbstractCDSPropertiesBean executionObject) {
152         assertThat(executionObject.getBlueprintName()).matches(TEST_PNF_RESOURCE_BLUEPRINT_NAME);
153         assertThat(executionObject.getBlueprintVersion()).matches(TEST_PNF_RESOURCE_BLUEPRINT_VERSION);
154         assertThat(executionObject.getRequestId()).matches(TEST_MSO_REQUEST_ID);
155         assertThat(executionObject.getSubRequestId()).matches(TEST_PNF_UUID);
156         assertThat(executionObject.getMode()).matches("async");
157         assertThat(executionObject.getActionName()).matches("config-deploy");
158         assertThat(executionObject.getOriginatorId()).matches("SO");
159
160         assertThat(executionObject.getRequestObject()).isNotNull();
161         String requestObject = executionObject.getRequestObject();
162
163         checkRequestJson(requestObject);
164     }
165
166     private void checkRequestJson(String requestObject) {
167         try {
168             ObjectMapper mapper = new ObjectMapper();
169             JsonNode tree = mapper.readTree(requestObject);
170             ConfigDeployRequestPnf configDeployRequestPnf = mapper
171                 .treeToValue(tree.at("/config-deploy-request"), ConfigDeployRequestPnf.class);
172             assertThat(configDeployRequestPnf.getResolutionKey()).matches(TEST_PNF_CORRELATION_ID);
173
174             ConfigDeployPropertiesForPnf properties = configDeployRequestPnf
175                 .getConfigDeployPropertiesForPnf();
176             assertThat(properties.getServiceInstanceId()).matches(TEST_SERVICE_INSTANCE_ID);
177             assertThat(properties.getPnfName()).matches(TEST_PNF_CORRELATION_ID);
178             assertThat(properties.getPnfId()).matches(TEST_PNF_UUID);
179             assertThat(properties.getPnfCustomizationUuid()).matches(TEST_PNF_RESOURCE_CUSTOMIZATION_UUID);
180             assertThat(properties.getServiceModelUuid()).matches(TEST_MODEL_UUID);
181             assertThat(properties.getPnfIpV4Address()).matches(TEST_IPV4_ADDRESS);
182             assertThat(properties.getPnfIpV6Address()).matches(TEST_IPV6_ADDRESS);
183         } catch (IOException e) {
184             e.printStackTrace();
185             fail("Check request body is json message" + e.getMessage());
186         }
187     }
188 }