Catalog alignment
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / operations / impl / ToscaElementLifecycleOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.operations.impl;
22
23 import org.janusgraph.core.JanusGraph;
24 import org.janusgraph.core.JanusGraphVertex;
25 import fj.data.Either;
26 import org.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.jsongraph.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         cat.setMetadataProperties(metadataProperties);
307         cat.updateMetadataJsonWithCurrentMetadataProperties();
308
309         GraphVertex subCat = new GraphVertex(VertexTypeEnum.RESOURCE_SUBCATEGORY);
310         metadataProperties = new HashMap<>();
311         String subCatId = UniqueIdBuilder.buildSubCategoryUid(cat.getUniqueId(), subcategory);
312         subCat.setUniqueId(subCatId);
313         metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, subCatId);
314         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_SUBCATEGORY.getName());
315         metadataProperties.put(GraphPropertyEnum.NAME, subcategory);
316         metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(subcategory));
317         subCat.setMetadataProperties(metadataProperties);
318         subCat.updateMetadataJsonWithCurrentMetadataProperties();
319
320         Either<GraphVertex, JanusGraphOperationStatus> catRes = janusGraphDao.createVertex(cat);
321
322         Either<GraphVertex, JanusGraphOperationStatus> subCatRes = janusGraphDao.createVertex(subCat);
323
324         JanusGraphOperationStatus
325             status = janusGraphDao
326             .createEdge(catRes.left().value().getVertex(), subCatRes.left().value().getVertex(), EdgeLabelEnum.SUB_CATEGORY, new HashMap<>());
327         assertEquals(JanusGraphOperationStatus.OK, status);
328     }
329
330     private void createServiceCategory() {
331
332         GraphVertex cat = new GraphVertex(VertexTypeEnum.SERVICE_CATEGORY);
333         Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
334         String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
335         cat.setUniqueId(catId);
336         metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId);
337         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.SERVICE_CATEGORY.getName());
338         metadataProperties.put(GraphPropertyEnum.NAME, categoryName);
339         metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName));
340         cat.setMetadataProperties(metadataProperties);
341         cat.updateMetadataJsonWithCurrentMetadataProperties();
342
343         Either<GraphVertex, JanusGraphOperationStatus> catRes = janusGraphDao.createVertex(cat);
344
345         assertTrue(catRes.isLeft());
346     }
347
348     private GraphVertex createTopologyTemplate(String name) {
349
350         TopologyTemplate service = new TopologyTemplate();
351         String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
352         service.setUniqueId(uniqueId);
353         service.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
354         service.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), name);
355         service.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
356         service.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1");
357         service.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name());
358         service.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE);
359         List<CategoryDefinition> categories = new ArrayList<>();
360         CategoryDefinition cat = new CategoryDefinition();
361         categories.add(cat);
362         cat.setName(categoryName);
363         service.setCategories(categories);
364
365         service.setComponentType(ComponentTypeEnum.SERVICE);
366         Either<TopologyTemplate, StorageOperationStatus> createRes = topologyTemplateOperation.createTopologyTemplate(service);
367         assertTrue(createRes.isLeft());
368
369         Either<GraphVertex, JanusGraphOperationStatus> getNodeTyeRes = janusGraphDao
370             .getVertexById(createRes.left().value().getUniqueId());
371         assertTrue(getNodeTyeRes.isLeft());
372
373         // serviceVertex = getNodeTyeRes.left().value();
374
375         return getNodeTyeRes.left().value();
376     }
377
378     private <T extends ToscaDataDefinition> NodeType createNodeType(String nodeTypeName) {
379
380         NodeType vf = new NodeType();
381         String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
382         vf.setUniqueId(uniqueId);
383         vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
384         vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), nodeTypeName);
385         vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
386         vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1");
387         vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name());
388         vf.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE);
389         List<CategoryDefinition> categories = new ArrayList<>();
390         CategoryDefinition cat = new CategoryDefinition();
391         categories.add(cat);
392         cat.setName(categoryName);
393         List<SubCategoryDefinition> subCategories = new ArrayList<>();
394         SubCategoryDefinition subCat = new SubCategoryDefinition();
395         subCat.setName(subcategory);
396         subCategories.add(subCat);
397         cat.setSubcategories(subCategories);
398         vf.setCategories(categories);
399
400         List<String> derivedFrom = new ArrayList<>();
401         derivedFrom.add("root");
402         vf.setDerivedFrom(derivedFrom);
403
404         // Map<String, PropertyDataDefinition> properties = new HashMap<>();
405         // PropertyDataDefinition prop1 = new PropertyDataDefinition();
406         // prop1.setName("prop1");
407         // prop1.setDefaultValue("def1");
408         //
409         // properties.put("prop1", prop1);
410         //
411         // PropertyDataDefinition prop2 = new PropertyDataDefinition();
412         // prop2.setName("prop2");
413         // prop2.setDefaultValue("def2");
414         // properties.put("prop2", prop2);
415         //
416         // PropertyDataDefinition prop3 = new PropertyDataDefinition();
417         // prop3.setName("prop3");
418         // prop3.setDefaultValue("def3");
419         // properties.put("prop3", prop3);
420         //
421         // vf.setProperties(properties);
422         vf.setComponentType(ComponentTypeEnum.RESOURCE);
423         Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
424         assertTrue(createVFRes.isLeft());
425
426         Either<GraphVertex, JanusGraphOperationStatus> getNodeTyeRes = janusGraphDao
427             .getVertexById(createVFRes.left().value().getUniqueId());
428         assertTrue(getNodeTyeRes.isLeft());
429
430         vfVertex = getNodeTyeRes.left().value();
431
432         List<PropertyDataDefinition> addProperties = new ArrayList<>();
433         PropertyDataDefinition prop11 = new PropertyDataDefinition();
434         prop11.setName("prop11");
435         prop11.setDefaultValue("def11");
436
437         addProperties.add(prop11);
438
439         PropertyDataDefinition prop22 = new PropertyDataDefinition();
440         prop22.setName("prop22");
441         prop22.setDefaultValue("def22");
442         addProperties.add(prop22);
443
444         StorageOperationStatus status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, addProperties, JsonPresentationFields.NAME);
445         assertSame(status, StorageOperationStatus.OK);
446
447         PropertyDataDefinition prop33 = new PropertyDataDefinition();
448         prop33.setName("prop33");
449         prop33.setDefaultValue("def33");
450
451         status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop33, JsonPresentationFields.NAME);
452         assertSame(status, StorageOperationStatus.OK);
453
454         PropertyDataDefinition prop44 = new PropertyDataDefinition();
455         prop44.setName("prop44");
456         prop44.setDefaultValue("def44");
457
458         status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop44, JsonPresentationFields.NAME);
459         assertSame(status, StorageOperationStatus.OK);
460
461         PropertyDataDefinition capProp = new PropertyDataDefinition();
462         capProp.setName("capProp");
463         capProp.setDefaultValue("capPropDef");
464
465         MapDataDefinition dataToCreate = new MapPropertiesDataDefinition();
466         dataToCreate.put("capProp", capProp);
467
468         Map<String, MapDataDefinition> capProps = new HashMap();
469         capProps.put("capName", dataToCreate);
470
471         Either<GraphVertex, StorageOperationStatus> res = nodeTypeOperation.associateElementToData(vfVertex, VertexTypeEnum.CAPABILITIES_PROPERTIES, EdgeLabelEnum.CAPABILITIES_PROPERTIES, capProps);
472
473         // exportGraphMl(janusGraphDao.getGraph().left().value());
474
475         List<String> pathKeys = new ArrayList<>();
476         pathKeys.add("capName");
477         capProp.setDefaultValue("BBBB");
478         status = nodeTypeOperation.updateToscaDataDeepElementOfToscaElement(vfVertex, EdgeLabelEnum.CAPABILITIES_PROPERTIES, VertexTypeEnum.CAPABILITIES_PROPERTIES, capProp, pathKeys, JsonPresentationFields.NAME);
479         return vf;
480     }
481
482     private GraphVertex createRootNodeType() {
483
484         NodeType vf = new NodeType();
485         String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
486         vf.setUniqueId(uniqueId);
487         vf.setComponentType(ComponentTypeEnum.RESOURCE);
488         vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
489         vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), "root");
490         vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
491         vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "1.0");
492         vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VFC.name());
493         vf.getMetadata().put(JsonPresentationFields.LIFECYCLE_STATE.getPresentation(), LifecycleStateEnum.CERTIFIED.name());
494         vf.getMetadata().put(JsonPresentationFields.TOSCA_RESOURCE_NAME.getPresentation(), "root");
495         vf.getMetadata().put(JsonPresentationFields.HIGHEST_VERSION.getPresentation(), true);
496
497         List<CategoryDefinition> categories = new ArrayList<>();
498         CategoryDefinition cat = new CategoryDefinition();
499         categories.add(cat);
500         cat.setName(categoryName);
501         List<SubCategoryDefinition> subCategories = new ArrayList<>();
502         SubCategoryDefinition subCat = new SubCategoryDefinition();
503         subCat.setName(subcategory);
504         subCategories.add(subCat);
505         cat.setSubcategories(subCategories);
506         vf.setCategories(categories);
507
508         List<String> derivedFrom = new ArrayList<>();
509         vf.setDerivedFrom(derivedFrom);
510
511         Map<String, PropertyDataDefinition> properties = new HashMap<>();
512         PropertyDataDefinition prop1 = new PropertyDataDefinition();
513         prop1.setName("derived1");
514         prop1.setDefaultValue("deriveddef1");
515
516         properties.put("derived1", prop1);
517
518         PropertyDataDefinition prop2 = new PropertyDataDefinition();
519         prop2.setUniqueId("derived2");
520         prop2.setName("deriveddef2");
521         properties.put("derived2", prop2);
522
523         PropertyDataDefinition prop3 = new PropertyDataDefinition();
524         prop3.setName("derived3");
525         prop3.setDefaultValue("deriveddef3");
526         properties.put("derived3", prop3);
527
528         vf.setProperties(properties);
529         vf.setComponentType(ComponentTypeEnum.RESOURCE);
530         Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
531         assertTrue(createVFRes.isLeft());
532
533         Either<GraphVertex, JanusGraphOperationStatus> getNodeTyeRes = janusGraphDao
534             .getVertexById(createVFRes.left().value().getUniqueId());
535         assertTrue(getNodeTyeRes.isLeft());
536         return getNodeTyeRes.left().value();
537     }
538
539     private void createUsers() {
540
541         GraphVertex ownerV = new GraphVertex(VertexTypeEnum.USER);
542         ownerV.setUniqueId("user1");
543
544         Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
545         metadataProperties.put(GraphPropertyEnum.USERID, ownerV.getUniqueId());
546         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName());
547         metadataProperties.put(GraphPropertyEnum.NAME, "user1");
548         ownerV.setMetadataProperties(metadataProperties);
549         ownerV.updateMetadataJsonWithCurrentMetadataProperties();
550         ownerV.setJson(new HashMap<>());
551         Either<GraphVertex, JanusGraphOperationStatus> createUserRes = janusGraphDao.createVertex(ownerV);
552         assertTrue(createUserRes.isLeft());
553
554         ownerVertex = createUserRes.left().value();
555
556         GraphVertex modifierV = new GraphVertex(VertexTypeEnum.USER);
557         modifierV.setUniqueId("user2");
558
559         metadataProperties = new HashMap<>();
560         metadataProperties.put(GraphPropertyEnum.USERID, modifierV.getUniqueId());
561         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName());
562         metadataProperties.put(GraphPropertyEnum.NAME, "user2");
563         modifierV.setMetadataProperties(metadataProperties);
564         modifierV.updateMetadataJsonWithCurrentMetadataProperties();
565         modifierV.setJson(new HashMap<>());
566         createUserRes = janusGraphDao.createVertex(modifierV);
567         assertTrue(createUserRes.isLeft());
568
569         modifierVertex = createUserRes.left().value();
570
571         Either<GraphVertex, JanusGraphOperationStatus> getOwnerRes = lifecycleOperation.findUser(ownerVertex.getUniqueId());
572         assertTrue(getOwnerRes.isLeft());
573
574     }
575
576     public void verifyInCatalogData(int expected, List<String> expectedIds) {
577
578                 Either<List<CatalogComponent>, StorageOperationStatus> highestResourcesRes = topologyTemplateOperation.getElementCatalogData(true, null);
579         assertTrue(highestResourcesRes.isLeft());
580         List<CatalogComponent> highestResources = highestResourcesRes.left().value();
581         // calculate expected count value
582         assertEquals(expected, highestResources.stream().count());
583         if (expectedIds != null) {
584             highestResources.forEach(a -> assertTrue(expectedIds.contains(a.getUniqueId())));
585         }
586     }
587
588     @After
589     public void teardown() {
590         clearGraph();
591     }
592
593     private void clearGraph() {
594         Either<JanusGraph, JanusGraphOperationStatus> graphResult = janusGraphDao.getGraph();
595         JanusGraph graph = graphResult.left().value();
596
597         Iterable<JanusGraphVertex> vertices = graph.query().vertices();
598         if (vertices != null) {
599             Iterator<JanusGraphVertex> iterator = vertices.iterator();
600             while (iterator.hasNext()) {
601                 JanusGraphVertex vertex = iterator.next();
602                 vertex.remove();
603             }
604         }
605         janusGraphDao.commit();
606     }
607
608     private String exportGraphMl(JanusGraph graph) {
609         String result = null;
610         String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".graphml";
611         try {
612             try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
613                 graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
614             }
615             result = outputFile;
616             graph.tx().commit();
617         } catch (Exception e) {
618             graph.tx().rollback();
619             e.printStackTrace();
620         }
621         return result;
622
623     }
624
625 }