1 package org.openecomp.sdc.be.model.jsonjanusgraph.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.janusgraph.JanusGraphOperationStatus;
21 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
22 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
23 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
24 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
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.jsonjanusgraph.datamodel.NodeType;
42 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
43 import org.openecomp.sdc.be.model.jsonjanusgraph.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 private static final String RESOURCE_NAME = "Resource Name";
55 private static final String RESOURCE_ID = "resourceID";
56 private static final String SERVICE_NAME = "Service Name";
57 private static final String SERVICE_ID = "serviceID";
58 private final String categoryName = "category";
59 private final String subcategory = "mycategory";
60 private final Service service = createService();
61 private final org.openecomp.sdc.be.model.Resource resource = createResource();
63 protected JanusGraphDao janusGraphDao;
65 protected NodeTypeOperation nodeTypeOperation;
67 protected TopologyTemplateOperation topologyTemplateOperation;
69 private InterfaceOperation interfaceOperation;
71 private ToscaElementLifecycleOperation lifecycleOperation;
72 private GraphVertex ownerVertex;
75 public static void initInterfacesOperation() {
80 public void setupBefore() {
81 GraphTestUtils.clearGraph(janusGraphDao);
83 createResourceCategory();
84 createServiceCategory();
85 GraphTestUtils.createRootCatalogVertex(janusGraphDao);
87 createNodeType("resource", RESOURCE_ID);
88 createNodeType("service", SERVICE_ID);
89 createTopologyTemplate("firstService");
92 private void createUsers() {
93 GraphVertex ownerV = new GraphVertex(VertexTypeEnum.USER);
94 ownerV.setUniqueId("user1");
96 Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
97 metadataProperties.put(GraphPropertyEnum.USERID, ownerV.getUniqueId());
98 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName());
99 metadataProperties.put(GraphPropertyEnum.NAME, "user1");
100 ownerV.setMetadataProperties(metadataProperties);
101 ownerV.updateMetadataJsonWithCurrentMetadataProperties();
102 ownerV.setJson(new HashMap<>());
103 Either<GraphVertex, JanusGraphOperationStatus> createUserRes = janusGraphDao.createVertex(ownerV);
105 ownerVertex = createUserRes.left().value();
107 GraphVertex modifierV = new GraphVertex(VertexTypeEnum.USER);
108 modifierV.setUniqueId("user2");
110 metadataProperties = new HashMap<>();
111 metadataProperties.put(GraphPropertyEnum.USERID, modifierV.getUniqueId());
112 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName());
113 metadataProperties.put(GraphPropertyEnum.NAME, "user2");
114 modifierV.setMetadataProperties(metadataProperties);
115 modifierV.updateMetadataJsonWithCurrentMetadataProperties();
116 modifierV.setJson(new HashMap<>());
117 createUserRes = janusGraphDao.createVertex(modifierV);
118 createUserRes.left().value();
120 lifecycleOperation.findUser(ownerVertex.getUniqueId());
123 private void createResourceCategory() {
124 GraphVertex cat = new GraphVertex(VertexTypeEnum.RESOURCE_CATEGORY);
125 Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
126 String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.RESOURCE_CATEGORY);
127 cat.setUniqueId(catId);
128 metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId);
129 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_CATEGORY.getName());
130 metadataProperties.put(GraphPropertyEnum.NAME, categoryName);
132 .put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName));
133 cat.setMetadataProperties(metadataProperties);
134 cat.updateMetadataJsonWithCurrentMetadataProperties();
136 GraphVertex subCat = new GraphVertex(VertexTypeEnum.RESOURCE_SUBCATEGORY);
137 metadataProperties = new HashMap<>();
138 String subCatId = UniqueIdBuilder.buildSubCategoryUid(cat.getUniqueId(), subcategory);
139 subCat.setUniqueId(subCatId);
140 metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, subCatId);
141 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_SUBCATEGORY.getName());
142 metadataProperties.put(GraphPropertyEnum.NAME, subcategory);
144 .put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(subcategory));
145 subCat.setMetadataProperties(metadataProperties);
146 subCat.updateMetadataJsonWithCurrentMetadataProperties();
148 Either<GraphVertex, JanusGraphOperationStatus> catRes = janusGraphDao.createVertex(cat);
149 Either<GraphVertex, JanusGraphOperationStatus> subCatRes = janusGraphDao.createVertex(subCat);
150 janusGraphDao.createEdge(catRes.left().value().getVertex(), subCatRes.left().value().getVertex(),
151 EdgeLabelEnum.SUB_CATEGORY, new HashMap<>());
154 private void createServiceCategory() {
155 GraphVertex cat = new GraphVertex(VertexTypeEnum.SERVICE_CATEGORY);
156 Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
157 String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
158 cat.setUniqueId(catId);
159 metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId);
160 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.SERVICE_CATEGORY.getName());
161 metadataProperties.put(GraphPropertyEnum.NAME, categoryName);
163 .put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName));
164 cat.setMetadataProperties(metadataProperties);
165 cat.updateMetadataJsonWithCurrentMetadataProperties();
166 janusGraphDao.createVertex(cat);
169 private void createRootNodeType() {
170 NodeType vf = new NodeType();
171 String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
172 vf.setUniqueId(uniqueId);
173 vf.setComponentType(ComponentTypeEnum.RESOURCE);
174 vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
175 vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), "root");
176 vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
177 vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "1.0");
178 vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VFC.name());
180 .put(JsonPresentationFields.LIFECYCLE_STATE.getPresentation(), LifecycleStateEnum.CERTIFIED.name());
181 vf.getMetadata().put(JsonPresentationFields.TOSCA_RESOURCE_NAME.getPresentation(), "root");
182 vf.getMetadata().put(JsonPresentationFields.HIGHEST_VERSION.getPresentation(), true);
184 List<CategoryDefinition> categories = new ArrayList<>();
185 CategoryDefinition cat = new CategoryDefinition();
187 cat.setName(categoryName);
188 List<SubCategoryDefinition> subCategories = new ArrayList<>();
189 SubCategoryDefinition subCat = new SubCategoryDefinition();
190 subCat.setName(subcategory);
191 subCategories.add(subCat);
192 cat.setSubcategories(subCategories);
193 vf.setCategories(categories);
195 List<String> derivedFrom = new ArrayList<>();
196 vf.setDerivedFrom(derivedFrom);
198 Map<String, PropertyDataDefinition> properties = new HashMap<>();
199 PropertyDataDefinition prop1 = new PropertyDataDefinition();
200 prop1.setName("derived1");
201 prop1.setDefaultValue("deriveddef1");
202 properties.put("derived1", prop1);
204 PropertyDataDefinition prop2 = new PropertyDataDefinition();
205 prop2.setUniqueId("derived2");
206 prop2.setName("deriveddef2");
207 properties.put("derived2", prop2);
209 PropertyDataDefinition prop3 = new PropertyDataDefinition();
210 prop3.setName("derived3");
211 prop3.setDefaultValue("deriveddef3");
212 properties.put("derived3", prop3);
214 vf.setProperties(properties);
215 vf.setComponentType(ComponentTypeEnum.RESOURCE);
216 Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
218 Either<GraphVertex, JanusGraphOperationStatus> getNodeTyeRes =
219 janusGraphDao.getVertexById(createVFRes.left().value().getUniqueId());
220 getNodeTyeRes.left().value();
223 private <T extends ToscaDataDefinition> void createNodeType(String nodeTypeName, String uniqueId) {
224 NodeType vf = new NodeType();
225 vf.setUniqueId(uniqueId);
226 vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
227 vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), nodeTypeName);
228 vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
229 vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1");
230 vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name());
231 vf.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE);
232 List<CategoryDefinition> categories = new ArrayList<>();
233 CategoryDefinition cat = new CategoryDefinition();
235 cat.setName(categoryName);
236 List<SubCategoryDefinition> subCategories = new ArrayList<>();
237 SubCategoryDefinition subCat = new SubCategoryDefinition();
238 subCat.setName(subcategory);
239 subCategories.add(subCat);
240 cat.setSubcategories(subCategories);
241 vf.setCategories(categories);
243 List<String> derivedFrom = new ArrayList<>();
244 derivedFrom.add("root");
245 vf.setDerivedFrom(derivedFrom);
247 vf.setComponentType(ComponentTypeEnum.RESOURCE);
249 List<PropertyDataDefinition> addProperties = new ArrayList<>();
250 PropertyDataDefinition prop11 = new PropertyDataDefinition();
251 prop11.setName("prop11");
252 prop11.setDefaultValue("def11");
254 addProperties.add(prop11);
256 PropertyDataDefinition prop22 = new PropertyDataDefinition();
257 prop22.setName("prop22");
258 prop22.setDefaultValue("def22");
259 addProperties.add(prop22);
261 Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
262 Either<GraphVertex, JanusGraphOperationStatus> getNodeTyeRes =
263 janusGraphDao.getVertexById(createVFRes.left().value().getUniqueId());
264 GraphVertex vfVertex = getNodeTyeRes.left().value();
265 StorageOperationStatus status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES,
266 VertexTypeEnum.PROPERTIES, addProperties, JsonPresentationFields.NAME);
267 assertSame(StorageOperationStatus.OK, status);
269 PropertyDataDefinition prop33 = new PropertyDataDefinition();
270 prop33.setName("prop33");
271 prop33.setDefaultValue("def33");
273 status = nodeTypeOperation
274 .addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES,
275 prop33, JsonPresentationFields.NAME);
276 assertSame(StorageOperationStatus.OK, status);
278 PropertyDataDefinition prop44 = new PropertyDataDefinition();
279 prop44.setName("prop44");
280 prop44.setDefaultValue("def44");
282 status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex.getUniqueId(), EdgeLabelEnum.PROPERTIES,
283 VertexTypeEnum.PROPERTIES, prop44, JsonPresentationFields.NAME);
284 assertSame(StorageOperationStatus.OK, status);
286 PropertyDataDefinition capProp = new PropertyDataDefinition();
287 capProp.setName("capProp");
288 capProp.setDefaultValue("capPropDef");
290 MapDataDefinition dataToCreate = new MapPropertiesDataDefinition();
291 dataToCreate.put("capProp", capProp);
293 Map<String, MapDataDefinition> capProps = new HashMap<>();
294 capProps.put("capName", dataToCreate);
296 nodeTypeOperation.associateElementToData(vfVertex, VertexTypeEnum.CAPABILITIES_PROPERTIES,
297 EdgeLabelEnum.CAPABILITIES_PROPERTIES, capProps);
299 List<String> pathKeys = new ArrayList<>();
300 pathKeys.add("capName");
301 capProp.setDefaultValue("BBBB");
302 nodeTypeOperation.updateToscaDataDeepElementOfToscaElement(vfVertex, EdgeLabelEnum.CAPABILITIES_PROPERTIES,
303 VertexTypeEnum.CAPABILITIES_PROPERTIES, capProp, pathKeys, JsonPresentationFields.NAME);
306 private void createTopologyTemplate(String name) {
307 TopologyTemplate service = new TopologyTemplate();
308 String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
309 service.setUniqueId(uniqueId);
310 service.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
311 service.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), name);
312 service.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
313 service.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1");
314 service.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name());
315 service.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE);
316 List<CategoryDefinition> categories = new ArrayList<>();
317 CategoryDefinition cat = new CategoryDefinition();
319 cat.setName(categoryName);
320 service.setCategories(categories);
322 service.setComponentType(ComponentTypeEnum.SERVICE);
323 Either<TopologyTemplate, StorageOperationStatus> createRes =
324 topologyTemplateOperation.createTopologyTemplate(service);
325 Either<GraphVertex, JanusGraphOperationStatus> getNodeTyeRes =
326 janusGraphDao.getVertexById(createRes.left().value().getUniqueId());
328 getNodeTyeRes.left().value();
332 public void cleanAfter() {
333 GraphTestUtils.clearGraph(janusGraphDao);
337 public void testAddInterface_Service() {
338 testAddSingleInterface(service);
341 private void testAddSingleInterface(Component component) {
342 InterfaceDefinition interfaceDefinition = buildInterfaceDefinition("1");
343 Either<List<InterfaceDefinition>, StorageOperationStatus> res = interfaceOperation
344 .addInterfaces(component.getUniqueId(),
345 Collections.singletonList(
346 interfaceDefinition));
347 Assert.assertTrue(res.isLeft());
348 Assert.assertEquals("1", res.left().value().get(0).getUniqueId());
351 private InterfaceDefinition buildInterfaceDefinition(String uniqueId) {
352 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
353 interfaceDefinition.setType("tosca.interfaces.standard");
354 interfaceDefinition.setUniqueId(uniqueId);
355 interfaceDefinition.setOperationsMap(createMockOperationMap());
356 return interfaceDefinition;
359 private Map<String, Operation> createMockOperationMap() {
360 Map<String, Operation> operationMap = new HashMap<>();
361 operationMap.put("op1", createMockOperation());
365 private Operation createMockOperation() {
366 Operation operation = new Operation();
367 operation.setDefinition(false);
368 operation.setName("create");
369 operation.setUniqueId("op1");
374 public void testAddInterface_Resource() {
375 testAddMultipleInterface(resource);
378 private void testAddMultipleInterface(Component component) {
379 InterfaceDefinition interfaceDefinition1 = buildInterfaceDefinition("1");
380 InterfaceDefinition interfaceDefinition2 = buildInterfaceDefinition("2");
381 List<InterfaceDefinition> interfaceDefinitions = new ArrayList<>();
382 interfaceDefinitions.add(interfaceDefinition1);
383 interfaceDefinitions.add(interfaceDefinition2);
384 Either<List<InterfaceDefinition>, StorageOperationStatus> res =
385 interfaceOperation.addInterfaces(component.getUniqueId(), interfaceDefinitions);
386 Assert.assertTrue(res.isLeft());
387 Assert.assertEquals(2, res.left().value().size());
391 public void testUpdateInterface_Service() {
392 testUpdateInterface(service);
395 private void testUpdateInterface(Component component) {
396 InterfaceDefinition interfaceDefinition = buildInterfaceDefinition("1");
397 Either<List<InterfaceDefinition>, StorageOperationStatus> res = interfaceOperation
398 .addInterfaces(component.getUniqueId(),
399 Collections.singletonList(
400 interfaceDefinition));
401 Assert.assertTrue(res.isLeft());
402 List<InterfaceDefinition> value = res.left().value();
403 InterfaceDefinition createdInterfaceDef = value.get(0);
404 String newDescription = "New Description";
405 createdInterfaceDef.setDescription(newDescription);
406 res = interfaceOperation
407 .updateInterfaces(component.getUniqueId(), Collections.singletonList(createdInterfaceDef));
408 assertTrue(res.isLeft());
409 assertEquals(newDescription, res.left().value().get(0).getDescription());
413 public void testUpdateInterface_Resource() {
414 testUpdateInterface(resource);
418 public void testDeleteInterface_Service() {
419 testDeleteInterface(service);
422 private void testDeleteInterface(Component component) {
423 InterfaceDefinition interfaceDefinition = buildInterfaceDefinition("1");
424 Either<List<InterfaceDefinition>, StorageOperationStatus> res = interfaceOperation
425 .addInterfaces(component.getUniqueId(),
426 Collections.singletonList(
427 interfaceDefinition));
428 Assert.assertTrue(res.isLeft());
429 List<InterfaceDefinition> value = res.left().value();
430 Either<String, StorageOperationStatus> deleteInterfaceOperationRes =
431 interfaceOperation.deleteInterface(component.getUniqueId(), value.get(0).getUniqueId());
432 assertTrue(deleteInterfaceOperationRes.isLeft());
436 public void testDeleteInterface_Resource() {
437 testDeleteInterface(resource);
441 public void testUpdateInterfaceShouldFailWhenNOtCreatedFirst() {
442 Component component = createResource();
443 InterfaceDefinition interfaceDefinition = buildInterfaceDefinitionWithoutOperation();
444 interfaceDefinition.setOperationsMap(createMockOperationMap());
445 Either<List<InterfaceDefinition>, StorageOperationStatus> res = interfaceOperation.updateInterfaces(
446 component.getUniqueId(), Collections.singletonList(interfaceDefinition));
447 Assert.assertTrue(res.isRight());
450 private InterfaceDefinition buildInterfaceDefinitionWithoutOperation() {
451 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
452 interfaceDefinition.setType("tosca.interfaces.standard");
453 return interfaceDefinition;
456 private org.openecomp.sdc.be.model.Resource createResource() {
457 org.openecomp.sdc.be.model.Resource resource = new org.openecomp.sdc.be.model.Resource();
458 resource.setUniqueId(RESOURCE_ID);
459 resource.setName(RESOURCE_NAME);
460 resource.setDescription("My short description");
461 resource.setInterfaces(createMockInterfaceDefinition());
465 private Service createService() {
466 Service service = new Service();
467 service.setUniqueId(SERVICE_ID);
468 service.setName(SERVICE_NAME);
469 service.setDescription("My short description");
470 service.setInterfaces(createMockInterfaceDefinition());
474 private Map<String, InterfaceDefinition> createMockInterfaceDefinition() {
475 Map<String, Operation> operationMap = createMockOperationMap();
476 Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
477 interfaceDefinitionMap.put("int1", createInterface("int1", "Interface 1", "lifecycle", "tosca", operationMap));
478 return interfaceDefinitionMap;
481 private InterfaceDefinition createInterface(String uniqueId, String description, String type,
482 String toscaResourceName, Map<String, Operation> op) {
483 InterfaceDefinition id = new InterfaceDefinition();
485 id.setDescription(description);
486 id.setUniqueId(uniqueId);
487 id.setToscaResourceName(toscaResourceName);
488 id.setOperationsMap(op);
493 public void teardown() {
494 GraphTestUtils.clearGraph(janusGraphDao);