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