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.assertEquals;
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertTrue;
42 import static org.mockito.Mockito.when;
44 public class TopologyComparatorTest {
47 private TopologyComparator testInstance;
50 private RelationsComparator relationsComparator;
53 private ToscaOperationFacade toscaOperationFacade;
56 private ComponentsUtils componentsUtils;
59 public void setUp() throws Exception {
60 MockitoAnnotations.initMocks(this);
64 public void compareTopologies_NotSameNumOfInstances() throws Exception {
65 Resource resourceWith2Instances = ObjectGenerator.buildResourceWithComponentInstance("inst1", "inst2");
66 Resource resourceWith1Instances = ObjectGenerator.buildResourceWithComponentInstance("inst1");
67 Resource resourceWithNoInstances = new Resource();
68 assertTrue(testInstance.isTopologyChanged(resourceWithNoInstances, resourceWith2Instances).left().value());
69 assertTrue(testInstance.isTopologyChanged(resourceWithNoInstances, resourceWith1Instances).left().value());
70 assertTrue(testInstance.isTopologyChanged(resourceWith1Instances, resourceWith2Instances).left().value());
74 public void compareTopologies_notSameInstanceNames() throws Exception {
75 Resource resource1 = ObjectGenerator.buildResourceWithComponentInstance("inst1", "inst2");
76 Resource resource2 = ObjectGenerator.buildResourceWithComponentInstance("inst1", "inst3");
77 assertTrue(testInstance.isTopologyChanged(resource1, resource2).left().value());
81 public void compareTopologies_notSameInstanceTypes_notSameOriginInstanceTypes() throws Exception {
82 ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setComponentUid("inst1").setToscaName("a.b.c").build();
83 ComponentInstance inst2 = new ComponentInstanceBuilder().setName("inst2").setComponentUid("inst2").setToscaName("a.b.c.d").build();
84 ComponentInstance inst2DiffType = new ComponentInstanceBuilder().setName("inst2").setComponentUid("inst2DiffType").setToscaName("a.b.c.d.e").build();
85 Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
86 Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2DiffType);
87 Resource inst2OriginResource = new ResourceBuilder().setInvariantUUid("inst2Invariant").build();
88 Resource inst2DiffTypeOriginResource = new ResourceBuilder().setInvariantUUid("inst2DiffTypeInvariant").build();
89 when(toscaOperationFacade.getToscaElement(inst2.getComponentUid())).thenReturn(Either.left(inst2OriginResource));
90 when(toscaOperationFacade.getToscaElement(inst2DiffType.getComponentUid())).thenReturn(Either.left(inst2DiffTypeOriginResource));
91 assertTrue(testInstance.isTopologyChanged(resource1, resource2).left().value());
95 public void compareTopologies_notSameInstanceTypes_failToFetchOriginComponent() throws Exception {
96 ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setComponentUid("inst1").setToscaName("a.b.c").build();
97 ComponentInstance inst1DiffOriginCmpt = new ComponentInstanceBuilder().setName("inst1").setComponentUid("inst1Diff").setToscaName("a.b.c.d").build();
98 Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1);
99 Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1DiffOriginCmpt);
100 when(toscaOperationFacade.getToscaElement(inst1.getComponentUid())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
101 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
102 assertEquals(ActionStatus.GENERAL_ERROR, testInstance.isTopologyChanged(resource1, resource2).right().value());
106 public void compareTopologies_notSameRelations() throws Exception {
107 ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setToscaName("a.b.c").build();
108 ComponentInstance inst2 = new ComponentInstanceBuilder().setName("inst2").setToscaName("a.b.c.d").build();
109 Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
110 Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
111 when(relationsComparator.isRelationsChanged(resource1, resource2)).thenReturn(true);
112 assertTrue(testInstance.isTopologyChanged(resource1, resource2).left().value());
116 public void compareTopologies_sameInstances_sameRelations_noTopologyChange() throws Exception {
117 ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setToscaName("a.b.c").build();
118 ComponentInstance inst2 = new ComponentInstanceBuilder().setName("inst2").setToscaName("a.b.c.d").build();
119 Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
120 Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
121 when(relationsComparator.isRelationsChanged(resource1, resource2)).thenReturn(false);
122 assertFalse(testInstance.isTopologyChanged(resource1, resource2).left().value());
126 public void compareTopologies_sameInstancesInvariant_sameRelations_noTopologyChange() throws Exception {
127 ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setComponentUid("inst1").setToscaName("a.b.c").build();
128 ComponentInstance inst2 = new ComponentInstanceBuilder().setName("inst2").setComponentUid("inst2").setToscaName("a.b.c.d").build();
129 ComponentInstance inst2DiffType = new ComponentInstanceBuilder().setName("inst2").setComponentUid("inst2DiffType").setToscaName("a.b.c.d.e").build();
130 Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
131 Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2DiffType);
132 Resource inst2OriginResource = new ResourceBuilder().setInvariantUUid("inst2Invariant").build();
133 when(toscaOperationFacade.getToscaElement(inst2.getComponentUid())).thenReturn(Either.left(inst2OriginResource));
134 when(toscaOperationFacade.getToscaElement(inst2DiffType.getComponentUid())).thenReturn(Either.left(inst2OriginResource));
135 when(relationsComparator.isRelationsChanged(resource1, resource2)).thenReturn(false);
136 assertFalse(testInstance.isTopologyChanged(resource1, resource2).left().value());