2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertSame;
25 import static org.junit.Assert.assertTrue;
27 import fj.data.Either;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
33 import javax.annotation.Resource;
34 import org.junit.Assert;
35 import org.junit.jupiter.api.AfterEach;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
40 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
41 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
42 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
43 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
44 import org.openecomp.sdc.be.datatypes.elements.MapDataDefinition;
45 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
46 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
47 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
48 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
49 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
50 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
51 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
52 import org.openecomp.sdc.be.model.Component;
53 import org.openecomp.sdc.be.model.InterfaceDefinition;
54 import org.openecomp.sdc.be.model.LifecycleStateEnum;
55 import org.openecomp.sdc.be.model.ModelTestBase;
56 import org.openecomp.sdc.be.model.Operation;
57 import org.openecomp.sdc.be.model.Service;
58 import org.openecomp.sdc.be.model.category.CategoryDefinition;
59 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
60 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.NodeType;
61 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
62 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.GraphTestUtils;
63 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
64 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
65 import org.openecomp.sdc.common.util.ValidationUtils;
66 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
68 @SpringJUnitConfig(locations = "classpath:application-context-test.xml")
69 public class InterfaceOperationTest extends ModelTestBase {
71 private static final String RESOURCE_NAME = "Resource Name";
72 private static final String RESOURCE_ID = "resourceID";
73 private static final String SERVICE_NAME = "Service Name";
74 private static final String SERVICE_ID = "serviceID";
75 private final String categoryName = "category";
76 private final String subcategory = "mycategory";
77 private final Service service = createService();
78 private final org.openecomp.sdc.be.model.Resource resource = createResource();
80 protected JanusGraphDao janusGraphDao;
82 protected NodeTypeOperation nodeTypeOperation;
84 protected TopologyTemplateOperation topologyTemplateOperation;
86 private InterfaceOperation interfaceOperation;
88 private ToscaElementLifecycleOperation lifecycleOperation;
89 private GraphVertex ownerVertex;
92 public static void initInterfacesOperation() {
97 public void setupBefore() {
98 GraphTestUtils.clearGraph(janusGraphDao);
100 createResourceCategory();
101 createServiceCategory();
102 GraphTestUtils.createRootCatalogVertex(janusGraphDao);
103 createRootNodeType();
104 createNodeType("resource", RESOURCE_ID);
105 createNodeType("service", SERVICE_ID);
106 createTopologyTemplate("firstService");
109 private void createUsers() {
110 GraphVertex ownerV = new GraphVertex(VertexTypeEnum.USER);
111 ownerV.setUniqueId("user1");
113 Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
114 metadataProperties.put(GraphPropertyEnum.USERID, ownerV.getUniqueId());
115 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName());
116 metadataProperties.put(GraphPropertyEnum.NAME, "user1");
117 ownerV.setMetadataProperties(metadataProperties);
118 ownerV.updateMetadataJsonWithCurrentMetadataProperties();
119 ownerV.setJson(new HashMap<>());
120 Either<GraphVertex, JanusGraphOperationStatus> createUserRes = janusGraphDao.createVertex(ownerV);
122 ownerVertex = createUserRes.left().value();
124 GraphVertex modifierV = new GraphVertex(VertexTypeEnum.USER);
125 modifierV.setUniqueId("user2");
127 metadataProperties = new HashMap<>();
128 metadataProperties.put(GraphPropertyEnum.USERID, modifierV.getUniqueId());
129 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName());
130 metadataProperties.put(GraphPropertyEnum.NAME, "user2");
131 modifierV.setMetadataProperties(metadataProperties);
132 modifierV.updateMetadataJsonWithCurrentMetadataProperties();
133 modifierV.setJson(new HashMap<>());
134 createUserRes = janusGraphDao.createVertex(modifierV);
135 createUserRes.left().value();
137 lifecycleOperation.findUser(ownerVertex.getUniqueId());
140 private void createResourceCategory() {
141 GraphVertex cat = new GraphVertex(VertexTypeEnum.RESOURCE_CATEGORY);
142 Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
143 String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.RESOURCE_CATEGORY);
144 cat.setUniqueId(catId);
145 metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId);
146 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_CATEGORY.getName());
147 metadataProperties.put(GraphPropertyEnum.NAME, categoryName);
149 .put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName));
150 cat.setMetadataProperties(metadataProperties);
151 cat.updateMetadataJsonWithCurrentMetadataProperties();
153 GraphVertex subCat = new GraphVertex(VertexTypeEnum.RESOURCE_SUBCATEGORY);
154 metadataProperties = new HashMap<>();
155 String subCatId = UniqueIdBuilder.buildSubCategoryUid(cat.getUniqueId(), subcategory);
156 subCat.setUniqueId(subCatId);
157 metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, subCatId);
158 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_SUBCATEGORY.getName());
159 metadataProperties.put(GraphPropertyEnum.NAME, subcategory);
161 .put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(subcategory));
162 subCat.setMetadataProperties(metadataProperties);
163 subCat.updateMetadataJsonWithCurrentMetadataProperties();
165 Either<GraphVertex, JanusGraphOperationStatus> catRes = janusGraphDao.createVertex(cat);
166 Either<GraphVertex, JanusGraphOperationStatus> subCatRes = janusGraphDao.createVertex(subCat);
167 janusGraphDao.createEdge(catRes.left().value().getVertex(), subCatRes.left().value().getVertex(),
168 EdgeLabelEnum.SUB_CATEGORY, new HashMap<>());
171 private void createServiceCategory() {
172 GraphVertex cat = new GraphVertex(VertexTypeEnum.SERVICE_CATEGORY);
173 Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
174 String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
175 cat.setUniqueId(catId);
176 metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId);
177 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.SERVICE_CATEGORY.getName());
178 metadataProperties.put(GraphPropertyEnum.NAME, categoryName);
180 .put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName));
181 cat.setMetadataProperties(metadataProperties);
182 cat.updateMetadataJsonWithCurrentMetadataProperties();
183 janusGraphDao.createVertex(cat);
186 private void createRootNodeType() {
187 NodeType vf = new NodeType();
188 String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
189 vf.setUniqueId(uniqueId);
190 vf.setComponentType(ComponentTypeEnum.RESOURCE);
191 vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
192 vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), "root");
193 vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
194 vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "1.0");
195 vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VFC.name());
197 .put(JsonPresentationFields.LIFECYCLE_STATE.getPresentation(), LifecycleStateEnum.CERTIFIED.name());
198 vf.getMetadata().put(JsonPresentationFields.TOSCA_RESOURCE_NAME.getPresentation(), "root");
199 vf.getMetadata().put(JsonPresentationFields.HIGHEST_VERSION.getPresentation(), true);
201 List<CategoryDefinition> categories = new ArrayList<>();
202 CategoryDefinition cat = new CategoryDefinition();
204 cat.setName(categoryName);
205 List<SubCategoryDefinition> subCategories = new ArrayList<>();
206 SubCategoryDefinition subCat = new SubCategoryDefinition();
207 subCat.setName(subcategory);
208 subCategories.add(subCat);
209 cat.setSubcategories(subCategories);
210 vf.setCategories(categories);
212 List<String> derivedFrom = new ArrayList<>();
213 vf.setDerivedFrom(derivedFrom);
215 Map<String, PropertyDataDefinition> properties = new HashMap<>();
216 PropertyDataDefinition prop1 = new PropertyDataDefinition();
217 prop1.setName("derived1");
218 prop1.setDefaultValue("deriveddef1");
219 properties.put("derived1", prop1);
221 PropertyDataDefinition prop2 = new PropertyDataDefinition();
222 prop2.setUniqueId("derived2");
223 prop2.setName("deriveddef2");
224 properties.put("derived2", prop2);
226 PropertyDataDefinition prop3 = new PropertyDataDefinition();
227 prop3.setName("derived3");
228 prop3.setDefaultValue("deriveddef3");
229 properties.put("derived3", prop3);
231 vf.setProperties(properties);
232 vf.setComponentType(ComponentTypeEnum.RESOURCE);
233 Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
235 Either<GraphVertex, JanusGraphOperationStatus> getNodeTyeRes =
236 janusGraphDao.getVertexById(createVFRes.left().value().getUniqueId());
237 getNodeTyeRes.left().value();
240 private <T extends ToscaDataDefinition> void createNodeType(String nodeTypeName, String uniqueId) {
241 NodeType vf = new NodeType();
242 vf.setUniqueId(uniqueId);
243 vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
244 vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), nodeTypeName);
245 vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
246 vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1");
247 vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name());
248 vf.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE);
249 List<CategoryDefinition> categories = new ArrayList<>();
250 CategoryDefinition cat = new CategoryDefinition();
252 cat.setName(categoryName);
253 List<SubCategoryDefinition> subCategories = new ArrayList<>();
254 SubCategoryDefinition subCat = new SubCategoryDefinition();
255 subCat.setName(subcategory);
256 subCategories.add(subCat);
257 cat.setSubcategories(subCategories);
258 vf.setCategories(categories);
260 List<String> derivedFrom = new ArrayList<>();
261 derivedFrom.add("root");
262 vf.setDerivedFrom(derivedFrom);
264 vf.setComponentType(ComponentTypeEnum.RESOURCE);
266 List<PropertyDataDefinition> addProperties = new ArrayList<>();
267 PropertyDataDefinition prop11 = new PropertyDataDefinition();
268 prop11.setName("prop11");
269 prop11.setDefaultValue("def11");
271 addProperties.add(prop11);
273 PropertyDataDefinition prop22 = new PropertyDataDefinition();
274 prop22.setName("prop22");
275 prop22.setDefaultValue("def22");
276 addProperties.add(prop22);
278 Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
279 Either<GraphVertex, JanusGraphOperationStatus> getNodeTyeRes =
280 janusGraphDao.getVertexById(createVFRes.left().value().getUniqueId());
281 GraphVertex vfVertex = getNodeTyeRes.left().value();
282 StorageOperationStatus status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES,
283 VertexTypeEnum.PROPERTIES, addProperties, JsonPresentationFields.NAME);
284 assertSame(StorageOperationStatus.OK, status);
286 PropertyDataDefinition prop33 = new PropertyDataDefinition();
287 prop33.setName("prop33");
288 prop33.setDefaultValue("def33");
290 status = nodeTypeOperation
291 .addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES,
292 prop33, JsonPresentationFields.NAME);
293 assertSame(StorageOperationStatus.OK, status);
295 PropertyDataDefinition prop44 = new PropertyDataDefinition();
296 prop44.setName("prop44");
297 prop44.setDefaultValue("def44");
299 status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex.getUniqueId(), EdgeLabelEnum.PROPERTIES,
300 VertexTypeEnum.PROPERTIES, prop44, JsonPresentationFields.NAME);
301 assertSame(StorageOperationStatus.OK, status);
303 PropertyDataDefinition capProp = new PropertyDataDefinition();
304 capProp.setName("capProp");
305 capProp.setDefaultValue("capPropDef");
307 MapDataDefinition dataToCreate = new MapPropertiesDataDefinition();
308 dataToCreate.put("capProp", capProp);
310 Map<String, MapDataDefinition> capProps = new HashMap<>();
311 capProps.put("capName", dataToCreate);
313 nodeTypeOperation.associateElementToData(vfVertex, VertexTypeEnum.CAPABILITIES_PROPERTIES,
314 EdgeLabelEnum.CAPABILITIES_PROPERTIES, capProps);
316 List<String> pathKeys = new ArrayList<>();
317 pathKeys.add("capName");
318 capProp.setDefaultValue("BBBB");
319 nodeTypeOperation.updateToscaDataDeepElementOfToscaElement(vfVertex, EdgeLabelEnum.CAPABILITIES_PROPERTIES,
320 VertexTypeEnum.CAPABILITIES_PROPERTIES, capProp, pathKeys, JsonPresentationFields.NAME);
323 private void createTopologyTemplate(String name) {
324 TopologyTemplate service = new TopologyTemplate();
325 String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
326 service.setUniqueId(uniqueId);
327 service.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
328 service.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), name);
329 service.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
330 service.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1");
331 service.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name());
332 service.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE);
333 List<CategoryDefinition> categories = new ArrayList<>();
334 CategoryDefinition cat = new CategoryDefinition();
336 cat.setName(categoryName);
337 service.setCategories(categories);
339 service.setComponentType(ComponentTypeEnum.SERVICE);
340 Either<TopologyTemplate, StorageOperationStatus> createRes =
341 topologyTemplateOperation.createTopologyTemplate(service);
342 Either<GraphVertex, JanusGraphOperationStatus> getNodeTyeRes =
343 janusGraphDao.getVertexById(createRes.left().value().getUniqueId());
345 getNodeTyeRes.left().value();
349 public void cleanAfter() {
350 GraphTestUtils.clearGraph(janusGraphDao);
354 public void testAddInterface_Service() {
355 testAddSingleInterface(service);
358 private void testAddSingleInterface(Component component) {
359 InterfaceDefinition interfaceDefinition = buildInterfaceDefinition("1");
360 final var res = interfaceOperation.addInterfaces(component, Collections.singletonList(interfaceDefinition));
361 Assert.assertTrue(res.isLeft());
362 Assert.assertEquals("1", res.left().value().get(0).getUniqueId());
365 private InterfaceDefinition buildInterfaceDefinition(String uniqueId) {
366 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
367 interfaceDefinition.setType("tosca.interfaces.standard");
368 interfaceDefinition.setUniqueId(uniqueId);
369 interfaceDefinition.setOperationsMap(createMockOperationMap());
370 return interfaceDefinition;
373 private Map<String, Operation> createMockOperationMap() {
374 Map<String, Operation> operationMap = new HashMap<>();
375 operationMap.put("op1", createMockOperation());
379 private Operation createMockOperation() {
380 Operation operation = new Operation();
381 operation.setDefinition(false);
382 operation.setName("create");
383 operation.setUniqueId("op1");
388 public void testAddInterface_Resource() {
389 testAddMultipleInterface(resource);
392 private void testAddMultipleInterface(Component component) {
393 InterfaceDefinition interfaceDefinition1 = buildInterfaceDefinition("1");
394 InterfaceDefinition interfaceDefinition2 = buildInterfaceDefinition("2");
395 List<InterfaceDefinition> interfaceDefinitions = new ArrayList<>();
396 interfaceDefinitions.add(interfaceDefinition1);
397 interfaceDefinitions.add(interfaceDefinition2);
398 final var res = interfaceOperation.addInterfaces(component, interfaceDefinitions);
399 Assert.assertTrue(res.isLeft());
400 Assert.assertEquals(2, res.left().value().size());
404 public void testUpdateInterface_Service() {
405 testUpdateInterface(service);
408 private void testUpdateInterface(Component component) {
409 InterfaceDefinition interfaceDefinition = buildInterfaceDefinition("1");
410 var res = interfaceOperation.addInterfaces(component, Collections.singletonList(interfaceDefinition));
411 Assert.assertTrue(res.isLeft());
412 List<InterfaceDefinition> value = res.left().value();
413 InterfaceDefinition createdInterfaceDef = value.get(0);
414 String newDescription = "New Description";
415 createdInterfaceDef.setDescription(newDescription);
416 res = interfaceOperation.updateInterfaces(component, Collections.singletonList(createdInterfaceDef));
417 assertTrue(res.isLeft());
418 assertEquals(newDescription, res.left().value().get(0).getDescription());
422 public void testUpdateInterface_Resource() {
423 testUpdateInterface(resource);
427 public void testDeleteInterface_Service() {
428 testDeleteInterface(service);
431 private void testDeleteInterface(Component component) {
432 InterfaceDefinition interfaceDefinition = buildInterfaceDefinition("1");
433 var res = interfaceOperation.addInterfaces(component, Collections.singletonList(interfaceDefinition));
434 Assert.assertTrue(res.isLeft());
435 List<InterfaceDefinition> value = res.left().value();
436 final var deleteInterfaceOperationRes = interfaceOperation.deleteInterface(component, value.get(0).getUniqueId());
437 assertTrue(deleteInterfaceOperationRes.isLeft());
441 public void testDeleteInterface_Resource() {
442 testDeleteInterface(resource);
446 public void testUpdateInterfaceShouldFailWhenNOtCreatedFirst() {
447 Component component = createResource();
448 InterfaceDefinition interfaceDefinition = buildInterfaceDefinitionWithoutOperation();
449 interfaceDefinition.setOperationsMap(createMockOperationMap());
450 final var res = interfaceOperation.updateInterfaces(component, Collections.singletonList(interfaceDefinition));
451 Assert.assertTrue(res.isRight());
454 private InterfaceDefinition buildInterfaceDefinitionWithoutOperation() {
455 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
456 interfaceDefinition.setType("tosca.interfaces.standard");
457 return interfaceDefinition;
460 private org.openecomp.sdc.be.model.Resource createResource() {
461 org.openecomp.sdc.be.model.Resource resource = new org.openecomp.sdc.be.model.Resource();
462 resource.setUniqueId(RESOURCE_ID);
463 resource.setName(RESOURCE_NAME);
464 resource.setDescription("My short description");
465 resource.setInterfaces(createMockInterfaceDefinition());
469 private Service createService() {
470 Service service = new Service();
471 service.setUniqueId(SERVICE_ID);
472 service.setName(SERVICE_NAME);
473 service.setDescription("My short description");
474 service.setInterfaces(createMockInterfaceDefinition());
478 private Map<String, InterfaceDefinition> createMockInterfaceDefinition() {
479 Map<String, Operation> operationMap = createMockOperationMap();
480 Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
481 interfaceDefinitionMap.put("int1", createInterface("int1", "Interface 1", "lifecycle", "tosca", operationMap));
482 return interfaceDefinitionMap;
485 private InterfaceDefinition createInterface(String uniqueId, String description, String type,
486 String toscaResourceName, Map<String, Operation> op) {
487 InterfaceDefinition id = new InterfaceDefinition();
489 id.setDescription(description);
490 id.setUniqueId(uniqueId);
491 id.setToscaResourceName(toscaResourceName);
492 id.setOperationsMap(op);
497 public void teardown() {
498 GraphTestUtils.clearGraph(janusGraphDao);