Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / operations / impl / ToscaElementLifecycleOperationTest.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 org.janusgraph.core.JanusGraph;
24 import org.janusgraph.core.JanusGraphVertex;
25 import fj.data.Either;
26 import org.junit.*;
27 import org.junit.rules.TestName;
28 import org.junit.runner.RunWith;
29 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
30 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
31 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
32 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
33 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
34 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
35 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
36 import org.openecomp.sdc.be.datatypes.elements.MapDataDefinition;
37 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
38 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
39 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
40 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
41 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
42 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
43 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
44 import org.openecomp.sdc.be.model.LifecycleStateEnum;
45 import org.openecomp.sdc.be.model.ModelTestBase;
46 import org.openecomp.sdc.be.model.User;
47 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
48 import org.openecomp.sdc.be.model.category.CategoryDefinition;
49 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
50 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.NodeType;
51 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
52 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
53 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTypeOperation;
54 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
55 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementLifecycleOperation;
56 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.GraphTestUtils;
57 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
58 import org.openecomp.sdc.common.util.ValidationUtils;
59 import org.springframework.test.context.ContextConfiguration;
60 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
61
62 import java.util.*;
63
64 import static org.junit.Assert.assertEquals;
65 import static org.junit.Assert.assertSame;
66 import static org.junit.Assert.assertTrue;
67
68 @RunWith(SpringJUnit4ClassRunner.class)
69 @ContextConfiguration("classpath:application-context-test.xml")
70 public class ToscaElementLifecycleOperationTest extends ModelTestBase {
71
72     @javax.annotation.Resource
73     protected JanusGraphDao janusGraphDao;
74
75     @javax.annotation.Resource
76     private NodeTypeOperation nodeTypeOperation;
77
78     @javax.annotation.Resource
79     private TopologyTemplateOperation topologyTemplateOperation;
80
81     @javax.annotation.Resource
82     private ToscaElementLifecycleOperation lifecycleOperation;
83
84     String categoryName = "category";
85     String subcategory = "mycategory";
86     String outputDirectory = "C:\\Output";
87
88     @Rule
89     public TestName name = new TestName();
90
91     @BeforeClass
92     public static void initLifecycleOperation() {
93         ModelTestBase.init();
94     }
95
96     private GraphVertex ownerVertex;
97     private GraphVertex modifierVertex;
98     private GraphVertex vfVertex;
99     private GraphVertex serviceVertex;
100     private GraphVertex rootVertex;
101
102     @Before
103     public void setupBefore() {
104         clearGraph();
105         createUsers();
106         createResourceCategory();
107         createServiceCategory();
108         GraphTestUtils.createRootCatalogVertex(janusGraphDao);
109         rootVertex = createRootNodeType();
110         createNodeType("firstVf");
111         serviceVertex = createTopologyTemplate("firstService");
112     }
113
114     @Test
115     public void lifecycleTest() {
116         Either<ToscaElement, StorageOperationStatus> res = lifecycleOperation.checkinToscaELement(LifecycleStateEnum.findState((String) vfVertex.getMetadataProperty(GraphPropertyEnum.STATE)), vfVertex.getUniqueId(), modifierVertex.getUniqueId(),
117                 ownerVertex.getUniqueId());
118         StorageOperationStatus status;
119
120         assertTrue(res.isLeft());
121         // 1-node type
122         // 2-vf
123         // 3- service
124         verifyInCatalogData(3, null);
125
126         String id = res.left().value().getUniqueId();
127
128         res = lifecycleOperation.checkoutToscaElement(id, ownerVertex.getUniqueId(), modifierVertex.getUniqueId());
129         assertTrue(res.isLeft());
130         id = res.left().value().getUniqueId();
131
132         verifyInCatalogData(3, null);
133
134         PropertyDataDefinition prop55 = new PropertyDataDefinition();
135         prop55.setName("prop55");
136         prop55.setDefaultValue("def55");
137
138         status = nodeTypeOperation.addToscaDataToToscaElement(id, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop55, JsonPresentationFields.NAME);
139         assertSame(status, StorageOperationStatus.OK);
140
141         CapabilityDataDefinition cap1 = new CapabilityDataDefinition();
142         cap1.setName("cap1");
143         cap1.setDescription("create");
144         cap1.setUniqueId(UniqueIdBuilder.buildCapabilityUid(id, "cap1"));
145
146         status = nodeTypeOperation.addToscaDataToToscaElement(id, EdgeLabelEnum.CAPABILITIES, VertexTypeEnum.CAPABILITIES, cap1, JsonPresentationFields.NAME);
147         assertSame(status, StorageOperationStatus.OK);
148
149         res = lifecycleOperation.checkinToscaELement(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT, id, ownerVertex.getUniqueId(), ownerVertex.getUniqueId());
150         assertTrue(res.isLeft());
151         id = res.left().value().getUniqueId();
152
153         res = lifecycleOperation.checkoutToscaElement(id, ownerVertex.getUniqueId(), ownerVertex.getUniqueId());
154         assertTrue(res.isLeft());
155         id = res.left().value().getUniqueId();
156
157         prop55.setDefaultValue("AAAAAAAA");
158         status = nodeTypeOperation.updateToscaDataOfToscaElement(id, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop55, JsonPresentationFields.NAME);
159         assertSame(status, StorageOperationStatus.OK);
160
161         cap1.setDescription("update");
162
163         status = nodeTypeOperation.updateToscaDataOfToscaElement(id, EdgeLabelEnum.CAPABILITIES, VertexTypeEnum.CAPABILITIES, cap1, JsonPresentationFields.NAME);
164         assertSame(status, StorageOperationStatus.OK);
165
166         PropertyDataDefinition prop66 = new PropertyDataDefinition();
167         prop66.setName("prop66");
168         prop66.setDefaultValue("def66");
169
170         status = nodeTypeOperation.addToscaDataToToscaElement(id, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop66, JsonPresentationFields.NAME);
171         assertSame(status, StorageOperationStatus.OK);
172
173         res = lifecycleOperation.requestCertificationToscaElement(id, modifierVertex.getUniqueId(), ownerVertex.getUniqueId());
174         assertTrue(res.isLeft());
175         id = res.left().value().getUniqueId();
176
177         res = lifecycleOperation.startCertificationToscaElement(id, modifierVertex.getUniqueId(), ownerVertex.getUniqueId());
178         assertTrue(res.isLeft());
179         id = res.left().value().getUniqueId();
180
181         res = lifecycleOperation.certifyToscaElement(id, modifierVertex.getUniqueId(), ownerVertex.getUniqueId());
182         assertTrue(res.isLeft());
183         id = res.left().value().getUniqueId();
184
185         verifyInCatalogData(3, null);
186
187         res = lifecycleOperation.checkoutToscaElement(id, ownerVertex.getUniqueId(), modifierVertex.getUniqueId());
188         assertTrue(res.isLeft());
189         id = res.left().value().getUniqueId();
190
191         verifyInCatalogData(4, null);
192
193         PropertyDataDefinition prop77 = new PropertyDataDefinition();
194         prop77.setName("prop77");
195         prop77.setDefaultValue("def77");
196
197         status = nodeTypeOperation.addToscaDataToToscaElement(id, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop77, JsonPresentationFields.NAME);
198         assertSame(status, StorageOperationStatus.OK);
199
200         res = lifecycleOperation.checkinToscaELement(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT, id, ownerVertex.getUniqueId(), ownerVertex.getUniqueId());
201         assertTrue(res.isLeft());
202         id = res.left().value().getUniqueId();
203
204         res = lifecycleOperation.checkoutToscaElement(id, ownerVertex.getUniqueId(), ownerVertex.getUniqueId());
205         assertTrue(res.isLeft());
206         id = res.left().value().getUniqueId();
207
208         PropertyDataDefinition prop88 = new PropertyDataDefinition();
209         prop88.setName("prop88");
210         prop88.setDefaultValue("def88");
211
212         status = nodeTypeOperation.addToscaDataToToscaElement(id, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop88, JsonPresentationFields.NAME);
213         assertSame(status, StorageOperationStatus.OK);
214
215         res = lifecycleOperation.requestCertificationToscaElement(id, modifierVertex.getUniqueId(), ownerVertex.getUniqueId());
216         assertTrue(res.isLeft());
217         id = res.left().value().getUniqueId();
218
219         res = lifecycleOperation.startCertificationToscaElement(id, modifierVertex.getUniqueId(), ownerVertex.getUniqueId());
220         assertTrue(res.isLeft());
221         id = res.left().value().getUniqueId();
222
223         res = lifecycleOperation.certifyToscaElement(id, modifierVertex.getUniqueId(), ownerVertex.getUniqueId());
224         assertTrue(res.isLeft());
225         id = res.left().value().getUniqueId();
226         verifyInCatalogData(3, null);
227
228         res = lifecycleOperation.checkoutToscaElement(id, ownerVertex.getUniqueId(), ownerVertex.getUniqueId());
229         assertTrue(res.isLeft());
230         id = res.left().value().getUniqueId();
231
232         verifyInCatalogData(4, null);
233
234         PropertyDataDefinition prop99 = new PropertyDataDefinition();
235         prop99.setName("prop99");
236         prop99.setDefaultValue("def99");
237
238         status = nodeTypeOperation.addToscaDataToToscaElement(id, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop99, JsonPresentationFields.NAME);
239         assertSame(status, StorageOperationStatus.OK);
240
241         res = lifecycleOperation.requestCertificationToscaElement(id, modifierVertex.getUniqueId(), ownerVertex.getUniqueId());
242         assertTrue(res.isLeft());
243         id = res.left().value().getUniqueId();
244
245         res = lifecycleOperation.startCertificationToscaElement(id, modifierVertex.getUniqueId(), ownerVertex.getUniqueId());
246         assertTrue(res.isLeft());
247         id = res.left().value().getUniqueId();
248
249         status = nodeTypeOperation.deleteToscaDataElement(id, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, "prop99", JsonPresentationFields.NAME);
250         assertSame(status, StorageOperationStatus.OK);
251
252         // cancel certification
253         res = lifecycleOperation.cancelOrFailCertification(id, modifierVertex.getUniqueId(), ownerVertex.getUniqueId(), LifecycleStateEnum.READY_FOR_CERTIFICATION);
254         assertTrue(res.isLeft());
255         id = res.left().value().getUniqueId();
256
257         res = lifecycleOperation.startCertificationToscaElement(id, modifierVertex.getUniqueId(), ownerVertex.getUniqueId());
258         assertTrue(res.isLeft());
259         id = res.left().value().getUniqueId();
260
261         // fail certification
262         res = lifecycleOperation.cancelOrFailCertification(id, modifierVertex.getUniqueId(), ownerVertex.getUniqueId(), LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
263         assertTrue(res.isLeft());
264         id = res.left().value().getUniqueId();
265         verifyInCatalogData(4, null);
266         // exportGraphMl(janusGraphDao.getGraph().left().value());
267
268     }
269
270     @Test
271     public void serviceConformanceLevelTest() {
272         Either<ToscaElement, StorageOperationStatus> res = lifecycleOperation.checkinToscaELement(LifecycleStateEnum.findState((String) serviceVertex.getMetadataProperty(GraphPropertyEnum.STATE)), serviceVertex.getUniqueId(),
273                 modifierVertex.getUniqueId(), ownerVertex.getUniqueId());
274
275         assertTrue(res.isLeft());
276         String id = res.left().value().getUniqueId();
277
278         res = lifecycleOperation.checkoutToscaElement(id, ownerVertex.getUniqueId(), modifierVertex.getUniqueId());
279         assertTrue(res.isLeft());
280
281         String conformanceLevel = res.left().value().getMetadataValue(JsonPresentationFields.CONFORMANCE_LEVEL).toString();
282         assertEquals(conformanceLevel, ModelTestBase.configurationManager.getConfiguration().getToscaConformanceLevel());
283     }
284
285     @Test
286     public void catalogTest() {
287         // start position - 3 in catalog
288         List<String> expectedIds = new ArrayList<>();
289         expectedIds.add(rootVertex.getUniqueId());
290         expectedIds.add(vfVertex.getUniqueId());
291         expectedIds.add(serviceVertex.getUniqueId());
292
293         verifyInCatalogData(3, expectedIds);
294
295         GraphVertex vertex4 = createTopologyTemplate("topTemp4");
296         expectedIds.add(vertex4.getUniqueId());
297         verifyInCatalogData(4, expectedIds);
298
299         Either<ToscaElement, StorageOperationStatus> res = lifecycleOperation.undoCheckout(vertex4.getUniqueId());
300         expectedIds.remove(vertex4.getUniqueId());
301         verifyInCatalogData(3, expectedIds);
302
303         vertex4 = createTopologyTemplate("topTemp4");
304         expectedIds.add(vertex4.getUniqueId());
305         verifyInCatalogData(4, expectedIds);
306
307         res = lifecycleOperation.checkinToscaELement(LifecycleStateEnum.findState((String) vertex4.getMetadataProperty(GraphPropertyEnum.STATE)), vertex4.getUniqueId(), modifierVertex.getUniqueId(), ownerVertex.getUniqueId());
308         Either<ToscaElement, StorageOperationStatus> certifyToscaElement = lifecycleOperation.certifyToscaElement(vertex4.getUniqueId(), modifierVertex.getUniqueId(), ownerVertex.getUniqueId());
309         assertTrue(certifyToscaElement.isLeft());
310         expectedIds.remove(vertex4.getUniqueId());
311         String certifiedId = certifyToscaElement.left().value().getUniqueId();
312         expectedIds.add(certifiedId);
313         verifyInCatalogData(4, expectedIds);
314
315         res = lifecycleOperation.checkoutToscaElement(certifiedId, modifierVertex.getUniqueId(), ownerVertex.getUniqueId());
316         assertTrue(certifyToscaElement.isLeft());
317         expectedIds.add(res.left().value().getUniqueId());
318         verifyInCatalogData(5, expectedIds);
319     }
320
321     @Test
322     public void testGetToscaElOwner_Fail(){
323         Either<User, StorageOperationStatus> result;
324         String toscaEleId = "toscaElementId";
325         janusGraphDao.getVertexById(toscaEleId, JsonParseFlagEnum.NoParse);
326         result = lifecycleOperation.getToscaElementOwner(toscaEleId);
327         assertEquals(StorageOperationStatus.NOT_FOUND, result.right().value());
328     }
329
330     private void createResourceCategory() {
331
332         GraphVertex cat = new GraphVertex(VertexTypeEnum.RESOURCE_CATEGORY);
333         Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
334         String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.RESOURCE_CATEGORY);
335         cat.setUniqueId(catId);
336         metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId);
337         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_CATEGORY.getName());
338         metadataProperties.put(GraphPropertyEnum.NAME, categoryName);
339         metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName));
340         cat.setMetadataProperties(metadataProperties);
341         cat.updateMetadataJsonWithCurrentMetadataProperties();
342
343         GraphVertex subCat = new GraphVertex(VertexTypeEnum.RESOURCE_SUBCATEGORY);
344         metadataProperties = new HashMap<>();
345         String subCatId = UniqueIdBuilder.buildSubCategoryUid(cat.getUniqueId(), subcategory);
346         subCat.setUniqueId(subCatId);
347         metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, subCatId);
348         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_SUBCATEGORY.getName());
349         metadataProperties.put(GraphPropertyEnum.NAME, subcategory);
350         metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(subcategory));
351         subCat.setMetadataProperties(metadataProperties);
352         subCat.updateMetadataJsonWithCurrentMetadataProperties();
353
354         Either<GraphVertex, JanusGraphOperationStatus> catRes = janusGraphDao.createVertex(cat);
355
356         Either<GraphVertex, JanusGraphOperationStatus> subCatRes = janusGraphDao.createVertex(subCat);
357
358         JanusGraphOperationStatus
359             status = janusGraphDao
360             .createEdge(catRes.left().value().getVertex(), subCatRes.left().value().getVertex(), EdgeLabelEnum.SUB_CATEGORY, new HashMap<>());
361         assertEquals(JanusGraphOperationStatus.OK, status);
362     }
363
364     private void createServiceCategory() {
365
366         GraphVertex cat = new GraphVertex(VertexTypeEnum.SERVICE_CATEGORY);
367         Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
368         String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
369         cat.setUniqueId(catId);
370         metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId);
371         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.SERVICE_CATEGORY.getName());
372         metadataProperties.put(GraphPropertyEnum.NAME, categoryName);
373         metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName));
374         cat.setMetadataProperties(metadataProperties);
375         cat.updateMetadataJsonWithCurrentMetadataProperties();
376
377         Either<GraphVertex, JanusGraphOperationStatus> catRes = janusGraphDao.createVertex(cat);
378
379         assertTrue(catRes.isLeft());
380     }
381
382     private GraphVertex createTopologyTemplate(String name) {
383
384         TopologyTemplate service = new TopologyTemplate();
385         String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
386         service.setUniqueId(uniqueId);
387         service.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
388         service.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), name);
389         service.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
390         service.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1");
391         service.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name());
392         service.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE);
393         List<CategoryDefinition> categories = new ArrayList<>();
394         CategoryDefinition cat = new CategoryDefinition();
395         categories.add(cat);
396         cat.setName(categoryName);
397         service.setCategories(categories);
398
399         service.setComponentType(ComponentTypeEnum.SERVICE);
400         Either<TopologyTemplate, StorageOperationStatus> createRes = topologyTemplateOperation.createTopologyTemplate(service);
401         assertTrue(createRes.isLeft());
402
403         Either<GraphVertex, JanusGraphOperationStatus> getNodeTyeRes = janusGraphDao
404             .getVertexById(createRes.left().value().getUniqueId());
405         assertTrue(getNodeTyeRes.isLeft());
406
407         // serviceVertex = getNodeTyeRes.left().value();
408
409         return getNodeTyeRes.left().value();
410     }
411
412     private <T extends ToscaDataDefinition> NodeType createNodeType(String nodeTypeName) {
413
414         NodeType vf = new NodeType();
415         String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
416         vf.setUniqueId(uniqueId);
417         vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
418         vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), nodeTypeName);
419         vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
420         vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1");
421         vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name());
422         vf.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE);
423         List<CategoryDefinition> categories = new ArrayList<>();
424         CategoryDefinition cat = new CategoryDefinition();
425         categories.add(cat);
426         cat.setName(categoryName);
427         List<SubCategoryDefinition> subCategories = new ArrayList<>();
428         SubCategoryDefinition subCat = new SubCategoryDefinition();
429         subCat.setName(subcategory);
430         subCategories.add(subCat);
431         cat.setSubcategories(subCategories);
432         vf.setCategories(categories);
433
434         List<String> derivedFrom = new ArrayList<>();
435         derivedFrom.add("root");
436         vf.setDerivedFrom(derivedFrom);
437
438         vf.setComponentType(ComponentTypeEnum.RESOURCE);
439         Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
440         assertTrue(createVFRes.isLeft());
441
442         Either<GraphVertex, JanusGraphOperationStatus> getNodeTyeRes = janusGraphDao
443             .getVertexById(createVFRes.left().value().getUniqueId());
444         assertTrue(getNodeTyeRes.isLeft());
445
446         vfVertex = getNodeTyeRes.left().value();
447
448         List<PropertyDataDefinition> addProperties = new ArrayList<>();
449         PropertyDataDefinition prop11 = new PropertyDataDefinition();
450         prop11.setName("prop11");
451         prop11.setDefaultValue("def11");
452
453         addProperties.add(prop11);
454
455         PropertyDataDefinition prop22 = new PropertyDataDefinition();
456         prop22.setName("prop22");
457         prop22.setDefaultValue("def22");
458         addProperties.add(prop22);
459
460         StorageOperationStatus status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, addProperties, JsonPresentationFields.NAME);
461         assertSame(status, StorageOperationStatus.OK);
462
463         PropertyDataDefinition prop33 = new PropertyDataDefinition();
464         prop33.setName("prop33");
465         prop33.setDefaultValue("def33");
466
467         status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop33, JsonPresentationFields.NAME);
468         assertSame(status, StorageOperationStatus.OK);
469
470         PropertyDataDefinition prop44 = new PropertyDataDefinition();
471         prop44.setName("prop44");
472         prop44.setDefaultValue("def44");
473
474         status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop44, JsonPresentationFields.NAME);
475         assertSame(status, StorageOperationStatus.OK);
476
477         PropertyDataDefinition capProp = new PropertyDataDefinition();
478         capProp.setName("capProp");
479         capProp.setDefaultValue("capPropDef");
480
481         MapDataDefinition dataToCreate = new MapPropertiesDataDefinition();
482         dataToCreate.put("capProp", capProp);
483
484         Map<String, MapDataDefinition> capProps = new HashMap();
485         capProps.put("capName", dataToCreate);
486
487         Either<GraphVertex, StorageOperationStatus> res = nodeTypeOperation.associateElementToData(vfVertex, VertexTypeEnum.CAPABILITIES_PROPERTIES, EdgeLabelEnum.CAPABILITIES_PROPERTIES, capProps);
488
489         // exportGraphMl(janusGraphDao.getGraph().left().value());
490
491         List<String> pathKeys = new ArrayList<>();
492         pathKeys.add("capName");
493         capProp.setDefaultValue("BBBB");
494         status = nodeTypeOperation.updateToscaDataDeepElementOfToscaElement(vfVertex, EdgeLabelEnum.CAPABILITIES_PROPERTIES, VertexTypeEnum.CAPABILITIES_PROPERTIES, capProp, pathKeys, JsonPresentationFields.NAME);
495         return vf;
496     }
497
498     private GraphVertex createRootNodeType() {
499
500         NodeType vf = new NodeType();
501         String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
502         vf.setUniqueId(uniqueId);
503         vf.setComponentType(ComponentTypeEnum.RESOURCE);
504         vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
505         vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), "root");
506         vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
507         vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "1.0");
508         vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VFC.name());
509         vf.getMetadata().put(JsonPresentationFields.LIFECYCLE_STATE.getPresentation(), LifecycleStateEnum.CERTIFIED.name());
510         vf.getMetadata().put(JsonPresentationFields.TOSCA_RESOURCE_NAME.getPresentation(), "root");
511         vf.getMetadata().put(JsonPresentationFields.HIGHEST_VERSION.getPresentation(), true);
512
513         List<CategoryDefinition> categories = new ArrayList<>();
514         CategoryDefinition cat = new CategoryDefinition();
515         categories.add(cat);
516         cat.setName(categoryName);
517         List<SubCategoryDefinition> subCategories = new ArrayList<>();
518         SubCategoryDefinition subCat = new SubCategoryDefinition();
519         subCat.setName(subcategory);
520         subCategories.add(subCat);
521         cat.setSubcategories(subCategories);
522         vf.setCategories(categories);
523
524         List<String> derivedFrom = new ArrayList<>();
525         vf.setDerivedFrom(derivedFrom);
526
527         Map<String, PropertyDataDefinition> properties = new HashMap<>();
528         PropertyDataDefinition prop1 = new PropertyDataDefinition();
529         prop1.setName("derived1");
530         prop1.setDefaultValue("deriveddef1");
531
532         properties.put("derived1", prop1);
533
534         PropertyDataDefinition prop2 = new PropertyDataDefinition();
535         prop2.setUniqueId("derived2");
536         prop2.setName("deriveddef2");
537         properties.put("derived2", prop2);
538
539         PropertyDataDefinition prop3 = new PropertyDataDefinition();
540         prop3.setName("derived3");
541         prop3.setDefaultValue("deriveddef3");
542         properties.put("derived3", prop3);
543
544         vf.setProperties(properties);
545         vf.setComponentType(ComponentTypeEnum.RESOURCE);
546         Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
547         assertTrue(createVFRes.isLeft());
548
549         Either<GraphVertex, JanusGraphOperationStatus> getNodeTyeRes = janusGraphDao
550             .getVertexById(createVFRes.left().value().getUniqueId());
551         assertTrue(getNodeTyeRes.isLeft());
552         return getNodeTyeRes.left().value();
553     }
554
555     private void createUsers() {
556
557         GraphVertex ownerV = new GraphVertex(VertexTypeEnum.USER);
558         ownerV.setUniqueId("user1");
559
560         Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
561         metadataProperties.put(GraphPropertyEnum.USERID, ownerV.getUniqueId());
562         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName());
563         metadataProperties.put(GraphPropertyEnum.NAME, "user1");
564         ownerV.setMetadataProperties(metadataProperties);
565         ownerV.updateMetadataJsonWithCurrentMetadataProperties();
566         ownerV.setJson(new HashMap<>());
567         Either<GraphVertex, JanusGraphOperationStatus> createUserRes = janusGraphDao.createVertex(ownerV);
568         assertTrue(createUserRes.isLeft());
569
570         ownerVertex = createUserRes.left().value();
571
572         GraphVertex modifierV = new GraphVertex(VertexTypeEnum.USER);
573         modifierV.setUniqueId("user2");
574
575         metadataProperties = new HashMap<>();
576         metadataProperties.put(GraphPropertyEnum.USERID, modifierV.getUniqueId());
577         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName());
578         metadataProperties.put(GraphPropertyEnum.NAME, "user2");
579         modifierV.setMetadataProperties(metadataProperties);
580         modifierV.updateMetadataJsonWithCurrentMetadataProperties();
581         modifierV.setJson(new HashMap<>());
582         createUserRes = janusGraphDao.createVertex(modifierV);
583         assertTrue(createUserRes.isLeft());
584
585         modifierVertex = createUserRes.left().value();
586
587         Either<GraphVertex, JanusGraphOperationStatus> getOwnerRes = lifecycleOperation.findUser(ownerVertex.getUniqueId());
588         assertTrue(getOwnerRes.isLeft());
589
590     }
591
592     public void verifyInCatalogData(int expected, List<String> expectedIds) {
593
594         Either<List<CatalogComponent>, StorageOperationStatus> highestResourcesRes = topologyTemplateOperation.getElementCatalogData(true, null);
595         assertTrue(highestResourcesRes.isLeft());
596         List<CatalogComponent> highestResources = highestResourcesRes.left().value();
597         // calculate expected count value
598         assertEquals(expected, highestResources.stream().count());
599         if (expectedIds != null) {
600             highestResources.forEach(a -> assertTrue(expectedIds.contains(a.getUniqueId())));
601         }
602     }
603
604     @After
605     public void teardown() {
606         clearGraph();
607     }
608
609     private void clearGraph() {
610         Either<JanusGraph, JanusGraphOperationStatus> graphResult = janusGraphDao.getGraph();
611         JanusGraph graph = graphResult.left().value();
612
613         Iterable<JanusGraphVertex> vertices = graph.query().vertices();
614         if (vertices != null) {
615             Iterator<JanusGraphVertex> iterator = vertices.iterator();
616             while (iterator.hasNext()) {
617                 JanusGraphVertex vertex = iterator.next();
618                 vertex.remove();
619             }
620         }
621         janusGraphDao.commit();
622     }
623 }