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