2cf6be98393757f118c8c7f829e733fb56fb3746
[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.constants.Status;
47 import org.onap.so.db.catalog.beans.Recipe;
48 import org.onap.so.db.catalog.beans.ServiceRecipe;
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;
57
58 @RunWith(MockitoJUnitRunner.class)
59 public class ServiceInstanceRestHandlerTest {
60
61     @Rule
62     public ExpectedException exceptionRule = ExpectedException.none();
63
64     @InjectMocks
65     ServiceInstanceRestHandler restHandler;
66
67     @Mock
68     ContainerRequestContext mockRequestContext;
69
70     @Mock
71     private CatalogDbClient catalogDbClient;
72
73     @Mock
74     private RequestsDbClient infraActiveRequestsClient;
75
76     private ObjectMapper mapper = new ObjectMapper();
77
78     @Test
79     public void test_find_service_recipe() throws MalformedURLException, NoRecipeException {
80         ServiceRecipe expected = new ServiceRecipe();
81         expected.setAction("createInstance");
82         doReturn(expected).when(catalogDbClient)
83                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
84         Recipe actual = restHandler.findServiceRecipe("testModelId", Action.createInstance.toString());
85         assertThat(actual, sameBeanAs(expected));
86         Mockito.verify(catalogDbClient, Mockito.times(1))
87                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
88     }
89
90     @Test
91     public void test_find_service_recipe_default_recipe() throws MalformedURLException, NoRecipeException {
92         ServiceRecipe expected = new ServiceRecipe();
93         expected.setAction("createInstance");
94         doReturn(null).when(catalogDbClient)
95                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
96         doReturn(expected).when(catalogDbClient).findServiceRecipeByActionAndServiceModelUUID(
97                 Action.createInstance.toString(), "d88da85c-d9e8-4f73-b837-3a72a431622b");
98         Recipe actual = restHandler.findServiceRecipe("testModelId", Action.createInstance.toString());
99         assertThat(actual, sameBeanAs(expected));
100         Mockito.verify(catalogDbClient, Mockito.times(1))
101                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
102         Mockito.verify(catalogDbClient, Mockito.times(1)).findServiceRecipeByActionAndServiceModelUUID(
103                 Action.createInstance.toString(), "d88da85c-d9e8-4f73-b837-3a72a431622b");
104     }
105
106     @Test
107     public void test_find_service_recipe_not_found() throws MalformedURLException, NoRecipeException {
108         ServiceRecipe expected = new ServiceRecipe();
109         expected.setAction("createInstance");
110         doReturn(null).when(catalogDbClient)
111                 .findServiceRecipeByActionAndServiceModelUUID(Action.createInstance.toString(), "testModelId");
112         doReturn(null).when(catalogDbClient).findServiceRecipeByActionAndServiceModelUUID(
113                 Action.createInstance.toString(), "d88da85c-d9e8-4f73-b837-3a72a431622b");
114         exceptionRule.expect(NoRecipeException.class);
115         exceptionRule.expectMessage(
116                 "Unable to locate custom or default recipe for, Action: createInstance, Model UUID: testModelId");
117         restHandler.findServiceRecipe("testModelId", Action.createInstance.toString());
118     }
119
120     @Test
121     public void test_checkDuplicateRequest()
122             throws MalformedURLException, NoRecipeException, RequestConflictedException {
123         ArgumentCaptor<HashMap> instanceIdCaptor = ArgumentCaptor.forClass(HashMap.class);
124         restHandler.checkDuplicateRequest("serviceInstanceId", "instanceName", "requestId");
125         Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).checkInstanceNameDuplicate(
126                 instanceIdCaptor.capture(), eq("instanceName"), eq(ModelType.service.toString()));
127         Map actualMap = instanceIdCaptor.getValue();
128         assertEquals("serviceInstanceId", actualMap.get("serviceInstanceId"));
129     }
130
131     @Test
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.getServiceInstanceName());
138     }
139
140     @Test
141     public void test_buildRequestParams() throws Exception {
142         RequestClientParameter expected =
143                 new RequestClientParameter.Builder().setRequestId("requestId").setServiceInstanceId("serviceInstanceId")
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");
149         assertThat(actual, sameBeanAs(expected));
150     }
151
152     @Test
153     public void test_createInfraActiveRequestForDelete() throws Exception {
154         InfraActiveRequests expected = new InfraActiveRequests();
155         expected.setRequestAction(Action.deleteInstance.toString());
156         expected.setServiceInstanceId("serviceInstanceId");
157         expected.setRequestId("requestId");
158         expected.setRequestorId("userId");
159         expected.setSource("VID");
160         expected.setRequestStatus(Status.IN_PROGRESS.toString());
161         expected.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
162         expected.setRequestUrl("http://localhost:9090");
163         expected.setRequestScope(ModelType.service.toString());
164         InfraActiveRequests actual = restHandler.createInfraActiveRequestForDelete("requestId", "serviceInstanceId",
165                 "userId", "VID", "http://localhost:9090");
166         assertThat(actual, sameBeanAs(expected).ignoring("startTime"));
167         Mockito.verify(infraActiveRequestsClient, Mockito.times(1)).save(actual);
168     }
169
170     private ServiceInstancesRequest createTestRequest() {
171
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 }