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=========================================================
21 package org.openecomp.sdc.be.model.operations.impl;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.anyMap;
27 import static org.mockito.Mockito.any;
28 import static org.mockito.Mockito.eq;
29 import static org.mockito.Mockito.isNull;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
33 import fj.data.Either;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
39 import org.apache.tinkerpop.gremlin.structure.T;
40 import org.junit.jupiter.api.BeforeAll;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.mockito.InjectMocks;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.openecomp.sdc.be.config.ArtifactConfiguration;
47 import org.openecomp.sdc.be.config.CategoryBaseTypeConfig;
48 import org.openecomp.sdc.be.dao.api.ActionStatus;
49 import org.openecomp.sdc.be.dao.impl.HealingPipelineDao;
50 import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphDao;
51 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient;
52 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
53 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
54 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
55 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
56 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
57 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
58 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
59 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
60 import org.openecomp.sdc.be.model.ArtifactType;
61 import org.openecomp.sdc.be.model.BaseType;
62 import org.openecomp.sdc.be.model.LifecycleStateEnum;
63 import org.openecomp.sdc.be.model.ModelTestBase;
64 import org.openecomp.sdc.be.model.PropertyScope;
65 import org.openecomp.sdc.be.model.Tag;
66 import org.openecomp.sdc.be.model.category.CategoryDefinition;
67 import org.openecomp.sdc.be.model.category.GroupingDefinition;
68 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
69 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
70 import org.openecomp.sdc.be.model.operations.impl.util.OperationTestsUtil;
71 import org.openecomp.sdc.be.resources.data.category.CategoryData;
72 import org.springframework.test.context.ContextConfiguration;
74 @ContextConfiguration("classpath:application-context-test.xml")
75 public class ElementOperationTest extends ModelTestBase {
78 private ElementOperation elementOperation;
81 private JanusGraphGenericDao janusGraphDao;
83 private HealingJanusGraphDao healingJanusGraphDao;
85 private static final String CATEGORY = "category";
86 private static final String SUBCATEGORY = "subcategory";
89 public static void setupBeforeClass() {
94 void beforeEachInit() {
95 MockitoAnnotations.openMocks(this);
99 public void testGetArtifactsTypes() {
100 final List<ArtifactConfiguration> expectedArtifactConfigurationList = new ArrayList<>();
101 final ArtifactConfiguration artifactConfiguration1 = new ArtifactConfiguration();
102 artifactConfiguration1.setType("type1");
103 expectedArtifactConfigurationList.add(artifactConfiguration1);
104 final ArtifactConfiguration artifactConfiguration2 = new ArtifactConfiguration();
105 artifactConfiguration2.setType("type2");
106 expectedArtifactConfigurationList.add(artifactConfiguration2);
107 final ArtifactConfiguration artifactConfiguration3 = new ArtifactConfiguration();
108 artifactConfiguration3.setType("type3");
109 expectedArtifactConfigurationList.add(artifactConfiguration3);
110 configurationManager.getConfiguration().setArtifacts(expectedArtifactConfigurationList);
112 List<ArtifactType> actualArtifactTypes = elementOperation.getAllArtifactTypes();
113 assertNotNull(actualArtifactTypes);
114 assertEquals(expectedArtifactConfigurationList.size(), actualArtifactTypes.size());
115 boolean allMatch = actualArtifactTypes.stream().allMatch(artifactType ->
116 expectedArtifactConfigurationList.stream()
117 .anyMatch(artifactConfiguration -> artifactConfiguration.getType().equals(artifactType.getName()))
119 assertTrue(allMatch);
121 expectedArtifactConfigurationList.remove(0);
122 actualArtifactTypes = elementOperation.getAllArtifactTypes();
123 assertNotNull(actualArtifactTypes);
124 assertEquals(expectedArtifactConfigurationList.size(), actualArtifactTypes.size());
126 allMatch = actualArtifactTypes.stream().allMatch(artifactType ->
127 expectedArtifactConfigurationList.stream()
128 .anyMatch(artifactConfiguration -> artifactConfiguration.getType().equals(artifactType.getName()))
130 assertTrue(allMatch);
134 public void testGetResourceAndServiceCategoty() {
135 String id = OperationTestsUtil.deleteAndCreateResourceCategory(CATEGORY, SUBCATEGORY, janusGraphDao);
137 Either<CategoryDefinition, ActionStatus> res = elementOperation.getCategory(NodeTypeEnum.ResourceNewCategory, id);
138 assertTrue(res.isLeft());
139 CategoryDefinition categoryDefinition = (CategoryDefinition) res.left().value();
140 assertEquals(CATEGORY, categoryDefinition.getName());
141 assertEquals(SUBCATEGORY, categoryDefinition.getSubcategories().get(0).getName());
143 id = OperationTestsUtil.deleteAndCreateServiceCategory(CATEGORY, janusGraphDao);
145 res = elementOperation.getCategory(NodeTypeEnum.ServiceNewCategory, id);
146 assertTrue(res.isLeft());
147 categoryDefinition = (CategoryDefinition) res.left().value();
148 assertEquals(CATEGORY, categoryDefinition.getName());
151 private ElementOperation createTestSubject() {
152 return new ElementOperation(new JanusGraphGenericDao(new JanusGraphClient()),
153 new HealingJanusGraphDao(new HealingPipelineDao(), new JanusGraphClient()));
157 public void testGetAllServiceCategories() throws Exception {
158 ElementOperation testSubject;
159 Either<List<CategoryDefinition>, ActionStatus> result;
162 testSubject = createTestSubject();
163 result = testSubject.getAllServiceCategories();
167 public void testGetAllResourceCategories() throws Exception {
168 ElementOperation testSubject;
169 Either<List<CategoryDefinition>, ActionStatus> result;
172 testSubject = createTestSubject();
173 result = testSubject.getAllResourceCategories();
177 public void testGetAllProductCategories() throws Exception {
178 ElementOperation testSubject;
179 Either<List<CategoryDefinition>, ActionStatus> result;
182 testSubject = createTestSubject();
183 result = testSubject.getAllProductCategories();
187 public void testCreateCategory() throws Exception {
188 ElementOperation testSubject;
189 CategoryDefinition category = new CategoryDefinition();
190 NodeTypeEnum nodeType = NodeTypeEnum.AdditionalInfoParameters;
191 Either<CategoryDefinition, ActionStatus> result;
194 testSubject = createTestSubject();
195 result = testSubject.createCategory(category, nodeType);
199 public void testCreateCategory_1() throws Exception {
200 ElementOperation testSubject;
201 CategoryDefinition category = new CategoryDefinition();
202 NodeTypeEnum nodeType = NodeTypeEnum.ArtifactRef;
203 boolean inTransaction = false;
204 Either<CategoryDefinition, ActionStatus> result;
207 testSubject = createTestSubject();
208 result = testSubject.createCategory(category, nodeType, inTransaction);
212 public void testCreateSubCategory() throws Exception {
213 ElementOperation testSubject;
214 String categoryId = "";
215 SubCategoryDefinition subCategory = null;
216 NodeTypeEnum nodeType = null;
217 Either<SubCategoryDefinition, ActionStatus> result;
220 testSubject = createTestSubject();
221 result = testSubject.createSubCategory(categoryId, subCategory, nodeType);
225 public void testCreateSubCategory_1() throws Exception {
226 ElementOperation testSubject;
227 String categoryId = "";
228 SubCategoryDefinition subCategory = null;
229 NodeTypeEnum nodeType = null;
230 boolean inTransaction = false;
231 Either<SubCategoryDefinition, ActionStatus> result;
234 testSubject = createTestSubject();
235 result = testSubject.createSubCategory(categoryId, subCategory, nodeType, inTransaction);
239 public void testCreateGrouping() throws Exception {
240 ElementOperation testSubject;
241 String subCategoryId = "";
242 GroupingDefinition grouping = null;
243 NodeTypeEnum nodeType = null;
244 Either<GroupingDefinition, ActionStatus> result;
247 testSubject = createTestSubject();
248 result = testSubject.createGrouping(subCategoryId, grouping, nodeType);
252 public void testGetAllCategories() throws Exception {
253 ElementOperation testSubject;
254 NodeTypeEnum nodeType = NodeTypeEnum.Capability;
255 boolean inTransaction = false;
256 Either<List<CategoryDefinition>, ActionStatus> result;
259 testSubject = createTestSubject();
260 result = testSubject.getAllCategories(nodeType, inTransaction);
264 public void testGetCategory() throws Exception {
265 ElementOperation testSubject;
266 NodeTypeEnum nodeType = NodeTypeEnum.CapabilityType;
267 String categoryId = "";
268 Either<CategoryDefinition, ActionStatus> result;
271 testSubject = createTestSubject();
272 result = testSubject.getCategory(nodeType, categoryId);
276 public void testGetSubCategory() throws Exception {
277 ElementOperation testSubject;
278 NodeTypeEnum nodeType = NodeTypeEnum.Group;
279 String subCategoryId = "";
280 Either<SubCategoryDefinition, ActionStatus> result;
283 testSubject = createTestSubject();
284 result = testSubject.getSubCategory(nodeType, subCategoryId);
288 public void testDeleteCategory() throws Exception {
289 ElementOperation testSubject;
290 NodeTypeEnum nodeType = NodeTypeEnum.getByName("resource");
291 String categoryId = "";
292 Either<CategoryDefinition, ActionStatus> result;
295 testSubject = createTestSubject();
296 result = testSubject.deleteCategory(nodeType, categoryId);
300 public void testDeleteSubCategory() throws Exception {
301 ElementOperation testSubject;
302 NodeTypeEnum nodeType = NodeTypeEnum.Attribute;
303 String subCategoryId = "";
304 Either<SubCategoryDefinition, ActionStatus> result;
307 testSubject = createTestSubject();
308 result = testSubject.deleteSubCategory(nodeType, subCategoryId);
312 public void testDeleteGrouping() throws Exception {
313 ElementOperation testSubject;
314 NodeTypeEnum nodeType = NodeTypeEnum.DataType;
315 String groupingId = "";
316 Either<GroupingDefinition, ActionStatus> result;
319 testSubject = createTestSubject();
320 result = testSubject.deleteGrouping(nodeType, groupingId);
324 public void testIsCategoryUniqueForType() throws Exception {
325 ElementOperation testSubject;
326 NodeTypeEnum nodeType = null;
327 String normalizedName = "";
328 Either<Boolean, ActionStatus> result;
331 testSubject = createTestSubject();
332 result = testSubject.isCategoryUniqueForType(nodeType, normalizedName);
336 public void testIsSubCategoryUniqueForCategory() throws Exception {
337 ElementOperation testSubject;
338 NodeTypeEnum nodeType = null;
339 String subCategoryNormName = "";
340 String parentCategoryId = "";
341 Either<Boolean, ActionStatus> result;
344 testSubject = createTestSubject();
345 result = testSubject.isSubCategoryUniqueForCategory(nodeType, subCategoryNormName, parentCategoryId);
349 public void testIsGroupingUniqueForSubCategory() throws Exception {
350 ElementOperation testSubject;
351 NodeTypeEnum nodeType = null;
352 String groupingNormName = "";
353 String parentSubCategoryId = "";
354 Either<Boolean, ActionStatus> result;
357 testSubject = createTestSubject();
358 result = testSubject.isGroupingUniqueForSubCategory(nodeType, groupingNormName, parentSubCategoryId);
362 public void testGetSubCategoryUniqueForType() throws Exception {
363 ElementOperation testSubject;
364 NodeTypeEnum nodeType = null;
365 String normalizedName = "";
366 Either<SubCategoryDefinition, ActionStatus> result;
369 testSubject = createTestSubject();
370 result = testSubject.getSubCategoryUniqueForType(nodeType, normalizedName);
374 public void testGetGroupingUniqueForType() throws Exception {
375 ElementOperation testSubject;
376 NodeTypeEnum nodeType = null;
377 String groupingNormalizedName = "";
378 Either<GroupingDefinition, ActionStatus> result;
381 testSubject = createTestSubject();
382 result = testSubject.getGroupingUniqueForType(nodeType, groupingNormalizedName);
386 public void testGetAllTags() throws Exception {
387 ElementOperation testSubject;
388 Either<List<Tag>, ActionStatus> result;
391 testSubject = createTestSubject();
392 result = testSubject.getAllTags();
396 public void testGetCategoryData() throws Exception {
397 ElementOperation testSubject;
399 NodeTypeEnum type = NodeTypeEnum.DataType;
400 Class<T> clazz = null;
401 Either<org.openecomp.sdc.be.resources.data.CategoryData, StorageOperationStatus> result;
404 testSubject = createTestSubject();
406 result = testSubject.getCategoryData(name, type, null);
409 testSubject = createTestSubject();
411 result = testSubject.getCategoryData(name, type, null);
415 public void testGetAllPropertyScopes() throws Exception {
416 ElementOperation testSubject;
417 Either<List<PropertyScope>, ActionStatus> result;
420 testSubject = createTestSubject();
421 result = testSubject.getAllPropertyScopes();
425 public void testGetResourceTypesMap() throws Exception {
426 ElementOperation testSubject;
427 Either<Map<String, String>, ActionStatus> result;
430 testSubject = createTestSubject();
431 result = testSubject.getResourceTypesMap();
435 public void testGetNewCategoryData() throws Exception {
436 ElementOperation testSubject;
438 NodeTypeEnum type = NodeTypeEnum.HeatParameter;
439 Class<T> clazz = null;
440 Either<CategoryData, StorageOperationStatus> result;
443 testSubject = createTestSubject();
445 result = testSubject.getNewCategoryData(name, type, null);
448 testSubject = createTestSubject();
450 result = testSubject.getNewCategoryData(name, type, null);
454 public void testBaseTypes_serviceSpecific() {
455 Map<String, CategoryBaseTypeConfig> preExistingServiceNodeTypes = configurationManager.getConfiguration().getServiceBaseNodeTypes();
456 Map<String, String> preExistingGenericNodeTypes = configurationManager.getConfiguration().getGenericAssetNodeTypes();
459 final Map<String, CategoryBaseTypeConfig> serviceBaseNodeTypeConfigMap = new HashMap<>();
460 final var categoryBaseTypeConfig = new CategoryBaseTypeConfig();
461 categoryBaseTypeConfig.setBaseTypes(List.of("org.base.type"));
462 serviceBaseNodeTypeConfigMap.put("serviceCategoryA", categoryBaseTypeConfig);
463 configurationManager.getConfiguration().setServiceBaseNodeTypes(serviceBaseNodeTypeConfigMap);
465 Map<String, String> genericNodeTypes = new HashMap<>();
466 genericNodeTypes.put("service", "org.service.default");
467 configurationManager.getConfiguration().setGenericAssetNodeTypes(genericNodeTypes);
469 HealingJanusGraphDao healingJanusGraphDao = mock(HealingJanusGraphDao.class);
470 ElementOperation elementOperation = new ElementOperation(new JanusGraphGenericDao(new JanusGraphClient()), healingJanusGraphDao);
472 GraphVertex baseTypeVertex = mock(GraphVertex.class);
473 when(baseTypeVertex.getMetadataProperty(GraphPropertyEnum.VERSION)).thenReturn("1.0");
474 when(healingJanusGraphDao.getByCriteria(eq(VertexTypeEnum.NODE_TYPE), any(), isNull(), eq(JsonParseFlagEnum.ParseAll), any()))
475 .thenReturn(Either.left(Collections.singletonList(baseTypeVertex)));
477 GraphVertex derivedTypeVertex = mock(GraphVertex.class);
478 when(derivedTypeVertex.getMetadataProperty(GraphPropertyEnum.STATE)).thenReturn(LifecycleStateEnum.CERTIFIED.name());
479 when(derivedTypeVertex.getMetadataProperty(GraphPropertyEnum.VERSION)).thenReturn("1.0");
481 GraphVertex derivedTypeVertexUncertified = mock(GraphVertex.class);
482 when(derivedTypeVertexUncertified.getMetadataProperty(GraphPropertyEnum.STATE)).thenReturn(
483 LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name());
484 when(derivedTypeVertexUncertified.getMetadataProperty(GraphPropertyEnum.VERSION)).thenReturn("1.1");
486 when(healingJanusGraphDao.getParentVertices(baseTypeVertex, EdgeLabelEnum.DERIVED_FROM,
487 JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(Collections.singletonList(derivedTypeVertex)));
488 when(healingJanusGraphDao.getParentVertices(derivedTypeVertex, EdgeLabelEnum.DERIVED_FROM,
489 JsonParseFlagEnum.ParseAll)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
490 when(derivedTypeVertex.getMetadataProperty(GraphPropertyEnum.TOSCA_RESOURCE_NAME)).thenReturn("org.parent.type");
492 List<BaseType> baseTypes = elementOperation.getServiceBaseTypes("serviceCategoryA", null);
494 assertEquals(2, baseTypes.size());
495 assertEquals("org.base.type", baseTypes.get(0).getToscaResourceName());
496 assertEquals(1, baseTypes.get(0).getVersions().size());
497 assertEquals("1.0", baseTypes.get(0).getVersions().get(0));
498 assertEquals("org.parent.type", baseTypes.get(1).getToscaResourceName());
500 configurationManager.getConfiguration().setServiceBaseNodeTypes(preExistingServiceNodeTypes);
501 configurationManager.getConfiguration().setGenericAssetNodeTypes(preExistingGenericNodeTypes);
506 public void testBaseTypes_default() {
507 Map<String, CategoryBaseTypeConfig> preExistingServiceNodeTypes = configurationManager.getConfiguration().getServiceBaseNodeTypes();
508 Map<String, String> preExistingGenericNodeTypes =
509 configurationManager.getConfiguration().getGenericAssetNodeTypes();
512 Map<String, String> genericNodeTypes = new HashMap<>();
513 genericNodeTypes.put("Service", "org.service.default");
514 configurationManager.getConfiguration().setGenericAssetNodeTypes(genericNodeTypes);
515 configurationManager.getConfiguration().setServiceBaseNodeTypes(null);
517 HealingJanusGraphDao healingJanusGraphDao = mock(HealingJanusGraphDao.class);
518 final var elementOperation = new ElementOperation(new JanusGraphGenericDao(new JanusGraphClient()), healingJanusGraphDao);
520 GraphVertex baseTypeVertex = mock(GraphVertex.class);
521 when(baseTypeVertex.getMetadataProperty(GraphPropertyEnum.VERSION)).thenReturn("1.0");
522 when(healingJanusGraphDao.getByCriteria(eq(VertexTypeEnum.NODE_TYPE), any(), isNull(), eq(JsonParseFlagEnum.ParseAll), any()))
523 .thenReturn(Either.left(Collections.singletonList(baseTypeVertex)));
525 when(healingJanusGraphDao.getParentVertices(baseTypeVertex, EdgeLabelEnum.DERIVED_FROM,
526 JsonParseFlagEnum.ParseAll)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
528 List<BaseType> baseTypes = elementOperation.getServiceBaseTypes("serviceCategoryA", null);
530 assertEquals(1, baseTypes.size());
531 assertEquals("org.service.default", baseTypes.get(0).getToscaResourceName());
532 assertEquals(1, baseTypes.get(0).getVersions().size());
534 configurationManager.getConfiguration().setServiceBaseNodeTypes(preExistingServiceNodeTypes);
535 configurationManager.getConfiguration().setGenericAssetNodeTypes(preExistingGenericNodeTypes);
540 public void testGetServiceBaseTypes_categoryWithRequiredBaseType() {
541 defaultBaseTypeMock();
542 final List<BaseType> actualBaseTypeList = elementOperation.getServiceBaseTypes("CategoryA", null);
543 assertEquals(actualBaseTypeList.size(), 1);
544 final BaseType expectedBaseType = actualBaseTypeList.get(0);
545 assertEquals(expectedBaseType.getToscaResourceName(), "org.openecomp.resource.abstract.nodes.A");
550 public void testGetServiceBaseTypes_categoryWithOptionalBaseType() {
551 defaultBaseTypeMock();
552 final List<BaseType> actualBaseTypeList = elementOperation.getServiceBaseTypes("CategoryC", null);
553 assertEquals(actualBaseTypeList.size(), 2);
554 assertEquals(actualBaseTypeList.get(0).getToscaResourceName(), "org.openecomp.resource.abstract.nodes.C1");
555 assertEquals(actualBaseTypeList.get(1).getToscaResourceName(), "org.openecomp.resource.abstract.nodes.C2");
559 public void testGetServiceBaseTypes_categoryWithNoBaseType() {
560 defaultBaseTypeMock();
561 final List<BaseType> actualBaseTypeList = elementOperation.getServiceBaseTypes("CategoryB", null);
562 assertTrue(actualBaseTypeList.isEmpty());
566 public void testGetServiceBaseTypes_notConfiguredCategoryThatFallsBackToGenericType() {
567 defaultBaseTypeMock();
568 final List<BaseType> actualBaseTypeList = elementOperation.getServiceBaseTypes("CategoryD", null);
570 assertEquals(actualBaseTypeList.size(), 1);
571 final BaseType expectedBaseType = actualBaseTypeList.get(0);
572 assertEquals(expectedBaseType.getToscaResourceName(), "org.openecomp.resource.abstract.nodes.service");
575 private void defaultBaseTypeMock() {
576 final GraphVertex baseTypeVertex = mock(GraphVertex.class);
577 when(baseTypeVertex.getMetadataProperty(GraphPropertyEnum.VERSION)).thenReturn("1.0");
578 when(healingJanusGraphDao.getByCriteria(eq(VertexTypeEnum.NODE_TYPE), anyMap(), isNull(), eq(JsonParseFlagEnum.ParseAll), isNull()))
579 .thenReturn(Either.left(Collections.singletonList(baseTypeVertex)));
580 when(healingJanusGraphDao.getParentVertices(eq(baseTypeVertex), eq(EdgeLabelEnum.DERIVED_FROM), eq(JsonParseFlagEnum.ParseAll)))
581 .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));