2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.so.apihandlerinfra.infra.rest.handler;
 
  23 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
 
  24 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
 
  25 import static org.junit.Assert.assertEquals;
 
  26 import static org.mockito.ArgumentMatchers.eq;
 
  27 import static org.mockito.Mockito.doReturn;
 
  28 import java.net.MalformedURLException;
 
  29 import java.util.HashMap;
 
  31 import javax.ws.rs.container.ContainerRequestContext;
 
  32 import org.junit.Rule;
 
  33 import org.junit.Test;
 
  34 import org.junit.rules.ExpectedException;
 
  35 import org.junit.runner.RunWith;
 
  36 import org.mockito.ArgumentCaptor;
 
  37 import org.mockito.InjectMocks;
 
  38 import org.mockito.Mock;
 
  39 import org.mockito.Mockito;
 
  40 import org.mockito.junit.MockitoJUnitRunner;
 
  41 import org.onap.so.apihandler.common.RequestClientParameter;
 
  42 import org.onap.so.apihandlerinfra.Action;
 
  43 import org.onap.so.apihandlerinfra.Constants;
 
  44 import org.onap.so.apihandlerinfra.infra.rest.exception.NoRecipeException;
 
  45 import org.onap.so.apihandlerinfra.infra.rest.handler.VFModuleRestHandler;
 
  46 import org.onap.so.constants.Status;
 
  47 import org.onap.so.db.catalog.beans.Recipe;
 
  48 import org.onap.so.db.catalog.beans.VnfComponentsRecipe;
 
  49 import org.onap.so.db.catalog.client.CatalogDbClient;
 
  50 import org.onap.so.db.request.beans.InfraActiveRequests;
 
  51 import org.onap.so.db.request.client.RequestsDbClient;
 
  52 import org.onap.so.serviceinstancebeans.ModelType;
 
  53 import org.onap.so.serviceinstancebeans.RequestDetails;
 
  54 import org.onap.so.serviceinstancebeans.RequestInfo;
 
  55 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
 
  56 import com.fasterxml.jackson.databind.ObjectMapper;
 
  58 @RunWith(MockitoJUnitRunner.class)
 
  59 public class VfModuleRestHandlerTest {
 
  62     public ExpectedException exceptionRule = ExpectedException.none();
 
  65     VFModuleRestHandler restHandler;
 
  68     ContainerRequestContext mockRequestContext;
 
  71     private CatalogDbClient catalogDbClient;
 
  74     private RequestsDbClient infraActiveRequestsClient;
 
  76     private ObjectMapper mapper = new ObjectMapper();
 
  79     public void test_find_vf_module_recipe() throws MalformedURLException, NoRecipeException {
 
  80         VnfComponentsRecipe expected = new VnfComponentsRecipe();
 
  81         expected.setAction("createInstance");
 
  82         doReturn(expected).when(catalogDbClient)
 
  83                 .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction("testModelId",
 
  84                         ModelType.vfModule.toString(), Action.createInstance.toString());
 
  85         Recipe actual = restHandler.findVfModuleRecipe("testModelId", ModelType.vfModule.toString(),
 
  86                 Action.createInstance.toString());
 
  87         assertThat(actual, sameBeanAs(expected));
 
  88         Mockito.verify(catalogDbClient, Mockito.times(1))
 
  89                 .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction("testModelId",
 
  90                         ModelType.vfModule.toString(), Action.createInstance.toString());
 
  94     public void test_find_vf_module_recipe_default_recipe() throws MalformedURLException, NoRecipeException {
 
  95         VnfComponentsRecipe expected = new VnfComponentsRecipe();
 
  96         expected.setAction("createInstance");
 
  97         doReturn(null).when(catalogDbClient).getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
 
  98                 "testModelId", ModelType.vfModule.toString(), Action.createInstance.toString());
 
  99         doReturn(expected).when(catalogDbClient)
 
 100                 .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction("GR-API-DEFAULT",
 
 101                         ModelType.vfModule.toString(), Action.createInstance.toString());
 
 102         Recipe actual = restHandler.findVfModuleRecipe("testModelId", ModelType.vfModule.toString(),
 
 103                 Action.createInstance.toString());
 
 104         assertThat(actual, sameBeanAs(expected));
 
 105         Mockito.verify(catalogDbClient, Mockito.times(1))
 
 106                 .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction("testModelId",
 
 107                         ModelType.vfModule.toString(), Action.createInstance.toString());
 
 111     public void test_find_vf_module_recipe_not_found() throws MalformedURLException, NoRecipeException {
 
 112         VnfComponentsRecipe expected = new VnfComponentsRecipe();
 
 113         expected.setAction("createInstance");
 
 114         exceptionRule.expect(NoRecipeException.class);
 
 115         exceptionRule.expectMessage(
 
 116                 "Unable to locate custom or default recipe for ModelType: vfModule , Action: createInstance, CustomizationId: testModelId");
 
 117         restHandler.findVfModuleRecipe("testModelId", ModelType.vfModule.toString(), Action.createInstance.toString());
 
 121     public void test_checkDuplicateRequest() throws MalformedURLException, NoRecipeException {
 
 122         ArgumentCaptor<HashMap> instanceIdCaptor = ArgumentCaptor.forClass(HashMap.class);
 
 123         restHandler.checkDuplicateRequest("serviceInstanceId", "vnfId", "vfModuleId", "instanceName", "requestId");
 
 124         Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).checkInstanceNameDuplicate(
 
 125                 instanceIdCaptor.capture(), eq("instanceName"), eq(ModelType.vfModule.toString()));
 
 126         Map actualMap = instanceIdCaptor.getValue();
 
 127         assertEquals("ServiceInstanceID should exist in map", "serviceInstanceId", actualMap.get("serviceInstanceId"));
 
 128         assertEquals("VnfId should exit in map", "vnfId", actualMap.get("vnfInstanceId"));
 
 129         assertEquals("VFModuleId should exit in map", "vfModuleId", actualMap.get("vfModuleInstanceId"));
 
 133     public void test_saveInstanceName() throws MalformedURLException, NoRecipeException {
 
 134         ServiceInstancesRequest request = createTestRequest();
 
 135         InfraActiveRequests dbRequest = createDatabaseRecord();
 
 136         restHandler.saveInstanceName(request, dbRequest);
 
 137         Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).updateInfraActiveRequests(dbRequest);
 
 138         assertEquals("InstanceName Should Be Equal", "instanceName", dbRequest.getVfModuleName());
 
 142     public void test_buildRequestParams() throws Exception {
 
 143         RequestClientParameter expected = new RequestClientParameter.Builder().setRequestId("requestId")
 
 144                 .setServiceInstanceId("serviceInstanceId").setVnfId("vnfId").setVfModuleId("vfModuleId")
 
 145                 .setALaCarte(true).setRequestDetails(mapper.writeValueAsString(createTestRequest()))
 
 146                 .setRequestAction(Action.deleteInstance.toString())
 
 147                 .setRequestUri("http://localhost:8080/serviceInstances").setApiVersion("v8").build();
 
 148         RequestClientParameter actual = restHandler.buildRequestParams(createTestRequest(),
 
 149                 "http://localhost:8080/serviceInstances", "requestId", "serviceInstanceId", "vnfId", "vfModuleId");
 
 150         assertThat(actual, sameBeanAs(expected));
 
 154     public void test_createInfraActiveRequestForDelete() throws Exception {
 
 155         InfraActiveRequests expected = new InfraActiveRequests();
 
 156         expected.setRequestAction(Action.deleteInstance.toString());
 
 157         expected.setAction(Action.deleteInstance.toString());
 
 158         expected.setServiceInstanceId("serviceInstanceId");
 
 159         expected.setVnfId("vnfId");
 
 160         expected.setVfModuleId("vfModuleId");
 
 161         expected.setRequestId("requestId");
 
 162         expected.setRequestorId("userId");
 
 163         expected.setSource("VID");
 
 164         expected.setRequestStatus(Status.IN_PROGRESS.toString());
 
 165         expected.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
 
 166         expected.setRequestUrl("http://localhost:9090");
 
 167         expected.setRequestScope(ModelType.vfModule.toString());
 
 168         InfraActiveRequests actual = restHandler.createInfraActiveRequestForDelete("requestId", "vfModuleId",
 
 169                 "serviceInstanceId", "vnfId", "userId", "VID", "http://localhost:9090");
 
 170         assertThat(actual, sameBeanAs(expected).ignoring("startTime"));
 
 171         Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).save(actual);
 
 174     private ServiceInstancesRequest createTestRequest() {
 
 175         ServiceInstancesRequest request = new ServiceInstancesRequest();
 
 176         RequestDetails requestDetails = new RequestDetails();
 
 177         RequestInfo requestInfo = new RequestInfo();
 
 178         requestInfo.setInstanceName("instanceName");
 
 179         requestDetails.setRequestInfo(requestInfo);
 
 180         request.setRequestDetails(requestDetails);
 
 184     private InfraActiveRequests createDatabaseRecord() {
 
 185         InfraActiveRequests request = new InfraActiveRequests();
 
 186         request.setRequestId("requestId");