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;
65 import static org.assertj.core.api.Assertions.assertThat;
67 import java.util.HashMap;
68 import java.util.List;
70 import java.util.ArrayList;
71 import java.util.EnumMap;
73 import java.util.HashSet;
74 import java.util.stream.Collectors;
75 import java.util.stream.IntStream;
77 import static org.junit.Assert.assertEquals;
78 import static org.junit.Assert.assertSame;
79 import static org.junit.Assert.assertTrue;
80 import static org.mockito.Mockito.when;
81 import static org.mockito.ArgumentMatchers.any;
82 import static org.mockito.ArgumentMatchers.anyMap;
83 import static org.mockito.ArgumentMatchers.anyInt;
84 import static org.mockito.ArgumentMatchers.eq;
86 @RunWith(MockitoJUnitRunner.class)
87 public class ToscaOperationFacadeTest {
90 private ToscaOperationFacade testInstance;
93 private HealingTitanDao titanDaoMock;
96 private TopologyTemplateOperation topologyTemplateOperationMock;
99 private NodeTypeOperation nodeTypeOperation;
102 public void setUp() throws Exception {
103 testInstance = new ToscaOperationFacade();
104 MockitoAnnotations.initMocks(this);
107 @SuppressWarnings("unchecked")
109 public void fetchMetaDataByResourceType() throws Exception {
110 ArgumentCaptor<Map> criteriaCapture = ArgumentCaptor.forClass(Map.class);
111 ArgumentCaptor<Map> criteriaNotCapture = ArgumentCaptor.forClass(Map.class);
112 ComponentParametersView dataFilter = new ComponentParametersView();
113 List<GraphVertex> mockVertices = getMockVertices(2);
114 Either<List<GraphVertex>, TitanOperationStatus> returnedVertices = Either.left(mockVertices);
116 when(titanDaoMock.getByCriteria(eq(null), criteriaCapture.capture(), criteriaNotCapture.capture(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(returnedVertices);
117 when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(0), dataFilter)).thenReturn(Either.left(getResourceToscaElement("0")));
118 when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(1), dataFilter)).thenReturn(Either.left(getResourceToscaElement("1")));
119 Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), dataFilter);
121 verifyCriteriaForHighestVersionAndVfResourceType(criteriaCapture);
122 verifyCriteriaNotIsDeleted(criteriaNotCapture);
124 assertTrue(fetchedComponents.isLeft());
125 List<Component> cmpts = fetchedComponents.left().value();
126 assertEquals(2, cmpts.size());
127 assertEquals("0", cmpts.get(0).getUniqueId());
128 assertEquals("1", cmpts.get(1).getUniqueId());
131 private void verifyCriteriaForHighestVersionAndVfResourceType(ArgumentCaptor<Map> criteriaCapture) {
132 Map<GraphPropertyEnum, Object> criteria = (Map<GraphPropertyEnum, Object>)criteriaCapture.getValue();
133 assertEquals(2, criteria.size());
134 assertEquals(criteria.get(GraphPropertyEnum.RESOURCE_TYPE), "VF");
135 assertEquals(criteria.get(GraphPropertyEnum.IS_HIGHEST_VERSION), true);
138 private void verifyCriteriaNotIsDeleted(ArgumentCaptor<Map> criteriaNotCapture) {
139 Map<GraphPropertyEnum, Object> notCriteria = (Map<GraphPropertyEnum, Object>)criteriaNotCapture.getValue();
140 assertEquals(1, notCriteria.size());
141 assertEquals(notCriteria.get(GraphPropertyEnum.IS_DELETED), true);
144 @SuppressWarnings("unchecked")
146 public void fetchMetaDataByResourceType_failedToGetData() throws Exception {
147 when(titanDaoMock.getByCriteria(eq(null), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.right(TitanOperationStatus.GENERAL_ERROR));
148 Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), new ComponentParametersView());
149 assertTrue(fetchedComponents.isRight());
150 assertEquals(StorageOperationStatus.GENERAL_ERROR, fetchedComponents.right().value());
154 public void associatePolicyToComponentSuccessTest(){
155 Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.OK);
156 assertTrue(result.isLeft());
160 public void associatePolicyToComponentFailureTest(){
161 Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.BAD_REQUEST);
162 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST);
166 public void updatePolicyOfComponentSuccessTest(){
167 Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.OK);
168 assertTrue(result.isLeft());
172 public void updatePolicyOfComponentFailureTest(){
173 Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.NOT_FOUND);
174 assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND);
178 public void removePolicyFromComponentSuccessTest(){
179 removePolicyFromComponentWithStatus(StorageOperationStatus.OK);
183 public void removePolicyFromComponentFailureTest(){
184 removePolicyFromComponentWithStatus(StorageOperationStatus.NOT_FOUND);
188 public void testFindLastCertifiedToscaElementByUUID(){
189 Either<Component, StorageOperationStatus> result;
190 Component component = new Resource();
191 List<GraphVertex> list = new ArrayList<>();
192 GraphVertex graphVertex = getTopologyTemplateVertex();
193 list.add(graphVertex);
194 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
195 props.put(GraphPropertyEnum.UUID, component.getUUID());
196 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
197 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
198 ToscaElement toscaElement = getToscaElementForTest();
199 when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
200 when(titanDaoMock.getByCriteria(ModelConverter.getVertexType(component), props)).thenReturn(Either.left(list));
201 result = testInstance.findLastCertifiedToscaElementByUUID(component);
202 Component resultComp = result.left().value();
203 assertEquals(resultComp.getToscaType(),ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
207 public void testLatestComponentByToscaResourceName(){
208 Either<Component, StorageOperationStatus> result;
209 TopologyTemplate toscaElement = new TopologyTemplate();
210 toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
211 List<GraphVertex> list = new ArrayList<>();
212 GraphVertex graphVertex = getTopologyTemplateVertex();
213 Map<GraphPropertyEnum, Object> props = new HashMap<>();
214 props.put(GraphPropertyEnum.VERSION, "1.0");
215 graphVertex.setMetadataProperties(props);
216 list.add(graphVertex);
218 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
219 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
220 propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, "toscaResourceName");
221 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
222 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
224 when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
225 when(topologyTemplateOperationMock.getToscaElement(ArgumentMatchers.eq(graphVertex),any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
227 result = testInstance.getFullLatestComponentByToscaResourceName("toscaResourceName");
228 assertThat(result.isLeft());
232 public void testValidateCsarUuidUniqueness() {
233 StorageOperationStatus result;
234 String csarUUID = "csarUUID";
235 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
236 properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
237 List<GraphVertex> vertexList = new ArrayList<>();
238 when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
239 result = testInstance.validateCsarUuidUniqueness(csarUUID);
240 assertEquals(StorageOperationStatus.ENTITY_ALREADY_EXISTS, result);
244 public void testValidateCsarUuidUnique_true() {
245 StorageOperationStatus result;
246 String csarUUID = "csarUUID";
247 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
248 properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
249 when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
250 result = testInstance.validateCsarUuidUniqueness(csarUUID);
251 assertEquals(StorageOperationStatus.OK, result);
255 public void testGetLatestCertiNodeTypeByToscaResourceName() {
256 Either<Resource, StorageOperationStatus> result;
257 String toscaResourceName = "resourceName";
258 String uniqueId = "uniqueId";
259 GraphVertex graphVertex = getTopologyTemplateVertex();
260 graphVertex.setJsonMetadataField(JsonPresentationFields.VERSION, "1.0");
261 graphVertex.setUniqueId(uniqueId);
262 List<GraphVertex> vertexList = new ArrayList<>();
263 vertexList.add(graphVertex);
264 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
265 props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
266 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
267 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
268 ToscaElement topologyTemplate = new TopologyTemplate();
269 topologyTemplate.setComponentType(ComponentTypeEnum.SERVICE);
270 when(titanDaoMock.getByCriteria(VertexTypeEnum.NODE_TYPE, props, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(vertexList));
271 when(titanDaoMock.getVertexById(uniqueId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
272 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(topologyTemplate));
273 result = testInstance.getLatestCertifiedNodeTypeByToscaResourceName(toscaResourceName);
274 assertThat(result.isLeft());
278 public void testValidateCompExists() {
279 Either<Boolean, StorageOperationStatus> result;
280 String componentId = "componentId";
281 GraphVertex graphVertex = getTopologyTemplateVertex();
282 when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
283 result = testInstance.validateComponentExists(componentId);
284 assertEquals(true, result.left().value());
288 public void testValidateCompExists_NotFound() {
289 Either<Boolean, StorageOperationStatus> result;
290 String componentId = "componentId";
291 when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
292 result = testInstance.validateComponentExists(componentId);
293 assertEquals(false, result.left().value());
297 public void testValidateToscaResourceNameExists() {
298 Either<Boolean, StorageOperationStatus> result;
299 String templateName = "templateName";
300 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
301 properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
302 List<GraphVertex> graphVertexList = new ArrayList<>();
303 GraphVertex graphVertex = getTopologyTemplateVertex();
304 graphVertexList.add(graphVertex);
305 when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
306 result = testInstance.validateToscaResourceNameExists(templateName);
307 assertEquals(true, result.left().value());
311 public void testValidateToscaResourceNameExists_false() {
312 Either<Boolean, StorageOperationStatus> result;
313 String templateName = "templateName";
314 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
315 properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, templateName);
316 List<GraphVertex> graphVertexList = new ArrayList<>();
317 GraphVertex graphVertex = getTopologyTemplateVertex();
318 graphVertexList.add(graphVertex);
319 when(titanDaoMock.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND));
320 result = testInstance.validateToscaResourceNameExists(templateName);
321 assertEquals(false, result.left().value());
325 public void testOverrideComponent() {
326 Either<Resource, StorageOperationStatus> result;
327 Resource resource = new Resource();
329 resource.setUniqueId(id);
330 GraphVertex graphVertex = getTopologyTemplateVertex();
331 graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
332 NodeType nodeType = new NodeType();
333 nodeType.setComponentType(ComponentTypeEnum.RESOURCE);
334 ToscaElement toscaElement = new TopologyTemplate();
335 toscaElement.setComponentType(ComponentTypeEnum.SERVICE);
336 when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
337 when(titanDaoMock.getParentVertex(graphVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
338 when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
339 when(nodeTypeOperation.createToscaElement(any(ToscaElement.class))).thenReturn(Either.left(nodeType));
340 when(titanDaoMock.getVertexById(null, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
341 when(titanDaoMock.createEdge(graphVertex, graphVertex, EdgeLabelEnum.VERSION, null)).thenReturn(TitanOperationStatus.OK);
342 result = testInstance.overrideComponent(resource, resource);
343 assertTrue(result.isLeft());
347 public void testGetToscaElement() {
348 Either<Component, StorageOperationStatus> result;
350 GraphVertex graphVertex = getTopologyTemplateVertex();
351 ToscaElement toscaElement = getToscaElementForTest();
352 when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
353 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
354 result = testInstance.getToscaElement(id, JsonParseFlagEnum.ParseAll);
355 assertTrue(result.isLeft());
359 public void testMarkComponentToDelete() {
360 StorageOperationStatus result;
361 Component component = new Resource();
363 component.setUniqueId(id);
364 GraphVertex graphVertex = getTopologyTemplateVertex();
365 when(titanDaoMock.getVertexById(id, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
366 when(nodeTypeOperation.markComponentToDelete(graphVertex)).thenReturn(Either.left(graphVertex));
367 result = testInstance.markComponentToDelete(component);
368 assertEquals(result, StorageOperationStatus.OK);
372 public void testDelToscaComponent() {
373 Either<Component, StorageOperationStatus> result;
374 String componentId = "compId";
375 GraphVertex graphVertex = getTopologyTemplateVertex();
376 ToscaElement toscaElement = getToscaElementForTest();
377 when(titanDaoMock.getVertexById(componentId, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(graphVertex));
378 when(topologyTemplateOperationMock.deleteToscaElement(graphVertex)).thenReturn(Either.left(toscaElement));
379 result = testInstance.deleteToscaComponent(componentId);
380 assertTrue(result.isLeft());
384 public void testGetLatestByToscaResourceName() {
385 Either<Component, StorageOperationStatus> result;
386 String toscaResourceName = "name";
387 ToscaElement toscaElement = getToscaElementForTest();
389 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
390 propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
391 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
392 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
393 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
395 List<GraphVertex> graphVertexList = new ArrayList<>();
396 GraphVertex graphVertex = getTopologyTemplateVertex();
397 graphVertex.setUniqueId(toscaResourceName);
398 Map<GraphPropertyEnum, Object> props = new HashMap<>();
399 props.put(GraphPropertyEnum.VERSION, "1.0");
400 graphVertex.setMetadataProperties(props);
401 graphVertexList.add(graphVertex);
403 when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata)).thenReturn(Either.left(graphVertexList));
404 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
405 result = testInstance.getLatestByToscaResourceName(toscaResourceName);
406 assertTrue(result.isLeft());
410 public void testGetFollowed() {
411 Either<Set<Component>, StorageOperationStatus> result;
412 String userId = "id";
413 Set<LifecycleStateEnum> lifecycleStates = new HashSet<>();
414 Set<LifecycleStateEnum> lastStateStates = new HashSet<>();
415 lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
416 lifecycleStates.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
417 lifecycleStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
418 lifecycleStates.add(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
419 lifecycleStates.add(LifecycleStateEnum.CERTIFIED);
420 lastStateStates.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
421 ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
422 List<ToscaElement> toscaEleList = new ArrayList<>();
423 ToscaElement toscaElement = getToscaElementForTest();
424 toscaEleList.add(toscaElement);
425 when(nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType)).thenReturn(Either.left(toscaEleList));
426 result = testInstance.getFollowed(userId, lifecycleStates, lastStateStates, componentType);
427 assertTrue(result.isLeft());
428 assertEquals(1, result.left().value().size());
432 public void testGetBySystemName() {
433 Either<List<Component>, StorageOperationStatus> result;
434 String sysName = "sysName";
435 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.RESOURCE;
436 ToscaElement toscaElement = getToscaElementForTest();
437 List<GraphVertex> componentVertices = new ArrayList<>();
438 GraphVertex graphVertex = getTopologyTemplateVertex();
439 componentVertices.add(graphVertex);
440 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
441 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
443 propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, sysName);
444 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentTypeEnum.name());
446 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
448 when(titanDaoMock.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(componentVertices));
449 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
450 result = testInstance.getBySystemName(componentTypeEnum, sysName);
451 assertTrue(result.isLeft());
452 assertEquals(1, result.left().value().size());
456 public void testGetCompByNameAndVersion() {
457 Either<Component, StorageOperationStatus> result;
458 ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
459 String name = "name";
460 String version = "1.0";
461 JsonParseFlagEnum parseFlag = JsonParseFlagEnum.ParseAll;
462 List<GraphVertex> graphVertexList = new ArrayList<>();
463 GraphVertex graphVertex = getTopologyTemplateVertex();
464 graphVertexList.add(graphVertex);
465 ToscaElement toscaElement = getToscaElementForTest();
466 Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
467 Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
469 hasProperties.put(GraphPropertyEnum.NAME, name);
470 hasProperties.put(GraphPropertyEnum.VERSION, version);
471 hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
472 hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
473 when(titanDaoMock.getByCriteria(null, hasProperties, hasNotProperties, parseFlag)).thenReturn(Either.left(graphVertexList));
474 when(topologyTemplateOperationMock.getToscaElement(any(GraphVertex.class), any(ComponentParametersView.class))).thenReturn(Either.left(toscaElement));
475 result = testInstance.getComponentByNameAndVersion(componentType, name, version, parseFlag);
476 assertTrue(result.isLeft());
479 private ToscaElement getToscaElementForTest() {
480 ToscaElement toscaElement = new TopologyTemplate();
481 toscaElement.setComponentType(ComponentTypeEnum.RESOURCE);
485 private Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponentWithStatus(StorageOperationStatus status) {
486 PolicyDefinition policy = new PolicyDefinition();
487 String componentId = "componentId";
490 if(status == StorageOperationStatus.OK){
491 vertex = getTopologyTemplateVertex();
493 vertex = getNodeTypeVertex();
495 Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
496 when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(getVertexEither);
497 when(topologyTemplateOperationMock.addPolicyToToscaElement(eq(vertex), any(PolicyDefinition.class), anyInt())).thenReturn(status);
498 return testInstance.associatePolicyToComponent(componentId, policy, counter);
501 private Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponentWithStatus(StorageOperationStatus status) {
502 PolicyDefinition policy = new PolicyDefinition();
503 String componentId = "componentId";
504 GraphVertex vertex = getTopologyTemplateVertex();
505 when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(Either.left(vertex));
506 when(topologyTemplateOperationMock.updatePolicyOfToscaElement(eq(vertex), any(PolicyDefinition.class))).thenReturn(status);
507 return testInstance.updatePolicyOfComponent(componentId, policy);
510 private void removePolicyFromComponentWithStatus(StorageOperationStatus status) {
511 String componentId = "componentId";
512 String policyId = "policyId";
513 GraphVertex vertex = getTopologyTemplateVertex();
514 Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex);
515 when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(getVertexEither);
516 when(topologyTemplateOperationMock.removePolicyFromToscaElement(eq(vertex), eq(policyId))).thenReturn(status);
517 StorageOperationStatus result = testInstance.removePolicyFromComponent(componentId, policyId);
518 assertSame(result, status);
521 private List<GraphVertex> getMockVertices(int numOfVertices) {
522 return IntStream.range(0, numOfVertices).mapToObj(i -> getTopologyTemplateVertex()).collect(Collectors.toList());
525 private ToscaElement getResourceToscaElement(String id) {
526 ToscaElement toscaElement = new TopologyTemplate();
527 toscaElement.setMetadata(new HashMap<>());
528 toscaElement.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), "RESOURCE");
529 toscaElement.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), id);
533 private GraphVertex getTopologyTemplateVertex() {
534 GraphVertex graphVertex = new GraphVertex();
535 graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
539 private GraphVertex getNodeTypeVertex() {
540 GraphVertex graphVertex = new GraphVertex();
541 graphVertex.setLabel(VertexTypeEnum.NODE_TYPE);