2071e18430193a3a3c7ccffb9d6963ab4457493d
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.apihandlerinfra.infra.rest.handler;
22
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;
29 import java.util.Map;
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;
55
56 @RunWith(MockitoJUnitRunner.class)
57 public class VfModuleRestHandlerTest {
58
59     @Rule
60     public ExpectedException exceptionRule = ExpectedException.none();
61
62     @InjectMocks
63     VFModuleRestHandler restHandler;
64
65     @Mock
66     ContainerRequestContext mockRequestContext;
67
68     @Mock
69     private CatalogDbClient catalogDbClient;
70
71     @Mock
72     private RequestsDbClient infraActiveRequestsClient;
73
74     private ObjectMapper mapper = new ObjectMapper();
75
76     @Test
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());
89     }
90
91     @Test
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());
106     }
107
108     @Test
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());
116     }
117
118     @Test
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"));
128     }
129
130     @Test
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());
137     }
138
139     @Test
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));
149     }
150
151     @Test
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);
169     }
170
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);
178         return request;
179     }
180
181     private InfraActiveRequests createDatabaseRecord() {
182         InfraActiveRequests request = new InfraActiveRequests();
183         request.setRequestId("requestId");
184         return request;
185     }
186
187 }