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.Resource;
55 import org.openecomp.sdc.be.model.LifecycleStateEnum;
56 import org.openecomp.sdc.be.model.ComponentParametersView;
57 import org.openecomp.sdc.be.model.PolicyDefinition;
58 import org.openecomp.sdc.be.model.jsontitan.datamodel.NodeType;
59 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
60 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
61 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
62 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
63 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
64 import org.openecomp.sdc.be.model.DataTypeDefinition;
65 import org.openecomp.sdc.be.model.PropertyDefinition;
66 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
68 import static org.assertj.core.api.Assertions.assertThat;
70 import java.util.HashMap;
71 import java.util.List;
73 import java.util.ArrayList;
74 import java.util.EnumMap;
76 import java.util.HashSet;
77 import java.util.stream.Collectors;
78 import java.util.stream.IntStream;
79 import java.util.Collections;
80 import java.util.Arrays;
82 import static org.junit.Assert.assertEquals;
83 import static org.junit.Assert.assertSame;
84 import static org.junit.Assert.assertTrue;
85 import static org.mockito.Mockito.when;
86 import static org.mockito.ArgumentMatchers.any;
87 import static org.mockito.ArgumentMatchers.anyMap;
88 import static org.mockito.ArgumentMatchers.anyInt;
89 import static org.mockito.ArgumentMatchers.anyString;
90 import static org.mockito.ArgumentMatchers.eq;
92 @RunWith(MockitoJUnitRunner.class)
93 public class ToscaOperationFacadeTest {
95 private ToscaOperationFacade testInstance;
98 private HealingTitanDao titanDaoMock;
101 private TopologyTemplateOperation topologyTemplateOperationMock;
104 private NodeTypeOperation nodeTypeOperation;
107 public void setUp() throws Exception {
108 testInstance = new ToscaOperationFacade();
109 MockitoAnnotations.initMocks(this);
112 @SuppressWarnings("unchecked")
114 public void fetchMetaDataByResourceType() throws Exception {
115 ArgumentCaptor<Map> criteriaCapture = ArgumentCaptor.forClass(Map.class);
116 ArgumentCaptor<Map> criteriaNotCapture = ArgumentCaptor.forClass(Map.class);
117 ComponentParametersView dataFilter = new ComponentParametersView();
118 List<GraphVertex> mockVertices = getMockVertices(2);
119 Either<List<GraphVertex>, TitanOperationStatus> returnedVertices = Either.left(mockVertices);
121 when(titanDaoMock.getByCriteria(eq(null), criteriaCapture.capture(), criteriaNotCapture.capture(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(returnedVertices);
122 when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(0), dataFilter)).thenReturn(Either.left(getResourceToscaElement("0")));
123 when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(1), dataFilter)).thenReturn(Either.left(getResourceToscaElement("1")));
124 Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), dataFilter);
126 verifyCriteriaForHighestVersionAndVfResourceType(criteriaCapture);
127 verifyCriteriaNotIsDeleted(criteriaNotCapture);
129 assertTrue(fetchedComponents.isLeft());
130 List<Component> cmpts = fetchedComponents.left().value();
131 assertEquals(2, cmpts.size());
132 assertEquals("0", cmpts.get(0).getUniqueId());
133 assertEquals("1", cmpts.get(1).getUniqueId());
136 private void verifyCriteriaForHighestVersionAndVfResourceType(ArgumentCaptor<Map> criteriaCapture) {
137 Map<GraphPropertyEnum, Object> criteria = (Map<GraphPropertyEnum, Object>)criteriaCapture.getValue();
138 assertEquals(2, criteria.size());
139 assertEquals(criteria.get(GraphPropertyEnum.RESOURCE_TYPE), "VF");
140 assertEquals(criteria.get(GraphPropertyEnum.IS_HIGHEST_VERSION), true);
143 private void verifyCriteriaNotIsDeleted(ArgumentCaptor<Map> criteriaNotCapture) {
144 Map<GraphPropertyEnum, Object> notCriteria = (Map<GraphPropertyEnum, Object>)criteriaNotCapture.getValue();
145 assertEquals(1, notCriteria.size());
146 assertEquals(notCriteria.get(GraphPropertyEnum.IS_DELETED), true);
149 @SuppressWarnings("unchecked")
151 public void fetchMetaDataByResourceType_failedToGetData() throws Exception {
152 when(titanDaoMock.getByCriteria(eq(null), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.right(TitanOperationStatus.GENERAL_ERROR));
153 Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), new ComponentParametersView());
154 assertTrue(fetchedComponents.isRight());
155 assertEquals(StorageOperationStatus.GENERAL_ERROR, fetchedComponents.right().value());
159 public void associatePolicyToComponentSuccessTest(){
160 Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.OK);
161 assertTrue(result.isLeft());
165 public void associatePolicyToComponentFailureTest(){
166 Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
167 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
171 public void updatePolicyOfComponentSuccessTest(){
172 Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.OK);
173 assertTrue(result.isLeft());
177 public void updatePolicyOfComponentFailureTest(){
178 Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.NOT_FOUND);
179 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
183 public void removePolicyFromComponentSuccessTest(){
184 removePolicyFromComponentWithStatus(StorageOperationStatus.OK);
188 public void removePolicyFromComponentFailureTest(){
189 removePolicyFromComponentWithStatus(StorageOperationStatus.NOT_FOUND);
193 public void testFindLastCertifiedToscaElementByUUID(){
194 Either<Component, StorageOperationStatus> result;
195 Component component = new Resource();
196 List<GraphVertex> list = new ArrayList<>();
197 GraphVertex graphVertex = getTopologyTemplateVertex();
198 list.add(graphVertex);
199 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
200 props.put(GraphPropertyEnum.UUID, component.getUUID());
201 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
202 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
203 ToscaElement toscaElement = getToscaElementForTest();
204 when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
205 when(titanDaoMock.getByCriteria(ModelConverter.getVertexType(component), props)).thenReturn(Either.left(list));
206 result = testInstance.findLastCertifiedToscaElementByUUID(component);
207 Component resultComp = result.left().value();
208 assertEquals(resultComp.getToscaType(),ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
212 public void testLatestComponentByToscaResourceName(){
213 Either<Component, StorageOperationStatus> result;
214 TopologyTemplate toscaElement = new TopologyTemplate();
215 toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
216 List<GraphVertex> list = new ArrayList<>();
217 GraphVertex graphVertex = getTopologyTemplateVertex();
218 Map<GraphPropertyEnum, Object> props = new HashMap<>();
219 props.put(GraphPropertyEnum.VERSION, "1.0");
220 graphVertex.setMetadataProperties(props);
221 list.add(graphVertex);
223 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
224 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
225 propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, "toscaResourceName");
226 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
227 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
229 when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
230 when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
232 result = testInstance.getFullLatestComponentByToscaResourceName("toscaResourceName");
233 assertThat(result.isLeft());
237 public void testValidateCsarUuidUniqueness() {
238 StorageOperationStatus result;
239 String csarUUID = "csarUUID";
240 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
241 properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
242 List<GraphVertex> vertexList = new ArrayList<>();
243 when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
244 result = testInstance.validateCsarUuidUniqueness(csarUUID);
245 assertEquals(StorageOperationStatus.ENTITY_ALREADY_EXISTS, result);
249 public void testValidateCsarUuidUnique_true() {
250 StorageOperationStatus result;
251 String csarUUID = "csarUUID";
252 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
253 properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
254 when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
255 result = testInstance.validateCsarUuidUniqueness(csarUUID);
256 assertEquals(StorageOperationStatus.OK, result);
260 public void testGetLatestCertiNodeTypeByToscaResourceName() {
261 Either<Resource, StorageOperationStatus> result;
262 String toscaResourceName = "resourceName";
263 String uniqueId = "uniqueId";
264 GraphVertex graphVertex = getTopologyTemplateVertex();
265 graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, "1.0");
266 graphVertex.setUniqueId(uniqueId);
267 List<GraphVertex> vertexList = new ArrayList<>();
268 vertexList.add(graphVertex);
269 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
270 props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
271 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
272 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
273 ToscaElement topologyTemplate = new TopologyTemplate();
274 topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
275 when(titanDaoMock.getByCriteria(VertexTypeEnum.NODE_TYPE, props, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
276 when(titanDaoMock.getVertexById(uniqueId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
277 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(topologyTemplate));
278 result = testInstance.getLatestCertifiedNodeTypeByToscaResourceName(toscaResourceName);
279 assertThat(result.isLeft());
283 public void testValidateCompExists() {
284 Either<Boolean, StorageOperationStatus> result;
285 String componentId = "componentId";
286 GraphVertex graphVertex = getTopologyTemplateVertex();
287 when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
288 result = testInstance.validateComponentExists(componentId);
289 assertEquals(true, result.left().value());
293 public void testValidateCompExists_NotFound() {
294 Either<Boolean, StorageOperationStatus> result;
295 String componentId = "componentId";
296 when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
297 result = testInstance.validateComponentExists(componentId);
298 assertEquals(false, result.left().value());
302 public void testValidateToscaResourceNameExists() {
303 Either<Boolean, StorageOperationStatus> result;
304 String templateName = "templateName";
305 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
306 properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
307 List<GraphVertex> graphVertexList = new ArrayList<>();
308 GraphVertex graphVertex = getTopologyTemplateVertex();
309 graphVertexList.add(graphVertex);
310 when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
311 result = testInstance.validateToscaResourceNameExists(templateName);
312 assertEquals(true, result.left().value());
316 public void testValidateToscaResourceNameExists_false() {
317 Either<Boolean, StorageOperationStatus> result;
318 String templateName = "templateName";
319 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
320 properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
321 List<GraphVertex> graphVertexList = new ArrayList<>();
322 GraphVertex graphVertex = getTopologyTemplateVertex();
323 graphVertexList.add(graphVertex);
324 when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
325 result = testInstance.validateToscaResourceNameExists(templateName);
326 assertEquals(false, result.left().value());
330 public void testOverrideComponent() {
331 Either<Resource, StorageOperationStatus> result;
332 Resource resource = new Resource();
334 resource.setUniqueId(id);
335 GraphVertex graphVertex = getTopologyTemplateVertex();
336 graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
337 NodeType nodeType = new NodeType();
338 nodeType.setComponentType(ComponentTypeEnum.RESOURCE);
339 ToscaElement toscaElement = new TopologyTemplate();
340 toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
341 when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
342 when(titanDaoMock.getParentVertex(graphVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
343 when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
344 when(nodeTypeOperation.createToscaElement(any(ToscaElement.class))).thenReturn(Either.left(nodeType));
345 when(titanDaoMock.getVertexById(null, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
346 when(titanDaoMock.createEdge(graphVertex, graphVertex, EdgeLabelEnum.VERSION, null)).thenReturn(TitanOperationStatus.OK);
347 result = testInstance.overrideComponent(resource, resource);
348 assertTrue(result.isLeft());
352 public void testGetToscaElement() {
353 Either<Component, StorageOperationStatus> result;
355 GraphVertex graphVertex = getTopologyTemplateVertex();
356 ToscaElement toscaElement = getToscaElementForTest();
357 when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
358 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
359 result = testInstance.getToscaElement(id, JsonParseFlagEnum.ParseAll);
360 assertTrue(result.isLeft());
364 public void testMarkComponentToDelete() {
365 StorageOperationStatus result;
366 Component component = new Resource();
368 component.setUniqueId(id);
369 GraphVertex graphVertex = getTopologyTemplateVertex();
370 when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
371 when(nodeTypeOperation.markComponentToDelete(graphVertex)).thenReturn(Either.left(graphVertex));
372 result = testInstance.markComponentToDelete(component);
373 assertEquals(result, StorageOperationStatus.OK);
377 public void testDelToscaComponent() {
378 Either<Component, StorageOperationStatus> result;
379 String componentId = "compId";
380 GraphVertex graphVertex = getTopologyTemplateVertex();
381 ToscaElement toscaElement = getToscaElementForTest();
382 when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
383 when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
384 result = testInstance.deleteToscaComponent(componentId);
385 assertTrue(result.isLeft());
389 public void testGetLatestByToscaResourceName() {
390 Either<Component, StorageOperationStatus> result;
391 String toscaResourceName = "name";
392 ToscaElement toscaElement = getToscaElementForTest();
394 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
395 propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
396 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
397 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
398 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
400 List<GraphVertex> graphVertexList = new ArrayList<>();
401 GraphVertex graphVertex = getTopologyTemplateVertex();
402 graphVertex.setUniqueId(toscaResourceName);
403 Map<GraphPropertyEnum, Object> props = new HashMap<>();
404 props.put(GraphPropertyEnum.VERSION, "1.0");
405 graphVertex.setMetadataProperties(props);
406 graphVertexList.add(graphVertex);
408 when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
409 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
410 result = testInstance.getLatestByToscaResourceName(toscaResourceName);
411 assertTrue(result.isLeft());
415 public void testGetFollowed() {
416 Either<Set<Component>, StorageOperationStatus> result;
417 String userId = "id";
418 Set<LifecycleStateEnum> lifecycleStates = new HashSet<>();
419 Set<LifecycleStateEnum> lastStateStates = new HashSet<>();
420 lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
421 lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
422 lifecycleStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
423 lifecycleStates.add(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
424 lifecycleStates.add(LifecycleStateEnum.CERTIFIED);
425 lastStateStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
426 ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
427 List<ToscaElement> toscaEleList = new ArrayList<>();
428 ToscaElement toscaElement = getToscaElementForTest();
429 toscaEleList.add(toscaElement);
430 when(nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType)).thenReturn(Either.left(toscaEleList));
431 result = testInstance.getFollowed(userId, lifecycleStates, lastStateStates, componentType);
432 assertTrue(result.isLeft());
433 assertEquals(1, result.left().value().size());
437 public void testGetBySystemName() {
438 Either<List<Component>, StorageOperationStatus> result;
439 String sysName = "sysName";
440 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
441 ToscaElement toscaElement = getToscaElementForTest();
442 List<GraphVertex> componentVertices = new ArrayList<>();
443 GraphVertex graphVertex = getTopologyTemplateVertex();
444 componentVertices.add(graphVertex);
445 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
446 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
448 propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, sysName);
449 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentTypeEnum.name());
451 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
453 when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(componentVertices));
454 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
455 result = testInstance.getBySystemName(componentTypeEnum, sysName);
456 assertTrue(result.isLeft());
457 assertEquals(1, result.left().value().size());
461 public void testGetCompByNameAndVersion() {
462 Either<Component, StorageOperationStatus> result;
463 ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
464 String name = "name";
465 String version = "1.0";
466 JsonParseFlagEnum parseFlag = JsonParseFlagEnum.ParseAll;
467 List<GraphVertex> graphVertexList = new ArrayList<>();
468 GraphVertex graphVertex = getTopologyTemplateVertex();
469 graphVertexList.add(graphVertex);
470 ToscaElement toscaElement = getToscaElementForTest();
471 Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
472 Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
474 hasProperties.put(GraphPropertyEnum.NAME, name);
475 hasProperties.put(GraphPropertyEnum.VERSION, version);
476 hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
477 hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
478 when(titanDaoMock.getByCriteria(null, hasProperties, hasNotProperties, parseFlag)).thenReturn(Either.left(graphVertexList));
479 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
480 result = testInstance.getComponentByNameAndVersion(componentType, name, version, parseFlag);
481 assertTrue(result.isLeft());
484 private ToscaElement getToscaElementForTest() {
485 ToscaElement toscaElement = new TopologyTemplate();
486 toscaElement.setComponentType(ComponentTypeEnum.RESOURCE);
491 public void addDataTypesToComponentSuccessTest(){
492 Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.OK);
493 assertTrue(result.isLeft());
497 public void addDataTypesToComponentFailureTest_BadRequest(){
498 Either<List<DataTypeDefinition>, StorageOperationStatus> result = addDataTypesToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
499 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
502 private Either<List<DataTypeDefinition>, StorageOperationStatus> addDataTypesToComponentWithStatus(StorageOperationStatus status) {
503 Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
504 String componentId = "componentid";
507 PropertyDefinition noDefaultProp = new PropertyDefinition();
508 noDefaultProp.setName("noDefaultProp");
509 PropertyDefinition prop1 = new PropertyDefinition();
510 prop1.setDefaultValue("def1");
511 prop1.setName("prop1");
512 PropertyDefinition prop2 = new PropertyDefinition();
513 prop2.setType("dataType1");
514 prop2.setName("prop2");
515 PropertyDefinition prop3 = new PropertyDefinition();
516 prop3.setDefaultValue("def3");
517 prop3.setName("prop3");
519 DataTypeDefinition noDefaultValue = new DataTypeDefinition();
520 noDefaultValue.setProperties(Collections.singletonList(noDefaultProp));
521 noDefaultValue.setDerivedFromName("name0");
523 DataTypeDefinition dataType1 = new DataTypeDefinition();
524 dataType1.setProperties(Arrays.asList(prop1, prop3));
525 dataType1.setName("name1");
526 dataType1.setDerivedFromName("derivedfromname1");
528 DataTypeDefinition dataType2 = new DataTypeDefinition();
529 dataType2.setDerivedFrom(dataType1);
530 dataType2.setName("name2");
531 dataType2.setDerivedFromName("derivedfromname2");
533 DataTypeDefinition dataType3 = new DataTypeDefinition();
534 dataType3.setProperties(Collections.singletonList(prop2));
535 dataType3.setDerivedFrom(noDefaultValue);
536 dataType3.setName("name3");
537 dataType3.setDerivedFromName("derivedfromname3");
539 dataTypes.put("noDefault", noDefaultValue);
540 dataTypes.put("dataType1", dataType1);
541 dataTypes.put("dataType2", dataType2);
542 dataTypes.put("dataType3", dataType3);
545 if(status == StorageOperationStatus.OK){
546 vertex = getTopologyTemplateVertex();
548 vertex = getNodeTypeVertex();
550 Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
551 when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(getVertexEither);
552 when(topologyTemplateOperationMock.addToscaDataToToscaElement(eq(vertex),
553 eq(EdgeLabelEnum.DATA_TYPES), eq(VertexTypeEnum.DATA_TYPES), anyMap(), eq(JsonPresentationFields.NAME))).thenReturn(status);
554 return testInstance.addDataTypesToComponent(dataTypes, componentId);
558 public void testDataTypesToComponentFailureTest_NotFound() {
559 Either<List<DataTypeDefinition>, StorageOperationStatus> result;
560 String componentId = "componentId";
561 GraphVertex vertex = getNodeTypeVertex();
562 Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
563 when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
564 result = testInstance.addDataTypesToComponent(dataTypes, componentId);
565 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
569 public void testDeleteDataTypeOfComponent() {
570 StorageOperationStatus result;
571 Component component = new Resource();
573 component.setUniqueId(id);
574 String datatype = null;
576 DataTypeDefinition dataType1 = new DataTypeDefinition();
577 dataType1.setName("name1");
578 Map<String, DataTypeDataDefinition> dataTypeDataMap = new HashMap<>();
579 dataTypeDataMap.put("datatype1", dataType1);
580 List<DataTypeDefinition> dataTypeMap = dataTypeDataMap.values().stream().map(e -> { DataTypeDefinition dataType = new DataTypeDefinition(e);return dataType; }).collect(Collectors.toList());
581 component.setDataTypes(dataTypeMap);
582 GraphVertex graphVertex = getTopologyTemplateVertex();
583 result = testInstance.deleteDataTypeOfComponent(component, "datatype1");
584 assertEquals(datatype, result);
587 private Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponentWithStatus(StorageOperationStatus status) {
588 PolicyDefinition policy = new PolicyDefinition();
589 String componentId = "componentId";
592 if(status == StorageOperationStatus.OK){
593 vertex = getTopologyTemplateVertex();
595 vertex = getNodeTypeVertex();
597 Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
598 when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(getVertexEither);
599 when(topologyTemplateOperationMock.addPolicyToToscaElement(eq(vertex), any(PolicyDefinition.class), anyInt())).thenReturn(status);
600 return testInstance.associatePolicyToComponent(componentId, policy, counter);
603 private Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponentWithStatus(StorageOperationStatus status) {
604 PolicyDefinition policy = new PolicyDefinition();
605 String componentId = "componentId";
606 GraphVertex vertex = getTopologyTemplateVertex();
607 when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(Either.left(vertex));
608 when(topologyTemplateOperationMock.updatePolicyOfToscaElement(eq(vertex), any(PolicyDefinition.class))).thenReturn(status);
609 return testInstance.updatePolicyOfComponent(componentId, policy);
612 private void removePolicyFromComponentWithStatus(StorageOperationStatus status) {
613 String componentId = "componentId";
614 String policyId = "policyId";
615 GraphVertex vertex = getTopologyTemplateVertex();
616 Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
617 when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(getVertexEither);
618 when(topologyTemplateOperationMock.removePolicyFromToscaElement(eq(vertex), eq(policyId))).thenReturn(status);
619 StorageOperationStatus result = testInstance.removePolicyFromComponent(componentId, policyId);
620 assertSame(result, status);
623 private List<GraphVertex> getMockVertices(int numOfVertices) {
624 return IntStream.range(0, numOfVertices).mapToObj(i -> getTopologyTemplateVertex()).collect(Collectors.toList());
627 private ToscaElement getResourceToscaElement(String id) {
628 ToscaElement toscaElement = new TopologyTemplate();
629 toscaElement.setMetadata(new HashMap<>());
630 toscaElement.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), "RESOURCE");
631 toscaElement.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), id);
635 private GraphVertex getTopologyTemplateVertex() {
636 GraphVertex graphVertex = new GraphVertex();
637 graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
641 private GraphVertex getNodeTypeVertex() {
642 GraphVertex graphVertex = new GraphVertex();
643 graphVertex.setLabel(VertexTypeEnum.NODE_TYPE);