2  * ============LICENSE_START=======================================================
 
   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
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  23 package org.onap.so.bpmn.infrastructure.appc.tasks;
 
  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.so.client.aai.entities.AAIResultWrapper;
 
  50 import org.onap.so.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;
 
  55 public class AppcOrchestratorPreProcessorTest extends BaseTaskTest {
 
  57     private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/";
 
  60     private AppcOrchestratorPreProcessor appcOrchestratorPreProcessor = new AppcOrchestratorPreProcessor();
 
  62     private ObjectMapper mapper = new JettisonStyleMapperProvider().getMapper();
 
  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));
 
  85     public void getVserversForAppcTest() throws Exception {
 
  87         GenericVnf genericVnf = getTestGenericVnf();
 
  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);
 
 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");
 
 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);
 
 149     private void fillRequiredAppcExecutionFields() {
 
 150         RequestContext context = new RequestContext();
 
 151         context.setMsoRequestId("TEST-MSO-ID");
 
 152         execution.setVariable("aicIdentity", "AIC-TEST");
 
 153         execution.setVariable("vmIdList", "VM-ID-LIST-TEST");
 
 154         execution.setVariable("vserverIdList", "VSERVER-ID-LIST");
 
 155         execution.setVariable("identityUrl", "IDENTITY-URL-TEST");
 
 156         execution.getGeneralBuildingBlock().setRequestContext(context);
 
 159     private GenericVnf getTestGenericVnf() {
 
 160         GenericVnf genericVnf = new GenericVnf();
 
 161         genericVnf.setVnfId("TEST-VNF-ID");
 
 162         genericVnf.setVnfType("TEST-VNF-TYPE");
 
 163         genericVnf.setVnfName("TEST-VNF-NAME");
 
 164         genericVnf.setIpv4OamAddress("127.0.0.1");
 
 169     public void buildAppcTaskRequestConfigModifyTest() throws Exception {
 
 170         final String expectedRequestJson =
 
 171                 new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "appcTaskRequestConfigModify.json")));
 
 172         ApplicationControllerTaskRequest expectedTaskRequest =
 
 173                 mapper.readValue(expectedRequestJson, ApplicationControllerTaskRequest.class);
 
 174         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "-TEST");
 
 175         fillRequiredAppcExecutionFieldsConfigModify();
 
 176         GenericVnf genericVnf = getTestGenericVnf();
 
 177         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(genericVnf);
 
 178         mockReferenceResponseForConfigModify();
 
 179         execution.getLookupMap().put(ResourceKey.VF_MODULE_ID, "VF-MODULE-ID-TEST");
 
 180         VfModule vfModule = new VfModule();
 
 181         vfModule.setVfModuleId("VF-MODULE-ID");
 
 182         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
 
 183         appcOrchestratorPreProcessor.buildAppcTaskRequest(execution, "ConfigModify");
 
 184         ApplicationControllerTaskRequest actualTaskRequest = execution.getVariable("appcOrchestratorRequest");
 
 185         assertThat(actualTaskRequest, sameBeanAs(expectedTaskRequest));
 
 188     private void fillRequiredAppcExecutionFieldsConfigModify() {
 
 189         RequestContext context = new RequestContext();
 
 190         RequestParameters requestParameters = new RequestParameters();
 
 191         requestParameters.setPayload(
 
 192                 "{\"request_parameters\":{\"host_ip_address\":\"10.10.10.10\"},\"configuration_parameters\":{\"name1\":\"value1\",\"name2\":\"value2\"}}");
 
 193         context.setRequestParameters(requestParameters);
 
 194         context.setMsoRequestId("TEST-MSO-ID");
 
 195         execution.setVariable("aicIdentity", "AIC-TEST");
 
 196         execution.setVariable("vmIdList", "VM-ID-LIST-TEST");
 
 197         execution.setVariable("vserverIdList", "VSERVER-ID-LIST");
 
 198         execution.setVariable("identityUrl", "IDENTITY-URL-TEST");
 
 199         execution.getGeneralBuildingBlock().setRequestContext(context);
 
 202     private void mockReferenceResponseForConfigModify() {
 
 203         ControllerSelectionReference reference = new ControllerSelectionReference();
 
 204         reference.setControllerName("APPC");
 
 205         when(catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(eq("TEST-VNF-TYPE"),
 
 206                 eq(Action.ConfigModify.toString()))).thenReturn(reference);