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