Merge "Enable DeleteChildService functionality" into recursive-orch
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / appc / tasks / AppcOrchestratorPreProcessorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.appc.tasks;
24
25 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
26 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.junit.Assert.assertEquals;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.doReturn;
31 import static org.mockito.Mockito.when;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Optional;
36 import org.junit.Test;
37 import org.mockito.ArgumentMatchers;
38 import org.mockito.InjectMocks;
39 import org.onap.aai.domain.yang.Vserver;
40 import org.onap.appc.client.lcm.model.Action;
41 import org.onap.so.appc.orchestrator.service.beans.ApplicationControllerTaskRequest;
42 import org.onap.so.appc.orchestrator.service.beans.ApplicationControllerVnf;
43 import org.onap.so.bpmn.BaseTaskTest;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
45 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
46 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
47 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
48 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters;
49 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
50 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
51 import org.onap.so.client.policy.JettisonStyleMapperProvider;
52 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
53 import com.fasterxml.jackson.databind.ObjectMapper;
54
55 public class AppcOrchestratorPreProcessorTest extends BaseTaskTest {
56
57     private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/";
58
59     @InjectMocks
60     private AppcOrchestratorPreProcessor appcOrchestratorPreProcessor = new AppcOrchestratorPreProcessor();
61
62     private ObjectMapper mapper = new JettisonStyleMapperProvider().getMapper();
63
64     @Test
65     public void buildAppcTaskRequestTest() throws Exception {
66         final String expectedRequestJson =
67                 new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "appcTaskRequest.json")));
68         ApplicationControllerTaskRequest expectedTaskRequest =
69                 mapper.readValue(expectedRequestJson, ApplicationControllerTaskRequest.class);
70         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "-TEST");
71         fillRequiredAppcExecutionFields();
72         GenericVnf genericVnf = getTestGenericVnf();
73         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(genericVnf);
74         mockReferenceResponse();
75         execution.getLookupMap().put(ResourceKey.VF_MODULE_ID, "VF-MODULE-ID-TEST");
76         VfModule vfModule = new VfModule();
77         vfModule.setVfModuleId("VF-MODULE-ID");
78         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
79         appcOrchestratorPreProcessor.buildAppcTaskRequest(execution, "Lock");
80         ApplicationControllerTaskRequest actualTaskRequest = execution.getVariable("appcOrchestratorRequest");
81         assertThat(actualTaskRequest, sameBeanAs(expectedTaskRequest));
82     }
83
84     @Test
85     public void getVserversForAppcTest() throws Exception {
86
87         GenericVnf genericVnf = getTestGenericVnf();
88
89         final String aaiVnfJson =
90                 new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "aaiGenericVnfWithVservers.json")));
91         final String aaiVserverJson =
92                 new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "aaiVserverQueryResponse.json")));
93         AAIResultWrapper aaiResultWrapper = new AAIResultWrapper(aaiVnfJson);
94         ObjectMapper mapper = new ObjectMapper();
95         Vserver vserver = mapper.readValue(aaiVserverJson, Vserver.class);
96         doReturn(aaiResultWrapper).when(aaiVnfResources).queryVnfWrapperById(genericVnf);
97         doReturn(Optional.of(vserver)).when(aaiVnfResources).getVserver(ArgumentMatchers.any(AAIResourceUri.class));
98         appcOrchestratorPreProcessor.getVserversForAppc(execution, genericVnf);
99         ArrayList<String> vserverIdList = execution.getVariable("vserverIdList");
100         ArrayList<String> expectedVserverIdList = new ArrayList<String>();
101         expectedVserverIdList.add("1b3f44e5-d96d-4aac-bd9a-310e8cfb0af5");
102         expectedVserverIdList.add("14551849-1e70-45cd-bc5d-a256d49548a2");
103         expectedVserverIdList.add("48bd7f11-408f-417c-b834-b41c1b98f7d7");
104         ArrayList<String> vmIdList = execution.getVariable("vmIdList");
105         ArrayList<String> expectedVmIdList = new ArrayList<String>();
106         expectedVmIdList.add("http://VSERVER-link.com");
107         expectedVmIdList.add("http://VSERVER-link.com");
108         expectedVmIdList.add("http://VSERVER-link.com");
109         assertEquals(vserverIdList, expectedVserverIdList);
110         assertEquals(vmIdList, expectedVmIdList);
111     }
112
113     @Test
114     public void addVmInfoToAppcTaskRequestTest() throws Exception {
115         ApplicationControllerTaskRequest appcTaskRequest = new ApplicationControllerTaskRequest();
116         ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
117         appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
118         execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
119         ArrayList<String> vmIdList = new ArrayList<String>();
120         vmIdList.add("http://VSERVER-link.com");
121         vmIdList.add("http://VSERVER-link.com");
122         vmIdList.add("http://VSERVER-link.com");
123         execution.setVariable("vmIdList", vmIdList);
124         ArrayList<String> vserverIdList = new ArrayList<String>();
125         vserverIdList.add("1b3f44e5-d96d-4aac-bd9a-310e8cfb0af5");
126         vserverIdList.add("14551849-1e70-45cd-bc5d-a256d49548a2");
127         vserverIdList.add("48bd7f11-408f-417c-b834-b41c1b98f7d7");
128         execution.setVariable("vserverIdList", vserverIdList);
129         execution.setVariable("vmIndex", 1);
130         appcOrchestratorPreProcessor.addVmInfoToAppcTaskRequest(execution);
131         Integer nextVmIndex = execution.getVariable("vmIndex");
132         assertThat(nextVmIndex == 2);
133         Integer vmIdListSize = execution.getVariable("vmIdListSize");
134         assertThat(vmIdListSize == 3);
135         appcTaskRequest = execution.getVariable("appcOrchestratorRequest");
136         assertEquals(appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVserverId(),
137                 "14551849-1e70-45cd-bc5d-a256d49548a2");
138         assertEquals(appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVmId(),
139                 "http://VSERVER-link.com");
140     }
141
142     private void mockReferenceResponse() {
143         ControllerSelectionReference reference = new ControllerSelectionReference();
144         reference.setControllerName("TEST-CONTROLLER-NAME");
145         when(catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(eq("TEST-VNF-TYPE"),
146                 eq(Action.Lock.toString()))).thenReturn(reference);
147     }
148
149     private void fillRequiredAppcExecutionFields() {
150         RequestContext context = new RequestContext();
151         context.setMsoRequestId("TEST-MSO-ID");
152         context.setRequestorId("testRequestorId");
153         execution.setVariable("aicIdentity", "AIC-TEST");
154         execution.setVariable("vmIdList", "VM-ID-LIST-TEST");
155         execution.setVariable("vserverIdList", "VSERVER-ID-LIST");
156         execution.setVariable("identityUrl", "IDENTITY-URL-TEST");
157         execution.getGeneralBuildingBlock().setRequestContext(context);
158     }
159
160     private GenericVnf getTestGenericVnf() {
161         GenericVnf genericVnf = new GenericVnf();
162         genericVnf.setVnfId("TEST-VNF-ID");
163         genericVnf.setVnfType("TEST-VNF-TYPE");
164         genericVnf.setVnfName("TEST-VNF-NAME");
165         genericVnf.setIpv4OamAddress("127.0.0.1");
166         return genericVnf;
167     }
168
169     @Test
170     public void buildAppcTaskRequestConfigModifyTest() throws Exception {
171         final String expectedRequestJson =
172                 new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "appcTaskRequestConfigModify.json")));
173         ApplicationControllerTaskRequest expectedTaskRequest =
174                 mapper.readValue(expectedRequestJson, ApplicationControllerTaskRequest.class);
175         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "-TEST");
176         fillRequiredAppcExecutionFieldsConfigModify();
177         GenericVnf genericVnf = getTestGenericVnf();
178         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(genericVnf);
179         mockReferenceResponseForConfigModify();
180         execution.getLookupMap().put(ResourceKey.VF_MODULE_ID, "VF-MODULE-ID-TEST");
181         VfModule vfModule = new VfModule();
182         vfModule.setVfModuleId("VF-MODULE-ID");
183         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
184         appcOrchestratorPreProcessor.buildAppcTaskRequest(execution, "ConfigModify");
185         ApplicationControllerTaskRequest actualTaskRequest = execution.getVariable("appcOrchestratorRequest");
186         assertThat(actualTaskRequest, sameBeanAs(expectedTaskRequest));
187     }
188
189     private void fillRequiredAppcExecutionFieldsConfigModify() {
190         RequestContext context = new RequestContext();
191         RequestParameters requestParameters = new RequestParameters();
192         requestParameters.setPayload(
193                 "{\"request_parameters\":{\"host_ip_address\":\"10.10.10.10\"},\"configuration_parameters\":{\"name1\":\"value1\",\"name2\":\"value2\"}}");
194         context.setRequestParameters(requestParameters);
195         context.setMsoRequestId("TEST-MSO-ID");
196         context.setRequestorId("testRequestorId");
197         execution.setVariable("aicIdentity", "AIC-TEST");
198         execution.setVariable("vmIdList", "VM-ID-LIST-TEST");
199         execution.setVariable("vserverIdList", "VSERVER-ID-LIST");
200         execution.setVariable("identityUrl", "IDENTITY-URL-TEST");
201         execution.getGeneralBuildingBlock().setRequestContext(context);
202     }
203
204     private void mockReferenceResponseForConfigModify() {
205         ControllerSelectionReference reference = new ControllerSelectionReference();
206         reference.setControllerName("APPC");
207         when(catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(eq("TEST-VNF-TYPE"),
208                 eq(Action.ConfigModify.toString()))).thenReturn(reference);
209     }
210
211 }