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