230fbe1abaef05f2feee0c58f1eddde11b717a3d
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / operations / impl / ElementOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  */
20
21 package org.openecomp.sdc.be.model.operations.impl;
22
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;
55
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;
61 import java.util.Map;
62
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.*;
67
68 @RunWith(SpringJUnit4ClassRunner.class)
69 @ContextConfiguration("classpath:application-context-test.xml")
70 public class ElementOperationTest extends ModelTestBase {
71
72     @javax.annotation.Resource(name = "element-operation")
73     private ElementOperation elementOperation;
74
75     @javax.annotation.Resource(name = "janusgraph-generic-dao")
76     private JanusGraphGenericDao janusGraphDao;
77
78     private static String CATEGORY = "category";
79     private static String SUBCATEGORY = "subcategory";
80
81     @BeforeClass
82     public static void setupBeforeClass() {
83         ModelTestBase.init();
84     }
85
86     @Test
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);
99
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()))
106                 );
107                 assertTrue(allMatch);
108
109         expectedArtifactConfigurationList.remove(0);
110         actualArtifactTypes = elementOperation.getAllArtifactTypes();
111                 assertNotNull(actualArtifactTypes);
112         assertEquals(expectedArtifactConfigurationList.size(), actualArtifactTypes.size());
113
114                 allMatch = actualArtifactTypes.stream().allMatch(artifactType ->
115                         expectedArtifactConfigurationList.stream()
116                                 .anyMatch(artifactConfiguration -> artifactConfiguration.getType().equals(artifactType.getName()))
117                 );
118                 assertTrue(allMatch);
119     }
120
121     // @Test
122     public void testGetResourceAndServiceCategoty() {
123         String id = OperationTestsUtil.deleteAndCreateResourceCategory(CATEGORY, SUBCATEGORY, janusGraphDao);
124
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());
130
131         id = OperationTestsUtil.deleteAndCreateServiceCategory(CATEGORY, janusGraphDao);
132
133         res = elementOperation.getCategory(NodeTypeEnum.ServiceNewCategory, id);
134         assertTrue(res.isLeft());
135         categoryDefinition = (CategoryDefinition) res.left().value();
136         assertEquals(CATEGORY, categoryDefinition.getName());
137         }
138
139         private ElementOperation createTestSubject() {
140                 return new ElementOperation(new JanusGraphGenericDao(new JanusGraphClient()), new HealingJanusGraphDao(new HealingPipelineDao(), new JanusGraphClient()));
141         }
142
143         
144         @Test
145         public void testGetAllServiceCategories() throws Exception {
146                 ElementOperation testSubject;
147                 Either<List<CategoryDefinition>, ActionStatus> result;
148
149                 // default test
150                 testSubject = createTestSubject();
151                 result = testSubject.getAllServiceCategories();
152         }
153
154         
155         @Test
156         public void testGetAllResourceCategories() throws Exception {
157                 ElementOperation testSubject;
158                 Either<List<CategoryDefinition>, ActionStatus> result;
159
160                 // default test
161                 testSubject = createTestSubject();
162                 result = testSubject.getAllResourceCategories();
163         }
164
165         
166         @Test
167         public void testGetAllProductCategories() throws Exception {
168                 ElementOperation testSubject;
169                 Either<List<CategoryDefinition>, ActionStatus> result;
170
171                 // default test
172                 testSubject = createTestSubject();
173                 result = testSubject.getAllProductCategories();
174         }
175
176         
177         @Test
178         public void testCreateCategory() throws Exception {
179                 ElementOperation testSubject;
180                 CategoryDefinition category = new CategoryDefinition();
181                 NodeTypeEnum nodeType = NodeTypeEnum.AdditionalInfoParameters;
182                 Either<CategoryDefinition, ActionStatus> result;
183
184                 // default test
185                 testSubject = createTestSubject();
186                 result = testSubject.createCategory(category, nodeType);
187         }
188
189         
190         @Test
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;
197
198                 // default test
199                 testSubject = createTestSubject();
200                 result = testSubject.createCategory(category, nodeType, inTransaction);
201         }
202
203         
204         @Test
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;
211
212                 // default test
213                 testSubject = createTestSubject();
214                 result = testSubject.createSubCategory(categoryId, subCategory, nodeType);
215         }
216
217         
218         @Test
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;
226
227                 // default test
228                 testSubject = createTestSubject();
229                 result = testSubject.createSubCategory(categoryId, subCategory, nodeType, inTransaction);
230         }
231
232         
233         @Test
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;
240
241                 // default test
242                 testSubject = createTestSubject();
243                 result = testSubject.createGrouping(subCategoryId, grouping, nodeType);
244         }
245
246         
247         @Test
248         public void testGetAllCategories() throws Exception {
249                 ElementOperation testSubject;
250                 NodeTypeEnum nodeType = NodeTypeEnum.Capability;
251                 boolean inTransaction = false;
252                 Either<List<CategoryDefinition>, ActionStatus> result;
253
254                 // default test
255                 testSubject = createTestSubject();
256                 result = testSubject.getAllCategories(nodeType, inTransaction);
257         }
258
259         
260
261
262         
263
264         
265         
266         
267         @Test
268         public void testGetCategory() throws Exception {
269                 ElementOperation testSubject;
270                 NodeTypeEnum nodeType = NodeTypeEnum.CapabilityType;
271                 String categoryId = "";
272                 Either<CategoryDefinition, ActionStatus> result;
273
274                 // default test
275                 testSubject = createTestSubject();
276                 result = testSubject.getCategory(nodeType, categoryId);
277         }
278
279         
280         @Test
281         public void testGetSubCategory() throws Exception {
282                 ElementOperation testSubject;
283                 NodeTypeEnum nodeType = NodeTypeEnum.Group;
284                 String subCategoryId = "";
285                 Either<SubCategoryDefinition, ActionStatus> result;
286
287                 // default test
288                 testSubject = createTestSubject();
289                 result = testSubject.getSubCategory(nodeType, subCategoryId);
290         }
291
292         
293         @Test
294         public void testDeleteCategory() throws Exception {
295                 ElementOperation testSubject;
296                 NodeTypeEnum nodeType = NodeTypeEnum.getByName("resource");
297                 String categoryId = "";
298                 Either<CategoryDefinition, ActionStatus> result;
299
300                 // default test
301                 testSubject = createTestSubject();
302                 result = testSubject.deleteCategory(nodeType, categoryId);
303         }
304
305         
306         @Test
307         public void testDeleteSubCategory() throws Exception {
308                 ElementOperation testSubject;
309                 NodeTypeEnum nodeType = NodeTypeEnum.Attribute;
310                 String subCategoryId = "";
311                 Either<SubCategoryDefinition, ActionStatus> result;
312
313                 // default test
314                 testSubject = createTestSubject();
315                 result = testSubject.deleteSubCategory(nodeType, subCategoryId);
316         }
317
318         
319         @Test
320         public void testDeleteGrouping() throws Exception {
321                 ElementOperation testSubject;
322                 NodeTypeEnum nodeType = NodeTypeEnum.DataType;
323                 String groupingId = "";
324                 Either<GroupingDefinition, ActionStatus> result;
325
326                 // default test
327                 testSubject = createTestSubject();
328                 result = testSubject.deleteGrouping(nodeType, groupingId);
329         }
330
331         
332         @Test
333         public void testIsCategoryUniqueForType() throws Exception {
334                 ElementOperation testSubject;
335                 NodeTypeEnum nodeType = null;
336                 String normalizedName = "";
337                 Either<Boolean, ActionStatus> result;
338
339                 // default test
340                 testSubject = createTestSubject();
341                 result = testSubject.isCategoryUniqueForType(nodeType, normalizedName);
342         }
343
344         
345         @Test
346         public void testIsSubCategoryUniqueForCategory() throws Exception {
347                 ElementOperation testSubject;
348                 NodeTypeEnum nodeType = null;
349                 String subCategoryNormName = "";
350                 String parentCategoryId = "";
351                 Either<Boolean, ActionStatus> result;
352
353                 // default test
354                 testSubject = createTestSubject();
355                 result = testSubject.isSubCategoryUniqueForCategory(nodeType, subCategoryNormName, parentCategoryId);
356         }
357
358         
359         @Test
360         public void testIsGroupingUniqueForSubCategory() throws Exception {
361                 ElementOperation testSubject;
362                 NodeTypeEnum nodeType = null;
363                 String groupingNormName = "";
364                 String parentSubCategoryId = "";
365                 Either<Boolean, ActionStatus> result;
366
367                 // default test
368                 testSubject = createTestSubject();
369                 result = testSubject.isGroupingUniqueForSubCategory(nodeType, groupingNormName, parentSubCategoryId);
370         }
371
372         
373         @Test
374         public void testGetSubCategoryUniqueForType() throws Exception {
375                 ElementOperation testSubject;
376                 NodeTypeEnum nodeType = null;
377                 String normalizedName = "";
378                 Either<SubCategoryDefinition, ActionStatus> result;
379
380                 // default test
381                 testSubject = createTestSubject();
382                 result = testSubject.getSubCategoryUniqueForType(nodeType, normalizedName);
383         }
384
385         
386         @Test
387         public void testGetGroupingUniqueForType() throws Exception {
388                 ElementOperation testSubject;
389                 NodeTypeEnum nodeType = null;
390                 String groupingNormalizedName = "";
391                 Either<GroupingDefinition, ActionStatus> result;
392
393                 // default test
394                 testSubject = createTestSubject();
395                 result = testSubject.getGroupingUniqueForType(nodeType, groupingNormalizedName);
396         }
397
398         
399         @Test
400         public void testGetAllTags() throws Exception {
401                 ElementOperation testSubject;
402                 Either<List<Tag>, ActionStatus> result;
403
404                 // default test
405                 testSubject = createTestSubject();
406                 result = testSubject.getAllTags();
407         }
408
409         
410         @Test
411         public void testGetCategoryData() throws Exception {
412                 ElementOperation testSubject;
413                 String name = "";
414                 NodeTypeEnum type = NodeTypeEnum.DataType;
415                 Class<T> clazz = null;
416                 Either<org.openecomp.sdc.be.resources.data.CategoryData, StorageOperationStatus> result;
417
418                 // test 1
419                 testSubject = createTestSubject();
420                 name = null;
421                 result = testSubject.getCategoryData(name, type, null);
422
423                 // test 2
424                 testSubject = createTestSubject();
425                 name = "";
426                 result = testSubject.getCategoryData(name, type, null);
427         }
428
429         
430
431
432         
433         @Test
434         public void testGetAllPropertyScopes() throws Exception {
435                 ElementOperation testSubject;
436                 Either<List<PropertyScope>, ActionStatus> result;
437
438                 // default test
439                 testSubject = createTestSubject();
440                 result = testSubject.getAllPropertyScopes();
441         }
442         
443         @Test
444         public void testGetResourceTypesMap() throws Exception {
445                 ElementOperation testSubject;
446                 Either<Map<String, String>, ActionStatus> result;
447
448                 // default test
449                 testSubject = createTestSubject();
450                 result = testSubject.getResourceTypesMap();
451         }
452
453         @Test
454         public void testGetNewCategoryData() throws Exception {
455                 ElementOperation testSubject;
456                 String name = "";
457                 NodeTypeEnum type = NodeTypeEnum.HeatParameter;
458                 Class<T> clazz = null;
459                 Either<CategoryData, StorageOperationStatus> result;
460
461                 // test 1
462                 testSubject = createTestSubject();
463                 name = null;
464                 result = testSubject.getNewCategoryData(name, type, null);
465
466                 // test 2
467                 testSubject = createTestSubject();
468                 name = "";
469                 result = testSubject.getNewCategoryData(name, type, null);
470     }
471         
472     @Test
473     public void testBaseTypes_serviceSpecific() {
474         Map<String, String> preExistingServiceNodeTypes = configurationManager.getConfiguration().getServiceNodeTypes();
475         Map<String, String> preExistingGenericNodeTypes =
476                 configurationManager.getConfiguration().getGenericAssetNodeTypes();
477
478         try {
479             Map<String, String> serviceNodeTypes = new HashMap<>();
480             serviceNodeTypes.put("serviceCategoryA", "org.base.type");
481             configurationManager.getConfiguration().setServiceNodeTypes(serviceNodeTypes);
482
483             Map<String, String> genericNodeTypes = new HashMap<>();
484             genericNodeTypes.put("service", "org.service.default");
485             configurationManager.getConfiguration().setGenericAssetNodeTypes(genericNodeTypes);
486
487             HealingJanusGraphDao healingJanusGraphDao = mock(HealingJanusGraphDao.class);
488             ElementOperation elementOperation =
489                     new ElementOperation(new JanusGraphGenericDao(new JanusGraphClient()), healingJanusGraphDao);
490
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)));
495
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");
499             
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");
503             
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");
510
511             List<BaseType> baseTypes = elementOperation.getBaseTypes("serviceCategoryA");
512
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());
518         } finally {
519             configurationManager.getConfiguration().setServiceNodeTypes(preExistingServiceNodeTypes);
520             configurationManager.getConfiguration().setGenericAssetNodeTypes(preExistingGenericNodeTypes);
521         }
522     }
523         
524     @Test
525     public void testBaseTypes_default() {
526         Map<String, String> preExistingServiceNodeTypes = configurationManager.getConfiguration().getServiceNodeTypes();
527         Map<String, String> preExistingGenericNodeTypes =
528                 configurationManager.getConfiguration().getGenericAssetNodeTypes();
529
530         try {
531             Map<String, String> genericNodeTypes = new HashMap<>();
532             genericNodeTypes.put("Service", "org.service.default");
533             configurationManager.getConfiguration().setGenericAssetNodeTypes(genericNodeTypes);
534             configurationManager.getConfiguration().setServiceNodeTypes(null);
535
536             HealingJanusGraphDao healingJanusGraphDao = mock(HealingJanusGraphDao.class);
537             ElementOperation elementOperation =
538                     new ElementOperation(new JanusGraphGenericDao(new JanusGraphClient()), healingJanusGraphDao);
539
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)));
544
545             when(healingJanusGraphDao.getParentVertices(baseTypeVertex, EdgeLabelEnum.DERIVED_FROM,
546                     JsonParseFlagEnum.ParseAll)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
547
548             List<BaseType> baseTypes = elementOperation.getBaseTypes("serviceCategoryA");
549
550             assertEquals(1, baseTypes.size());
551             assertEquals("org.service.default", baseTypes.get(0).getToscaResourceName());
552             assertEquals(1, baseTypes.get(0).getVersions().size());
553         } finally {
554             configurationManager.getConfiguration().setServiceNodeTypes(preExistingServiceNodeTypes);
555             configurationManager.getConfiguration().setGenericAssetNodeTypes(preExistingGenericNodeTypes);
556         }
557     }
558 }