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.util.HashMap;
30 import javax.ws.rs.container.ContainerRequestContext;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.junit.runner.RunWith;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.so.apihandler.common.RequestClientParameter;
41 import org.onap.so.apihandlerinfra.Action;
42 import org.onap.so.apihandlerinfra.Constants;
43 import org.onap.so.apihandlerinfra.infra.rest.exception.NoRecipeException;
44 import org.onap.so.constants.Status;
45 import org.onap.so.db.catalog.beans.Recipe;
46 import org.onap.so.db.catalog.beans.VnfComponentsRecipe;
47 import org.onap.so.db.catalog.client.CatalogDbClient;
48 import org.onap.so.db.request.beans.InfraActiveRequests;
49 import org.onap.so.db.request.client.RequestsDbClient;
50 import org.onap.so.serviceinstancebeans.ModelType;
51 import org.onap.so.serviceinstancebeans.RequestDetails;
52 import org.onap.so.serviceinstancebeans.RequestInfo;
53 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
54 import com.fasterxml.jackson.databind.ObjectMapper;
56 @RunWith(MockitoJUnitRunner.class)
57 public class VfModuleRestHandlerTest {
60 public ExpectedException exceptionRule = ExpectedException.none();
63 VFModuleRestHandler restHandler;
66 ContainerRequestContext mockRequestContext;
69 private CatalogDbClient catalogDbClient;
72 private RequestsDbClient infraActiveRequestsClient;
74 private ObjectMapper mapper = new ObjectMapper();
77 public void test_find_vf_module_recipe() throws NoRecipeException {
78 VnfComponentsRecipe expected = new VnfComponentsRecipe();
79 expected.setAction("createInstance");
80 doReturn(expected).when(catalogDbClient)
81 .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction("testModelId",
82 ModelType.vfModule.toString(), Action.createInstance.toString());
83 Recipe actual = restHandler.findVfModuleRecipe("testModelId", ModelType.vfModule.toString(),
84 Action.createInstance.toString());
85 assertThat(actual, sameBeanAs(expected));
86 Mockito.verify(catalogDbClient, Mockito.times(1))
87 .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction("testModelId",
88 ModelType.vfModule.toString(), Action.createInstance.toString());
92 public void test_find_vf_module_recipe_default_recipe() throws NoRecipeException {
93 VnfComponentsRecipe expected = new VnfComponentsRecipe();
94 expected.setAction("createInstance");
95 doReturn(null).when(catalogDbClient).getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
96 "testModelId", ModelType.vfModule.toString(), Action.createInstance.toString());
97 doReturn(expected).when(catalogDbClient)
98 .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction("GR-API-DEFAULT",
99 ModelType.vfModule.toString(), Action.createInstance.toString());
100 Recipe actual = restHandler.findVfModuleRecipe("testModelId", ModelType.vfModule.toString(),
101 Action.createInstance.toString());
102 assertThat(actual, sameBeanAs(expected));
103 Mockito.verify(catalogDbClient, Mockito.times(1))
104 .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction("testModelId",
105 ModelType.vfModule.toString(), Action.createInstance.toString());
109 public void test_find_vf_module_recipe_not_found() throws NoRecipeException {
110 VnfComponentsRecipe expected = new VnfComponentsRecipe();
111 expected.setAction("createInstance");
112 exceptionRule.expect(NoRecipeException.class);
113 exceptionRule.expectMessage(
114 "Unable to locate custom or default recipe for ModelType: vfModule , Action: createInstance, CustomizationId: testModelId");
115 restHandler.findVfModuleRecipe("testModelId", ModelType.vfModule.toString(), Action.createInstance.toString());
119 public void test_checkDuplicateRequest() {
120 ArgumentCaptor<HashMap> instanceIdCaptor = ArgumentCaptor.forClass(HashMap.class);
121 restHandler.checkDuplicateRequest("serviceInstanceId", "vnfId", "vfModuleId", "instanceName", "requestId");
122 Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).checkInstanceNameDuplicate(
123 instanceIdCaptor.capture(), eq("instanceName"), eq(ModelType.vfModule.toString()));
124 Map actualMap = instanceIdCaptor.getValue();
125 assertEquals("ServiceInstanceID should exist in map", "serviceInstanceId", actualMap.get("serviceInstanceId"));
126 assertEquals("VnfId should exit in map", "vnfId", actualMap.get("vnfInstanceId"));
127 assertEquals("VFModuleId should exit in map", "vfModuleId", actualMap.get("vfModuleInstanceId"));
131 public void test_saveInstanceName() {
132 ServiceInstancesRequest request = createTestRequest();
133 InfraActiveRequests dbRequest = createDatabaseRecord();
134 restHandler.saveInstanceName(request, dbRequest);
135 Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).updateInfraActiveRequests(dbRequest);
136 assertEquals("InstanceName Should Be Equal", "instanceName", dbRequest.getVfModuleName());
140 public void test_buildRequestParams() throws Exception {
141 RequestClientParameter expected = new RequestClientParameter.Builder().setRequestId("requestId")
142 .setServiceInstanceId("serviceInstanceId").setVnfId("vnfId").setVfModuleId("vfModuleId")
143 .setALaCarte(true).setRequestDetails(mapper.writeValueAsString(createTestRequest()))
144 .setRequestAction(Action.deleteInstance.toString())
145 .setRequestUri("http://localhost:8080/serviceInstances").setApiVersion("v8").build();
146 RequestClientParameter actual = restHandler.buildRequestParams(createTestRequest(),
147 "http://localhost:8080/serviceInstances", "requestId", "serviceInstanceId", "vnfId", "vfModuleId");
148 assertThat(actual, sameBeanAs(expected));
152 public void test_createInfraActiveRequestForDelete() {
153 InfraActiveRequests expected = new InfraActiveRequests();
154 expected.setRequestAction(Action.deleteInstance.toString());
155 expected.setServiceInstanceId("serviceInstanceId");
156 expected.setVnfId("vnfId");
157 expected.setVfModuleId("vfModuleId");
158 expected.setRequestId("requestId");
159 expected.setRequestorId("userId");
160 expected.setSource("VID");
161 expected.setRequestStatus(Status.IN_PROGRESS.toString());
162 expected.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
163 expected.setRequestUrl("http://localhost:9090");
164 expected.setRequestScope(ModelType.vfModule.toString());
165 InfraActiveRequests actual = restHandler.createInfraActiveRequestForDelete("requestId", "vfModuleId",
166 "serviceInstanceId", "vnfId", "userId", "VID", "http://localhost:9090");
167 assertThat(actual, sameBeanAs(expected).ignoring("startTime"));
168 Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).save(actual);
171 private ServiceInstancesRequest createTestRequest() {
172 ServiceInstancesRequest request = new ServiceInstancesRequest();
173 RequestDetails requestDetails = new RequestDetails();
174 RequestInfo requestInfo = new RequestInfo();
175 requestInfo.setInstanceName("instanceName");
176 requestDetails.setRequestInfo(requestInfo);
177 request.setRequestDetails(requestDetails);
181 private InfraActiveRequests createDatabaseRecord() {
182 InfraActiveRequests request = new InfraActiveRequests();
183 request.setRequestId("requestId");