1 package org.openecomp.sdc.be.model.jsontitan.operations;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertSame;
5 import static org.junit.Assert.assertTrue;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.HashMap;
11 import java.util.List;
13 import javax.annotation.Resource;
14 import org.junit.After;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
21 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
22 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
23 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
24 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
25 import org.openecomp.sdc.be.datatypes.elements.MapDataDefinition;
26 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
27 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
28 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
29 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
30 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
31 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
32 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
33 import org.openecomp.sdc.be.model.Component;
34 import org.openecomp.sdc.be.model.InterfaceDefinition;
35 import org.openecomp.sdc.be.model.LifecycleStateEnum;
36 import org.openecomp.sdc.be.model.ModelTestBase;
37 import org.openecomp.sdc.be.model.Operation;
38 import org.openecomp.sdc.be.model.Service;
39 import org.openecomp.sdc.be.model.category.CategoryDefinition;
40 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
41 import org.openecomp.sdc.be.model.jsontitan.datamodel.NodeType;
42 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
43 import org.openecomp.sdc.be.model.jsontitan.utils.GraphTestUtils;
44 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
45 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
46 import org.openecomp.sdc.common.util.ValidationUtils;
47 import org.springframework.test.context.ContextConfiguration;
48 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
50 @RunWith(SpringJUnit4ClassRunner.class)
51 @ContextConfiguration("classpath:application-context-test.xml")
52 public class InterfaceOperationTest extends ModelTestBase {
54 protected TitanDao titanDao;
57 private InterfaceOperation interfaceOperation;
60 protected NodeTypeOperation nodeTypeOperation;
63 protected TopologyTemplateOperation topologyTemplateOperation;
66 private ToscaElementLifecycleOperation lifecycleOperation;
68 private static final String RESOURCE_NAME = "Resource Name";
69 private static final String RESOURCE_ID = "resourceID";
71 private static final String SERVICE_NAME = "Service Name";
72 private static final String SERVICE_ID = "serviceID";
74 private final String categoryName = "category";
75 private final String subcategory = "mycategory";
77 private GraphVertex ownerVertex;
79 private final Service service = createService();
80 private final org.openecomp.sdc.be.model.Resource resource = createResource();
83 public static void initInterfacesOperation() {
88 public void setupBefore() {
89 GraphTestUtils.clearGraph(titanDao);
91 createResourceCategory();
92 createServiceCategory();
93 GraphTestUtils.createRootCatalogVertex(titanDao);
95 createNodeType("resource", RESOURCE_ID);
96 createNodeType("service", SERVICE_ID);
97 createTopologyTemplate("firstService");
101 public void cleanAfter() {
102 GraphTestUtils.clearGraph(titanDao);
106 public void testAddInterface_Service(){testAddSingleInterface(service);}
109 public void testAddInterface_Resource(){testAddMultipleInterface(resource);}
112 public void testUpdateInterface_Service(){testUpdateInterface(service);}
115 public void testUpdateInterface_Resource(){testUpdateInterface(resource);}
118 public void testDeleteInterface_Service(){testDeleteInterface(service);}
121 public void testDeleteInterface_Resource(){testDeleteInterface(resource);}
124 public void testUpdateInterfaceShouldFailWhenNOtCreatedFirst() {
125 Component component = createResource();
126 InterfaceDefinition interfaceDefinition = buildInterfaceDefinitionWithoutOperation();
127 interfaceDefinition.setOperationsMap(createMockOperationMap());
128 Either<List<InterfaceDefinition>, StorageOperationStatus> res = interfaceOperation.updateInterfaces(component.getUniqueId(), Collections.singletonList(interfaceDefinition));
129 Assert.assertTrue(res.isRight());
132 private void testAddSingleInterface(Component component) {
133 InterfaceDefinition interfaceDefinition = buildInterfaceDefinition("1");
134 Either<List<InterfaceDefinition>, StorageOperationStatus> res = interfaceOperation.addInterfaces(component.getUniqueId(), Collections.singletonList(interfaceDefinition));
135 Assert.assertTrue(res.isLeft());
136 Assert.assertEquals("1", res.left().value().get(0).getUniqueId());
139 private void testAddMultipleInterface(Component component) {
140 InterfaceDefinition interfaceDefinition1 = buildInterfaceDefinition("1");
141 InterfaceDefinition interfaceDefinition2 = buildInterfaceDefinition("2");
142 List<InterfaceDefinition> interfaceDefinitions = new ArrayList<>();
143 interfaceDefinitions.add(interfaceDefinition1);
144 interfaceDefinitions.add(interfaceDefinition2);
145 Either<List<InterfaceDefinition>, StorageOperationStatus> res = interfaceOperation.addInterfaces(component.getUniqueId(), interfaceDefinitions);
146 Assert.assertTrue(res.isLeft());
147 Assert.assertEquals(2, res.left().value().size());
150 private void testUpdateInterface(Component component) {
151 InterfaceDefinition interfaceDefinition = buildInterfaceDefinition("1");
152 Either<List<InterfaceDefinition>, StorageOperationStatus> res = interfaceOperation.addInterfaces(component.getUniqueId(), Collections.singletonList(interfaceDefinition));
153 Assert.assertTrue(res.isLeft());
154 List<InterfaceDefinition> value = res.left().value();
155 InterfaceDefinition createdInterfaceDef = value.get(0);
156 String new_description = "New Description";
157 createdInterfaceDef.setDescription(new_description);
158 res = interfaceOperation.updateInterfaces(component.getUniqueId(), Collections.singletonList(createdInterfaceDef));
159 assertTrue(res.isLeft());
160 assertEquals(new_description,res.left().value().get(0).getDescription());
163 private void testDeleteInterface(Component component) {
164 InterfaceDefinition interfaceDefinition = buildInterfaceDefinition("1");
165 Either<List<InterfaceDefinition>, StorageOperationStatus> res = interfaceOperation.addInterfaces(component.getUniqueId(), Collections.singletonList(interfaceDefinition));
166 Assert.assertTrue(res.isLeft());
167 List<InterfaceDefinition> value = res.left().value();
168 Either<String, StorageOperationStatus> deleteInterfaceOperationRes = interfaceOperation.deleteInterface(component.getUniqueId(), value.get(0).getUniqueId());
169 assertTrue(deleteInterfaceOperationRes.isLeft());
172 private InterfaceDefinition buildInterfaceDefinition(String uniqueId) {
173 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
174 interfaceDefinition.setType("tosca.interfaces.standard");
175 interfaceDefinition.setUniqueId(uniqueId);
176 interfaceDefinition.setOperationsMap(createMockOperationMap());
177 return interfaceDefinition;
180 private InterfaceDefinition buildInterfaceDefinitionWithoutOperation() {
181 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
182 interfaceDefinition.setType("tosca.interfaces.standard");
183 return interfaceDefinition;
186 private org.openecomp.sdc.be.model.Resource createResource() {
187 org.openecomp.sdc.be.model.Resource resource = new org.openecomp.sdc.be.model.Resource();
188 resource.setUniqueId(RESOURCE_ID);
189 resource.setName(RESOURCE_NAME);
190 resource.setDescription("My short description");
191 resource.setInterfaces(createMockInterfaceDefinition());
195 private Service createService() {
196 Service service = new Service();
197 service.setUniqueId(SERVICE_ID);
198 service.setName(SERVICE_NAME);
199 service.setDescription("My short description");
200 service.setInterfaces(createMockInterfaceDefinition());
204 private InterfaceDefinition createInterface(String uniqueID, String description, String type, String toscaResourceName,
205 Map<String, Operation> op) {
206 InterfaceDefinition id = new InterfaceDefinition();
208 id.setDescription(description);
209 id.setUniqueId(uniqueID);
210 id.setToscaResourceName(toscaResourceName);
211 id.setOperationsMap(op);
215 private Map<String, InterfaceDefinition> createMockInterfaceDefinition() {
216 Map<String, Operation> operationMap = createMockOperationMap();
217 Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
218 interfaceDefinitionMap.put("int1", createInterface("int1", "Interface 1",
219 "lifecycle", "tosca", operationMap));
220 return interfaceDefinitionMap;
223 private Map<String, Operation> createMockOperationMap() {
224 Map<String, Operation> operationMap = new HashMap<>();
225 operationMap.put("op1", createMockOperation());
229 private Operation createMockOperation() {
230 Operation operation = new Operation();
231 operation.setDefinition(false);
232 operation.setName("create");
233 operation.setUniqueId("op1");
237 private void createResourceCategory() {
238 GraphVertex cat = new GraphVertex(VertexTypeEnum.RESOURCE_CATEGORY);
239 Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
240 String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.RESOURCE_CATEGORY);
241 cat.setUniqueId(catId);
242 metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId);
243 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_CATEGORY.getName());
244 metadataProperties.put(GraphPropertyEnum.NAME, categoryName);
245 metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName));
246 cat.setMetadataProperties(metadataProperties);
247 cat.updateMetadataJsonWithCurrentMetadataProperties();
249 GraphVertex subCat = new GraphVertex(VertexTypeEnum.RESOURCE_SUBCATEGORY);
250 metadataProperties = new HashMap<>();
251 String subCatId = UniqueIdBuilder.buildSubCategoryUid(cat.getUniqueId(), subcategory);
252 subCat.setUniqueId(subCatId);
253 metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, subCatId);
254 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_SUBCATEGORY.getName());
255 metadataProperties.put(GraphPropertyEnum.NAME, subcategory);
256 metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(subcategory));
257 subCat.setMetadataProperties(metadataProperties);
258 subCat.updateMetadataJsonWithCurrentMetadataProperties();
260 Either<GraphVertex, TitanOperationStatus> catRes = titanDao.createVertex(cat);
261 Either<GraphVertex, TitanOperationStatus> subCatRes = titanDao.createVertex(subCat);
262 titanDao.createEdge(catRes.left().value().getVertex(), subCatRes.left().value().getVertex(), EdgeLabelEnum.SUB_CATEGORY, new HashMap<>());
265 private void createServiceCategory() {
266 GraphVertex cat = new GraphVertex(VertexTypeEnum.SERVICE_CATEGORY);
267 Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
268 String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
269 cat.setUniqueId(catId);
270 metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId);
271 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.SERVICE_CATEGORY.getName());
272 metadataProperties.put(GraphPropertyEnum.NAME, categoryName);
273 metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName));
274 cat.setMetadataProperties(metadataProperties);
275 cat.updateMetadataJsonWithCurrentMetadataProperties();
276 titanDao.createVertex(cat);
279 private void createTopologyTemplate(String name) {
280 TopologyTemplate service = new TopologyTemplate();
281 String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
282 service.setUniqueId(uniqueId);
283 service.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
284 service.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), name);
285 service.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
286 service.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1");
287 service.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name());
288 service.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE);
289 List<CategoryDefinition> categories = new ArrayList<>();
290 CategoryDefinition cat = new CategoryDefinition();
292 cat.setName(categoryName);
293 service.setCategories(categories);
295 service.setComponentType(ComponentTypeEnum.SERVICE);
296 Either<TopologyTemplate, StorageOperationStatus> createRes = topologyTemplateOperation.createTopologyTemplate(service);
297 Either<GraphVertex, TitanOperationStatus> getNodeTyeRes = titanDao.getVertexById(createRes.left().value().getUniqueId());
299 getNodeTyeRes.left().value();
302 private <T extends ToscaDataDefinition> void createNodeType(String nodeTypeName, String uniqueId) {
303 NodeType vf = new NodeType();
304 vf.setUniqueId(uniqueId);
305 vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
306 vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), nodeTypeName);
307 vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
308 vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1");
309 vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name());
310 vf.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE);
311 List<CategoryDefinition> categories = new ArrayList<>();
312 CategoryDefinition cat = new CategoryDefinition();
314 cat.setName(categoryName);
315 List<SubCategoryDefinition> subCategories = new ArrayList<>();
316 SubCategoryDefinition subCat = new SubCategoryDefinition();
317 subCat.setName(subcategory);
318 subCategories.add(subCat);
319 cat.setSubcategories(subCategories);
320 vf.setCategories(categories);
322 List<String> derivedFrom = new ArrayList<>();
323 derivedFrom.add("root");
324 vf.setDerivedFrom(derivedFrom);
326 vf.setComponentType(ComponentTypeEnum.RESOURCE);
327 Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
329 Either<GraphVertex, TitanOperationStatus> getNodeTyeRes = titanDao.getVertexById(createVFRes.left().value().getUniqueId());
331 GraphVertex vfVertex = getNodeTyeRes.left().value();
333 List<PropertyDataDefinition> addProperties = new ArrayList<>();
334 PropertyDataDefinition prop11 = new PropertyDataDefinition();
335 prop11.setName("prop11");
336 prop11.setDefaultValue("def11");
338 addProperties.add(prop11);
340 PropertyDataDefinition prop22 = new PropertyDataDefinition();
341 prop22.setName("prop22");
342 prop22.setDefaultValue("def22");
343 addProperties.add(prop22);
345 StorageOperationStatus status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, addProperties, JsonPresentationFields.NAME);
346 assertSame(StorageOperationStatus.OK, status);
348 PropertyDataDefinition prop33 = new PropertyDataDefinition();
349 prop33.setName("prop33");
350 prop33.setDefaultValue("def33");
352 status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop33, JsonPresentationFields.NAME);
353 assertSame(StorageOperationStatus.OK, status);
355 PropertyDataDefinition prop44 = new PropertyDataDefinition();
356 prop44.setName("prop44");
357 prop44.setDefaultValue("def44");
359 status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop44, JsonPresentationFields.NAME);
360 assertSame(StorageOperationStatus.OK, status);
362 PropertyDataDefinition capProp = new PropertyDataDefinition();
363 capProp.setName("capProp");
364 capProp.setDefaultValue("capPropDef");
366 MapDataDefinition dataToCreate = new MapPropertiesDataDefinition();
367 dataToCreate.put("capProp", capProp);
369 Map<String, MapDataDefinition> capProps = new HashMap<>();
370 capProps.put("capName", dataToCreate);
372 nodeTypeOperation.associateElementToData(
373 vfVertex, VertexTypeEnum.CAPABILITIES_PROPERTIES, EdgeLabelEnum.CAPABILITIES_PROPERTIES, capProps);
375 List<String> pathKeys = new ArrayList<>();
376 pathKeys.add("capName");
377 capProp.setDefaultValue("BBBB");
378 nodeTypeOperation.updateToscaDataDeepElementOfToscaElement(vfVertex, EdgeLabelEnum.CAPABILITIES_PROPERTIES, VertexTypeEnum.CAPABILITIES_PROPERTIES, capProp, pathKeys, JsonPresentationFields.NAME);
381 private void createRootNodeType() {
382 NodeType vf = new NodeType();
383 String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
384 vf.setUniqueId(uniqueId);
385 vf.setComponentType(ComponentTypeEnum.RESOURCE);
386 vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
387 vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), "root");
388 vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
389 vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "1.0");
390 vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VFC.name());
391 vf.getMetadata().put(JsonPresentationFields.LIFECYCLE_STATE.getPresentation(), LifecycleStateEnum.CERTIFIED.name());
392 vf.getMetadata().put(JsonPresentationFields.TOSCA_RESOURCE_NAME.getPresentation(), "root");
393 vf.getMetadata().put(JsonPresentationFields.HIGHEST_VERSION.getPresentation(), true);
395 List<CategoryDefinition> categories = new ArrayList<>();
396 CategoryDefinition cat = new CategoryDefinition();
398 cat.setName(categoryName);
399 List<SubCategoryDefinition> subCategories = new ArrayList<>();
400 SubCategoryDefinition subCat = new SubCategoryDefinition();
401 subCat.setName(subcategory);
402 subCategories.add(subCat);
403 cat.setSubcategories(subCategories);
404 vf.setCategories(categories);
406 List<String> derivedFrom = new ArrayList<>();
407 vf.setDerivedFrom(derivedFrom);
409 Map<String, PropertyDataDefinition> properties = new HashMap<>();
410 PropertyDataDefinition prop1 = new PropertyDataDefinition();
411 prop1.setName("derived1");
412 prop1.setDefaultValue("deriveddef1");
413 properties.put("derived1", prop1);
415 PropertyDataDefinition prop2 = new PropertyDataDefinition();
416 prop2.setUniqueId("derived2");
417 prop2.setName("deriveddef2");
418 properties.put("derived2", prop2);
420 PropertyDataDefinition prop3 = new PropertyDataDefinition();
421 prop3.setName("derived3");
422 prop3.setDefaultValue("deriveddef3");
423 properties.put("derived3", prop3);
425 vf.setProperties(properties);
426 vf.setComponentType(ComponentTypeEnum.RESOURCE);
427 Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
429 Either<GraphVertex, TitanOperationStatus> getNodeTyeRes = titanDao.getVertexById(createVFRes.left().value().getUniqueId());
430 getNodeTyeRes.left().value();
433 private void createUsers() {
434 GraphVertex ownerV = new GraphVertex(VertexTypeEnum.USER);
435 ownerV.setUniqueId("user1");
437 Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
438 metadataProperties.put(GraphPropertyEnum.USERID, ownerV.getUniqueId());
439 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName());
440 metadataProperties.put(GraphPropertyEnum.NAME, "user1");
441 ownerV.setMetadataProperties(metadataProperties);
442 ownerV.updateMetadataJsonWithCurrentMetadataProperties();
443 ownerV.setJson(new HashMap<>());
444 Either<GraphVertex, TitanOperationStatus> createUserRes = titanDao.createVertex(ownerV);
446 ownerVertex = createUserRes.left().value();
448 GraphVertex modifierV = new GraphVertex(VertexTypeEnum.USER);
449 modifierV.setUniqueId("user2");
451 metadataProperties = new HashMap<>();
452 metadataProperties.put(GraphPropertyEnum.USERID, modifierV.getUniqueId());
453 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName());
454 metadataProperties.put(GraphPropertyEnum.NAME, "user2");
455 modifierV.setMetadataProperties(metadataProperties);
456 modifierV.updateMetadataJsonWithCurrentMetadataProperties();
457 modifierV.setJson(new HashMap<>());
458 createUserRes = titanDao.createVertex(modifierV);
459 createUserRes.left().value();
461 lifecycleOperation.findUser(ownerVertex.getUniqueId());
465 public void teardown() {
466 GraphTestUtils.clearGraph(titanDao);