3 * Copyright (c) 2018 AT&T Intellectual Property.
7 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
27 * limitations under the License.
30 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
32 import fj.data.Either;
33 import org.apache.commons.lang3.tuple.ImmutablePair;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.ArgumentMatchers;
41 import org.mockito.MockitoAnnotations;
42 import org.mockito.junit.MockitoJUnitRunner;
43 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
44 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
45 import org.openecomp.sdc.be.dao.jsongraph.HealingJanusGraphDao;
46 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
47 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
48 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
49 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
50 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
51 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
52 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
53 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
54 import org.openecomp.sdc.be.model.Component;
55 import org.openecomp.sdc.be.model.ComponentInstance;
56 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
57 import org.openecomp.sdc.be.model.Resource;
58 import org.openecomp.sdc.be.model.LifecycleStateEnum;
59 import org.openecomp.sdc.be.model.ComponentParametersView;
60 import org.openecomp.sdc.be.model.PolicyDefinition;
61 import org.openecomp.sdc.be.model.Service;
62 import org.openecomp.sdc.be.model.User;
63 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.NodeType;
64 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
65 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
66 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
67 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
68 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
69 import org.openecomp.sdc.be.model.DataTypeDefinition;
70 import org.openecomp.sdc.be.model.PropertyDefinition;
71 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
73 import static org.assertj.core.api.Assertions.assertThat;
75 import java.util.HashMap;
76 import java.util.List;
78 import java.util.ArrayList;
79 import java.util.EnumMap;
81 import java.util.HashSet;
82 import java.util.stream.Collectors;
83 import java.util.stream.IntStream;
84 import java.util.Collections;
85 import java.util.Arrays;
87 import static org.junit.Assert.assertEquals;
88 import static org.junit.Assert.assertSame;
89 import static org.junit.Assert.assertTrue;
90 import static org.mockito.Mockito.times;
91 import static org.mockito.Mockito.verify;
92 import static org.mockito.Mockito.when;
93 import static org.mockito.ArgumentMatchers.any;
94 import static org.mockito.ArgumentMatchers.anyMap;
95 import static org.mockito.ArgumentMatchers.anyInt;
96 import static org.mockito.ArgumentMatchers.eq;
98 @RunWith(MockitoJUnitRunner.class)
99 public class ToscaOperationFacadeTest {
100 private static final String COMPONENT_ID = "componentId";
101 private static final String PROPERTY1_NAME = "prop1";
102 private static final String PROPERTY1_TYPE = "string";
103 private static final String PROPERTY2_NAME = "prop2";
104 private static final String PROPERTY2_TYPE = "integer";
105 private static final String ICON_NAME = "icon";
106 private static final String SERVICE_MODEL_NAME = "Test_Service";
107 private static final String SERVICE_PROXY_INSTANCE0_NAME = "testservice_proxy0";
110 private ToscaOperationFacade testInstance;
113 private HealingJanusGraphDao janusGraphDaoMock;
116 private TopologyTemplateOperation topologyTemplateOperationMock;
119 private NodeTypeOperation nodeTypeOperation;
122 private NodeTemplateOperation nodeTemplateOperationMock;
125 public void setUp() throws Exception {
126 testInstance = new ToscaOperationFacade();
127 MockitoAnnotations.initMocks(this);
130 @SuppressWarnings("unchecked")
132 public void fetchMetaDataByResourceType() throws Exception {
133 ArgumentCaptor<Map> criteriaCapture = ArgumentCaptor.forClass(Map.class);
134 ArgumentCaptor<Map> criteriaNotCapture = ArgumentCaptor.forClass(Map.class);
135 ComponentParametersView dataFilter = new ComponentParametersView();
136 List<GraphVertex> mockVertices = getMockVertices(2);
137 Either<List<GraphVertex>, JanusGraphOperationStatus> returnedVertices = Either.left(mockVertices);
139 when(janusGraphDaoMock.getByCriteria(eq(null), criteriaCapture.capture(), criteriaNotCapture.capture(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(returnedVertices);
140 when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(0), dataFilter)).thenReturn(Either.left(getResourceToscaElement("0")));
141 when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(1), dataFilter)).thenReturn(Either.left(getResourceToscaElement("1")));
142 Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), dataFilter);
144 verifyCriteriaForHighestVersionAndVfResourceType(criteriaCapture);
145 verifyCriteriaNotIsDeleted(criteriaNotCapture);
147 assertTrue(fetchedComponents.isLeft());
148 List<Component> cmpts = fetchedComponents.left().value();
149 assertEquals(2, cmpts.size());
150 assertEquals("0", cmpts.get(0).getUniqueId());
151 assertEquals("1", cmpts.get(1).getUniqueId());
154 private void verifyCriteriaForHighestVersionAndVfResourceType(ArgumentCaptor<Map> criteriaCapture) {
155 Map<GraphPropertyEnum, Object> criteria = (Map<GraphPropertyEnum, Object>)criteriaCapture.getValue();
156 assertEquals(2, criteria.size());
157 assertEquals(criteria.get(GraphPropertyEnum.RESOURCE_TYPE), "VF");
158 assertEquals(criteria.get(GraphPropertyEnum.IS_HIGHEST_VERSION), true);
161 private void verifyCriteriaNotIsDeleted(ArgumentCaptor<Map> criteriaNotCapture) {
162 Map<GraphPropertyEnum, Object> notCriteria = (Map<GraphPropertyEnum, Object>)criteriaNotCapture.getValue();
163 assertEquals(1, notCriteria.size());
164 assertEquals(notCriteria.get(GraphPropertyEnum.IS_DELETED), true);
167 @SuppressWarnings("unchecked")
169 public void fetchMetaDataByResourceType_failedToGetData() throws Exception {
170 when(janusGraphDaoMock.getByCriteria(eq(null), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.right(
171 JanusGraphOperationStatus.GENERAL_ERROR));
172 Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), new ComponentParametersView());
173 assertTrue(fetchedComponents.isRight());
174 assertEquals(StorageOperationStatus.GENERAL_ERROR, fetchedComponents.right().value());
178 public void associatePolicyToComponentSuccessTest(){
179 Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.OK);
180 assertTrue(result.isLeft());
184 public void associatePolicyToComponentFailureTest(){
185 Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
186 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
190 public void updatePolicyOfComponentSuccessTest(){
191 Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.OK);
192 assertTrue(result.isLeft());
196 public void updatePolicyOfComponentFailureTest(){
197 Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.NOT_FOUND);
198 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
202 public void removePolicyFromComponentSuccessTest(){
203 removePolicyFromComponentWithStatus(StorageOperationStatus.OK);
207 public void removePolicyFromComponentFailureTest(){
208 removePolicyFromComponentWithStatus(StorageOperationStatus.NOT_FOUND);
212 public void testFindLastCertifiedToscaElementByUUID(){
213 Either<Component, StorageOperationStatus> result;
214 Component component = new Resource();
215 List<GraphVertex> list = new ArrayList<>();
216 GraphVertex graphVertex = getTopologyTemplateVertex();
217 list.add(graphVertex);
218 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
219 props.put(GraphPropertyEnum.UUID, component.getUUID());
220 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
221 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
222 ToscaElement toscaElement = getToscaElementForTest();
223 when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
224 when(janusGraphDaoMock.getByCriteria(ModelConverter.getVertexType(component), props)).thenReturn(Either.left(list));
225 result = testInstance.findLastCertifiedToscaElementByUUID(component);
226 Component resultComp = result.left().value();
227 assertEquals(resultComp.getToscaType(),ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
231 public void testLatestComponentByToscaResourceName(){
232 Either<Component, StorageOperationStatus> result;
233 TopologyTemplate toscaElement = new TopologyTemplate();
234 toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
235 List<GraphVertex> list = new ArrayList<>();
236 GraphVertex graphVertex = getTopologyTemplateVertex();
237 Map<GraphPropertyEnum, Object> props = new HashMap<>();
238 props.put(GraphPropertyEnum.VERSION, "1.0");
239 graphVertex.setMetadataProperties(props);
240 list.add(graphVertex);
242 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
243 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
244 propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, "toscaResourceName");
245 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
246 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
248 when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
249 when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
251 result = testInstance.getFullLatestComponentByToscaResourceName("toscaResourceName");
252 assertThat(result.isLeft());
256 public void testValidateCsarUuidUniqueness() {
257 StorageOperationStatus result;
258 String csarUUID = "csarUUID";
259 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
260 properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
261 List<GraphVertex> vertexList = new ArrayList<>();
262 when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
263 result = testInstance.validateCsarUuidUniqueness(csarUUID);
264 assertEquals(StorageOperationStatus.ENTITY_ALREADY_EXISTS, result);
268 public void testValidateCsarUuidUnique_true() {
269 StorageOperationStatus result;
270 String csarUUID = "csarUUID";
271 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
272 properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
273 when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(
274 JanusGraphOperationStatus.NOT_FOUND));
275 result = testInstance.validateCsarUuidUniqueness(csarUUID);
276 assertEquals(StorageOperationStatus.OK, result);
280 public void testGetLatestCertiNodeTypeByToscaResourceName() {
281 Either<Resource, StorageOperationStatus> result;
282 String toscaResourceName = "resourceName";
283 String uniqueId = "uniqueId";
284 GraphVertex graphVertex = getTopologyTemplateVertex();
285 graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, "1.0");
286 graphVertex.setUniqueId(uniqueId);
287 List<GraphVertex> vertexList = new ArrayList<>();
288 vertexList.add(graphVertex);
289 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
290 props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
291 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
292 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
293 ToscaElement topologyTemplate = new TopologyTemplate();
294 topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
295 when(janusGraphDaoMock.getByCriteria(VertexTypeEnum.NODE_TYPE, props, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
296 when(janusGraphDaoMock.getVertexById(uniqueId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
297 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(topologyTemplate));
298 result = testInstance.getLatestCertifiedNodeTypeByToscaResourceName(toscaResourceName);
299 assertThat(result.isLeft());
303 public void testValidateCompExists() {
304 Either<Boolean, StorageOperationStatus> result;
305 String componentId = "componentId";
306 GraphVertex graphVertex = getTopologyTemplateVertex();
307 when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
308 result = testInstance.validateComponentExists(componentId);
309 assertEquals(true, result.left().value());
313 public void testValidateCompExists_NotFound() {
314 Either<Boolean, StorageOperationStatus> result;
315 String componentId = "componentId";
316 when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(
317 JanusGraphOperationStatus.NOT_FOUND));
318 result = testInstance.validateComponentExists(componentId);
319 assertEquals(false, result.left().value());
323 public void testValidateToscaResourceNameExists() {
324 Either<Boolean, StorageOperationStatus> result;
325 String templateName = "templateName";
326 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
327 properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
328 List<GraphVertex> graphVertexList = new ArrayList<>();
329 GraphVertex graphVertex = getTopologyTemplateVertex();
330 graphVertexList.add(graphVertex);
331 when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
332 result = testInstance.validateToscaResourceNameExists(templateName);
333 assertEquals(true, result.left().value());
337 public void testValidateToscaResourceNameExists_false() {
338 Either<Boolean, StorageOperationStatus> result;
339 String templateName = "templateName";
340 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
341 properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
342 List<GraphVertex> graphVertexList = new ArrayList<>();
343 GraphVertex graphVertex = getTopologyTemplateVertex();
344 graphVertexList.add(graphVertex);
345 when(janusGraphDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(
346 JanusGraphOperationStatus.NOT_FOUND));
347 result = testInstance.validateToscaResourceNameExists(templateName);
348 assertEquals(false, result.left().value());
352 public void testOverrideComponent() {
353 Either<Resource, StorageOperationStatus> result;
354 Resource resource = new Resource();
356 resource.setUniqueId(id);
357 GraphVertex graphVertex = getTopologyTemplateVertex();
358 graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
359 NodeType nodeType = new NodeType();
360 nodeType.setComponentType(ComponentTypeEnum.RESOURCE);
361 ToscaElement toscaElement = new TopologyTemplate();
362 toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
363 when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
364 when(janusGraphDaoMock.getParentVertex(graphVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
365 when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
366 when(nodeTypeOperation.createToscaElement(any(ToscaElement.class))).thenReturn(Either.left(nodeType));
367 when(janusGraphDaoMock.getVertexById(null, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
368 when(janusGraphDaoMock.createEdge(graphVertex, graphVertex, EdgeLabelEnum.VERSION, null)).thenReturn(
369 JanusGraphOperationStatus.OK);
370 result = testInstance.overrideComponent(resource, resource);
371 assertTrue(result.isLeft());
375 public void testGetToscaElement() {
376 Either<Component, StorageOperationStatus> result;
378 GraphVertex graphVertex = getTopologyTemplateVertex();
379 ToscaElement toscaElement = getToscaElementForTest();
380 when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
381 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
382 result = testInstance.getToscaElement(id, JsonParseFlagEnum.ParseAll);
383 assertTrue(result.isLeft());
387 public void testMarkComponentToDelete() {
388 StorageOperationStatus result;
389 Component component = new Resource();
391 component.setUniqueId(id);
392 GraphVertex graphVertex = getTopologyTemplateVertex();
393 when(janusGraphDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
394 when(nodeTypeOperation.markComponentToDelete(graphVertex)).thenReturn(Either.left(graphVertex));
395 result = testInstance.markComponentToDelete(component);
396 assertEquals(result, StorageOperationStatus.OK);
400 public void testDelToscaComponent() {
401 Either<Component, StorageOperationStatus> result;
402 String componentId = "compId";
403 GraphVertex graphVertex = getTopologyTemplateVertex();
404 ToscaElement toscaElement = getToscaElementForTest();
405 when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
406 when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
407 result = testInstance.deleteToscaComponent(componentId);
408 assertTrue(result.isLeft());
412 public void testGetLatestByToscaResourceName() {
413 Either<Component, StorageOperationStatus> result;
414 String toscaResourceName = "name";
415 ToscaElement toscaElement = getToscaElementForTest();
417 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
418 propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
419 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
420 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
421 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
423 List<GraphVertex> graphVertexList = new ArrayList<>();
424 GraphVertex graphVertex = getTopologyTemplateVertex();
425 graphVertex.setUniqueId(toscaResourceName);
426 Map<GraphPropertyEnum, Object> props = new HashMap<>();
427 props.put(GraphPropertyEnum.VERSION, "1.0");
428 graphVertex.setMetadataProperties(props);
429 graphVertexList.add(graphVertex);
431 when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
432 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
433 result = testInstance.getLatestByToscaResourceName(toscaResourceName);
434 assertTrue(result.isLeft());
438 public void testGetFollowed() {
439 Either<Set<Component>, StorageOperationStatus> result;
440 String userId = "id";
441 Set<LifecycleStateEnum> lifecycleStates = new HashSet<>();
442 Set<LifecycleStateEnum> lastStateStates = new HashSet<>();
443 lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
444 lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
445 lifecycleStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
446 lifecycleStates.add(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
447 lifecycleStates.add(LifecycleStateEnum.CERTIFIED);
448 lastStateStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
449 ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
450 List<ToscaElement> toscaEleList = new ArrayList<>();
451 ToscaElement toscaElement = getToscaElementForTest();
452 toscaEleList.add(toscaElement);
453 when(nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType)).thenReturn(Either.left(toscaEleList));
454 result = testInstance.getFollowed(userId, lifecycleStates, lastStateStates, componentType);
455 assertTrue(result.isLeft());
456 assertEquals(1, result.left().value().size());
460 public void testGetBySystemName() {
461 Either<List<Component>, StorageOperationStatus> result;
462 String sysName = "sysName";
463 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
464 ToscaElement toscaElement = getToscaElementForTest();
465 List<GraphVertex> componentVertices = new ArrayList<>();
466 GraphVertex graphVertex = getTopologyTemplateVertex();
467 componentVertices.add(graphVertex);
468 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
469 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
471 propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, sysName);
472 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentTypeEnum.name());
474 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
476 when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(componentVertices));
477 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
478 result = testInstance.getBySystemName(componentTypeEnum, sysName);
479 assertTrue(result.isLeft());
480 assertEquals(1, result.left().value().size());
484 public void testGetCompByNameAndVersion() {
485 Either<Component, StorageOperationStatus> result;
486 ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
487 String name = "name";
488 String version = "1.0";
489 JsonParseFlagEnum parseFlag = JsonParseFlagEnum.ParseAll;
490 List<GraphVertex> graphVertexList = new ArrayList<>();
491 GraphVertex graphVertex = getTopologyTemplateVertex();
492 graphVertexList.add(graphVertex);
493 ToscaElement toscaElement = getToscaElementForTest();
494 Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
495 Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
497 hasProperties.put(GraphPropertyEnum.NAME, name);
498 hasProperties.put(GraphPropertyEnum.VERSION, version);
499 hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
500 hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
501 when(janusGraphDaoMock.getByCriteria(null, hasProperties, hasNotProperties, parseFlag)).thenReturn(Either.left(graphVertexList));
502 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
503 result = testInstance.getComponentByNameAndVersion(componentType, name, version, parseFlag);
504 assertTrue(result.isLeft());
507 private ToscaElement getToscaElementForTest() {
508 ToscaElement toscaElement = new TopologyTemplate();
509 toscaElement.setComponentType(ComponentTypeEnum.RESOURCE);
514 public void addDataTypesToComponentSuccessTest(){
515 Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.OK);
516 assertTrue(result.isLeft());
520 public void addDataTypesToComponentFailureTest_BadRequest(){
521 Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
522 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
525 private Either<List<DataTypeDefinition>, StorageOperationStatus> addDataTypesToComponentWithStatus(StorageOperationStatus status) {
526 Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
527 String componentId = "componentid";
530 PropertyDefinition noDefaultProp = new PropertyDefinition();
531 noDefaultProp.setName("noDefaultProp");
532 PropertyDefinition prop1 = new PropertyDefinition();
533 prop1.setDefaultValue("def1");
534 prop1.setName("prop1");
535 PropertyDefinition prop2 = new PropertyDefinition();
536 prop2.setType("dataType1");
537 prop2.setName("prop2");
538 PropertyDefinition prop3 = new PropertyDefinition();
539 prop3.setDefaultValue("def3");
540 prop3.setName("prop3");
542 DataTypeDefinition noDefaultValue = new DataTypeDefinition();
543 noDefaultValue.setProperties(Collections.singletonList(noDefaultProp));
544 noDefaultValue.setDerivedFromName("name0");
546 DataTypeDefinition dataType1 = new DataTypeDefinition();
547 dataType1.setProperties(Arrays.asList(prop1, prop3));
548 dataType1.setName("name1");
549 dataType1.setDerivedFromName("derivedfromname1");
551 DataTypeDefinition dataType2 = new DataTypeDefinition();
552 dataType2.setDerivedFrom(dataType1);
553 dataType2.setName("name2");
554 dataType2.setDerivedFromName("derivedfromname2");
556 DataTypeDefinition dataType3 = new DataTypeDefinition();
557 dataType3.setProperties(Collections.singletonList(prop2));
558 dataType3.setDerivedFrom(noDefaultValue);
559 dataType3.setName("name3");
560 dataType3.setDerivedFromName("derivedfromname3");
562 dataTypes.put("noDefault", noDefaultValue);
563 dataTypes.put("dataType1", dataType1);
564 dataTypes.put("dataType2", dataType2);
565 dataTypes.put("dataType3", dataType3);
568 if(status == StorageOperationStatus.OK){
569 vertex = getTopologyTemplateVertex();
571 vertex = getNodeTypeVertex();
573 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
574 when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither);
575 when(topologyTemplateOperationMock.addToscaDataToToscaElement(eq(vertex),
576 eq(EdgeLabelEnum.DATA_TYPES), eq(VertexTypeEnum.DATA_TYPES), anyMap(), eq(JsonPresentationFields.NAME))).thenReturn(status);
577 return testInstance.addDataTypesToComponent(dataTypes, componentId);
581 public void testDataTypesToComponentFailureTest_NotFound() {
582 Either<List<DataTypeDefinition>, StorageOperationStatus> result;
583 String componentId = "componentId";
584 GraphVertex vertex = getNodeTypeVertex();
585 Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
586 when(janusGraphDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(
587 JanusGraphOperationStatus.NOT_FOUND));
588 result = testInstance.addDataTypesToComponent(dataTypes, componentId);
589 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
593 public void testDeleteDataTypeOfComponent() {
594 StorageOperationStatus result;
595 Component component = new Resource();
597 component.setUniqueId(id);
598 String datatype = null;
600 DataTypeDefinition dataType1 = new DataTypeDefinition();
601 dataType1.setName("name1");
602 Map<String, DataTypeDataDefinition> dataTypeDataMap = new HashMap<>();
603 dataTypeDataMap.put("datatype1", dataType1);
604 List<DataTypeDefinition> dataTypeMap = dataTypeDataMap.values().stream().map(e -> { DataTypeDefinition dataType = new DataTypeDefinition(e);return dataType; }).collect(Collectors.toList());
605 component.setDataTypes(dataTypeMap);
606 GraphVertex graphVertex = getTopologyTemplateVertex();
607 result = testInstance.deleteDataTypeOfComponent(component, "datatype1");
608 assertEquals(datatype, result);
612 public void testAddComponentInstancePropertiesToComponent() {
613 // set up component object
614 Component component = new Resource();
615 component.setUniqueId(COMPONENT_ID);
616 List<ComponentInstanceProperty> instanceProps = new ArrayList<>();
617 ComponentInstanceProperty instanceProp = new ComponentInstanceProperty();
618 instanceProp.setName(PROPERTY1_NAME);
619 instanceProp.setType(PROPERTY1_TYPE);
620 instanceProps.add(instanceProp);
621 instanceProp = new ComponentInstanceProperty();
622 instanceProp.setName(PROPERTY2_NAME);
623 instanceProp.setType(PROPERTY2_TYPE);
624 instanceProps.add(instanceProp);
625 Map<String, List<ComponentInstanceProperty>> instancePropsMap =
626 Collections.singletonMap(COMPONENT_ID, instanceProps);
627 component.setComponentInstancesProperties(Collections.singletonMap(COMPONENT_ID, new ArrayList<>()));
629 when(nodeTemplateOperationMock.addComponentInstanceProperty(any(), any(), any()))
630 .thenReturn(StorageOperationStatus.OK);
632 Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> result =
633 testInstance.addComponentInstancePropertiesToComponent(component, instancePropsMap);
634 assertTrue(result.isLeft());
635 verify(nodeTemplateOperationMock, times(2)).addComponentInstanceProperty(any(), any(), any());
636 List<ComponentInstanceProperty> resultProps = result.left().value().get(COMPONENT_ID);
637 assertTrue(resultProps.stream().anyMatch(e -> e.getName().equals(PROPERTY1_NAME)));
638 assertTrue(resultProps.stream().anyMatch(e -> e.getName().equals(PROPERTY2_NAME)));
642 public void testAddComponentInstanceToTopologyTemplate() {
643 Component containerComponent = new Service();
644 Component originalComponent = new Service();
645 ComponentInstance componentInstance = new ComponentInstance();
646 ComponentInstance existingComponentInstance = new ComponentInstance();
647 User user = new User();
649 containerComponent.setComponentType(ComponentTypeEnum.SERVICE);
651 originalComponent.setComponentType(ComponentTypeEnum.SERVICE);
652 originalComponent.setIcon(ICON_NAME);
654 componentInstance.setOriginType(OriginTypeEnum.ServiceProxy);
655 componentInstance.setSourceModelName(SERVICE_MODEL_NAME);
657 List<ComponentInstance> existingInstances = new ArrayList<>();
658 existingComponentInstance.setNormalizedName(SERVICE_PROXY_INSTANCE0_NAME);
659 existingInstances.add(existingComponentInstance);
660 containerComponent.setComponentInstances(existingInstances);
662 when(nodeTemplateOperationMock
663 .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user)))
664 .thenReturn(Either.left(new ImmutablePair<>(new TopologyTemplate(), COMPONENT_ID)));
665 TopologyTemplate topologyTemplate = new TopologyTemplate();
666 // preset COMPONENT_TYPE field for internal ModelConverter call
667 topologyTemplate.setMetadataValue(JsonPresentationFields.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
668 when(topologyTemplateOperationMock.getToscaElement(containerComponent.getUniqueId()))
669 .thenReturn(Either.left(topologyTemplate));
671 Either<ImmutablePair<Component, String>, StorageOperationStatus> result =
672 testInstance.addComponentInstanceToTopologyTemplate(
673 containerComponent, originalComponent, componentInstance, false, user);
675 assertTrue(result.isLeft());
676 assertEquals(componentInstance.getIcon(), ICON_NAME);
677 assertEquals(result.left().value().getRight(), COMPONENT_ID);
678 // the instance counter must be 1 because the service proxy instance with suffix 0 already exists.
679 verify(nodeTemplateOperationMock, times(1))
680 .addComponentInstanceToTopologyTemplate(any(), any(), eq("1"), eq(componentInstance), eq(false), eq(user));
683 private Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponentWithStatus(StorageOperationStatus status) {
684 PolicyDefinition policy = new PolicyDefinition();
685 String componentId = "componentId";
688 if(status == StorageOperationStatus.OK){
689 vertex = getTopologyTemplateVertex();
691 vertex = getNodeTypeVertex();
693 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
694 when(janusGraphDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(getVertexEither);
695 when(topologyTemplateOperationMock.addPolicyToToscaElement(eq(vertex), any(PolicyDefinition.class), anyInt())).thenReturn(status);
696 return testInstance.associatePolicyToComponent(componentId, policy, counter);
699 private Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponentWithStatus(StorageOperationStatus status) {
700 PolicyDefinition policy = new PolicyDefinition();
701 String componentId = "componentId";
702 GraphVertex vertex = getTopologyTemplateVertex();
703 when(janusGraphDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(Either.left(vertex));
704 when(topologyTemplateOperationMock.updatePolicyOfToscaElement(eq(vertex), any(PolicyDefinition.class))).thenReturn(status);
705 return testInstance.updatePolicyOfComponent(componentId, policy);
708 private void removePolicyFromComponentWithStatus(StorageOperationStatus status) {
709 String componentId = "componentId";
710 String policyId = "policyId";
711 GraphVertex vertex = getTopologyTemplateVertex();
712 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = Either.left(vertex);
713 when(janusGraphDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(getVertexEither);
714 when(topologyTemplateOperationMock.removePolicyFromToscaElement(eq(vertex), eq(policyId))).thenReturn(status);
715 StorageOperationStatus result = testInstance.removePolicyFromComponent(componentId, policyId);
716 assertSame(result, status);
719 private List<GraphVertex> getMockVertices(int numOfVertices) {
720 return IntStream.range(0, numOfVertices).mapToObj(i -> getTopologyTemplateVertex()).collect(Collectors.toList());
723 private ToscaElement getResourceToscaElement(String id) {
724 ToscaElement toscaElement = new TopologyTemplate();
725 toscaElement.setMetadata(new HashMap<>());
726 toscaElement.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), "RESOURCE");
727 toscaElement.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), id);
731 private GraphVertex getTopologyTemplateVertex() {
732 GraphVertex graphVertex = new GraphVertex();
733 graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
737 private GraphVertex getNodeTypeVertex() {
738 GraphVertex graphVertex = new GraphVertex();
739 graphVertex.setLabel(VertexTypeEnum.NODE_TYPE);