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.jsontitan.operations;
32 import fj.data.Either;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.ArgumentMatchers;
40 import org.mockito.MockitoAnnotations;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
43 import org.openecomp.sdc.be.dao.jsongraph.HealingTitanDao;
44 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
45 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
46 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
47 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
48 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
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.ResourceTypeEnum;
53 import org.openecomp.sdc.be.model.Component;
54 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
55 import org.openecomp.sdc.be.model.Resource;
56 import org.openecomp.sdc.be.model.LifecycleStateEnum;
57 import org.openecomp.sdc.be.model.ComponentParametersView;
58 import org.openecomp.sdc.be.model.PolicyDefinition;
59 import org.openecomp.sdc.be.model.jsontitan.datamodel.NodeType;
60 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
61 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
62 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
63 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
64 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
65 import org.openecomp.sdc.be.model.DataTypeDefinition;
66 import org.openecomp.sdc.be.model.PropertyDefinition;
67 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
69 import static org.assertj.core.api.Assertions.assertThat;
71 import java.util.HashMap;
72 import java.util.List;
74 import java.util.ArrayList;
75 import java.util.EnumMap;
77 import java.util.HashSet;
78 import java.util.stream.Collectors;
79 import java.util.stream.IntStream;
80 import java.util.Collections;
81 import java.util.Arrays;
83 import static org.junit.Assert.assertEquals;
84 import static org.junit.Assert.assertSame;
85 import static org.junit.Assert.assertTrue;
86 import static org.mockito.Mockito.times;
87 import static org.mockito.Mockito.verify;
88 import static org.mockito.Mockito.when;
89 import static org.mockito.ArgumentMatchers.any;
90 import static org.mockito.ArgumentMatchers.anyMap;
91 import static org.mockito.ArgumentMatchers.anyInt;
92 import static org.mockito.ArgumentMatchers.anyString;
93 import static org.mockito.ArgumentMatchers.eq;
95 @RunWith(MockitoJUnitRunner.class)
96 public class ToscaOperationFacadeTest {
97 private static final String COMPONENT_ID = "componentId";
98 private static final String PROPERTY1_NAME = "prop1";
99 private static final String PROPERTY1_TYPE = "string";
100 private static final String PROPERTY2_NAME = "prop2";
101 private static final String PROPERTY2_TYPE = "integer";
104 private ToscaOperationFacade testInstance;
107 private HealingTitanDao titanDaoMock;
110 private TopologyTemplateOperation topologyTemplateOperationMock;
113 private NodeTypeOperation nodeTypeOperation;
116 private NodeTemplateOperation nodeTemplateOperationMock;
119 public void setUp() throws Exception {
120 testInstance = new ToscaOperationFacade();
121 MockitoAnnotations.initMocks(this);
124 @SuppressWarnings("unchecked")
126 public void fetchMetaDataByResourceType() throws Exception {
127 ArgumentCaptor<Map> criteriaCapture = ArgumentCaptor.forClass(Map.class);
128 ArgumentCaptor<Map> criteriaNotCapture = ArgumentCaptor.forClass(Map.class);
129 ComponentParametersView dataFilter = new ComponentParametersView();
130 List<GraphVertex> mockVertices = getMockVertices(2);
131 Either<List<GraphVertex>, TitanOperationStatus> returnedVertices = Either.left(mockVertices);
133 when(titanDaoMock.getByCriteria(eq(null), criteriaCapture.capture(), criteriaNotCapture.capture(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(returnedVertices);
134 when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(0), dataFilter)).thenReturn(Either.left(getResourceToscaElement("0")));
135 when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(1), dataFilter)).thenReturn(Either.left(getResourceToscaElement("1")));
136 Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), dataFilter);
138 verifyCriteriaForHighestVersionAndVfResourceType(criteriaCapture);
139 verifyCriteriaNotIsDeleted(criteriaNotCapture);
141 assertTrue(fetchedComponents.isLeft());
142 List<Component> cmpts = fetchedComponents.left().value();
143 assertEquals(2, cmpts.size());
144 assertEquals("0", cmpts.get(0).getUniqueId());
145 assertEquals("1", cmpts.get(1).getUniqueId());
148 private void verifyCriteriaForHighestVersionAndVfResourceType(ArgumentCaptor<Map> criteriaCapture) {
149 Map<GraphPropertyEnum, Object> criteria = (Map<GraphPropertyEnum, Object>)criteriaCapture.getValue();
150 assertEquals(2, criteria.size());
151 assertEquals(criteria.get(GraphPropertyEnum.RESOURCE_TYPE), "VF");
152 assertEquals(criteria.get(GraphPropertyEnum.IS_HIGHEST_VERSION), true);
155 private void verifyCriteriaNotIsDeleted(ArgumentCaptor<Map> criteriaNotCapture) {
156 Map<GraphPropertyEnum, Object> notCriteria = (Map<GraphPropertyEnum, Object>)criteriaNotCapture.getValue();
157 assertEquals(1, notCriteria.size());
158 assertEquals(notCriteria.get(GraphPropertyEnum.IS_DELETED), true);
161 @SuppressWarnings("unchecked")
163 public void fetchMetaDataByResourceType_failedToGetData() throws Exception {
164 when(titanDaoMock.getByCriteria(eq(null), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.right(TitanOperationStatus.GENERAL_ERROR));
165 Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), new ComponentParametersView());
166 assertTrue(fetchedComponents.isRight());
167 assertEquals(StorageOperationStatus.GENERAL_ERROR, fetchedComponents.right().value());
171 public void associatePolicyToComponentSuccessTest(){
172 Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.OK);
173 assertTrue(result.isLeft());
177 public void associatePolicyToComponentFailureTest(){
178 Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
179 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
183 public void updatePolicyOfComponentSuccessTest(){
184 Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.OK);
185 assertTrue(result.isLeft());
189 public void updatePolicyOfComponentFailureTest(){
190 Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.NOT_FOUND);
191 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
195 public void removePolicyFromComponentSuccessTest(){
196 removePolicyFromComponentWithStatus(StorageOperationStatus.OK);
200 public void removePolicyFromComponentFailureTest(){
201 removePolicyFromComponentWithStatus(StorageOperationStatus.NOT_FOUND);
205 public void testFindLastCertifiedToscaElementByUUID(){
206 Either<Component, StorageOperationStatus> result;
207 Component component = new Resource();
208 List<GraphVertex> list = new ArrayList<>();
209 GraphVertex graphVertex = getTopologyTemplateVertex();
210 list.add(graphVertex);
211 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
212 props.put(GraphPropertyEnum.UUID, component.getUUID());
213 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
214 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
215 ToscaElement toscaElement = getToscaElementForTest();
216 when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
217 when(titanDaoMock.getByCriteria(ModelConverter.getVertexType(component), props)).thenReturn(Either.left(list));
218 result = testInstance.findLastCertifiedToscaElementByUUID(component);
219 Component resultComp = result.left().value();
220 assertEquals(resultComp.getToscaType(),ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
224 public void testLatestComponentByToscaResourceName(){
225 Either<Component, StorageOperationStatus> result;
226 TopologyTemplate toscaElement = new TopologyTemplate();
227 toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
228 List<GraphVertex> list = new ArrayList<>();
229 GraphVertex graphVertex = getTopologyTemplateVertex();
230 Map<GraphPropertyEnum, Object> props = new HashMap<>();
231 props.put(GraphPropertyEnum.VERSION, "1.0");
232 graphVertex.setMetadataProperties(props);
233 list.add(graphVertex);
235 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
236 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
237 propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, "toscaResourceName");
238 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
239 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
241 when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
242 when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
244 result = testInstance.getFullLatestComponentByToscaResourceName("toscaResourceName");
245 assertThat(result.isLeft());
249 public void testValidateCsarUuidUniqueness() {
250 StorageOperationStatus result;
251 String csarUUID = "csarUUID";
252 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
253 properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
254 List<GraphVertex> vertexList = new ArrayList<>();
255 when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
256 result = testInstance.validateCsarUuidUniqueness(csarUUID);
257 assertEquals(StorageOperationStatus.ENTITY_ALREADY_EXISTS, result);
261 public void testValidateCsarUuidUnique_true() {
262 StorageOperationStatus result;
263 String csarUUID = "csarUUID";
264 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
265 properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
266 when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
267 result = testInstance.validateCsarUuidUniqueness(csarUUID);
268 assertEquals(StorageOperationStatus.OK, result);
272 public void testGetLatestCertiNodeTypeByToscaResourceName() {
273 Either<Resource, StorageOperationStatus> result;
274 String toscaResourceName = "resourceName";
275 String uniqueId = "uniqueId";
276 GraphVertex graphVertex = getTopologyTemplateVertex();
277 graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, "1.0");
278 graphVertex.setUniqueId(uniqueId);
279 List<GraphVertex> vertexList = new ArrayList<>();
280 vertexList.add(graphVertex);
281 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
282 props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
283 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
284 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
285 ToscaElement topologyTemplate = new TopologyTemplate();
286 topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
287 when(titanDaoMock.getByCriteria(VertexTypeEnum.NODE_TYPE, props, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
288 when(titanDaoMock.getVertexById(uniqueId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
289 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(topologyTemplate));
290 result = testInstance.getLatestCertifiedNodeTypeByToscaResourceName(toscaResourceName);
291 assertThat(result.isLeft());
295 public void testValidateCompExists() {
296 Either<Boolean, StorageOperationStatus> result;
297 String componentId = "componentId";
298 GraphVertex graphVertex = getTopologyTemplateVertex();
299 when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
300 result = testInstance.validateComponentExists(componentId);
301 assertEquals(true, result.left().value());
305 public void testValidateCompExists_NotFound() {
306 Either<Boolean, StorageOperationStatus> result;
307 String componentId = "componentId";
308 when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
309 result = testInstance.validateComponentExists(componentId);
310 assertEquals(false, result.left().value());
314 public void testValidateToscaResourceNameExists() {
315 Either<Boolean, StorageOperationStatus> result;
316 String templateName = "templateName";
317 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
318 properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
319 List<GraphVertex> graphVertexList = new ArrayList<>();
320 GraphVertex graphVertex = getTopologyTemplateVertex();
321 graphVertexList.add(graphVertex);
322 when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
323 result = testInstance.validateToscaResourceNameExists(templateName);
324 assertEquals(true, result.left().value());
328 public void testValidateToscaResourceNameExists_false() {
329 Either<Boolean, StorageOperationStatus> result;
330 String templateName = "templateName";
331 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
332 properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
333 List<GraphVertex> graphVertexList = new ArrayList<>();
334 GraphVertex graphVertex = getTopologyTemplateVertex();
335 graphVertexList.add(graphVertex);
336 when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
337 result = testInstance.validateToscaResourceNameExists(templateName);
338 assertEquals(false, result.left().value());
342 public void testOverrideComponent() {
343 Either<Resource, StorageOperationStatus> result;
344 Resource resource = new Resource();
346 resource.setUniqueId(id);
347 GraphVertex graphVertex = getTopologyTemplateVertex();
348 graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
349 NodeType nodeType = new NodeType();
350 nodeType.setComponentType(ComponentTypeEnum.RESOURCE);
351 ToscaElement toscaElement = new TopologyTemplate();
352 toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
353 when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
354 when(titanDaoMock.getParentVertex(graphVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
355 when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
356 when(nodeTypeOperation.createToscaElement(any(ToscaElement.class))).thenReturn(Either.left(nodeType));
357 when(titanDaoMock.getVertexById(null, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
358 when(titanDaoMock.createEdge(graphVertex, graphVertex, EdgeLabelEnum.VERSION, null)).thenReturn(TitanOperationStatus.OK);
359 result = testInstance.overrideComponent(resource, resource);
360 assertTrue(result.isLeft());
364 public void testGetToscaElement() {
365 Either<Component, StorageOperationStatus> result;
367 GraphVertex graphVertex = getTopologyTemplateVertex();
368 ToscaElement toscaElement = getToscaElementForTest();
369 when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
370 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
371 result = testInstance.getToscaElement(id, JsonParseFlagEnum.ParseAll);
372 assertTrue(result.isLeft());
376 public void testMarkComponentToDelete() {
377 StorageOperationStatus result;
378 Component component = new Resource();
380 component.setUniqueId(id);
381 GraphVertex graphVertex = getTopologyTemplateVertex();
382 when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
383 when(nodeTypeOperation.markComponentToDelete(graphVertex)).thenReturn(Either.left(graphVertex));
384 result = testInstance.markComponentToDelete(component);
385 assertEquals(result, StorageOperationStatus.OK);
389 public void testDelToscaComponent() {
390 Either<Component, StorageOperationStatus> result;
391 String componentId = "compId";
392 GraphVertex graphVertex = getTopologyTemplateVertex();
393 ToscaElement toscaElement = getToscaElementForTest();
394 when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
395 when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
396 result = testInstance.deleteToscaComponent(componentId);
397 assertTrue(result.isLeft());
401 public void testGetLatestByToscaResourceName() {
402 Either<Component, StorageOperationStatus> result;
403 String toscaResourceName = "name";
404 ToscaElement toscaElement = getToscaElementForTest();
406 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
407 propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
408 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
409 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
410 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
412 List<GraphVertex> graphVertexList = new ArrayList<>();
413 GraphVertex graphVertex = getTopologyTemplateVertex();
414 graphVertex.setUniqueId(toscaResourceName);
415 Map<GraphPropertyEnum, Object> props = new HashMap<>();
416 props.put(GraphPropertyEnum.VERSION, "1.0");
417 graphVertex.setMetadataProperties(props);
418 graphVertexList.add(graphVertex);
420 when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
421 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
422 result = testInstance.getLatestByToscaResourceName(toscaResourceName);
423 assertTrue(result.isLeft());
427 public void testGetFollowed() {
428 Either<Set<Component>, StorageOperationStatus> result;
429 String userId = "id";
430 Set<LifecycleStateEnum> lifecycleStates = new HashSet<>();
431 Set<LifecycleStateEnum> lastStateStates = new HashSet<>();
432 lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
433 lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
434 lifecycleStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
435 lifecycleStates.add(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
436 lifecycleStates.add(LifecycleStateEnum.CERTIFIED);
437 lastStateStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
438 ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
439 List<ToscaElement> toscaEleList = new ArrayList<>();
440 ToscaElement toscaElement = getToscaElementForTest();
441 toscaEleList.add(toscaElement);
442 when(nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType)).thenReturn(Either.left(toscaEleList));
443 result = testInstance.getFollowed(userId, lifecycleStates, lastStateStates, componentType);
444 assertTrue(result.isLeft());
445 assertEquals(1, result.left().value().size());
449 public void testGetBySystemName() {
450 Either<List<Component>, StorageOperationStatus> result;
451 String sysName = "sysName";
452 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
453 ToscaElement toscaElement = getToscaElementForTest();
454 List<GraphVertex> componentVertices = new ArrayList<>();
455 GraphVertex graphVertex = getTopologyTemplateVertex();
456 componentVertices.add(graphVertex);
457 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
458 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
460 propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, sysName);
461 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentTypeEnum.name());
463 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
465 when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(componentVertices));
466 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
467 result = testInstance.getBySystemName(componentTypeEnum, sysName);
468 assertTrue(result.isLeft());
469 assertEquals(1, result.left().value().size());
473 public void testGetCompByNameAndVersion() {
474 Either<Component, StorageOperationStatus> result;
475 ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
476 String name = "name";
477 String version = "1.0";
478 JsonParseFlagEnum parseFlag = JsonParseFlagEnum.ParseAll;
479 List<GraphVertex> graphVertexList = new ArrayList<>();
480 GraphVertex graphVertex = getTopologyTemplateVertex();
481 graphVertexList.add(graphVertex);
482 ToscaElement toscaElement = getToscaElementForTest();
483 Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
484 Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
486 hasProperties.put(GraphPropertyEnum.NAME, name);
487 hasProperties.put(GraphPropertyEnum.VERSION, version);
488 hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
489 hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
490 when(titanDaoMock.getByCriteria(null, hasProperties, hasNotProperties, parseFlag)).thenReturn(Either.left(graphVertexList));
491 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
492 result = testInstance.getComponentByNameAndVersion(componentType, name, version, parseFlag);
493 assertTrue(result.isLeft());
496 private ToscaElement getToscaElementForTest() {
497 ToscaElement toscaElement = new TopologyTemplate();
498 toscaElement.setComponentType(ComponentTypeEnum.RESOURCE);
503 public void addDataTypesToComponentSuccessTest(){
504 Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.OK);
505 assertTrue(result.isLeft());
509 public void addDataTypesToComponentFailureTest_BadRequest(){
510 Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
511 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
514 private Either<List<DataTypeDefinition>, StorageOperationStatus> addDataTypesToComponentWithStatus(StorageOperationStatus status) {
515 Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
516 String componentId = "componentid";
519 PropertyDefinition noDefaultProp = new PropertyDefinition();
520 noDefaultProp.setName("noDefaultProp");
521 PropertyDefinition prop1 = new PropertyDefinition();
522 prop1.setDefaultValue("def1");
523 prop1.setName("prop1");
524 PropertyDefinition prop2 = new PropertyDefinition();
525 prop2.setType("dataType1");
526 prop2.setName("prop2");
527 PropertyDefinition prop3 = new PropertyDefinition();
528 prop3.setDefaultValue("def3");
529 prop3.setName("prop3");
531 DataTypeDefinition noDefaultValue = new DataTypeDefinition();
532 noDefaultValue.setProperties(Collections.singletonList(noDefaultProp));
533 noDefaultValue.setDerivedFromName("name0");
535 DataTypeDefinition dataType1 = new DataTypeDefinition();
536 dataType1.setProperties(Arrays.asList(prop1, prop3));
537 dataType1.setName("name1");
538 dataType1.setDerivedFromName("derivedfromname1");
540 DataTypeDefinition dataType2 = new DataTypeDefinition();
541 dataType2.setDerivedFrom(dataType1);
542 dataType2.setName("name2");
543 dataType2.setDerivedFromName("derivedfromname2");
545 DataTypeDefinition dataType3 = new DataTypeDefinition();
546 dataType3.setProperties(Collections.singletonList(prop2));
547 dataType3.setDerivedFrom(noDefaultValue);
548 dataType3.setName("name3");
549 dataType3.setDerivedFromName("derivedfromname3");
551 dataTypes.put("noDefault", noDefaultValue);
552 dataTypes.put("dataType1", dataType1);
553 dataTypes.put("dataType2", dataType2);
554 dataTypes.put("dataType3", dataType3);
557 if(status == StorageOperationStatus.OK){
558 vertex = getTopologyTemplateVertex();
560 vertex = getNodeTypeVertex();
562 Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
563 when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither);
564 when(topologyTemplateOperationMock.addToscaDataToToscaElement(eq(vertex),
565 eq(EdgeLabelEnum.DATA_TYPES), eq(VertexTypeEnum.DATA_TYPES), anyMap(), eq(JsonPresentationFields.NAME))).thenReturn(status);
566 return testInstance.addDataTypesToComponent(dataTypes, componentId);
570 public void testDataTypesToComponentFailureTest_NotFound() {
571 Either<List<DataTypeDefinition>, StorageOperationStatus> result;
572 String componentId = "componentId";
573 GraphVertex vertex = getNodeTypeVertex();
574 Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
575 when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
576 result = testInstance.addDataTypesToComponent(dataTypes, componentId);
577 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
581 public void testDeleteDataTypeOfComponent() {
582 StorageOperationStatus result;
583 Component component = new Resource();
585 component.setUniqueId(id);
586 String datatype = null;
588 DataTypeDefinition dataType1 = new DataTypeDefinition();
589 dataType1.setName("name1");
590 Map<String, DataTypeDataDefinition> dataTypeDataMap = new HashMap<>();
591 dataTypeDataMap.put("datatype1", dataType1);
592 List<DataTypeDefinition> dataTypeMap = dataTypeDataMap.values().stream().map(e -> { DataTypeDefinition dataType = new DataTypeDefinition(e);return dataType; }).collect(Collectors.toList());
593 component.setDataTypes(dataTypeMap);
594 GraphVertex graphVertex = getTopologyTemplateVertex();
595 result = testInstance.deleteDataTypeOfComponent(component, "datatype1");
596 assertEquals(datatype, result);
600 public void testAddComponentInstancePropertiesToComponent() {
601 // set up component object
602 Component component = new Resource();
603 component.setUniqueId(COMPONENT_ID);
604 List<ComponentInstanceProperty> instanceProps = new ArrayList<>();
605 ComponentInstanceProperty instanceProp = new ComponentInstanceProperty();
606 instanceProp.setName(PROPERTY1_NAME);
607 instanceProp.setType(PROPERTY1_TYPE);
608 instanceProps.add(instanceProp);
609 instanceProp = new ComponentInstanceProperty();
610 instanceProp.setName(PROPERTY2_NAME);
611 instanceProp.setType(PROPERTY2_TYPE);
612 instanceProps.add(instanceProp);
613 Map<String, List<ComponentInstanceProperty>> instancePropsMap =
614 Collections.singletonMap(COMPONENT_ID, instanceProps);
615 component.setComponentInstancesProperties(Collections.singletonMap(COMPONENT_ID, new ArrayList<>()));
617 when(nodeTemplateOperationMock.addComponentInstanceProperty(any(), any(), any()))
618 .thenReturn(StorageOperationStatus.OK);
620 Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> result =
621 testInstance.addComponentInstancePropertiesToComponent(component, instancePropsMap);
622 assertTrue(result.isLeft());
623 verify(nodeTemplateOperationMock, times(2)).addComponentInstanceProperty(any(), any(), any());
624 List<ComponentInstanceProperty> resultProps = result.left().value().get(COMPONENT_ID);
625 assertTrue(resultProps.stream().anyMatch(e -> e.getName().equals(PROPERTY1_NAME)));
626 assertTrue(resultProps.stream().anyMatch(e -> e.getName().equals(PROPERTY2_NAME)));
629 private Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponentWithStatus(StorageOperationStatus status) {
630 PolicyDefinition policy = new PolicyDefinition();
631 String componentId = "componentId";
634 if(status == StorageOperationStatus.OK){
635 vertex = getTopologyTemplateVertex();
637 vertex = getNodeTypeVertex();
639 Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
640 when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(getVertexEither);
641 when(topologyTemplateOperationMock.addPolicyToToscaElement(eq(vertex), any(PolicyDefinition.class), anyInt())).thenReturn(status);
642 return testInstance.associatePolicyToComponent(componentId, policy, counter);
645 private Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponentWithStatus(StorageOperationStatus status) {
646 PolicyDefinition policy = new PolicyDefinition();
647 String componentId = "componentId";
648 GraphVertex vertex = getTopologyTemplateVertex();
649 when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(Either.left(vertex));
650 when(topologyTemplateOperationMock.updatePolicyOfToscaElement(eq(vertex), any(PolicyDefinition.class))).thenReturn(status);
651 return testInstance.updatePolicyOfComponent(componentId, policy);
654 private void removePolicyFromComponentWithStatus(StorageOperationStatus status) {
655 String componentId = "componentId";
656 String policyId = "policyId";
657 GraphVertex vertex = getTopologyTemplateVertex();
658 Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
659 when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(getVertexEither);
660 when(topologyTemplateOperationMock.removePolicyFromToscaElement(eq(vertex), eq(policyId))).thenReturn(status);
661 StorageOperationStatus result = testInstance.removePolicyFromComponent(componentId, policyId);
662 assertSame(result, status);
665 private List<GraphVertex> getMockVertices(int numOfVertices) {
666 return IntStream.range(0, numOfVertices).mapToObj(i -> getTopologyTemplateVertex()).collect(Collectors.toList());
669 private ToscaElement getResourceToscaElement(String id) {
670 ToscaElement toscaElement = new TopologyTemplate();
671 toscaElement.setMetadata(new HashMap<>());
672 toscaElement.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), "RESOURCE");
673 toscaElement.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), id);
677 private GraphVertex getTopologyTemplateVertex() {
678 GraphVertex graphVertex = new GraphVertex();
679 graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
683 private GraphVertex getNodeTypeVertex() {
684 GraphVertex graphVertex = new GraphVertex();
685 graphVertex.setLabel(VertexTypeEnum.NODE_TYPE);