2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Fujitsu Limited. 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.openecomp.sdc.be.servlets;
23 import fj.data.Either;
24 import org.glassfish.jersey.server.ResourceConfig;
25 import org.glassfish.grizzly.http.util.HttpStatus;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.ArgumentCaptor;
29 import org.openecomp.sdc.be.components.impl.DataTypeBusinessLogic;
30 import org.openecomp.sdc.be.components.impl.InputsBusinessLogic;
31 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
32 import org.openecomp.sdc.be.dao.api.ActionStatus;
33 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
34 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
35 import org.openecomp.sdc.be.impl.ComponentsUtils;
36 import org.openecomp.sdc.be.impl.ServletUtils;
37 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
38 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
39 import org.openecomp.sdc.be.model.ComponentInstListInput;
40 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
41 import org.openecomp.sdc.be.model.DataTypeDefinition;
42 import org.openecomp.sdc.be.model.InputDefinition;
43 import org.openecomp.sdc.be.model.User;
44 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
45 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
46 import org.openecomp.sdc.common.api.Constants;
47 import org.openecomp.sdc.exception.ResponseFormat;
48 import org.springframework.web.context.WebApplicationContext;
50 import javax.servlet.ServletContext;
51 import javax.servlet.http.HttpSession;
52 import javax.ws.rs.client.Entity;
53 import javax.ws.rs.client.Invocation;
54 import javax.ws.rs.core.MediaType;
55 import javax.ws.rs.core.Response;
56 import java.util.ArrayList;
57 import java.util.Collections;
58 import java.util.HashMap;
59 import java.util.List;
62 import static org.assertj.core.api.Assertions.assertThat;
63 import static org.mockito.ArgumentMatchers.any;
64 import static org.mockito.ArgumentMatchers.eq;
65 import static org.mockito.Mockito.*;
67 public class InputsServletTest extends JerseySpringBaseTest {
70 private static final String RESOURCE_ID = "serviceId";
71 private static final String USER_ID = "userId";
72 private static final String COMPONENT_INSTANCE_ID = "instanceId";
73 private static final String COMPONENT_ID = "componentId";
74 private static final String INSTANCE_INPUT_ID = "inputId";
75 private static final String LISTINPUT_NAME = "listInput";
76 private static final String LISTINPUT_SCHEMA_TYPE = "org.onap.datatypes.listinput";
77 private static final String LISTINPUT_PROP1_NAME = "prop1";
78 private static final String LISTINPUT_PROP1_TYPE = "string";
79 private static final String LISTINPUT_PROP2_NAME = "prop2";
80 private static final String LISTINPUT_PROP2_TYPE = "integer";
83 private InputsServletForTest testSubject;
86 private InputsBusinessLogic inputsBusinessLogic;
87 private DataTypeBusinessLogic dataTypeBusinessLogic;
88 private HttpSession httpSession;
89 private ServletContext servletContext;
90 private WebApplicationContext webApplicationContext;
91 private ComponentsUtils componentsUtils;
92 private ServletUtils servletUtils;
95 * This class extends the original InputsServlet
96 * and provides methods to inject mocks
98 class InputsServletForTest extends InputsServlet {
99 public void setComponentsUtils(ComponentsUtils componentsUtils) {
100 this.componentsUtils = componentsUtils;
103 public void setServletUtils(ServletUtils servletUtils) {
104 this.servletUtils = servletUtils;
110 public void setUp() throws Exception {
112 inputsBusinessLogic = mock(InputsBusinessLogic.class);
113 dataTypeBusinessLogic = mock(DataTypeBusinessLogic.class);
114 servletContext = mock(ServletContext.class);
115 httpSession = mock(HttpSession.class);
116 webApplicationContext = mock(WebApplicationContext.class);
117 componentsUtils = mock(ComponentsUtils.class);
118 servletUtils = mock(ServletUtils.class);
119 when(request.getSession()).thenReturn(httpSession);
120 when(httpSession.getServletContext()).thenReturn(servletContext);
121 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(new WebAppContextWrapper());
122 when(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)).thenReturn(webApplicationContext);
123 when(webApplicationContext.getBean(InputsBusinessLogic.class)).thenReturn(inputsBusinessLogic);
124 when(webApplicationContext.getBean(DataTypeBusinessLogic.class)).thenReturn(dataTypeBusinessLogic);
125 testSubject.setComponentsUtils(componentsUtils);
126 testSubject.setServletUtils(servletUtils);
127 when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils);
132 protected ResourceConfig configure() {
133 testSubject = new InputsServletForTest();
134 return super.configure().register(testSubject);
138 private InputDefinition setUpListInput() {
139 InputDefinition listInput = new InputDefinition();
140 listInput.setName(LISTINPUT_NAME);
141 listInput.setType("list");
142 SchemaDefinition listInputSchema = new SchemaDefinition();
143 listInputSchema.setProperty(new PropertyDataDefinitionBuilder()
144 .setType(LISTINPUT_SCHEMA_TYPE)
145 .setIsRequired(false)
148 listInput.setSchema(listInputSchema);
152 private ComponentInstListInput setUpCreateListInputParams() {
153 ComponentInstListInput componentInstListInput = new ComponentInstListInput();
155 // Create a "list input"
156 InputDefinition listInput = setUpListInput();
157 componentInstListInput.setListInput(listInput);
159 // Create ComponentInstancePropInputs
160 // for inputs in the ComponentInstance
161 Map<String, List<ComponentInstancePropInput>> propInputsListMap = new HashMap<>();
162 // Add 2 PropInputs. property owner is COMPONENT_INSTANCE_ID
163 List<ComponentInstancePropInput> propInputsList = new ArrayList<>();
164 ComponentInstancePropInput propInput = new ComponentInstancePropInput();
165 propInput.setName(LISTINPUT_PROP1_NAME);
166 propInput.setType(LISTINPUT_PROP1_TYPE);
167 propInput.setUniqueId(COMPONENT_INSTANCE_ID + "." + LISTINPUT_PROP1_NAME);
168 propInputsList.add(propInput);
169 propInput = new ComponentInstancePropInput();
170 propInput.setName(LISTINPUT_PROP2_NAME);
171 propInput.setType(LISTINPUT_PROP2_TYPE);
172 propInput.setUniqueId(COMPONENT_INSTANCE_ID + "." + LISTINPUT_PROP2_NAME);
173 propInputsList.add(propInput);
174 propInputsListMap.put(COMPONENT_INSTANCE_ID, propInputsList);
175 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
176 componentInstInputsMap.setComponentInstanceInputsMap(propInputsListMap);
177 componentInstListInput.setComponentInstInputsMap(componentInstInputsMap);
179 return componentInstListInput;
183 public void test_createListInput_success() throws Exception {
184 ComponentInstListInput requestBodyObj = setUpCreateListInputParams();
185 Entity<ComponentInstListInput> entity = Entity.entity(requestBodyObj, MediaType.APPLICATION_JSON);
187 // for parseToComponentInstListInput
188 when(componentsUtils.convertJsonToObjectUsingObjectMapper(any(), any(), eq(ComponentInstListInput.class),
189 eq(AuditingActionEnum.CREATE_RESOURCE), eq(ComponentTypeEnum.SERVICE)))
190 .thenReturn(Either.left(requestBodyObj));
192 when(inputsBusinessLogic.createListInput(eq(USER_ID), eq(RESOURCE_ID), eq(ComponentTypeEnum.SERVICE),
193 any(), eq(true), eq(false)))
194 .thenReturn(Either.left(Collections.emptyList()));
195 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(new ResponseFormat(HttpStatus.OK_200.getStatusCode()));
197 Response response = buildCreateListInputCall().post(entity);
198 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200.getStatusCode());
199 verify(inputsBusinessLogic, times(1)).createListInput(USER_ID, RESOURCE_ID,
200 ComponentTypeEnum.SERVICE, requestBodyObj, true, false);
204 public void test_createListInput_fail_parse() throws Exception {
205 ComponentInstListInput requestBodyObj = setUpCreateListInputParams();
206 Entity<ComponentInstListInput> entity = Entity.entity(requestBodyObj, MediaType.APPLICATION_JSON);
208 // for parseToComponentInstListInput
209 ArgumentCaptor<User> userCaptor = ArgumentCaptor.forClass(User.class);
210 when(componentsUtils.convertJsonToObjectUsingObjectMapper(any(), userCaptor.capture(), eq(ComponentInstListInput.class),
211 eq(AuditingActionEnum.CREATE_RESOURCE), eq(ComponentTypeEnum.SERVICE)))
212 .thenReturn(Either.right(new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode())));
214 when(inputsBusinessLogic.createListInput(eq(USER_ID), eq(RESOURCE_ID), eq(ComponentTypeEnum.SERVICE),
215 any(), eq(true), eq(false)))
216 .thenReturn(Either.left(Collections.emptyList()));
218 Response response = buildCreateListInputCall().post(entity);
219 assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());
220 verify(componentsUtils, times(1))
221 .convertJsonToObjectUsingObjectMapper(any(), any(), eq(ComponentInstListInput.class),
222 eq(AuditingActionEnum.CREATE_RESOURCE), eq(ComponentTypeEnum.SERVICE));
223 assertThat(userCaptor.getValue().getUserId()).isEqualTo(USER_ID);
224 verify(inputsBusinessLogic, never()).createListInput(USER_ID, RESOURCE_ID,
225 ComponentTypeEnum.SERVICE, requestBodyObj, true, false);
230 public void test_createListInput_fail_createInput() throws Exception {
231 ComponentInstListInput requestBodyObj = setUpCreateListInputParams();
232 Entity<ComponentInstListInput> entity = Entity.entity(requestBodyObj, MediaType.APPLICATION_JSON);
234 // for parseToComponentInstListInput
235 ArgumentCaptor<User> userCaptor = ArgumentCaptor.forClass(User.class);
236 when(componentsUtils.convertJsonToObjectUsingObjectMapper(any(), userCaptor.capture(), eq(ComponentInstListInput.class),
237 eq(AuditingActionEnum.CREATE_RESOURCE), eq(ComponentTypeEnum.SERVICE)))
238 .thenReturn(Either.left(requestBodyObj));
240 when(inputsBusinessLogic.createListInput(eq(USER_ID), eq(RESOURCE_ID), eq(ComponentTypeEnum.SERVICE),
241 any(), eq(true), eq(false)))
242 .thenReturn(Either.right(new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode())));
244 Response response = buildCreateListInputCall().post(entity);
245 assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());
246 verify(inputsBusinessLogic, times(1))
247 .createListInput(eq(USER_ID), eq(RESOURCE_ID), eq(ComponentTypeEnum.SERVICE),
248 any(), eq(true), eq(false));
253 public void test_createListInput_fail_exception() throws Exception {
254 ComponentInstListInput requestBodyObj = setUpCreateListInputParams();
255 Entity<ComponentInstListInput> entity = Entity.entity(requestBodyObj, MediaType.APPLICATION_JSON);
257 when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode()));
259 Response response = buildCreateListInputCall().post(entity);
260 assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());
264 public void test_getDataType_success() throws Exception {
265 when(dataTypeBusinessLogic.getPrivateDataType(eq(RESOURCE_ID),eq(LISTINPUT_SCHEMA_TYPE))).thenReturn(Either.left(new DataTypeDefinition()));
267 ResponseFormat responseFormat = new ResponseFormat();
268 responseFormat.setStatus(HttpStatus.OK_200.getStatusCode());
269 when(componentsUtils.getResponseFormat(eq(ActionStatus.OK))).thenReturn(responseFormat);
271 Response response = buildGetDataTypeCall().get();
272 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200.getStatusCode());
276 public void test_getDataType_fail() throws Exception {
277 when(dataTypeBusinessLogic.getPrivateDataType(eq(RESOURCE_ID),eq(LISTINPUT_SCHEMA_TYPE))).thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
279 ResponseFormat responseFormat = new ResponseFormat();
280 responseFormat.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500.getStatusCode());
281 when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.BAD_REQUEST))).thenReturn(ActionStatus.GENERAL_ERROR);
282 when(componentsUtils.getResponseFormat(eq(ActionStatus.GENERAL_ERROR))).thenReturn(responseFormat);
284 Response response = buildGetDataTypeCall().get();
285 assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500.getStatusCode());
289 public void test_getDataType_fail_exception() throws Exception {
290 when(dataTypeBusinessLogic.getPrivateDataType(eq(RESOURCE_ID),eq(LISTINPUT_SCHEMA_TYPE))).thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
291 when(componentsUtils.getResponseFormat(eq(ActionStatus.GENERAL_ERROR))).thenReturn(new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode()));
293 Response response = buildGetDataTypeCall().get();
294 assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());
298 public void test_getDataTypes_success() throws Exception {
299 when(dataTypeBusinessLogic.getPrivateDataTypes(eq(RESOURCE_ID))).thenReturn(Either.left(Collections.emptyList()));
301 ResponseFormat responseFormat = new ResponseFormat();
302 responseFormat.setStatus(HttpStatus.OK_200.getStatusCode());
303 when(componentsUtils.getResponseFormat(eq(ActionStatus.OK))).thenReturn(responseFormat);
305 Response response = buildGetDataTypesCall().get();
306 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200.getStatusCode());
310 public void test_getDataTypes_fail() throws Exception {
311 when(dataTypeBusinessLogic.getPrivateDataTypes(eq(RESOURCE_ID))).thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
313 ResponseFormat responseFormat = new ResponseFormat();
314 responseFormat.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500.getStatusCode());
315 when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.BAD_REQUEST))).thenReturn(ActionStatus.GENERAL_ERROR);
316 when(componentsUtils.getResponseFormat(eq(ActionStatus.GENERAL_ERROR))).thenReturn(responseFormat);
318 Response response = buildGetDataTypesCall().get();
319 assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500.getStatusCode());
323 public void test_getDataTypes_fail_exception() throws Exception {
324 when(dataTypeBusinessLogic.getPrivateDataType(eq(RESOURCE_ID),eq(LISTINPUT_SCHEMA_TYPE))).thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
325 when(componentsUtils.getResponseFormat(eq(ActionStatus.GENERAL_ERROR))).thenReturn(new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode()));
327 Response response = buildGetDataTypesCall().get();
328 assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());
333 public void test_deleteDataType_success() throws Exception {
334 when(dataTypeBusinessLogic.deletePrivateDataType(RESOURCE_ID, LISTINPUT_SCHEMA_TYPE)).thenReturn(Either.left(new DataTypeDefinition()));
336 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(new ResponseFormat(HttpStatus.OK_200.getStatusCode()));
338 Response response = buildGetDataTypeCall().delete();
339 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200.getStatusCode());
343 public void test_deleteDataType_failure_exception() throws Exception {
344 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(new ResponseFormat(HttpStatus.OK_200.getStatusCode()));
345 when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(new ResponseFormat(HttpStatus.INTERNAL_SERVER_ERROR_500.getStatusCode()));
346 Response response = buildGetDataTypeCall().delete();
347 assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500.getStatusCode());
348 verify(componentsUtils, never()).getResponseFormat(ActionStatus.OK);
352 public void test_deleteDataType_failure_notFound() throws Exception {
353 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(new ResponseFormat(HttpStatus.OK_200.getStatusCode()));
354 when(dataTypeBusinessLogic.deletePrivateDataType(RESOURCE_ID, LISTINPUT_SCHEMA_TYPE)).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
355 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
356 when(componentsUtils.getResponseFormat(ActionStatus.ARTIFACT_NOT_FOUND)).thenReturn(new ResponseFormat(HttpStatus.NOT_FOUND_404.getStatusCode()));
357 Response response = buildGetDataTypeCall().delete();
358 assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND_404.getStatusCode());
359 verify(componentsUtils, never()).getResponseFormat(ActionStatus.OK);
363 public void test_deleteInput_success() throws Exception {
364 when(inputsBusinessLogic.deleteInput(RESOURCE_ID, USER_ID, LISTINPUT_NAME))
365 .thenReturn(Either.left(new InputDefinition()));
366 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(new ResponseFormat(HttpStatus.OK_200.getStatusCode()));
368 // invoke delete call
369 Response response = target("/v1/catalog/services/{id}/delete/{inputId}/input")
370 .resolveTemplate("id", RESOURCE_ID)
371 .resolveTemplate("inputId", LISTINPUT_NAME)
372 .request(MediaType.APPLICATION_JSON)
373 .header(Constants.USER_ID_HEADER, USER_ID)
375 assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200.getStatusCode());
376 verify(inputsBusinessLogic, times(1)).deleteInput(RESOURCE_ID, USER_ID, LISTINPUT_NAME);
381 public void test_deleteInput_failure_deleteInput() throws Exception {
382 when(inputsBusinessLogic.deleteInput(RESOURCE_ID, USER_ID, LISTINPUT_NAME))
383 .thenReturn(Either.right(new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode())));
384 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(new ResponseFormat(HttpStatus.OK_200.getStatusCode()));
386 // invoke delete call
387 Response response = target("/v1/catalog/services/{id}/delete/{inputId}/input")
388 .resolveTemplate("id", RESOURCE_ID)
389 .resolveTemplate("inputId", LISTINPUT_NAME)
390 .request(MediaType.APPLICATION_JSON)
391 .header(Constants.USER_ID_HEADER, USER_ID)
393 assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());
394 verify(componentsUtils, never()).getResponseFormat(ActionStatus.OK);
399 public void test_deleteInput_failure_exception() throws Exception {
400 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(new ResponseFormat(HttpStatus.OK_200.getStatusCode()));
401 when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode()));
403 // invoke delete call
404 Response response = target("/v1/catalog/services/{id}/delete/{inputId}/input")
405 .resolveTemplate("id", RESOURCE_ID)
406 .resolveTemplate("inputId", LISTINPUT_NAME)
407 .request(MediaType.APPLICATION_JSON)
408 .header(Constants.USER_ID_HEADER, USER_ID)
410 assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());
411 verify(componentsUtils, never()).getResponseFormat(ActionStatus.OK);
415 private Invocation.Builder buildCreateListInputCall() {
416 return target("/v1/catalog/services/{id}/create/listInput")
417 .resolveTemplate("id", RESOURCE_ID)
418 .request(MediaType.APPLICATION_JSON)
419 .header(Constants.USER_ID_HEADER, USER_ID);
422 private Invocation.Builder buildGetDataTypeCall() {
423 return target("/v1/catalog/services/{id}/dataType/{dataTypeName}")
424 .resolveTemplate("id", RESOURCE_ID)
425 .resolveTemplate("dataTypeName", LISTINPUT_SCHEMA_TYPE)
426 .request(MediaType.APPLICATION_JSON)
427 .header(Constants.USER_ID_HEADER, USER_ID);
430 private Invocation.Builder buildGetDataTypesCall() {
431 return target("/v1/catalog/services/{id}/dataTypes")
432 .resolveTemplate("id", RESOURCE_ID)
433 .request(MediaType.APPLICATION_JSON)
434 .header(Constants.USER_ID_HEADER, USER_ID);