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 fj.data.Either;
24 import org.apache.tinkerpop.gremlin.structure.T;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.openecomp.sdc.be.config.ArtifactConfiguration;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.dao.impl.HealingPipelineDao;
31 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient;
32 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
33 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
34 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
35 import org.openecomp.sdc.be.dao.jsongraph.HealingJanusGraphDao;
36 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
37 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
38 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
39 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
40 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
41 import org.openecomp.sdc.be.model.ArtifactType;
42 import org.openecomp.sdc.be.model.BaseType;
43 import org.openecomp.sdc.be.model.LifecycleStateEnum;
44 import org.openecomp.sdc.be.model.ModelTestBase;
45 import org.openecomp.sdc.be.model.PropertyScope;
46 import org.openecomp.sdc.be.model.Tag;
47 import org.openecomp.sdc.be.model.category.CategoryDefinition;
48 import org.openecomp.sdc.be.model.category.GroupingDefinition;
49 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
50 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
51 import org.openecomp.sdc.be.model.operations.impl.util.OperationTestsUtil;
52 import org.openecomp.sdc.be.resources.data.category.CategoryData;
53 import org.springframework.test.context.ContextConfiguration;
54 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
56 import java.util.ArrayList;
57 import java.util.Collections;
58 import java.util.EnumMap;
59 import java.util.HashMap;
60 import java.util.List;
63 import static org.junit.Assert.assertEquals;
64 import static org.junit.Assert.assertNotNull;
65 import static org.junit.Assert.assertTrue;
66 import static org.mockito.Mockito.*;
68 @RunWith(SpringJUnit4ClassRunner.class)
69 @ContextConfiguration("classpath:application-context-test.xml")
70 public class ElementOperationTest extends ModelTestBase {
72 @javax.annotation.Resource(name = "element-operation")
73 private ElementOperation elementOperation;
75 @javax.annotation.Resource(name = "janusgraph-generic-dao")
76 private JanusGraphGenericDao janusGraphDao;
78 private static String CATEGORY = "category";
79 private static String SUBCATEGORY = "subcategory";
82 public static void setupBeforeClass() {
87 public void testGetArtifactsTypes() {
88 final List<ArtifactConfiguration> expectedArtifactConfigurationList = new ArrayList<>();
89 final ArtifactConfiguration artifactConfiguration1 = new ArtifactConfiguration();
90 artifactConfiguration1.setType("type1");
91 expectedArtifactConfigurationList.add(artifactConfiguration1);
92 final ArtifactConfiguration artifactConfiguration2 = new ArtifactConfiguration();
93 artifactConfiguration2.setType("type2");
94 expectedArtifactConfigurationList.add(artifactConfiguration2);
95 final ArtifactConfiguration artifactConfiguration3 = new ArtifactConfiguration();
96 artifactConfiguration3.setType("type3");
97 expectedArtifactConfigurationList.add(artifactConfiguration3);
98 configurationManager.getConfiguration().setArtifacts(expectedArtifactConfigurationList);
100 List<ArtifactType> actualArtifactTypes = elementOperation.getAllArtifactTypes();
101 assertNotNull(actualArtifactTypes);
102 assertEquals(expectedArtifactConfigurationList.size(), actualArtifactTypes.size());
103 boolean allMatch = actualArtifactTypes.stream().allMatch(artifactType ->
104 expectedArtifactConfigurationList.stream()
105 .anyMatch(artifactConfiguration -> artifactConfiguration.getType().equals(artifactType.getName()))
107 assertTrue(allMatch);
109 expectedArtifactConfigurationList.remove(0);
110 actualArtifactTypes = elementOperation.getAllArtifactTypes();
111 assertNotNull(actualArtifactTypes);
112 assertEquals(expectedArtifactConfigurationList.size(), actualArtifactTypes.size());
114 allMatch = actualArtifactTypes.stream().allMatch(artifactType ->
115 expectedArtifactConfigurationList.stream()
116 .anyMatch(artifactConfiguration -> artifactConfiguration.getType().equals(artifactType.getName()))
118 assertTrue(allMatch);
122 public void testGetResourceAndServiceCategoty() {
123 String id = OperationTestsUtil.deleteAndCreateResourceCategory(CATEGORY, SUBCATEGORY, janusGraphDao);
125 Either<CategoryDefinition, ActionStatus> res = elementOperation.getCategory(NodeTypeEnum.ResourceNewCategory, id);
126 assertTrue(res.isLeft());
127 CategoryDefinition categoryDefinition = (CategoryDefinition) res.left().value();
128 assertEquals(CATEGORY, categoryDefinition.getName());
129 assertEquals(SUBCATEGORY, categoryDefinition.getSubcategories().get(0).getName());
131 id = OperationTestsUtil.deleteAndCreateServiceCategory(CATEGORY, janusGraphDao);
133 res = elementOperation.getCategory(NodeTypeEnum.ServiceNewCategory, id);
134 assertTrue(res.isLeft());
135 categoryDefinition = (CategoryDefinition) res.left().value();
136 assertEquals(CATEGORY, categoryDefinition.getName());
139 private ElementOperation createTestSubject() {
140 return new ElementOperation(new JanusGraphGenericDao(new JanusGraphClient()), new HealingJanusGraphDao(new HealingPipelineDao(), new JanusGraphClient()));
145 public void testGetAllServiceCategories() throws Exception {
146 ElementOperation testSubject;
147 Either<List<CategoryDefinition>, ActionStatus> result;
150 testSubject = createTestSubject();
151 result = testSubject.getAllServiceCategories();
156 public void testGetAllResourceCategories() throws Exception {
157 ElementOperation testSubject;
158 Either<List<CategoryDefinition>, ActionStatus> result;
161 testSubject = createTestSubject();
162 result = testSubject.getAllResourceCategories();
167 public void testGetAllProductCategories() throws Exception {
168 ElementOperation testSubject;
169 Either<List<CategoryDefinition>, ActionStatus> result;
172 testSubject = createTestSubject();
173 result = testSubject.getAllProductCategories();
178 public void testCreateCategory() throws Exception {
179 ElementOperation testSubject;
180 CategoryDefinition category = new CategoryDefinition();
181 NodeTypeEnum nodeType = NodeTypeEnum.AdditionalInfoParameters;
182 Either<CategoryDefinition, ActionStatus> result;
185 testSubject = createTestSubject();
186 result = testSubject.createCategory(category, nodeType);
191 public void testCreateCategory_1() throws Exception {
192 ElementOperation testSubject;
193 CategoryDefinition category = new CategoryDefinition();
194 NodeTypeEnum nodeType = NodeTypeEnum.ArtifactRef;
195 boolean inTransaction = false;
196 Either<CategoryDefinition, ActionStatus> result;
199 testSubject = createTestSubject();
200 result = testSubject.createCategory(category, nodeType, inTransaction);
205 public void testCreateSubCategory() throws Exception {
206 ElementOperation testSubject;
207 String categoryId = "";
208 SubCategoryDefinition subCategory = null;
209 NodeTypeEnum nodeType = null;
210 Either<SubCategoryDefinition, ActionStatus> result;
213 testSubject = createTestSubject();
214 result = testSubject.createSubCategory(categoryId, subCategory, nodeType);
219 public void testCreateSubCategory_1() throws Exception {
220 ElementOperation testSubject;
221 String categoryId = "";
222 SubCategoryDefinition subCategory = null;
223 NodeTypeEnum nodeType = null;
224 boolean inTransaction = false;
225 Either<SubCategoryDefinition, ActionStatus> result;
228 testSubject = createTestSubject();
229 result = testSubject.createSubCategory(categoryId, subCategory, nodeType, inTransaction);
234 public void testCreateGrouping() throws Exception {
235 ElementOperation testSubject;
236 String subCategoryId = "";
237 GroupingDefinition grouping = null;
238 NodeTypeEnum nodeType = null;
239 Either<GroupingDefinition, ActionStatus> result;
242 testSubject = createTestSubject();
243 result = testSubject.createGrouping(subCategoryId, grouping, nodeType);
248 public void testGetAllCategories() throws Exception {
249 ElementOperation testSubject;
250 NodeTypeEnum nodeType = NodeTypeEnum.Capability;
251 boolean inTransaction = false;
252 Either<List<CategoryDefinition>, ActionStatus> result;
255 testSubject = createTestSubject();
256 result = testSubject.getAllCategories(nodeType, inTransaction);
268 public void testGetCategory() throws Exception {
269 ElementOperation testSubject;
270 NodeTypeEnum nodeType = NodeTypeEnum.CapabilityType;
271 String categoryId = "";
272 Either<CategoryDefinition, ActionStatus> result;
275 testSubject = createTestSubject();
276 result = testSubject.getCategory(nodeType, categoryId);
281 public void testGetSubCategory() throws Exception {
282 ElementOperation testSubject;
283 NodeTypeEnum nodeType = NodeTypeEnum.Group;
284 String subCategoryId = "";
285 Either<SubCategoryDefinition, ActionStatus> result;
288 testSubject = createTestSubject();
289 result = testSubject.getSubCategory(nodeType, subCategoryId);
294 public void testDeleteCategory() throws Exception {
295 ElementOperation testSubject;
296 NodeTypeEnum nodeType = NodeTypeEnum.getByName("resource");
297 String categoryId = "";
298 Either<CategoryDefinition, ActionStatus> result;
301 testSubject = createTestSubject();
302 result = testSubject.deleteCategory(nodeType, categoryId);
307 public void testDeleteSubCategory() throws Exception {
308 ElementOperation testSubject;
309 NodeTypeEnum nodeType = NodeTypeEnum.Attribute;
310 String subCategoryId = "";
311 Either<SubCategoryDefinition, ActionStatus> result;
314 testSubject = createTestSubject();
315 result = testSubject.deleteSubCategory(nodeType, subCategoryId);
320 public void testDeleteGrouping() throws Exception {
321 ElementOperation testSubject;
322 NodeTypeEnum nodeType = NodeTypeEnum.DataType;
323 String groupingId = "";
324 Either<GroupingDefinition, ActionStatus> result;
327 testSubject = createTestSubject();
328 result = testSubject.deleteGrouping(nodeType, groupingId);
333 public void testIsCategoryUniqueForType() throws Exception {
334 ElementOperation testSubject;
335 NodeTypeEnum nodeType = null;
336 String normalizedName = "";
337 Either<Boolean, ActionStatus> result;
340 testSubject = createTestSubject();
341 result = testSubject.isCategoryUniqueForType(nodeType, normalizedName);
346 public void testIsSubCategoryUniqueForCategory() throws Exception {
347 ElementOperation testSubject;
348 NodeTypeEnum nodeType = null;
349 String subCategoryNormName = "";
350 String parentCategoryId = "";
351 Either<Boolean, ActionStatus> result;
354 testSubject = createTestSubject();
355 result = testSubject.isSubCategoryUniqueForCategory(nodeType, subCategoryNormName, parentCategoryId);
360 public void testIsGroupingUniqueForSubCategory() throws Exception {
361 ElementOperation testSubject;
362 NodeTypeEnum nodeType = null;
363 String groupingNormName = "";
364 String parentSubCategoryId = "";
365 Either<Boolean, ActionStatus> result;
368 testSubject = createTestSubject();
369 result = testSubject.isGroupingUniqueForSubCategory(nodeType, groupingNormName, parentSubCategoryId);
374 public void testGetSubCategoryUniqueForType() throws Exception {
375 ElementOperation testSubject;
376 NodeTypeEnum nodeType = null;
377 String normalizedName = "";
378 Either<SubCategoryDefinition, ActionStatus> result;
381 testSubject = createTestSubject();
382 result = testSubject.getSubCategoryUniqueForType(nodeType, normalizedName);
387 public void testGetGroupingUniqueForType() throws Exception {
388 ElementOperation testSubject;
389 NodeTypeEnum nodeType = null;
390 String groupingNormalizedName = "";
391 Either<GroupingDefinition, ActionStatus> result;
394 testSubject = createTestSubject();
395 result = testSubject.getGroupingUniqueForType(nodeType, groupingNormalizedName);
400 public void testGetAllTags() throws Exception {
401 ElementOperation testSubject;
402 Either<List<Tag>, ActionStatus> result;
405 testSubject = createTestSubject();
406 result = testSubject.getAllTags();
411 public void testGetCategoryData() throws Exception {
412 ElementOperation testSubject;
414 NodeTypeEnum type = NodeTypeEnum.DataType;
415 Class<T> clazz = null;
416 Either<org.openecomp.sdc.be.resources.data.CategoryData, StorageOperationStatus> result;
419 testSubject = createTestSubject();
421 result = testSubject.getCategoryData(name, type, null);
424 testSubject = createTestSubject();
426 result = testSubject.getCategoryData(name, type, null);
434 public void testGetAllPropertyScopes() throws Exception {
435 ElementOperation testSubject;
436 Either<List<PropertyScope>, ActionStatus> result;
439 testSubject = createTestSubject();
440 result = testSubject.getAllPropertyScopes();
444 public void testGetResourceTypesMap() throws Exception {
445 ElementOperation testSubject;
446 Either<Map<String, String>, ActionStatus> result;
449 testSubject = createTestSubject();
450 result = testSubject.getResourceTypesMap();
454 public void testGetNewCategoryData() throws Exception {
455 ElementOperation testSubject;
457 NodeTypeEnum type = NodeTypeEnum.HeatParameter;
458 Class<T> clazz = null;
459 Either<CategoryData, StorageOperationStatus> result;
462 testSubject = createTestSubject();
464 result = testSubject.getNewCategoryData(name, type, null);
467 testSubject = createTestSubject();
469 result = testSubject.getNewCategoryData(name, type, null);
473 public void testBaseTypes_serviceSpecific() {
474 Map<String, String> preExistingServiceNodeTypes = configurationManager.getConfiguration().getServiceNodeTypes();
475 Map<String, String> preExistingGenericNodeTypes =
476 configurationManager.getConfiguration().getGenericAssetNodeTypes();
479 Map<String, String> serviceNodeTypes = new HashMap<>();
480 serviceNodeTypes.put("serviceCategoryA", "org.base.type");
481 configurationManager.getConfiguration().setServiceNodeTypes(serviceNodeTypes);
483 Map<String, String> genericNodeTypes = new HashMap<>();
484 genericNodeTypes.put("service", "org.service.default");
485 configurationManager.getConfiguration().setGenericAssetNodeTypes(genericNodeTypes);
487 HealingJanusGraphDao healingJanusGraphDao = mock(HealingJanusGraphDao.class);
488 ElementOperation elementOperation =
489 new ElementOperation(new JanusGraphGenericDao(new JanusGraphClient()), healingJanusGraphDao);
491 GraphVertex baseTypeVertex = mock(GraphVertex.class);
492 when(baseTypeVertex.getMetadataProperty(GraphPropertyEnum.VERSION)).thenReturn("1.0");
493 when(healingJanusGraphDao.getByCriteria(any(), any(), any()))
494 .thenReturn(Either.left(Collections.singletonList(baseTypeVertex)));
496 GraphVertex derivedTypeVertex = mock(GraphVertex.class);
497 when(derivedTypeVertex.getMetadataProperty(GraphPropertyEnum.STATE)).thenReturn(LifecycleStateEnum.CERTIFIED.name());
498 when(derivedTypeVertex.getMetadataProperty(GraphPropertyEnum.VERSION)).thenReturn("1.0");
500 GraphVertex derivedTypeVertexUncertified = mock(GraphVertex.class);
501 when(derivedTypeVertexUncertified.getMetadataProperty(GraphPropertyEnum.STATE)).thenReturn(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN.name());
502 when(derivedTypeVertexUncertified.getMetadataProperty(GraphPropertyEnum.VERSION)).thenReturn("1.1");
504 when(healingJanusGraphDao.getParentVertices(baseTypeVertex, EdgeLabelEnum.DERIVED_FROM,
505 JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(Collections.singletonList(derivedTypeVertex)));
506 when(healingJanusGraphDao.getParentVertices(derivedTypeVertex, EdgeLabelEnum.DERIVED_FROM,
507 JsonParseFlagEnum.ParseAll)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
508 when(derivedTypeVertex.getMetadataProperty(GraphPropertyEnum.TOSCA_RESOURCE_NAME))
509 .thenReturn("org.parent.type");
511 List<BaseType> baseTypes = elementOperation.getBaseTypes("serviceCategoryA");
513 assertEquals(2, baseTypes.size());
514 assertEquals("org.base.type", baseTypes.get(0).getToscaResourceName());
515 assertEquals(1, baseTypes.get(0).getVersions().size());
516 assertEquals("1.0", baseTypes.get(0).getVersions().get(0));
517 assertEquals("org.parent.type", baseTypes.get(1).getToscaResourceName());
519 configurationManager.getConfiguration().setServiceNodeTypes(preExistingServiceNodeTypes);
520 configurationManager.getConfiguration().setGenericAssetNodeTypes(preExistingGenericNodeTypes);
525 public void testBaseTypes_default() {
526 Map<String, String> preExistingServiceNodeTypes = configurationManager.getConfiguration().getServiceNodeTypes();
527 Map<String, String> preExistingGenericNodeTypes =
528 configurationManager.getConfiguration().getGenericAssetNodeTypes();
531 Map<String, String> genericNodeTypes = new HashMap<>();
532 genericNodeTypes.put("Service", "org.service.default");
533 configurationManager.getConfiguration().setGenericAssetNodeTypes(genericNodeTypes);
534 configurationManager.getConfiguration().setServiceNodeTypes(null);
536 HealingJanusGraphDao healingJanusGraphDao = mock(HealingJanusGraphDao.class);
537 ElementOperation elementOperation =
538 new ElementOperation(new JanusGraphGenericDao(new JanusGraphClient()), healingJanusGraphDao);
540 GraphVertex baseTypeVertex = mock(GraphVertex.class);
541 when(baseTypeVertex.getMetadataProperty(GraphPropertyEnum.VERSION)).thenReturn("1.0");
542 when(healingJanusGraphDao.getByCriteria(any(), any(), any()))
543 .thenReturn(Either.left(Collections.singletonList(baseTypeVertex)));
545 when(healingJanusGraphDao.getParentVertices(baseTypeVertex, EdgeLabelEnum.DERIVED_FROM,
546 JsonParseFlagEnum.ParseAll)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
548 List<BaseType> baseTypes = elementOperation.getBaseTypes("serviceCategoryA");
550 assertEquals(1, baseTypes.size());
551 assertEquals("org.service.default", baseTypes.get(0).getToscaResourceName());
552 assertEquals(1, baseTypes.get(0).getVersions().size());
554 configurationManager.getConfiguration().setServiceNodeTypes(preExistingServiceNodeTypes);
555 configurationManager.getConfiguration().setGenericAssetNodeTypes(preExistingGenericNodeTypes);