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