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.setServiceInstanceId("serviceInstanceId");
158 expected.setVnfId("vnfId");
159 expected.setVfModuleId("vfModuleId");
160 expected.setRequestId("requestId");
161 expected.setRequestorId("userId");
162 expected.setSource("VID");
163 expected.setRequestStatus(Status.IN_PROGRESS.toString());
164 expected.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
165 expected.setRequestUrl("http://localhost:9090");
166 expected.setRequestScope(ModelType.vfModule.toString());
167 InfraActiveRequests actual = restHandler.createInfraActiveRequestForDelete("requestId", "vfModuleId",
168 "serviceInstanceId", "vnfId", "userId", "VID", "http://localhost:9090");
169 assertThat(actual, sameBeanAs(expected).ignoring("startTime"));
170 Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).save(actual);
173 private ServiceInstancesRequest createTestRequest() {
174 ServiceInstancesRequest request = new ServiceInstancesRequest();
175 RequestDetails requestDetails = new RequestDetails();
176 RequestInfo requestInfo = new RequestInfo();
177 requestInfo.setInstanceName("instanceName");
178 requestDetails.setRequestInfo(requestInfo);
179 request.setRequestDetails(requestDetails);
183 private InfraActiveRequests createDatabaseRecord() {
184 InfraActiveRequests request = new InfraActiveRequests();
185 request.setRequestId("requestId");