2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.merge;
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
30 import org.openecomp.sdc.be.components.utils.ObjectGenerator;
31 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
32 import org.openecomp.sdc.be.dao.api.ActionStatus;
33 import org.openecomp.sdc.be.impl.ComponentsUtils;
34 import org.openecomp.sdc.be.model.ComponentInstance;
35 import org.openecomp.sdc.be.model.Resource;
36 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
37 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import static org.junit.Assert.*;
40 import static org.mockito.Mockito.when;
42 public class TopologyComparatorTest {
45 private TopologyComparator testInstance;
48 private RelationsComparator relationsComparator;
51 private ToscaOperationFacade toscaOperationFacade;
54 private ComponentsUtils componentsUtils;
57 public void setUp() throws Exception {
58 MockitoAnnotations.initMocks(this);
62 public void compareTopologies_NotSameNumOfInstances() throws Exception {
63 Resource resourceWith2Instances = ObjectGenerator.buildResourceWithComponentInstance("inst1", "inst2");
64 Resource resourceWith1Instances = ObjectGenerator.buildResourceWithComponentInstance("inst1");
65 Resource resourceWithNoInstances = new Resource();
66 assertTrue(testInstance.isTopologyChanged(resourceWithNoInstances, resourceWith2Instances).left().value());
67 assertTrue(testInstance.isTopologyChanged(resourceWithNoInstances, resourceWith1Instances).left().value());
68 assertTrue(testInstance.isTopologyChanged(resourceWith1Instances, resourceWith2Instances).left().value());
72 public void compareTopologies_notSameInstanceNames() throws Exception {
73 Resource resource1 = ObjectGenerator.buildResourceWithComponentInstance("inst1", "inst2");
74 Resource resource2 = ObjectGenerator.buildResourceWithComponentInstance("inst1", "inst3");
75 assertTrue(testInstance.isTopologyChanged(resource1, resource2).left().value());
79 public void compareTopologies_notSameInstanceTypes_notSameOriginInstanceTypes() throws Exception {
80 ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setComponentUid("inst1").setToscaName("a.b.c").build();
81 ComponentInstance inst2 = new ComponentInstanceBuilder().setName("inst2").setComponentUid("inst2").setToscaName("a.b.c.d").build();
82 ComponentInstance inst2DiffType = new ComponentInstanceBuilder().setName("inst2").setComponentUid("inst2DiffType").setToscaName("a.b.c.d.e").build();
83 Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
84 Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2DiffType);
85 Resource inst2OriginResource = new ResourceBuilder().setInvariantUUid("inst2Invariant").build();
86 Resource inst2DiffTypeOriginResource = new ResourceBuilder().setInvariantUUid("inst2DiffTypeInvariant").build();
87 when(toscaOperationFacade.getToscaElement(inst2.getComponentUid())).thenReturn(Either.left(inst2OriginResource));
88 when(toscaOperationFacade.getToscaElement(inst2DiffType.getComponentUid())).thenReturn(Either.left(inst2DiffTypeOriginResource));
89 assertTrue(testInstance.isTopologyChanged(resource1, resource2).left().value());
93 public void compareTopologies_notSameInstanceTypes_failToFetchOriginComponent() throws Exception {
94 ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setComponentUid("inst1").setToscaName("a.b.c").build();
95 ComponentInstance inst1DiffOriginCmpt = new ComponentInstanceBuilder().setName("inst1").setComponentUid("inst1Diff").setToscaName("a.b.c.d").build();
96 Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1);
97 Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1DiffOriginCmpt);
98 when(toscaOperationFacade.getToscaElement(inst1.getComponentUid())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
99 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
100 assertEquals(ActionStatus.GENERAL_ERROR, testInstance.isTopologyChanged(resource1, resource2).right().value());
104 public void compareTopologies_notSameRelations() throws Exception {
105 ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setToscaName("a.b.c").build();
106 ComponentInstance inst2 = new ComponentInstanceBuilder().setName("inst2").setToscaName("a.b.c.d").build();
107 Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
108 Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
109 when(relationsComparator.isRelationsChanged(resource1, resource2)).thenReturn(true);
110 assertTrue(testInstance.isTopologyChanged(resource1, resource2).left().value());
114 public void compareTopologies_sameInstances_sameRelations_noTopologyChange() throws Exception {
115 ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setToscaName("a.b.c").build();
116 ComponentInstance inst2 = new ComponentInstanceBuilder().setName("inst2").setToscaName("a.b.c.d").build();
117 Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
118 Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
119 when(relationsComparator.isRelationsChanged(resource1, resource2)).thenReturn(false);
120 assertFalse(testInstance.isTopologyChanged(resource1, resource2).left().value());
124 public void compareTopologies_sameInstancesInvariant_sameRelations_noTopologyChange() throws Exception {
125 ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setComponentUid("inst1").setToscaName("a.b.c").build();
126 ComponentInstance inst2 = new ComponentInstanceBuilder().setName("inst2").setComponentUid("inst2").setToscaName("a.b.c.d").build();
127 ComponentInstance inst2DiffType = new ComponentInstanceBuilder().setName("inst2").setComponentUid("inst2DiffType").setToscaName("a.b.c.d.e").build();
128 Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
129 Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2DiffType);
130 Resource inst2OriginResource = new ResourceBuilder().setInvariantUUid("inst2Invariant").build();
131 when(toscaOperationFacade.getToscaElement(inst2.getComponentUid())).thenReturn(Either.left(inst2OriginResource));
132 when(toscaOperationFacade.getToscaElement(inst2DiffType.getComponentUid())).thenReturn(Either.left(inst2OriginResource));
133 when(relationsComparator.isRelationsChanged(resource1, resource2)).thenReturn(false);
134 assertFalse(testInstance.isTopologyChanged(resource1, resource2).left().value());