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