4a655e5f3f28f5c17a70e1e36d4a9a763e11801f
[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.apihandlerinfra.infra.rest.exception.RequestConflictedException;
45 import org.onap.so.constants.Status;
46 import org.onap.so.db.catalog.beans.Recipe;
47 import org.onap.so.db.catalog.beans.ServiceRecipe;
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;
56
57 @RunWith(MockitoJUnitRunner.class)
58 public class ServiceInstanceRestHandlerTest {
59
60     @Rule
61     public ExpectedException exceptionRule = ExpectedException.none();
62
63     @InjectMocks
64     ServiceInstanceRestHandler restHandler;
65
66     @Mock
67     ContainerRequestContext mockRequestContext;
68
69     @Mock
70     private CatalogDbClient catalogDbClient;
71
72     @Mock
73     private RequestsDbClient infraActiveRequestsClient;
74
75     private ObjectMapper mapper = new ObjectMapper();
76
77     @Test
78     public void test_find_service_recipe() throws NoRecipeException {
79         ServiceRecipe expected = new ServiceRecipe();
80         expected.setAction("createInstance");
81         doReturn(expected).when(catalogDbClient)
82                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
83         Recipe actual = restHandler.findServiceRecipe("testModelId", Action.createInstance.toString());
84         assertThat(actual, sameBeanAs(expected));
85         Mockito.verify(catalogDbClient, Mockito.times(1))
86                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
87     }
88
89     @Test
90     public void test_find_service_recipe_default_recipe() throws NoRecipeException {
91         ServiceRecipe expected = new ServiceRecipe();
92         expected.setAction("createInstance");
93         doReturn(null).when(catalogDbClient)
94                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
95         doReturn(expected).when(catalogDbClient).findServiceRecipeByActionAndServiceModelUUID(
96                 Action.createInstance.toString(), "d88da85c-d9e8-4f73-b837-3a72a431622b");
97         Recipe actual = restHandler.findServiceRecipe("testModelId", Action.createInstance.toString());
98         assertThat(actual, sameBeanAs(expected));
99         Mockito.verify(catalogDbClient, Mockito.times(1))
100                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
101         Mockito.verify(catalogDbClient, Mockito.times(1)).findServiceRecipeByActionAndServiceModelUUID(
102                 Action.createInstance.toString(), "d88da85c-d9e8-4f73-b837-3a72a431622b");
103     }
104
105     @Test
106     public void test_find_service_recipe_not_found() throws NoRecipeException {
107         ServiceRecipe expected = new ServiceRecipe();
108         expected.setAction("createInstance");
109         doReturn(null).when(catalogDbClient)
110                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
111         doReturn(null).when(catalogDbClient).findServiceRecipeByActionAndServiceModelUUID(
112                 Action.createInstance.toString(), "d88da85c-d9e8-4f73-b837-3a72a431622b");
113         exceptionRule.expect(NoRecipeException.class);
114         exceptionRule.expectMessage(
115                 "Unable to locate custom or default recipe for, Action: createInstance, Model UUID: testModelId");
116         restHandler.findServiceRecipe("testModelId", Action.createInstance.toString());
117     }
118
119     @Test
120     public void test_checkDuplicateRequest() throws RequestConflictedException {
121         ArgumentCaptor<HashMap> instanceIdCaptor = ArgumentCaptor.forClass(HashMap.class);
122         restHandler.checkDuplicateRequest("serviceInstanceId", "instanceName", "requestId");
123         Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).checkInstanceNameDuplicate(
124                 instanceIdCaptor.capture(), eq("instanceName"), eq(ModelType.service.toString()));
125         Map actualMap = instanceIdCaptor.getValue();
126         assertEquals("serviceInstanceId", actualMap.get("serviceInstanceId"));
127     }
128
129     @Test
130     public void test_saveInstanceName() {
131         ServiceInstancesRequest request = createTestRequest();
132         InfraActiveRequests dbRequest = createDatabaseRecord();
133         restHandler.saveInstanceName(request, dbRequest);
134         Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).updateInfraActiveRequests(dbRequest);
135         assertEquals("InstanceName Should Be Equal", "instanceName", dbRequest.getServiceInstanceName());
136     }
137
138     @Test
139     public void test_buildRequestParams() throws Exception {
140         RequestClientParameter expected =
141                 new RequestClientParameter.Builder().setRequestId("requestId").setServiceInstanceId("serviceInstanceId")
142                         .setALaCarte(true).setRequestDetails(mapper.writeValueAsString(createTestRequest()))
143                         .setRequestAction(Action.deleteInstance.toString())
144                         .setRequestUri("http://localhost:8080/serviceInstances").setApiVersion("v8").build();
145         RequestClientParameter actual = restHandler.buildRequestParams(createTestRequest(),
146                 "http://localhost:8080/serviceInstances", "requestId", "serviceInstanceId");
147         assertThat(actual, sameBeanAs(expected));
148     }
149
150     @Test
151     public void test_createInfraActiveRequestForDelete() {
152         InfraActiveRequests expected = new InfraActiveRequests();
153         expected.setRequestAction(Action.deleteInstance.toString());
154         expected.setServiceInstanceId("serviceInstanceId");
155         expected.setRequestId("requestId");
156         expected.setRequestorId("userId");
157         expected.setSource("VID");
158         expected.setRequestStatus(Status.IN_PROGRESS.toString());
159         expected.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
160         expected.setRequestUrl("http://localhost:9090");
161         expected.setRequestScope(ModelType.service.toString());
162         InfraActiveRequests actual = restHandler.createInfraActiveRequestForDelete("requestId", "serviceInstanceId",
163                 "userId", "VID", "http://localhost:9090");
164         assertThat(actual, sameBeanAs(expected).ignoring("startTime"));
165         Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).save(actual);
166     }
167
168     private ServiceInstancesRequest createTestRequest() {
169
170         ServiceInstancesRequest request = new ServiceInstancesRequest();
171         RequestDetails requestDetails = new RequestDetails();
172         RequestInfo requestInfo = new RequestInfo();
173         requestInfo.setInstanceName("instanceName");
174         requestDetails.setRequestInfo(requestInfo);
175         request.setRequestDetails(requestDetails);
176         return request;
177     }
178
179     private InfraActiveRequests createDatabaseRecord() {
180         InfraActiveRequests request = new InfraActiveRequests();
181         request.setRequestId("requestId");
182         return request;
183     }
184
185 }