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 com.google.common.collect.Lists;
26 import fj.data.Either;
27 import org.apache.commons.text.StrSubstitutor;
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.apache.http.HttpStatus;
36 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
38 import org.openecomp.sdc.be.components.impl.ElementBusinessLogic;
39 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
40 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
41 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
42 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
43 import org.openecomp.sdc.be.components.scheduledtasks.ComponentsCleanBusinessLogic;
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.datatypes.enums.OriginTypeEnum;
49 import org.openecomp.sdc.be.impl.ComponentsUtils;
50 import org.openecomp.sdc.be.impl.ServletUtils;
51 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
52 import org.openecomp.sdc.be.model.ArtifactType;
53 import org.openecomp.sdc.be.model.PropertyScope;
54 import org.openecomp.sdc.be.model.Resource;
55 import org.openecomp.sdc.be.model.Tag;
56 import org.openecomp.sdc.be.model.User;
57 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
58 import org.openecomp.sdc.be.model.category.CategoryDefinition;
59 import org.openecomp.sdc.be.model.category.GroupingDefinition;
60 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
61 import org.openecomp.sdc.be.ui.model.UiCategories;
62 import org.openecomp.sdc.be.user.Role;
63 import org.openecomp.sdc.be.user.UserBusinessLogic;
64 import org.openecomp.sdc.common.api.ConfigurationSource;
65 import org.openecomp.sdc.common.api.Constants;
66 import org.openecomp.sdc.common.impl.ExternalConfiguration;
67 import org.openecomp.sdc.common.impl.FSConfigurationSource;
68 import org.openecomp.sdc.exception.ResponseFormat;
69 import org.springframework.context.ApplicationContext;
70 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
71 import org.springframework.web.context.WebApplicationContext;
73 import javax.servlet.ServletContext;
74 import javax.servlet.http.HttpServletRequest;
75 import javax.servlet.http.HttpSession;
76 import javax.ws.rs.client.Entity;
77 import javax.ws.rs.core.Application;
78 import javax.ws.rs.core.MediaType;
79 import javax.ws.rs.core.Response;
82 import java.util.stream.Collectors;
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 Resource resource = Mockito.mock(Resource.class);
96 public static final UserBusinessLogic userBusinessLogic = Mockito.mock(UserBusinessLogic.class);
97 public static final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
98 public static final ArtifactsBusinessLogic artifactsBusinessLogic = Mockito.mock(ArtifactsBusinessLogic.class);
100 private static final ServletContext servletContext = Mockito.mock(ServletContext.class);
101 public static final WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
102 private static final WebApplicationContext webApplicationContext = Mockito.mock(WebApplicationContext.class);
103 private static final ServletUtils servletUtils = Mockito.mock(ServletUtils.class);
104 private static final UserBusinessLogic userAdmin = Mockito.mock(UserBusinessLogic.class);
105 private static final ComponentsUtils componentUtils = Mockito.mock(ComponentsUtils.class);
106 private static final ComponentsCleanBusinessLogic componentsCleanBusinessLogic = Mockito.mock(ComponentsCleanBusinessLogic.class);
107 private static final ElementBusinessLogic elementBusinessLogic = Mockito.mock(ElementBusinessLogic.class);
109 private static final ResponseFormat okResponseFormat = new ResponseFormat(HttpStatus.SC_OK);
110 private static final ResponseFormat conflictResponseFormat = new ResponseFormat(HttpStatus.SC_CONFLICT);
111 private static final ResponseFormat generalErrorResponseFormat = new ResponseFormat(HttpStatus.SC_INTERNAL_SERVER_ERROR);
112 private static final ResponseFormat createdResponseFormat = new ResponseFormat(HttpStatus.SC_CREATED);
113 private static final ResponseFormat noContentResponseFormat = new ResponseFormat(HttpStatus.SC_NO_CONTENT);
114 private static final ResponseFormat unauthorizedResponseFormat = Mockito.mock(ResponseFormat.class);
115 private static final ResponseFormat notFoundResponseFormat = Mockito.mock(ResponseFormat.class);
116 private static final ResponseFormat badRequestResponseFormat = Mockito.mock(ResponseFormat.class);
117 private static final String EMPTY_JSON = "{}";
118 private static final String COMPONENT_TYPE = "componentType";
119 private static final String CATEGORY_UNIQUE_ID = "categoryUniqueId";
120 private static final String CATEGORY_ID = "categoryId";
121 private static final String SUB_CATEGORY_UNIQUE_ID = "subCategoryUniqueId";
122 private static final String SUB_CATEGORY_ID = "subCategoryId";
123 private static final String GROUPING_UNIQUE_ID = "groupingUniqueId";
126 private static User designerUser = new User("designer", "designer", "designer", "designer@email.com", Role.DESIGNER.name(), System
127 .currentTimeMillis());
130 public static void setup() {
132 //Needed for User Authorization
133 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
134 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
135 when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
136 when(servletUtils.getUserAdmin()).thenReturn(userAdmin);
137 when(servletUtils.getComponentsUtils()).thenReturn(componentUtils);
138 when(componentUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)).thenReturn(unauthorizedResponseFormat);
139 when(unauthorizedResponseFormat.getStatus()).thenReturn(HttpStatus.SC_UNAUTHORIZED);
141 when(componentUtils.getResponseFormat(ActionStatus.OK)) .thenReturn(okResponseFormat);
142 when(componentUtils.getResponseFormat(ActionStatus.CREATED)).thenReturn(createdResponseFormat);
143 when(componentUtils.getResponseFormat(ActionStatus.NO_CONTENT)).thenReturn(noContentResponseFormat);
144 when(componentUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(badRequestResponseFormat);
145 when(componentUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)) .thenReturn(generalErrorResponseFormat);
146 when(componentUtils.getResponseFormat(any(ComponentException.class)))
147 .thenReturn(generalErrorResponseFormat);
149 ByResponseFormatComponentException ce = Mockito.mock(ByResponseFormatComponentException.class);
150 when(ce.getResponseFormat()).thenReturn(unauthorizedResponseFormat);
152 //Needed for error configuration
153 when(notFoundResponseFormat.getStatus()).thenReturn(HttpStatus.SC_NOT_FOUND);
154 when(badRequestResponseFormat.getStatus()).thenReturn(HttpStatus.SC_BAD_REQUEST);
155 when(componentUtils.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), any())).thenReturn(notFoundResponseFormat);
156 when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_VERSION_NOT_FOUND), any())).thenReturn(notFoundResponseFormat);
157 when(componentUtils.getResponseFormat(eq(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND), any())).thenReturn(notFoundResponseFormat);
158 when(componentUtils.getResponseFormat(eq(ActionStatus.EXT_REF_NOT_FOUND), any())).thenReturn(notFoundResponseFormat);
159 when(componentUtils.getResponseFormat(eq(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID), any())).thenReturn(badRequestResponseFormat);
161 Either<User, ActionStatus> designerEither = Either.left(designerUser);
163 when(userAdmin.getUser(designerUser.getUserId(), false)).thenReturn(designerEither);
165 String appConfigDir = "src/test/resources/config";
166 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
167 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
169 org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration();
170 configuration.setJanusGraphInMemoryGraph(true);
172 configurationManager.setConfiguration(configuration);
173 ExternalConfiguration.setAppName("catalog-be");
177 public void resetSomeMocks() {
178 reset(elementBusinessLogic);
182 public void getComponentCategoriesNoCategoryFoundTest() {
183 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
184 Map<String,String> parametersMap = new HashMap<>();
185 parametersMap.put(COMPONENT_TYPE, componentType);
187 String formatEndpoint = "/v1/categories/{componentType}";
188 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
190 ResponseFormat notFoundResponseFormat = new ResponseFormat(HttpStatus.SC_NOT_FOUND);
191 Either<List<CategoryDefinition>, ResponseFormat> getAllCategoriesEither = Either.right(notFoundResponseFormat);
193 when(elementBusinessLogic.getAllCategories(componentType, designerUser.getUserId()))
194 .thenReturn(getAllCategoriesEither);
196 Response response = target()
199 .accept(MediaType.APPLICATION_JSON)
200 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
203 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NOT_FOUND);
207 public void getComponentCategoriesExceptionDuringProcessingTest() {
208 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
209 Map<String,String> parametersMap = new HashMap<>();
210 parametersMap.put(COMPONENT_TYPE, componentType);
212 String formatEndpoint = "/v1/categories/{componentType}";
213 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
215 when(elementBusinessLogic.getAllCategories(componentType, designerUser.getUserId()))
216 .thenThrow(new RuntimeException("Test exception: getComponentCategories"));
218 Response response = target()
221 .accept(MediaType.APPLICATION_JSON)
222 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
225 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
229 public void getComponentCategoriesTest() {
230 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
231 Map<String,String> parametersMap = new HashMap<>();
232 parametersMap.put(COMPONENT_TYPE, componentType);
234 String formatEndpoint = "/v1/categories/{componentType}";
235 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
237 Either<List<CategoryDefinition>, ResponseFormat> getAllCategoriesEither = Either.left(new ArrayList<>());
239 when(elementBusinessLogic.getAllCategories(componentType, designerUser.getUserId()))
240 .thenReturn(getAllCategoriesEither);
242 Response response = target()
245 .accept(MediaType.APPLICATION_JSON)
246 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
249 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
253 public void getAllCategoriesNoCategoryFoundTest() {
254 String path = "/v1/categories";
256 ResponseFormat notFoundResponseFormat = new ResponseFormat(HttpStatus.SC_NOT_FOUND);
257 Either<UiCategories, ResponseFormat> getAllCategoriesEither = Either.right(notFoundResponseFormat);
259 when(elementBusinessLogic.getAllCategories(designerUser.getUserId()))
260 .thenReturn(getAllCategoriesEither);
262 Response response = target()
265 .accept(MediaType.APPLICATION_JSON)
266 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
269 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NOT_FOUND);
273 public void getAllCategoriesExceptionDuringProcessingTest() {
274 String path = "/v1/categories";
275 when(elementBusinessLogic.getAllCategories(designerUser.getUserId()))
276 .thenThrow(new RuntimeException("Test exception: getAllCategories"));
278 Response response = target()
281 .accept(MediaType.APPLICATION_JSON)
282 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
285 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
289 public void getAllCategoriesTest() {
290 String path = "/v1/categories";
291 Either<UiCategories, ResponseFormat> getAllCategoriesEither = Either.left(new UiCategories());
293 when(elementBusinessLogic.getAllCategories(designerUser.getUserId()))
294 .thenReturn(getAllCategoriesEither);
296 Response response = target()
299 .accept(MediaType.APPLICATION_JSON)
300 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
303 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
307 public void createComponentCategoryCreationFailedTest() {
308 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
309 Map<String,String> parametersMap = new HashMap<>();
310 parametersMap.put(COMPONENT_TYPE, componentType);
312 String formatEndpoint = "/v1/category/{componentType}";
313 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
315 Either<CategoryDefinition, ResponseFormat> createComponentCategoryEither = Either.right(conflictResponseFormat);
317 when(elementBusinessLogic.createCategory(any(), eq(componentType), eq(designerUser.getUserId())))
318 .thenReturn(createComponentCategoryEither);
320 Response response = target()
323 .accept(MediaType.APPLICATION_JSON)
324 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
325 .post(Entity.json(EMPTY_JSON));
327 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CONFLICT);
331 public void createComponentCategoryExceptionDuringCreationTest() {
332 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
333 Map<String,String> parametersMap = new HashMap<>();
334 parametersMap.put(COMPONENT_TYPE, componentType);
336 String formatEndpoint = "/v1/category/{componentType}";
337 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
339 when(elementBusinessLogic.createCategory(any(), eq(componentType), eq(designerUser.getUserId())))
340 .thenThrow(new RuntimeException("Test exception: createComponentCategory"));
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_INTERNAL_SERVER_ERROR);
353 public void createComponentCategoryTest() {
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 Either<CategoryDefinition, ResponseFormat> createComponentCategoryEither = Either.left(new CategoryDefinition());
363 when(elementBusinessLogic.createCategory(any(), eq(componentType), eq(designerUser.getUserId())))
364 .thenReturn(createComponentCategoryEither);
366 Response response = target()
369 .accept(MediaType.APPLICATION_JSON)
370 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
371 .post(Entity.json(EMPTY_JSON));
373 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CREATED);
377 public void deleteComponentCategoryNoCategoryFoundTest() {
378 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
379 String categoryUniqueId = "categoryUniqueId";
380 Map<String,String> parametersMap = new HashMap<>();
381 parametersMap.put(COMPONENT_TYPE, componentType);
382 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
384 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}";
385 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
387 ResponseFormat notFoundResponseFormat = new ResponseFormat(HttpStatus.SC_NOT_FOUND);
388 Either<CategoryDefinition, ResponseFormat> deleteComponentCategoryEither = Either.right(notFoundResponseFormat);
390 when(elementBusinessLogic.deleteCategory(categoryUniqueId, componentType, designerUser.getUserId()))
391 .thenReturn(deleteComponentCategoryEither);
393 Response response = target()
396 .accept(MediaType.APPLICATION_JSON)
397 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
400 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NOT_FOUND);
404 public void deleteComponentCategoryExceptionDuringProcessingTest() {
405 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
406 String categoryUniqueId = "categoryUniqueId";
407 Map<String,String> parametersMap = new HashMap<>();
408 parametersMap.put(COMPONENT_TYPE, componentType);
409 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
411 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}";
412 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
414 when(elementBusinessLogic.deleteCategory(categoryUniqueId, componentType, designerUser.getUserId()))
415 .thenThrow(new RuntimeException("Test exception: deleteComponentCategory"));
417 Response response = target()
420 .accept(MediaType.APPLICATION_JSON)
421 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
424 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
428 public void deleteComponentCategoryTest() {
429 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
430 String categoryUniqueId = "categoryUniqueId";
431 Map<String,String> parametersMap = new HashMap<>();
432 parametersMap.put(COMPONENT_TYPE, componentType);
433 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
435 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}";
436 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
438 Either<CategoryDefinition, ResponseFormat> deleteComponentCategoryEither = Either.left(new CategoryDefinition());
439 when(elementBusinessLogic.deleteCategory(categoryUniqueId, componentType, designerUser.getUserId()))
440 .thenReturn(deleteComponentCategoryEither);
442 Response response = target()
445 .accept(MediaType.APPLICATION_JSON)
446 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
449 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
453 public void createComponentSubCategoryCreationFailedTest() {
454 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
455 String categoryId = "categoryId";
456 Map<String,String> parametersMap = new HashMap<>();
457 parametersMap.put(COMPONENT_TYPE, componentType);
458 parametersMap.put(CATEGORY_ID, categoryId);
460 String formatEndpoint = "/v1/category/{componentType}/{categoryId}/subCategory";
461 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
463 Either<SubCategoryDefinition, ResponseFormat> createComponentSubCategoryEither = Either.right(conflictResponseFormat);
465 when(elementBusinessLogic.createSubCategory(any(), eq(componentType), eq(categoryId), eq(designerUser.getUserId())))
466 .thenReturn(createComponentSubCategoryEither);
468 Response response = target()
471 .accept(MediaType.APPLICATION_JSON)
472 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
473 .post(Entity.json(EMPTY_JSON));
475 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CONFLICT);
479 public void createComponentSubCategoryExceptionDuringCreationTest() {
480 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
481 String categoryId = "categoryId";
482 Map<String,String> parametersMap = new HashMap<>();
483 parametersMap.put(COMPONENT_TYPE, componentType);
484 parametersMap.put(CATEGORY_ID, categoryId);
486 String formatEndpoint = "/v1/category/{componentType}/{categoryId}/subCategory";
487 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
489 when(elementBusinessLogic.createSubCategory(any(), eq(componentType), eq(categoryId), eq(designerUser.getUserId())))
490 .thenThrow(new RuntimeException("Test exception: createComponentSubCategory"));
492 Response response = target()
495 .accept(MediaType.APPLICATION_JSON)
496 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
497 .post(Entity.json(EMPTY_JSON));
499 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
503 public void createComponentSubCategoryTest() {
504 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
505 String categoryId = "categoryId";
506 Map<String,String> parametersMap = new HashMap<>();
507 parametersMap.put(COMPONENT_TYPE, componentType);
508 parametersMap.put(CATEGORY_ID, categoryId);
510 String formatEndpoint = "/v1/category/{componentType}/{categoryId}/subCategory";
511 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
513 Either<SubCategoryDefinition, ResponseFormat> createComponentSubCategoryEither = Either.left(new SubCategoryDefinition());
515 when(elementBusinessLogic.createSubCategory(any(), eq(componentType), eq(categoryId), eq(designerUser.getUserId())))
516 .thenReturn(createComponentSubCategoryEither);
518 Response response = target()
521 .accept(MediaType.APPLICATION_JSON)
522 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
523 .post(Entity.json(EMPTY_JSON));
525 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CREATED);
529 public void deleteComponentSubCategoryCreationFailedTest() {
530 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
531 String categoryUniqueId = "categoryUniqueId";
532 String subCategoryUniqueId = "subCategoryUniqueId";
533 Map<String,String> parametersMap = new HashMap<>();
534 parametersMap.put(COMPONENT_TYPE, componentType);
535 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
536 parametersMap.put(SUB_CATEGORY_UNIQUE_ID, subCategoryUniqueId);
538 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}";
539 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
541 Either<SubCategoryDefinition, ResponseFormat> deleteComponentSubCategoryEither = Either.right(conflictResponseFormat);
543 when(elementBusinessLogic.deleteSubCategory(eq(subCategoryUniqueId), eq(componentType), eq(designerUser.getUserId())))
544 .thenReturn(deleteComponentSubCategoryEither);
546 Response response = target()
549 .accept(MediaType.APPLICATION_JSON)
550 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
553 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CONFLICT);
557 public void deleteComponentSubCategoryExceptionDuringCreationTest() {
558 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
559 String categoryUniqueId = "categoryUniqueId";
560 String subCategoryUniqueId = "subCategoryUniqueId";
561 Map<String,String> parametersMap = new HashMap<>();
562 parametersMap.put(COMPONENT_TYPE, componentType);
563 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
564 parametersMap.put(SUB_CATEGORY_UNIQUE_ID, subCategoryUniqueId);
566 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}";
567 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
569 when(elementBusinessLogic.deleteSubCategory(eq(subCategoryUniqueId), eq(componentType), eq(designerUser.getUserId())))
570 .thenThrow(new RuntimeException("Test exception: deleteComponentSubCategory"));
572 Response response = target()
575 .accept(MediaType.APPLICATION_JSON)
576 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
579 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
583 public void deleteComponentSubCategoryTest() {
584 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
585 String categoryUniqueId = "categoryUniqueId";
586 String subCategoryUniqueId = "subCategoryUniqueId";
587 Map<String,String> parametersMap = new HashMap<>();
588 parametersMap.put(COMPONENT_TYPE, componentType);
589 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
590 parametersMap.put(SUB_CATEGORY_UNIQUE_ID, subCategoryUniqueId);
592 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}";
593 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
595 Either<SubCategoryDefinition, ResponseFormat> deleteComponentSubCategoryEither = Either.left(new SubCategoryDefinition());
597 when(elementBusinessLogic.deleteSubCategory(eq(subCategoryUniqueId), eq(componentType), eq(designerUser.getUserId())))
598 .thenReturn(deleteComponentSubCategoryEither);
600 Response response = target()
603 .accept(MediaType.APPLICATION_JSON)
604 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
607 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
611 public void createComponentGroupingCreationFailedTest() {
612 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
613 String categoryId = "categoryUniqueId";
614 String subCategoryId = "subCategoryId";
615 Map<String,String> parametersMap = new HashMap<>();
616 parametersMap.put(COMPONENT_TYPE, componentType);
617 parametersMap.put(CATEGORY_ID, categoryId);
618 parametersMap.put(SUB_CATEGORY_ID, subCategoryId);
620 String formatEndpoint = "/v1/category/{componentType}/{categoryId}/subCategory/{subCategoryId}/grouping";
621 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
623 Either<GroupingDefinition, ResponseFormat> createComponentGroupingEither = Either.right(conflictResponseFormat);
625 when(elementBusinessLogic.createGrouping(any(), eq(componentType), eq(categoryId), eq(subCategoryId), eq(designerUser.getUserId())))
626 .thenReturn(createComponentGroupingEither);
628 Response response = target()
631 .accept(MediaType.APPLICATION_JSON)
632 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
633 .post(Entity.json(EMPTY_JSON));
635 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CONFLICT);
639 public void createComponentGroupingExceptionDuringCreationTest() {
640 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
641 String categoryId = "categoryUniqueId";
642 String subCategoryId = "subCategoryId";
643 Map<String,String> parametersMap = new HashMap<>();
644 parametersMap.put(COMPONENT_TYPE, componentType);
645 parametersMap.put(CATEGORY_ID, categoryId);
646 parametersMap.put(SUB_CATEGORY_ID, subCategoryId);
648 String formatEndpoint = "/v1/category/{componentType}/{categoryId}/subCategory/{subCategoryId}/grouping";
649 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
652 when(elementBusinessLogic.createGrouping(any(), eq(componentType), eq(categoryId), eq(subCategoryId), eq(designerUser.getUserId())))
653 .thenThrow(new RuntimeException("Test exception: createComponentGrouping"));
655 Response response = target()
658 .accept(MediaType.APPLICATION_JSON)
659 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
660 .post(Entity.json(EMPTY_JSON));
662 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
666 public void createComponentGroupingTest() {
667 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
668 String categoryId = "categoryUniqueId";
669 String subCategoryId = "subCategoryId";
670 Map<String,String> parametersMap = new HashMap<>();
671 parametersMap.put(COMPONENT_TYPE, componentType);
672 parametersMap.put(CATEGORY_ID, categoryId);
673 parametersMap.put(SUB_CATEGORY_ID, subCategoryId);
675 String formatEndpoint = "/v1/category/{componentType}/{categoryId}/subCategory/{subCategoryId}/grouping";
676 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
678 Either<GroupingDefinition, ResponseFormat> createComponentGroupingEither = Either.left(new GroupingDefinition());
680 when(elementBusinessLogic.createGrouping(any(), eq(componentType), eq(categoryId), eq(subCategoryId), eq(designerUser.getUserId())))
681 .thenReturn(createComponentGroupingEither);
683 Response response = target()
686 .accept(MediaType.APPLICATION_JSON)
687 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
688 .post(Entity.json(EMPTY_JSON));
690 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CREATED);
694 public void deleteComponentGroupingCreationFailedTest() {
695 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
696 String categoryUniqueId = "categoryUniqueId";
697 String subCategoryUniqueId = "subCategoryUniqueId";
698 String groupingUniqueId = "groupingUniqueId";
699 Map<String,String> parametersMap = new HashMap<>();
700 parametersMap.put(COMPONENT_TYPE, componentType);
701 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
702 parametersMap.put(SUB_CATEGORY_UNIQUE_ID, subCategoryUniqueId);
703 parametersMap.put(GROUPING_UNIQUE_ID, groupingUniqueId);
705 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}/grouping/{groupingUniqueId}";
706 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
708 Either<GroupingDefinition, ResponseFormat> deleteComponentGroupingEither = Either.right(conflictResponseFormat);
710 when(elementBusinessLogic.deleteGrouping(eq(groupingUniqueId), eq(componentType), eq(designerUser.getUserId())))
711 .thenReturn(deleteComponentGroupingEither);
713 Response response = target()
716 .accept(MediaType.APPLICATION_JSON)
717 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
720 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CONFLICT);
724 public void deleteComponentGroupingExceptionDuringCreationTest() {
725 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
726 String categoryUniqueId = "categoryUniqueId";
727 String subCategoryUniqueId = "subCategoryUniqueId";
728 String groupingUniqueId = "groupingUniqueId";
729 Map<String,String> parametersMap = new HashMap<>();
730 parametersMap.put(COMPONENT_TYPE, componentType);
731 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
732 parametersMap.put(SUB_CATEGORY_UNIQUE_ID, subCategoryUniqueId);
733 parametersMap.put(GROUPING_UNIQUE_ID, groupingUniqueId);
735 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}/grouping/{groupingUniqueId}";
736 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
738 when(elementBusinessLogic.deleteGrouping(eq(groupingUniqueId), eq(componentType), eq(designerUser.getUserId())))
739 .thenThrow(new RuntimeException("Test exception: deleteComponentGrouping"));
741 Response response = target()
744 .accept(MediaType.APPLICATION_JSON)
745 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
748 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
752 public void deleteComponentGroupingTest() {
753 String componentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
754 String categoryUniqueId = "categoryUniqueId";
755 String subCategoryUniqueId = "subCategoryUniqueId";
756 String groupingUniqueId = "groupingUniqueId";
757 Map<String,String> parametersMap = new HashMap<>();
758 parametersMap.put(COMPONENT_TYPE, componentType);
759 parametersMap.put(CATEGORY_UNIQUE_ID, categoryUniqueId);
760 parametersMap.put(SUB_CATEGORY_UNIQUE_ID, subCategoryUniqueId);
761 parametersMap.put(GROUPING_UNIQUE_ID, groupingUniqueId);
763 String formatEndpoint = "/v1/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}/grouping/{groupingUniqueId}";
764 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{","}");
766 Either<GroupingDefinition, ResponseFormat> deleteComponentGroupingEither = Either.left(new GroupingDefinition());
768 when(elementBusinessLogic.deleteGrouping(eq(groupingUniqueId), eq(componentType), eq(designerUser.getUserId())))
769 .thenReturn(deleteComponentGroupingEither);
771 Response response = target()
774 .accept(MediaType.APPLICATION_JSON)
775 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
778 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
782 public void tagsNoTagsFoundTest() {
783 String path = "/v1/tags";
784 Either<List<Tag>, ActionStatus> tagsEither = Either.right(ActionStatus.NO_CONTENT);
786 when(elementBusinessLogic.getAllTags(designerUser.getUserId()))
787 .thenReturn(tagsEither);
789 Response response = target()
792 .accept(MediaType.APPLICATION_JSON)
793 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
796 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
800 public void tagsExceptionDuringProcessingTest() {
801 String path = "/v1/tags";
802 when(elementBusinessLogic.getAllTags(designerUser.getUserId()))
803 .thenThrow(new RuntimeException("Test exception: tags"));
805 Response response = target()
808 .accept(MediaType.APPLICATION_JSON)
809 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
812 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
816 public void tagsTest() {
817 String path = "/v1/tags";
818 Either<List<Tag>, ActionStatus> tagsEither = Either.left(new ArrayList<>());
819 when(elementBusinessLogic.getAllTags(designerUser.getUserId()))
820 .thenReturn(tagsEither);
822 Response response = target()
825 .accept(MediaType.APPLICATION_JSON)
826 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
829 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
833 public void propertyScopesNoPropertyScopesFoundTest() {
834 String path = "/v1/propertyScopes";
835 Either<List<PropertyScope>, ActionStatus> propertyScopesEither = Either.right(ActionStatus.NO_CONTENT);
837 when(elementBusinessLogic.getAllPropertyScopes(designerUser.getUserId()))
838 .thenReturn(propertyScopesEither);
840 Response response = target()
843 .accept(MediaType.APPLICATION_JSON)
844 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
847 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
851 public void propertyScopesExceptionDuringProcessingTest() {
852 String path = "/v1/propertyScopes";
853 when(elementBusinessLogic.getAllPropertyScopes(designerUser.getUserId()))
854 .thenThrow(new RuntimeException("Test exception: propertyScopes"));
856 Response response = target()
859 .accept(MediaType.APPLICATION_JSON)
860 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
863 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
867 public void propertyScopesTest() {
868 String path = "/v1/propertyScopes";
869 Either<List<PropertyScope>, ActionStatus> propertyScopesEither = Either.left(new ArrayList<>());
870 when(elementBusinessLogic.getAllPropertyScopes(designerUser.getUserId()))
871 .thenReturn(propertyScopesEither);
873 Response response = target()
876 .accept(MediaType.APPLICATION_JSON)
877 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
880 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
884 public void artifactTypesNoartifactTypesFoundTest() {
885 String path = "/v1/artifactTypes";
886 Either<List<ArtifactType>, ActionStatus> artifactTypesEither = Either.right(ActionStatus.NO_CONTENT);
888 when(elementBusinessLogic.getAllArtifactTypes(designerUser.getUserId()))
889 .thenReturn(artifactTypesEither);
891 Response response = target()
894 .accept(MediaType.APPLICATION_JSON)
895 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
898 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
902 public void artifactTypesExceptionDuringProcessingTest() {
903 String path = "/v1/artifactTypes";
904 when(elementBusinessLogic.getAllArtifactTypes(designerUser.getUserId()))
905 .thenThrow(new RuntimeException("Test exception: artifactTypes"));
907 Response response = target()
910 .accept(MediaType.APPLICATION_JSON)
911 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
914 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
918 public void artifactTypesTest() {
919 String path = "/v1/artifactTypes";
920 Either<List<ArtifactType>, ActionStatus> artifactTypesEither = Either.left(new ArrayList<>());
921 when(elementBusinessLogic.getAllArtifactTypes(designerUser.getUserId()))
922 .thenReturn(artifactTypesEither);
924 Response response = target()
927 .accept(MediaType.APPLICATION_JSON)
928 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
931 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
935 public void configurationNoConfigurationFoundTest() {
936 String path = "/v1/configuration/ui";
938 Either<List<ArtifactType>, ActionStatus> otherEither = Either.left(new ArrayList<>());
939 Either<Integer, ActionStatus> defaultHeatTimeoutEither = Either.left(1);
940 Either<Map<String, Object>, ActionStatus> deploymentEither = Either.left(new HashMap<>());
941 Either<Map<String, String>, ActionStatus> resourceTypesMapEither = Either.left(new HashMap<>());
943 when(elementBusinessLogic.getAllArtifactTypes(designerUser.getUserId()))
944 .thenReturn(otherEither);
945 when(elementBusinessLogic.getDefaultHeatTimeout())
946 .thenReturn(defaultHeatTimeoutEither);
947 when(elementBusinessLogic.getAllDeploymentArtifactTypes())
948 .thenReturn(deploymentEither);
949 when(elementBusinessLogic.getResourceTypesMap())
950 .thenReturn(resourceTypesMapEither);
952 Response response = target()
955 .accept(MediaType.APPLICATION_JSON)
956 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
959 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
963 public void configurationExceptionDuringProcessingTest() {
964 String path = "/v1/configuration/ui";
965 when(elementBusinessLogic.getAllArtifactTypes(designerUser.getUserId()))
966 .thenThrow(new RuntimeException("Test exception: artifactTypes"));
968 Response response = target()
971 .accept(MediaType.APPLICATION_JSON)
972 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
975 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
979 public void screenNoCatalogComponentsFoundTest() {
980 String path = "/v1/screen";
982 Either<Map<String, List<CatalogComponent>>, ResponseFormat> screenEither = Either.right(badRequestResponseFormat);
983 when(elementBusinessLogic.getCatalogComponents(eq(designerUser.getUserId()), any()))
984 .thenReturn(screenEither);
986 Response response = target()
989 .accept(MediaType.APPLICATION_JSON)
990 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
993 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_BAD_REQUEST);
997 public void screenExceptionDuringProcessingTest() {
998 String path = "/v1/screen";
1000 when(elementBusinessLogic.getCatalogComponents(eq(designerUser.getUserId()), any()))
1001 .thenThrow(new RuntimeException("Test exception: screen"));
1003 Response response = target()
1006 .accept(MediaType.APPLICATION_JSON)
1007 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
1010 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
1014 public void screenTest() {
1015 String path = "/v1/screen";
1017 Either<Map<String, List<CatalogComponent>>, ResponseFormat> screenEither = Either.left(new HashMap<>());
1018 when(elementBusinessLogic.getCatalogComponents(eq(designerUser.getUserId()), any()))
1019 .thenReturn(screenEither);
1021 Response response = target()
1024 .accept(MediaType.APPLICATION_JSON)
1025 .header(Constants.USER_ID_HEADER, designerUser.getUserId())
1028 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
1032 protected Application configure() {
1033 ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
1034 return new ResourceConfig(ElementServlet.class)
1035 .register(new AbstractBinder() {
1038 protected void configure() {
1039 bind(request).to(HttpServletRequest.class);
1040 bind(userBusinessLogic).to(UserBusinessLogic.class);
1041 bind(componentUtils).to(ComponentsUtils.class);
1042 bind(componentsCleanBusinessLogic).to(ComponentsCleanBusinessLogic.class);
1043 bind(elementBusinessLogic).to(ElementBusinessLogic.class);
1046 .property("contextConfig", context);