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.junit.runner.RunWith;
 
  38 import org.mockito.ArgumentMatchers;
 
  39 import org.mockito.InjectMocks;
 
  40 import org.mockito.Mock;
 
  41 import org.mockito.junit.MockitoJUnitRunner;
 
  42 import org.onap.aai.domain.yang.Vserver;
 
  43 import org.onap.appc.client.lcm.model.Action;
 
  44 import org.onap.so.appc.orchestrator.service.beans.ApplicationControllerTaskRequest;
 
  45 import org.onap.so.appc.orchestrator.service.beans.ApplicationControllerVnf;
 
  46 import org.onap.so.bpmn.common.data.TestDataSetup;
 
  47 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
 
  48 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
 
  49 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
 
  50 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
 
  51 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters;
 
  52 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
 
  53 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
 
  54 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
 
  55 import org.onap.so.client.orchestration.AAIVnfResources;
 
  56 import org.onap.so.client.policy.JettisonStyleMapperProvider;
 
  57 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
 
  58 import com.fasterxml.jackson.databind.ObjectMapper;
 
  59 import org.onap.so.db.catalog.client.CatalogDbClient;
 
  61 @RunWith(MockitoJUnitRunner.Silent.class)
 
  62 public class AppcOrchestratorPreProcessorTest extends TestDataSetup {
 
  64     private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/";
 
  66     protected ExtractPojosForBB extractPojosForBB;
 
  68     protected AAIVnfResources aaiVnfResources;
 
  70     protected CatalogDbClient catalogDbClient;
 
  72     private AppcOrchestratorPreProcessor appcOrchestratorPreProcessor = new AppcOrchestratorPreProcessor();
 
  74     private ObjectMapper mapper = new JettisonStyleMapperProvider().getMapper();
 
  77     public void buildAppcTaskRequestTest() throws Exception {
 
  78         final String expectedRequestJson =
 
  79                 new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "appcTaskRequest.json")));
 
  80         ApplicationControllerTaskRequest expectedTaskRequest =
 
  81                 mapper.readValue(expectedRequestJson, ApplicationControllerTaskRequest.class);
 
  82         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "-TEST");
 
  83         fillRequiredAppcExecutionFields();
 
  84         GenericVnf genericVnf = getTestGenericVnf();
 
  85         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(genericVnf);
 
  86         mockReferenceResponse();
 
  87         execution.getLookupMap().put(ResourceKey.VF_MODULE_ID, "VF-MODULE-ID-TEST");
 
  88         VfModule vfModule = new VfModule();
 
  89         vfModule.setVfModuleId("VF-MODULE-ID");
 
  90         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
 
  91         appcOrchestratorPreProcessor.buildAppcTaskRequest(execution, "Lock");
 
  92         ApplicationControllerTaskRequest actualTaskRequest = execution.getVariable("appcOrchestratorRequest");
 
  93         assertThat(actualTaskRequest, sameBeanAs(expectedTaskRequest));
 
  97     public void getVserversForAppcTest() throws Exception {
 
  99         GenericVnf genericVnf = getTestGenericVnf();
 
 101         final String aaiVnfJson =
 
 102                 new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "aaiGenericVnfWithVservers.json")));
 
 103         final String aaiVserverJson =
 
 104                 new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "aaiVserverQueryResponse.json")));
 
 105         AAIResultWrapper aaiResultWrapper = new AAIResultWrapper(aaiVnfJson);
 
 106         ObjectMapper mapper = new ObjectMapper();
 
 107         Vserver vserver = mapper.readValue(aaiVserverJson, Vserver.class);
 
 108         doReturn(aaiResultWrapper).when(aaiVnfResources).queryVnfWrapperById(genericVnf);
 
 109         doReturn(Optional.of(vserver)).when(aaiVnfResources).getVserver(ArgumentMatchers.any(AAIResourceUri.class));
 
 110         appcOrchestratorPreProcessor.getVserversForAppc(execution, genericVnf);
 
 111         ArrayList<String> vserverIdList = execution.getVariable("vserverIdList");
 
 112         ArrayList<String> expectedVserverIdList = new ArrayList<String>();
 
 113         expectedVserverIdList.add("1b3f44e5-d96d-4aac-bd9a-310e8cfb0af5");
 
 114         expectedVserverIdList.add("14551849-1e70-45cd-bc5d-a256d49548a2");
 
 115         expectedVserverIdList.add("48bd7f11-408f-417c-b834-b41c1b98f7d7");
 
 116         ArrayList<String> vmIdList = execution.getVariable("vmIdList");
 
 117         ArrayList<String> expectedVmIdList = new ArrayList<String>();
 
 118         expectedVmIdList.add("http://VSERVER-link.com");
 
 119         expectedVmIdList.add("http://VSERVER-link.com");
 
 120         expectedVmIdList.add("http://VSERVER-link.com");
 
 121         assertEquals(vserverIdList, expectedVserverIdList);
 
 122         assertEquals(vmIdList, expectedVmIdList);
 
 126     public void addVmInfoToAppcTaskRequestTest() {
 
 127         ApplicationControllerTaskRequest appcTaskRequest = new ApplicationControllerTaskRequest();
 
 128         ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
 
 129         appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
 
 130         execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
 
 131         ArrayList<String> vmIdList = new ArrayList<String>();
 
 132         vmIdList.add("http://VSERVER-link.com");
 
 133         vmIdList.add("http://VSERVER-link.com");
 
 134         vmIdList.add("http://VSERVER-link.com");
 
 135         execution.setVariable("vmIdList", vmIdList);
 
 136         ArrayList<String> vserverIdList = new ArrayList<String>();
 
 137         vserverIdList.add("1b3f44e5-d96d-4aac-bd9a-310e8cfb0af5");
 
 138         vserverIdList.add("14551849-1e70-45cd-bc5d-a256d49548a2");
 
 139         vserverIdList.add("48bd7f11-408f-417c-b834-b41c1b98f7d7");
 
 140         execution.setVariable("vserverIdList", vserverIdList);
 
 141         execution.setVariable("vmIndex", 1);
 
 142         appcOrchestratorPreProcessor.addVmInfoToAppcTaskRequest(execution);
 
 143         Integer nextVmIndex = execution.getVariable("vmIndex");
 
 144         assertThat(nextVmIndex == 2);
 
 145         Integer vmIdListSize = execution.getVariable("vmIdListSize");
 
 146         assertThat(vmIdListSize == 3);
 
 147         appcTaskRequest = execution.getVariable("appcOrchestratorRequest");
 
 148         assertEquals(appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVserverId(),
 
 149                 "14551849-1e70-45cd-bc5d-a256d49548a2");
 
 150         assertEquals(appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVmId(),
 
 151                 "http://VSERVER-link.com");
 
 154     private void mockReferenceResponse() {
 
 155         ControllerSelectionReference reference = new ControllerSelectionReference();
 
 156         reference.setControllerName("TEST-CONTROLLER-NAME");
 
 157         when(catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(eq("TEST-VNF-TYPE"),
 
 158                 eq(Action.Lock.toString()))).thenReturn(reference);
 
 161     private void fillRequiredAppcExecutionFields() {
 
 162         RequestContext context = new RequestContext();
 
 163         context.setMsoRequestId("TEST-MSO-ID");
 
 164         context.setRequestorId("testRequestorId");
 
 165         execution.setVariable("aicIdentity", "AIC-TEST");
 
 166         execution.setVariable("vmIdList", "VM-ID-LIST-TEST");
 
 167         execution.setVariable("vserverIdList", "VSERVER-ID-LIST");
 
 168         execution.setVariable("identityUrl", "IDENTITY-URL-TEST");
 
 169         execution.getGeneralBuildingBlock().setRequestContext(context);
 
 172     private GenericVnf getTestGenericVnf() {
 
 173         GenericVnf genericVnf = new GenericVnf();
 
 174         genericVnf.setVnfId("TEST-VNF-ID");
 
 175         genericVnf.setVnfType("TEST-VNF-TYPE");
 
 176         genericVnf.setVnfName("TEST-VNF-NAME");
 
 177         genericVnf.setIpv4OamAddress("127.0.0.1");
 
 182     public void buildAppcTaskRequestConfigModifyTest() throws Exception {
 
 183         final String expectedRequestJson =
 
 184                 new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "appcTaskRequestConfigModify.json")));
 
 185         ApplicationControllerTaskRequest expectedTaskRequest =
 
 186                 mapper.readValue(expectedRequestJson, ApplicationControllerTaskRequest.class);
 
 187         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "-TEST");
 
 188         fillRequiredAppcExecutionFieldsConfigModify();
 
 189         GenericVnf genericVnf = getTestGenericVnf();
 
 190         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(genericVnf);
 
 191         mockReferenceResponseForConfigModify();
 
 192         execution.getLookupMap().put(ResourceKey.VF_MODULE_ID, "VF-MODULE-ID-TEST");
 
 193         VfModule vfModule = new VfModule();
 
 194         vfModule.setVfModuleId("VF-MODULE-ID");
 
 195         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
 
 196         appcOrchestratorPreProcessor.buildAppcTaskRequest(execution, "ConfigModify");
 
 197         ApplicationControllerTaskRequest actualTaskRequest = execution.getVariable("appcOrchestratorRequest");
 
 198         assertThat(actualTaskRequest, sameBeanAs(expectedTaskRequest));
 
 201     private void fillRequiredAppcExecutionFieldsConfigModify() {
 
 202         RequestContext context = new RequestContext();
 
 203         RequestParameters requestParameters = new RequestParameters();
 
 204         requestParameters.setPayload(
 
 205                 "{\"request_parameters\":{\"host_ip_address\":\"10.10.10.10\"},\"configuration_parameters\":{\"name1\":\"value1\",\"name2\":\"value2\"}}");
 
 206         context.setRequestParameters(requestParameters);
 
 207         context.setMsoRequestId("TEST-MSO-ID");
 
 208         context.setRequestorId("testRequestorId");
 
 209         execution.setVariable("aicIdentity", "AIC-TEST");
 
 210         execution.setVariable("vmIdList", "VM-ID-LIST-TEST");
 
 211         execution.setVariable("vserverIdList", "VSERVER-ID-LIST");
 
 212         execution.setVariable("identityUrl", "IDENTITY-URL-TEST");
 
 213         execution.getGeneralBuildingBlock().setRequestContext(context);
 
 216     private void mockReferenceResponseForConfigModify() {
 
 217         ControllerSelectionReference reference = new ControllerSelectionReference();
 
 218         reference.setControllerName("APPC");
 
 219         when(catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(eq("TEST-VNF-TYPE"),
 
 220                 eq(Action.ConfigModify.toString()))).thenReturn(reference);