2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
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=========================================================
19 * Modifications copyright (c) 2019 Nokia
20 * ================================================================================
23 package org.openecomp.sdc.be.servlets;
25 import fj.data.Either;
26 import org.apache.commons.text.StrSubstitutor;
27 import org.apache.http.HttpStatus;
28 import org.glassfish.hk2.utilities.binding.AbstractBinder;
29 import org.glassfish.jersey.server.ResourceConfig;
30 import org.glassfish.jersey.test.JerseyTest;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
36 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.ElementBusinessLogic;
38 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
39 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
40 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
41 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
42 import org.openecomp.sdc.be.components.scheduledtasks.ComponentsCleanBusinessLogic;
43 import org.openecomp.sdc.be.config.Configuration;
44 import org.openecomp.sdc.be.config.ConfigurationManager;
45 import org.openecomp.sdc.be.config.SpringConfig;
46 import org.openecomp.sdc.be.dao.api.ActionStatus;
47 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
48 import org.openecomp.sdc.be.impl.ComponentsUtils;
49 import org.openecomp.sdc.be.impl.ServletUtils;
50 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
51 import org.openecomp.sdc.be.model.ArtifactType;
52 import org.openecomp.sdc.be.model.PropertyScope;
53 import org.openecomp.sdc.be.model.Resource;
54 import org.openecomp.sdc.be.model.Tag;
55 import org.openecomp.sdc.be.model.User;
56 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
57 import org.openecomp.sdc.be.model.category.CategoryDefinition;
58 import org.openecomp.sdc.be.model.category.GroupingDefinition;
59 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
60 import org.openecomp.sdc.be.ui.model.UiCategories;
61 import org.openecomp.sdc.be.user.Role;
62 import org.openecomp.sdc.be.user.UserBusinessLogic;
63 import org.openecomp.sdc.common.api.ConfigurationSource;
64 import org.openecomp.sdc.common.api.Constants;
65 import org.openecomp.sdc.common.impl.ExternalConfiguration;
66 import org.openecomp.sdc.common.impl.FSConfigurationSource;
67 import org.openecomp.sdc.exception.ResponseFormat;
68 import org.springframework.context.ApplicationContext;
69 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
70 import org.springframework.web.context.WebApplicationContext;
72 import javax.servlet.ServletContext;
73 import javax.servlet.http.HttpServletRequest;
74 import javax.servlet.http.HttpSession;
75 import javax.ws.rs.client.Entity;
76 import javax.ws.rs.core.Application;
77 import javax.ws.rs.core.MediaType;
78 import javax.ws.rs.core.Response;
79 import java.util.ArrayList;
80 import java.util.HashMap;
81 import java.util.List;
84 import static org.assertj.core.api.Assertions.assertThat;
85 import static org.mockito.ArgumentMatchers.any;
86 import static org.mockito.ArgumentMatchers.eq;
87 import static org.mockito.Mockito.reset;
88 import static org.mockito.Mockito.when;
90 public class ElementServletTest extends JerseyTest {
91 public static final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
92 public static final HttpSession session = Mockito.mock(HttpSession.class);
93 public static final ResourceImportManager resourceImportManager = Mockito.mock(ResourceImportManager.class);
94 public static final ResourceBusinessLogic resourceBusinessLogic = Mockito.mock(ResourceBusinessLogic.class);
95 public static final BeGenericServlet beGenericServlet = Mockito.mock(BeGenericServlet.class);
96 public static final Resource resource = Mockito.mock(Resource.class);
97 public static final UserBusinessLogic userBusinessLogic = Mockito.mock(UserBusinessLogic.class);
98 public static final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
99 public static final ArtifactsBusinessLogic artifactsBusinessLogic = Mockito.mock(ArtifactsBusinessLogic.class);
101 private static final ServletContext servletContext = Mockito.mock(ServletContext.class);
102 public static final WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
103 private static final WebApplicationContext webApplicationContext = Mockito.mock(WebApplicationContext.class);
104 private static final ServletUtils servletUtils = Mockito.mock(ServletUtils.class);
105 private static final UserBusinessLogic userAdmin = Mockito.mock(UserBusinessLogic.class);
106 private static final ComponentsUtils componentUtils = Mockito.mock(ComponentsUtils.class);
107 private static final ComponentsCleanBusinessLogic componentsCleanBusinessLogic = Mockito.mock(ComponentsCleanBusinessLogic.class);
108 private static final ElementBusinessLogic elementBusinessLogic = Mockito.mock(ElementBusinessLogic.class);
110 private static final ResponseFormat okResponseFormat = new ResponseFormat(HttpStatus.SC_OK);
111 private static final ResponseFormat conflictResponseFormat = new ResponseFormat(HttpStatus.SC_CONFLICT);
112 private static final ResponseFormat generalErrorResponseFormat = new ResponseFormat(HttpStatus.SC_INTERNAL_SERVER_ERROR);
113 private static final ResponseFormat createdResponseFormat = new ResponseFormat(HttpStatus.SC_CREATED);
114 private static final ResponseFormat noContentResponseFormat = new ResponseFormat(HttpStatus.SC_NO_CONTENT);
115 private static final ResponseFormat unauthorizedResponseFormat = Mockito.mock(ResponseFormat.class);
116 private static final ResponseFormat notFoundResponseFormat = Mockito.mock(ResponseFormat.class);
117 private static final ResponseFormat badRequestResponseFormat = Mockito.mock(ResponseFormat.class);
118 private static final String EMPTY_JSON = "{}";
119 private static final String COMPONENT_TYPE = "componentType";
120 private static final String CATEGORY_UNIQUE_ID = "categoryUniqueId";
121 private static final String CATEGORY_ID = "categoryId";
122 private static final String SUB_CATEGORY_UNIQUE_ID = "subCategoryUniqueId";
123 private static final String SUB_CATEGORY_ID = "subCategoryId";
124 private static final String GROUPING_UNIQUE_ID = "groupingUniqueId";
127 private static User designerUser = new User("designer", "designer", "designer", "designer@email.com", Role.DESIGNER.name(), System
128 .currentTimeMillis());
130 private static ConfigurationManager configurationManager;
133 public static void setup() {
135 //Needed for User Authorization
136 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
137 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
138 when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
139 when(servletUtils.getUserAdmin()).thenReturn(userAdmin);
140 when(servletUtils.getComponentsUtils()).thenReturn(componentUtils);
141 when(componentUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)).thenReturn(unauthorizedResponseFormat);
142 when(unauthorizedResponseFormat.getStatus()).thenReturn(HttpStatus.SC_UNAUTHORIZED);
144 when(componentUtils.getResponseFormat(ActionStatus.OK)) .thenReturn(okResponseFormat);
145 when(componentUtils.getResponseFormat(ActionStatus.CREATED)).thenReturn(createdResponseFormat);
146 when(componentUtils.getResponseFormat(ActionStatus.NO_CONTENT)).thenReturn(noContentResponseFormat);
147 when(componentUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(badRequestResponseFormat);
148 when(componentUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)) .thenReturn(generalErrorResponseFormat);
149 when(componentUtils.getResponseFormat(any(ComponentException.class)))
150 .thenReturn(generalErrorResponseFormat);
152 ByResponseFormatComponentException ce = Mockito.mock(ByResponseFormatComponentException.class);
153 when(ce.getResponseFormat()).thenReturn(unauthorizedResponseFormat);
155 //Needed for error configuration
156 when(notFoundResponseFormat.getStatus()).thenReturn(HttpStatus.SC_NOT_FOUND);
157 when(badRequestResponseFormat.getStatus()).thenReturn(HttpStatus.SC_BAD_REQUEST);
158 when(componentUtils.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), any())).thenReturn(notFoundResponseFormat);
159 when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_VERSION_NOT_FOUND), any())).thenReturn(notFoundResponseFormat);
160 when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND), any())).thenReturn(notFoundResponseFormat);
161 when(componentUtils.getResponseFormat(eq(ActionStatus.EXT_REF_NOT_FOUND), any())).thenReturn(notFoundResponseFormat);
162 when(componentUtils.getResponseFormat(eq(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID), any())).thenReturn(badRequestResponseFormat);
163 when(request.getSession()).thenReturn(session);
164 when(session.getServletContext()).thenReturn(servletContext);
165 when(beGenericServlet.getElementBL(any())).thenReturn(elementBusinessLogic);
166 when(webApplicationContext.getBean(ElementBusinessLogic.class)).thenReturn(elementBusinessLogic);
167 when(webApplicationContext.getBean(ComponentsUtils.class)).thenReturn(componentUtils);
168 when(beGenericServlet.getComponentsUtils()).thenReturn(componentUtils);
170 Either<User, ActionStatus> designerEither = Either.left(designerUser);
172 when(userAdmin.getUser(designerUser.getUserId(), false)).thenReturn(designerUser);
174 String appConfigDir = "src/test/resources/config";
175 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
176 configurationManager = new ConfigurationManager(configurationSource);
178 org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration();
179 configuration.setJanusGraphInMemoryGraph(true);
180 Configuration.HeatDeploymentArtifactTimeout testHeatDeploymentArtifactTimeout = new Configuration.HeatDeploymentArtifactTimeout();
181 testHeatDeploymentArtifactTimeout.setDefaultMinutes(1);
182 configuration.setHeatArtifactDeploymentTimeout(testHeatDeploymentArtifactTimeout);
184 configurationManager.setConfiguration(configuration);
185 ExternalConfiguration.setAppName("catalog-be");
191 public void resetSomeMocks() {
192 reset(elementBusinessLogic);
196 public void getComponentCategoriesNoCategoryFoundTest() {
197 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
198 Map<String,String> parametersMap = new HashMap<>();
199 parametersMap.put(COMPONENT_TYPE, componentType);
201 String formatEndpoint = "/v1/categories/{componentType}";
202 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
204 ResponseFormat notFoundResponseFormat = new ResponseFormat(HttpStatus.SC_NOT_FOUND);
205 Either<List<CategoryDefinition>, ResponseFormat> getAllCategoriesEither = Either.right(notFoundResponseFormat);
207 when(elementBusinessLogic.getAllCategories(componentType, designerUser.getUserId()))
208 .thenReturn(getAllCategoriesEither);
210 Response response = target()
213 .accept(MediaType.APPLICATION_JSON)
214 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
217 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NOT_FOUND);
221 public void getComponentCategoriesExceptionDuringProcessingTest() {
222 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
223 Map<String,String> parametersMap = new HashMap<>();
224 parametersMap.put(COMPONENT_TYPE, componentType);
226 String formatEndpoint = "/v1/categories/{componentType}";
227 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
229 when(elementBusinessLogic.getAllCategories(componentType, designerUser.getUserId()))
230 .thenThrow(new RuntimeException("Test exception: getComponentCategories"));
232 Response response = target()
235 .accept(MediaType.APPLICATION_JSON)
236 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
239 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
243 public void getComponentCategoriesTest() {
244 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
245 Map<String,String> parametersMap = new HashMap<>();
246 parametersMap.put(COMPONENT_TYPE, componentType);
248 String formatEndpoint = "/v1/categories/{componentType}";
249 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
251 Either<List<CategoryDefinition>, ResponseFormat> getAllCategoriesEither = Either.left(new ArrayList<>());
253 when(elementBusinessLogic.getAllCategories(componentType, designerUser.getUserId()))
254 .thenReturn(getAllCategoriesEither);
256 Response response = target()
259 .accept(MediaType.APPLICATION_JSON)
260 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
263 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
267 public void getAllCategoriesNoCategoryFoundTest() {
268 String path = "/v1/categories";
270 ResponseFormat notFoundResponseFormat = new ResponseFormat(HttpStatus.SC_NOT_FOUND);
271 Either<UiCategories, ResponseFormat> getAllCategoriesEither = Either.right(notFoundResponseFormat);
273 when(elementBusinessLogic.getAllCategories(designerUser.getUserId()))
274 .thenReturn(getAllCategoriesEither);
276 Response response = target()
279 .accept(MediaType.APPLICATION_JSON)
280 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
283 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NOT_FOUND);
287 public void getAllCategoriesExceptionDuringProcessingTest() {
288 String path = "/v1/setup/ui";
289 when(elementBusinessLogic.getAllCategories(designerUser.getUserId()))
290 .thenThrow(new RuntimeException("Test exception: getAllCategories"));
292 Response response = target()
295 .accept(MediaType.APPLICATION_JSON)
296 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
299 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
303 public void getAllCategoriesTest() {
304 String path = "/v1/setup/ui";
305 Either<UiCategories, ResponseFormat> getAllCategoriesEither = Either.left(new UiCategories());
306 Either<List<ArtifactType>, ActionStatus> otherEither = Either.left(new ArrayList<>());
307 when(elementBusinessLogic.getDefaultHeatTimeout()).thenReturn(Either.left(configurationManager.getConfiguration().getHeatArtifactDeploymentTimeout()));
308 when(elementBusinessLogic.getAllDeploymentArtifactTypes()).thenReturn(Either.left(new HashMap<String, Object>()));
309 when(elementBusinessLogic.getResourceTypesMap()).thenReturn(Either.left(new HashMap<String, String>()));
310 when(elementBusinessLogic.getAllArtifactTypes(designerUser.getUserId()))
311 .thenReturn(otherEither);
315 when(elementBusinessLogic.getAllCategories(designerUser.getUserId()))
316 .thenReturn(getAllCategoriesEither);
318 Response response = target()
321 .accept(MediaType.APPLICATION_JSON)
322 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
325 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
329 public void createComponentCategoryCreationFailedTest() {
330 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
331 Map<String,String> parametersMap = new HashMap<>();
332 parametersMap.put(COMPONENT_TYPE, componentType);
334 String formatEndpoint = "/v1/category/{componentType}";
335 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
337 Either<CategoryDefinition, ResponseFormat> createComponentCategoryEither = Either.right(conflictResponseFormat);
339 when(elementBusinessLogic.createCategory(any(), eq(componentType), eq(designerUser.getUserId())))
340 .thenReturn(createComponentCategoryEither);
342 Response response = target()
345 .accept(MediaType.APPLICATION_JSON)
346 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
347 .post(Entity.json(EMPTY_JSON));
349 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CONFLICT);
353 public void createComponentCategoryExceptionDuringCreationTest() {
354 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
355 Map<String,String> parametersMap = new HashMap<>();
356 parametersMap.put(COMPONENT_TYPE, componentType);
358 String formatEndpoint = "/v1/category/{componentType}";
359 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
361 when(elementBusinessLogic.createCategory(any(), eq(componentType), eq(designerUser.getUserId())))
362 .thenThrow(new RuntimeException("Test exception: createComponentCategory"));
364 Response response = target()
367 .accept(MediaType.APPLICATION_JSON)
368 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
369 .post(Entity.json(EMPTY_JSON));
371 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
375 public void createComponentCategoryTest() {
376 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
377 Map<String,String> parametersMap = new HashMap<>();
378 parametersMap.put(COMPONENT_TYPE, componentType);
380 String formatEndpoint = "/v1/category/{componentType}";
381 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
383 Either<CategoryDefinition, ResponseFormat> createComponentCategoryEither = Either.left(new CategoryDefinition());
385 when(elementBusinessLogic.createCategory(any(), eq(componentType), eq(designerUser.getUserId())))
386 .thenReturn(createComponentCategoryEither);
388 Response response = target()
391 .accept(MediaType.APPLICATION_JSON)
392 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
393 .post(Entity.json(EMPTY_JSON));
395 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CREATED);
399 public void deleteComponentCategoryNoCategoryFoundTest() {
400 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
401 String categoryUniqueId = "categoryUniqueId";
402 Map<String,String> parametersMap = new HashMap<>();
403 parametersMap.put(COMPONENT_TYPE, componentType);
404 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
406 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}";
407 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
409 ResponseFormat notFoundResponseFormat = new ResponseFormat(HttpStatus.SC_NOT_FOUND);
410 Either<CategoryDefinition, ResponseFormat> deleteComponentCategoryEither = Either.right(notFoundResponseFormat);
412 when(elementBusinessLogic.deleteCategory(categoryUniqueId, componentType, designerUser.getUserId()))
413 .thenReturn(deleteComponentCategoryEither);
415 Response response = target()
418 .accept(MediaType.APPLICATION_JSON)
419 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
422 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NOT_FOUND);
426 public void deleteComponentCategoryExceptionDuringProcessingTest() {
427 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
428 String categoryUniqueId = "categoryUniqueId";
429 Map<String,String> parametersMap = new HashMap<>();
430 parametersMap.put(COMPONENT_TYPE, componentType);
431 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
433 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}";
434 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
436 when(elementBusinessLogic.deleteCategory(categoryUniqueId, componentType, designerUser.getUserId()))
437 .thenThrow(new RuntimeException("Test exception: deleteComponentCategory"));
439 Response response = target()
442 .accept(MediaType.APPLICATION_JSON)
443 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
446 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
450 public void deleteComponentCategoryTest() {
451 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
452 String categoryUniqueId = "categoryUniqueId";
453 Map<String,String> parametersMap = new HashMap<>();
454 parametersMap.put(COMPONENT_TYPE, componentType);
455 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
457 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}";
458 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
460 Either<CategoryDefinition, ResponseFormat> deleteComponentCategoryEither = Either.left(new CategoryDefinition());
461 when(elementBusinessLogic.deleteCategory(categoryUniqueId, componentType, designerUser.getUserId()))
462 .thenReturn(deleteComponentCategoryEither);
464 Response response = target()
467 .accept(MediaType.APPLICATION_JSON)
468 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
471 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
475 public void createComponentSubCategoryCreationFailedTest() {
476 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
477 String categoryId = "categoryId";
478 Map<String,String> parametersMap = new HashMap<>();
479 parametersMap.put(COMPONENT_TYPE, componentType);
480 parametersMap.put(CATEGORY_ID, categoryId);
482 String formatEndpoint = "/v1/category/{componentType}/{categoryId}/subCategory";
483 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
485 Either<SubCategoryDefinition, ResponseFormat> createComponentSubCategoryEither = Either.right(conflictResponseFormat);
487 when(elementBusinessLogic.createSubCategory(any(), eq(componentType), eq(categoryId), eq(designerUser.getUserId())))
488 .thenReturn(createComponentSubCategoryEither);
490 Response response = target()
493 .accept(MediaType.APPLICATION_JSON)
494 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
495 .post(Entity.json(EMPTY_JSON));
497 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CONFLICT);
501 public void createComponentSubCategoryExceptionDuringCreationTest() {
502 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
503 String categoryId = "categoryId";
504 Map<String,String> parametersMap = new HashMap<>();
505 parametersMap.put(COMPONENT_TYPE, componentType);
506 parametersMap.put(CATEGORY_ID, categoryId);
508 String formatEndpoint = "/v1/category/{componentType}/{categoryId}/subCategory";
509 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
511 when(elementBusinessLogic.createSubCategory(any(), eq(componentType), eq(categoryId), eq(designerUser.getUserId())))
512 .thenThrow(new RuntimeException("Test exception: createComponentSubCategory"));
514 Response response = target()
517 .accept(MediaType.APPLICATION_JSON)
518 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
519 .post(Entity.json(EMPTY_JSON));
521 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
525 public void createComponentSubCategoryTest() {
526 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
527 String categoryId = "categoryId";
528 Map<String,String> parametersMap = new HashMap<>();
529 parametersMap.put(COMPONENT_TYPE, componentType);
530 parametersMap.put(CATEGORY_ID, categoryId);
532 String formatEndpoint = "/v1/category/{componentType}/{categoryId}/subCategory";
533 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
535 Either<SubCategoryDefinition, ResponseFormat> createComponentSubCategoryEither = Either.left(new SubCategoryDefinition());
537 when(elementBusinessLogic.createSubCategory(any(), eq(componentType), eq(categoryId), eq(designerUser.getUserId())))
538 .thenReturn(createComponentSubCategoryEither);
540 Response response = target()
543 .accept(MediaType.APPLICATION_JSON)
544 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
545 .post(Entity.json(EMPTY_JSON));
547 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CREATED);
551 public void deleteComponentSubCategoryCreationFailedTest() {
552 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
553 String categoryUniqueId = "categoryUniqueId";
554 String subCategoryUniqueId = "subCategoryUniqueId";
555 Map<String,String> parametersMap = new HashMap<>();
556 parametersMap.put(COMPONENT_TYPE, componentType);
557 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
558 parametersMap.put(SUB_CATEGORY_UNIQUE_ID, subCategoryUniqueId);
560 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}";
561 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
563 Either<SubCategoryDefinition, ResponseFormat> deleteComponentSubCategoryEither = Either.right(conflictResponseFormat);
565 when(elementBusinessLogic.deleteSubCategory(eq(subCategoryUniqueId), eq(componentType), eq(designerUser.getUserId())))
566 .thenReturn(deleteComponentSubCategoryEither);
568 Response response = target()
571 .accept(MediaType.APPLICATION_JSON)
572 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
575 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CONFLICT);
579 public void deleteComponentSubCategoryExceptionDuringCreationTest() {
580 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
581 String categoryUniqueId = "categoryUniqueId";
582 String subCategoryUniqueId = "subCategoryUniqueId";
583 Map<String,String> parametersMap = new HashMap<>();
584 parametersMap.put(COMPONENT_TYPE, componentType);
585 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
586 parametersMap.put(SUB_CATEGORY_UNIQUE_ID, subCategoryUniqueId);
588 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}";
589 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
591 when(elementBusinessLogic.deleteSubCategory(eq(subCategoryUniqueId), eq(componentType), eq(designerUser.getUserId())))
592 .thenThrow(new RuntimeException("Test exception: deleteComponentSubCategory"));
594 Response response = target()
597 .accept(MediaType.APPLICATION_JSON)
598 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
601 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
605 public void deleteComponentSubCategoryTest() {
606 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
607 String categoryUniqueId = "categoryUniqueId";
608 String subCategoryUniqueId = "subCategoryUniqueId";
609 Map<String,String> parametersMap = new HashMap<>();
610 parametersMap.put(COMPONENT_TYPE, componentType);
611 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
612 parametersMap.put(SUB_CATEGORY_UNIQUE_ID, subCategoryUniqueId);
614 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}";
615 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
617 Either<SubCategoryDefinition, ResponseFormat> deleteComponentSubCategoryEither = Either.left(new SubCategoryDefinition());
619 when(elementBusinessLogic.deleteSubCategory(eq(subCategoryUniqueId), eq(componentType), eq(designerUser.getUserId())))
620 .thenReturn(deleteComponentSubCategoryEither);
622 Response response = target()
625 .accept(MediaType.APPLICATION_JSON)
626 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
629 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
633 public void createComponentGroupingCreationFailedTest() {
634 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
635 String categoryId = "categoryUniqueId";
636 String subCategoryId = "subCategoryId";
637 Map<String,String> parametersMap = new HashMap<>();
638 parametersMap.put(COMPONENT_TYPE, componentType);
639 parametersMap.put(CATEGORY_ID, categoryId);
640 parametersMap.put(SUB_CATEGORY_ID, subCategoryId);
642 String formatEndpoint = "/v1/category/{componentType}/{categoryId}/subCategory/{subCategoryId}/grouping";
643 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
645 Either<GroupingDefinition, ResponseFormat> createComponentGroupingEither = Either.right(conflictResponseFormat);
647 when(elementBusinessLogic.createGrouping(any(), eq(componentType), eq(categoryId), eq(subCategoryId), eq(designerUser.getUserId())))
648 .thenReturn(createComponentGroupingEither);
650 Response response = target()
653 .accept(MediaType.APPLICATION_JSON)
654 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
655 .post(Entity.json(EMPTY_JSON));
657 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CONFLICT);
661 public void createComponentGroupingExceptionDuringCreationTest() {
662 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
663 String categoryId = "categoryUniqueId";
664 String subCategoryId = "subCategoryId";
665 Map<String,String> parametersMap = new HashMap<>();
666 parametersMap.put(COMPONENT_TYPE, componentType);
667 parametersMap.put(CATEGORY_ID, categoryId);
668 parametersMap.put(SUB_CATEGORY_ID, subCategoryId);
670 String formatEndpoint = "/v1/category/{componentType}/{categoryId}/subCategory/{subCategoryId}/grouping";
671 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
674 when(elementBusinessLogic.createGrouping(any(), eq(componentType), eq(categoryId), eq(subCategoryId), eq(designerUser.getUserId())))
675 .thenThrow(new RuntimeException("Test exception: createComponentGrouping"));
677 Response response = target()
680 .accept(MediaType.APPLICATION_JSON)
681 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
682 .post(Entity.json(EMPTY_JSON));
684 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
688 public void createComponentGroupingTest() {
689 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
690 String categoryId = "categoryUniqueId";
691 String subCategoryId = "subCategoryId";
692 Map<String,String> parametersMap = new HashMap<>();
693 parametersMap.put(COMPONENT_TYPE, componentType);
694 parametersMap.put(CATEGORY_ID, categoryId);
695 parametersMap.put(SUB_CATEGORY_ID, subCategoryId);
697 String formatEndpoint = "/v1/category/{componentType}/{categoryId}/subCategory/{subCategoryId}/grouping";
698 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
700 Either<GroupingDefinition, ResponseFormat> createComponentGroupingEither = Either.left(new GroupingDefinition());
702 when(elementBusinessLogic.createGrouping(any(), eq(componentType), eq(categoryId), eq(subCategoryId), eq(designerUser.getUserId())))
703 .thenReturn(createComponentGroupingEither);
705 Response response = target()
708 .accept(MediaType.APPLICATION_JSON)
709 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
710 .post(Entity.json(EMPTY_JSON));
712 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CREATED);
716 public void deleteComponentGroupingCreationFailedTest() {
717 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
718 String categoryUniqueId = "categoryUniqueId";
719 String subCategoryUniqueId = "subCategoryUniqueId";
720 String groupingUniqueId = "groupingUniqueId";
721 Map<String,String> parametersMap = new HashMap<>();
722 parametersMap.put(COMPONENT_TYPE, componentType);
723 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
724 parametersMap.put(SUB_CATEGORY_UNIQUE_ID, subCategoryUniqueId);
725 parametersMap.put(GROUPING_UNIQUE_ID, groupingUniqueId);
727 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}/grouping/{groupingUniqueId}";
728 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
730 Either<GroupingDefinition, ResponseFormat> deleteComponentGroupingEither = Either.right(conflictResponseFormat);
732 when(elementBusinessLogic.deleteGrouping(eq(groupingUniqueId), eq(componentType), eq(designerUser.getUserId())))
733 .thenReturn(deleteComponentGroupingEither);
735 Response response = target()
738 .accept(MediaType.APPLICATION_JSON)
739 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
742 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CONFLICT);
746 public void deleteComponentGroupingExceptionDuringCreationTest() {
747 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
748 String categoryUniqueId = "categoryUniqueId";
749 String subCategoryUniqueId = "subCategoryUniqueId";
750 String groupingUniqueId = "groupingUniqueId";
751 Map<String,String> parametersMap = new HashMap<>();
752 parametersMap.put(COMPONENT_TYPE, componentType);
753 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
754 parametersMap.put(SUB_CATEGORY_UNIQUE_ID, subCategoryUniqueId);
755 parametersMap.put(GROUPING_UNIQUE_ID, groupingUniqueId);
757 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}/grouping/{groupingUniqueId}";
758 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
760 when(elementBusinessLogic.deleteGrouping(eq(groupingUniqueId), eq(componentType), eq(designerUser.getUserId())))
761 .thenThrow(new RuntimeException("Test exception: deleteComponentGrouping"));
763 Response response = target()
766 .accept(MediaType.APPLICATION_JSON)
767 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
770 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
774 public void deleteComponentGroupingTest() {
775 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
776 String categoryUniqueId = "categoryUniqueId";
777 String subCategoryUniqueId = "subCategoryUniqueId";
778 String groupingUniqueId = "groupingUniqueId";
779 Map<String,String> parametersMap = new HashMap<>();
780 parametersMap.put(COMPONENT_TYPE, componentType);
781 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
782 parametersMap.put(SUB_CATEGORY_UNIQUE_ID, subCategoryUniqueId);
783 parametersMap.put(GROUPING_UNIQUE_ID, groupingUniqueId);
785 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}/grouping/{groupingUniqueId}";
786 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
788 Either<GroupingDefinition, ResponseFormat> deleteComponentGroupingEither = Either.left(new GroupingDefinition());
790 when(elementBusinessLogic.deleteGrouping(eq(groupingUniqueId), eq(componentType), eq(designerUser.getUserId())))
791 .thenReturn(deleteComponentGroupingEither);
793 Response response = target()
796 .accept(MediaType.APPLICATION_JSON)
797 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
800 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
804 public void tagsNoTagsFoundTest() {
805 String path = "/v1/tags";
806 Either<List<Tag>, ActionStatus> tagsEither = Either.right(ActionStatus.NO_CONTENT);
808 when(elementBusinessLogic.getAllTags(designerUser.getUserId()))
809 .thenReturn(tagsEither);
811 Response response = target()
814 .accept(MediaType.APPLICATION_JSON)
815 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
818 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
822 public void tagsExceptionDuringProcessingTest() {
823 String path = "/v1/tags";
824 when(elementBusinessLogic.getAllTags(designerUser.getUserId()))
825 .thenThrow(new RuntimeException("Test exception: tags"));
827 Response response = target()
830 .accept(MediaType.APPLICATION_JSON)
831 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
834 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
838 public void tagsTest() {
839 String path = "/v1/tags";
840 Either<List<Tag>, ActionStatus> tagsEither = Either.left(new ArrayList<>());
841 when(elementBusinessLogic.getAllTags(designerUser.getUserId()))
842 .thenReturn(tagsEither);
844 Response response = target()
847 .accept(MediaType.APPLICATION_JSON)
848 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
851 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
855 public void propertyScopesNoPropertyScopesFoundTest() {
856 String path = "/v1/propertyScopes";
857 Either<List<PropertyScope>, ActionStatus> propertyScopesEither = Either.right(ActionStatus.NO_CONTENT);
859 when(elementBusinessLogic.getAllPropertyScopes(designerUser.getUserId()))
860 .thenReturn(propertyScopesEither);
862 Response response = target()
865 .accept(MediaType.APPLICATION_JSON)
866 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
869 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
873 public void propertyScopesExceptionDuringProcessingTest() {
874 String path = "/v1/propertyScopes";
875 when(elementBusinessLogic.getAllPropertyScopes(designerUser.getUserId()))
876 .thenThrow(new RuntimeException("Test exception: propertyScopes"));
878 Response response = target()
881 .accept(MediaType.APPLICATION_JSON)
882 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
885 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
889 public void propertyScopesTest() {
890 String path = "/v1/propertyScopes";
891 Either<List<PropertyScope>, ActionStatus> propertyScopesEither = Either.left(new ArrayList<>());
892 when(elementBusinessLogic.getAllPropertyScopes(designerUser.getUserId()))
893 .thenReturn(propertyScopesEither);
895 Response response = target()
898 .accept(MediaType.APPLICATION_JSON)
899 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
902 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
906 public void artifactTypesNoartifactTypesFoundTest() {
907 String path = "/v1/artifactTypes";
908 Either<List<ArtifactType>, ActionStatus> artifactTypesEither = Either.right(ActionStatus.NO_CONTENT);
910 when(elementBusinessLogic.getAllArtifactTypes(designerUser.getUserId()))
911 .thenReturn(artifactTypesEither);
913 Response response = target()
916 .accept(MediaType.APPLICATION_JSON)
917 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
920 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
924 public void artifactTypesExceptionDuringProcessingTest() {
925 String path = "/v1/artifactTypes";
926 when(elementBusinessLogic.getAllArtifactTypes(designerUser.getUserId()))
927 .thenThrow(new RuntimeException("Test exception: artifactTypes"));
929 Response response = target()
932 .accept(MediaType.APPLICATION_JSON)
933 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
936 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
940 public void artifactTypesTest() {
941 String path = "/v1/artifactTypes";
942 Either<List<ArtifactType>, ActionStatus> artifactTypesEither = Either.left(new ArrayList<>());
943 when(elementBusinessLogic.getAllArtifactTypes(designerUser.getUserId()))
944 .thenReturn(artifactTypesEither);
946 Response response = target()
949 .accept(MediaType.APPLICATION_JSON)
950 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
953 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
957 public void configurationNoConfigurationFoundTest() {
958 String path = "/v1/setup/ui";
960 Either<List<ArtifactType>, ActionStatus> otherEither = Either.left(new ArrayList<>());
961 Configuration.HeatDeploymentArtifactTimeout heatDeploymentArtifactTimeout = new Configuration.HeatDeploymentArtifactTimeout();
962 heatDeploymentArtifactTimeout.setDefaultMinutes(1);
963 Either<Configuration.HeatDeploymentArtifactTimeout, ActionStatus> defaultHeatTimeoutEither = Either.left(heatDeploymentArtifactTimeout);
964 Either<Map<String, Object>, ActionStatus> deploymentEither = Either.left(new HashMap<>());
965 Either<Map<String, String>, ActionStatus> resourceTypesMapEither = Either.left(new HashMap<>());
967 when(elementBusinessLogic.getAllArtifactTypes(designerUser.getUserId()))
968 .thenReturn(otherEither);
969 when(elementBusinessLogic.getDefaultHeatTimeout())
970 .thenReturn(defaultHeatTimeoutEither);
971 when(elementBusinessLogic.getAllDeploymentArtifactTypes())
972 .thenReturn(deploymentEither);
973 when(elementBusinessLogic.getResourceTypesMap())
974 .thenReturn(resourceTypesMapEither);
976 Response response = target()
979 .accept(MediaType.APPLICATION_JSON)
980 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
983 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
987 public void configurationExceptionDuringProcessingTest() {
988 String path = "/v1/setup/ui";
989 when(elementBusinessLogic.getAllArtifactTypes(designerUser.getUserId()))
990 .thenThrow(new RuntimeException("Test exception: artifactTypes"));
992 Response response = target()
995 .accept(MediaType.APPLICATION_JSON)
996 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
999 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
1003 public void screenNoCatalogComponentsFoundTest() {
1004 String path = "/v1/screen";
1006 Either<Map<String, List<CatalogComponent>>, ResponseFormat> screenEither = Either.right(badRequestResponseFormat);
1007 when(elementBusinessLogic.getCatalogComponents(eq(designerUser.getUserId()), any()))
1008 .thenReturn(screenEither);
1010 Response response = target()
1013 .accept(MediaType.APPLICATION_JSON)
1014 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
1017 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_BAD_REQUEST);
1021 public void screenExceptionDuringProcessingTest() {
1022 String path = "/v1/screen";
1024 when(elementBusinessLogic.getCatalogComponents(eq(designerUser.getUserId()), any()))
1025 .thenThrow(new RuntimeException("Test exception: screen"));
1027 Response response = target()
1030 .accept(MediaType.APPLICATION_JSON)
1031 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
1034 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
1038 public void screenTest() {
1039 String path = "/v1/screen";
1041 Either<Map<String, List<CatalogComponent>>, ResponseFormat> screenEither = Either.left(new HashMap<>());
1042 when(elementBusinessLogic.getCatalogComponents(eq(designerUser.getUserId()), any()))
1043 .thenReturn(screenEither);
1045 Response response = target()
1048 .accept(MediaType.APPLICATION_JSON)
1049 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
1052 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
1056 protected Application configure() {
1057 ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
1058 return new ResourceConfig(ElementServlet.class)
1059 .register(new AbstractBinder() {
1062 protected void configure() {
1063 bind(request).to(HttpServletRequest.class);
1064 bind(userBusinessLogic).to(UserBusinessLogic.class);
1065 bind(componentUtils).to(ComponentsUtils.class);
1066 bind(componentsCleanBusinessLogic).to(ComponentsCleanBusinessLogic.class);
1067 bind(elementBusinessLogic).to(ElementBusinessLogic.class);
1070 .property("contextConfig", context);