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.constants.Status;
46 import org.onap.so.db.catalog.beans.Recipe;
47 import org.onap.so.db.catalog.beans.VnfComponentsRecipe;
48 import org.onap.so.db.catalog.client.CatalogDbClient;
49 import org.onap.so.db.request.beans.InfraActiveRequests;
50 import org.onap.so.db.request.client.RequestsDbClient;
51 import org.onap.so.serviceinstancebeans.ModelType;
52 import org.onap.so.serviceinstancebeans.RequestDetails;
53 import org.onap.so.serviceinstancebeans.RequestInfo;
54 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
55 import com.fasterxml.jackson.databind.ObjectMapper;
57 @RunWith(MockitoJUnitRunner.class)
58 public class VfModuleRestHandlerTest {
61 public ExpectedException exceptionRule = ExpectedException.none();
64 VFModuleRestHandler restHandler;
67 ContainerRequestContext mockRequestContext;
70 private CatalogDbClient catalogDbClient;
73 private RequestsDbClient infraActiveRequestsClient;
75 private ObjectMapper mapper = new ObjectMapper();
78 public void test_find_vf_module_recipe() throws MalformedURLException, NoRecipeException {
79 VnfComponentsRecipe expected = new VnfComponentsRecipe();
80 expected.setAction("createInstance");
81 doReturn(expected).when(catalogDbClient)
82 .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction("testModelId",
83 ModelType.vfModule.toString(), Action.createInstance.toString());
84 Recipe actual = restHandler.findVfModuleRecipe("testModelId", ModelType.vfModule.toString(),
85 Action.createInstance.toString());
86 assertThat(actual, sameBeanAs(expected));
87 Mockito.verify(catalogDbClient, Mockito.times(1))
88 .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction("testModelId",
89 ModelType.vfModule.toString(), Action.createInstance.toString());
93 public void test_find_vf_module_recipe_default_recipe() throws MalformedURLException, NoRecipeException {
94 VnfComponentsRecipe expected = new VnfComponentsRecipe();
95 expected.setAction("createInstance");
96 doReturn(null).when(catalogDbClient).getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
97 "testModelId", ModelType.vfModule.toString(), Action.createInstance.toString());
98 doReturn(expected).when(catalogDbClient)
99 .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction("GR-API-DEFAULT",
100 ModelType.vfModule.toString(), Action.createInstance.toString());
101 Recipe actual = restHandler.findVfModuleRecipe("testModelId", ModelType.vfModule.toString(),
102 Action.createInstance.toString());
103 assertThat(actual, sameBeanAs(expected));
104 Mockito.verify(catalogDbClient, Mockito.times(1))
105 .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction("testModelId",
106 ModelType.vfModule.toString(), Action.createInstance.toString());
110 public void test_find_vf_module_recipe_not_found() throws MalformedURLException, NoRecipeException {
111 VnfComponentsRecipe expected = new VnfComponentsRecipe();
112 expected.setAction("createInstance");
113 exceptionRule.expect(NoRecipeException.class);
114 exceptionRule.expectMessage(
115 "Unable to locate custom or default recipe for ModelType: vfModule , Action: createInstance, CustomizationId: testModelId");
116 restHandler.findVfModuleRecipe("testModelId", ModelType.vfModule.toString(), Action.createInstance.toString());
120 public void test_checkDuplicateRequest() throws MalformedURLException, NoRecipeException {
121 ArgumentCaptor<HashMap> instanceIdCaptor = ArgumentCaptor.forClass(HashMap.class);
122 restHandler.checkDuplicateRequest("serviceInstanceId", "vnfId", "vfModuleId", "instanceName", "requestId");
123 Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).checkInstanceNameDuplicate(
124 instanceIdCaptor.capture(), eq("instanceName"), eq(ModelType.vfModule.toString()));
125 Map actualMap = instanceIdCaptor.getValue();
126 assertEquals("ServiceInstanceID should exist in map", "serviceInstanceId", actualMap.get("serviceInstanceId"));
127 assertEquals("VnfId should exit in map", "vnfId", actualMap.get("vnfInstanceId"));
128 assertEquals("VFModuleId should exit in map", "vfModuleId", actualMap.get("vfModuleInstanceId"));
132 public void test_saveInstanceName() throws MalformedURLException, NoRecipeException {
133 ServiceInstancesRequest request = createTestRequest();
134 InfraActiveRequests dbRequest = createDatabaseRecord();
135 restHandler.saveInstanceName(request, dbRequest);
136 Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).updateInfraActiveRequests(dbRequest);
137 assertEquals("InstanceName Should Be Equal", "instanceName", dbRequest.getVfModuleName());
141 public void test_buildRequestParams() throws Exception {
142 RequestClientParameter expected = new RequestClientParameter.Builder().setRequestId("requestId")
143 .setServiceInstanceId("serviceInstanceId").setVnfId("vnfId").setVfModuleId("vfModuleId")
144 .setALaCarte(true).setRequestDetails(mapper.writeValueAsString(createTestRequest()))
145 .setRequestAction(Action.deleteInstance.toString())
146 .setRequestUri("http://localhost:8080/serviceInstances").setApiVersion("v8").build();
147 RequestClientParameter actual = restHandler.buildRequestParams(createTestRequest(),
148 "http://localhost:8080/serviceInstances", "requestId", "serviceInstanceId", "vnfId", "vfModuleId");
149 assertThat(actual, sameBeanAs(expected));
153 public void test_createInfraActiveRequestForDelete() throws Exception {
154 InfraActiveRequests expected = new InfraActiveRequests();
155 expected.setRequestAction(Action.deleteInstance.toString());
156 expected.setServiceInstanceId("serviceInstanceId");
157 expected.setVnfId("vnfId");
158 expected.setVfModuleId("vfModuleId");
159 expected.setRequestId("requestId");
160 expected.setRequestorId("userId");
161 expected.setSource("VID");
162 expected.setRequestStatus(Status.IN_PROGRESS.toString());
163 expected.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
164 expected.setRequestUrl("http://localhost:9090");
165 expected.setRequestScope(ModelType.vfModule.toString());
166 InfraActiveRequests actual = restHandler.createInfraActiveRequestForDelete("requestId", "vfModuleId",
167 "serviceInstanceId", "vnfId", "userId", "VID", "http://localhost:9090");
168 assertThat(actual, sameBeanAs(expected).ignoring("startTime"));
169 Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).save(actual);
172 private ServiceInstancesRequest createTestRequest() {
173 ServiceInstancesRequest request = new ServiceInstancesRequest();
174 RequestDetails requestDetails = new RequestDetails();
175 RequestInfo requestInfo = new RequestInfo();
176 requestInfo.setInstanceName("instanceName");
177 requestDetails.setRequestInfo(requestInfo);
178 request.setRequestDetails(requestDetails);
182 private InfraActiveRequests createDatabaseRecord() {
183 InfraActiveRequests request = new InfraActiveRequests();
184 request.setRequestId("requestId");