c1e6347943ea8460266f0849de99ab4aaa5c7ffa
[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.net.MalformedURLException;
29 import java.util.HashMap;
30 import java.util.Map;
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.exception.RequestConflictedException;
46 import org.onap.so.apihandlerinfra.infra.rest.handler.ServiceInstanceRestHandler;
47 import org.onap.so.constants.Status;
48 import org.onap.so.db.catalog.beans.Recipe;
49 import org.onap.so.db.catalog.beans.ServiceRecipe;
50 import org.onap.so.db.catalog.client.CatalogDbClient;
51 import org.onap.so.db.request.beans.InfraActiveRequests;
52 import org.onap.so.db.request.client.RequestsDbClient;
53 import org.onap.so.serviceinstancebeans.ModelType;
54 import org.onap.so.serviceinstancebeans.RequestDetails;
55 import org.onap.so.serviceinstancebeans.RequestInfo;
56 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
57 import com.fasterxml.jackson.databind.ObjectMapper;
58
59 @RunWith(MockitoJUnitRunner.class)
60 public class ServiceInstanceRestHandlerTest {
61
62     @Rule
63     public ExpectedException exceptionRule = ExpectedException.none();
64
65     @InjectMocks
66     ServiceInstanceRestHandler restHandler;
67
68     @Mock
69     ContainerRequestContext mockRequestContext;
70
71     @Mock
72     private CatalogDbClient catalogDbClient;
73
74     @Mock
75     private RequestsDbClient infraActiveRequestsClient;
76
77     private ObjectMapper mapper = new ObjectMapper();
78
79     @Test
80     public void test_find_service_recipe() throws MalformedURLException, NoRecipeException {
81         ServiceRecipe expected = new ServiceRecipe();
82         expected.setAction("createInstance");
83         doReturn(expected).when(catalogDbClient)
84                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
85         Recipe actual = restHandler.findServiceRecipe("testModelId", Action.createInstance.toString());
86         assertThat(actual, sameBeanAs(expected));
87         Mockito.verify(catalogDbClient, Mockito.times(1))
88                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
89     }
90
91     @Test
92     public void test_find_service_recipe_default_recipe() throws MalformedURLException, NoRecipeException {
93         ServiceRecipe expected = new ServiceRecipe();
94         expected.setAction("createInstance");
95         doReturn(null).when(catalogDbClient)
96                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
97         doReturn(expected).when(catalogDbClient).findServiceRecipeByActionAndServiceModelUUID(
98                 Action.createInstance.toString(), "d88da85c-d9e8-4f73-b837-3a72a431622b");
99         Recipe actual = restHandler.findServiceRecipe("testModelId", Action.createInstance.toString());
100         assertThat(actual, sameBeanAs(expected));
101         Mockito.verify(catalogDbClient, Mockito.times(1))
102                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
103         Mockito.verify(catalogDbClient, Mockito.times(1)).findServiceRecipeByActionAndServiceModelUUID(
104                 Action.createInstance.toString(), "d88da85c-d9e8-4f73-b837-3a72a431622b");
105     }
106
107     @Test
108     public void test_find_service_recipe_not_found() throws MalformedURLException, NoRecipeException {
109         ServiceRecipe expected = new ServiceRecipe();
110         expected.setAction("createInstance");
111         doReturn(null).when(catalogDbClient)
112                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
113         doReturn(null).when(catalogDbClient).findServiceRecipeByActionAndServiceModelUUID(
114                 Action.createInstance.toString(), "d88da85c-d9e8-4f73-b837-3a72a431622b");
115         exceptionRule.expect(NoRecipeException.class);
116         exceptionRule.expectMessage(
117                 "Unable to locate custom or default recipe for, Action: createInstance, Model UUID: testModelId");
118         restHandler.findServiceRecipe("testModelId", Action.createInstance.toString());
119     }
120
121     @Test
122     public void test_checkDuplicateRequest()
123             throws MalformedURLException, NoRecipeException, RequestConflictedException {
124         ArgumentCaptor<HashMap> instanceIdCaptor = ArgumentCaptor.forClass(HashMap.class);
125         restHandler.checkDuplicateRequest("serviceInstanceId", "instanceName", "requestId");
126         Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).checkInstanceNameDuplicate(
127                 instanceIdCaptor.capture(), eq("instanceName"), eq(ModelType.service.toString()));
128         Map actualMap = instanceIdCaptor.getValue();
129         assertEquals("serviceInstanceId", actualMap.get("serviceInstanceId"));
130     }
131
132     @Test
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.getServiceInstanceName());
139     }
140
141     @Test
142     public void test_buildRequestParams() throws Exception {
143         RequestClientParameter expected =
144                 new RequestClientParameter.Builder().setRequestId("requestId").setServiceInstanceId("serviceInstanceId")
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");
150         assertThat(actual, sameBeanAs(expected));
151     }
152
153     @Test
154     public void test_createInfraActiveRequestForDelete() throws Exception {
155         InfraActiveRequests expected = new InfraActiveRequests();
156         expected.setRequestAction(Action.deleteInstance.toString());
157         expected.setAction(Action.deleteInstance.toString());
158         expected.setServiceInstanceId("serviceInstanceId");
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.service.toString());
166         InfraActiveRequests actual = restHandler.createInfraActiveRequestForDelete("requestId", "serviceInstanceId",
167                 "userId", "VID", "http://localhost:9090");
168         assertThat(actual, sameBeanAs(expected).ignoring("startTime"));
169         Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).save(actual);
170     }
171
172     private ServiceInstancesRequest createTestRequest() {
173
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);
180         return request;
181     }
182
183     private InfraActiveRequests createDatabaseRecord() {
184         InfraActiveRequests request = new InfraActiveRequests();
185         request.setRequestId("requestId");
186         return request;
187     }
188
189 }