summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/TopologyComparatorTest.java
blob: d9cc1c8a335cd069ba355dc5a0180a748619837e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.components.merge;

import fj.data.Either;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
import org.openecomp.sdc.be.components.utils.ObjectGenerator;
import org.openecomp.sdc.be.components.utils.ResourceBuilder;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;

import static org.junit.Assert.*;
import static org.mockito.Mockito.when;

public class TopologyComparatorTest {

    @InjectMocks
    private TopologyComparator testInstance;

    @Mock
    private RelationsComparator relationsComparator;

    @Mock
    private ToscaOperationFacade toscaOperationFacade;

    @Mock
    private ComponentsUtils componentsUtils;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void compareTopologies_NotSameNumOfInstances() throws Exception {
        Resource resourceWith2Instances = ObjectGenerator.buildResourceWithComponentInstance("inst1", "inst2");
        Resource resourceWith1Instances = ObjectGenerator.buildResourceWithComponentInstance("inst1");
        Resource resourceWithNoInstances = new Resource();
        assertTrue(testInstance.isTopologyChanged(resourceWithNoInstances, resourceWith2Instances).left().value());
        assertTrue(testInstance.isTopologyChanged(resourceWithNoInstances, resourceWith1Instances).left().value());
        assertTrue(testInstance.isTopologyChanged(resourceWith1Instances, resourceWith2Instances).left().value());
    }
    
    @Test
    public void compareTopologies_notSameInstanceNames() throws Exception {
        Resource resource1 = ObjectGenerator.buildResourceWithComponentInstance("inst1", "inst2");
        Resource resource2 = ObjectGenerator.buildResourceWithComponentInstance("inst1", "inst3");
        assertTrue(testInstance.isTopologyChanged(resource1, resource2).left().value());
    }

    @Test
    public void compareTopologies_notSameInstanceTypes_notSameOriginInstanceTypes() throws Exception {
        ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setComponentUid("inst1").setToscaName("a.b.c").build();
        ComponentInstance inst2 = new ComponentInstanceBuilder().setName("inst2").setComponentUid("inst2").setToscaName("a.b.c.d").build();
        ComponentInstance inst2DiffType = new ComponentInstanceBuilder().setName("inst2").setComponentUid("inst2DiffType").setToscaName("a.b.c.d.e").build();
        Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
        Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2DiffType);
        Resource inst2OriginResource = new ResourceBuilder().setInvariantUUid("inst2Invariant").build();
        Resource inst2DiffTypeOriginResource = new ResourceBuilder().setInvariantUUid("inst2DiffTypeInvariant").build();
        when(toscaOperationFacade.getToscaElement(inst2.getComponentUid())).thenReturn(Either.left(inst2OriginResource));
        when(toscaOperationFacade.getToscaElement(inst2DiffType.getComponentUid())).thenReturn(Either.left(inst2DiffTypeOriginResource));
        assertTrue(testInstance.isTopologyChanged(resource1, resource2).left().value());
    }

    @Test
    public void compareTopologies_notSameInstanceTypes_failToFetchOriginComponent() throws Exception {
        ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setComponentUid("inst1").setToscaName("a.b.c").build();
        ComponentInstance inst1DiffOriginCmpt = new ComponentInstanceBuilder().setName("inst1").setComponentUid("inst1Diff").setToscaName("a.b.c.d").build();
        Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1);
        Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1DiffOriginCmpt);
        when(toscaOperationFacade.getToscaElement(inst1.getComponentUid())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
        when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
        assertEquals(ActionStatus.GENERAL_ERROR, testInstance.isTopologyChanged(resource1, resource2).right().value());
    }

    @Test
    public void compareTopologies_notSameRelations() throws Exception {
        ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setToscaName("a.b.c").build();
        ComponentInstance inst2 = new ComponentInstanceBuilder().setName("inst2").setToscaName("a.b.c.d").build();
        Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
        Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
        when(relationsComparator.isRelationsChanged(resource1, resource2)).thenReturn(true);
        assertTrue(testInstance.isTopologyChanged(resource1, resource2).left().value());
    }

    @Test
    public void compareTopologies_sameInstances_sameRelations_noTopologyChange() throws Exception {
        ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setToscaName("a.b.c").build();
        ComponentInstance inst2 = new ComponentInstanceBuilder().setName("inst2").setToscaName("a.b.c.d").build();
        Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
        Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
        when(relationsComparator.isRelationsChanged(resource1, resource2)).thenReturn(false);
        assertFalse(testInstance.isTopologyChanged(resource1, resource2).left().value());
    }

    @Test
    public void compareTopologies_sameInstancesInvariant_sameRelations_noTopologyChange() throws Exception {
        ComponentInstance inst1 = new ComponentInstanceBuilder().setName("inst1").setComponentUid("inst1").setToscaName("a.b.c").build();
        ComponentInstance inst2 = new ComponentInstanceBuilder().setName("inst2").setComponentUid("inst2").setToscaName("a.b.c.d").build();
        ComponentInstance inst2DiffType = new ComponentInstanceBuilder().setName("inst2").setComponentUid("inst2DiffType").setToscaName("a.b.c.d.e").build();
        Resource resource1 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2);
        Resource resource2 = ObjectGenerator.buildResourceWithComponentInstances(inst1, inst2DiffType);
        Resource inst2OriginResource = new ResourceBuilder().setInvariantUUid("inst2Invariant").build();
        when(toscaOperationFacade.getToscaElement(inst2.getComponentUid())).thenReturn(Either.left(inst2OriginResource));
        when(toscaOperationFacade.getToscaElement(inst2DiffType.getComponentUid())).thenReturn(Either.left(inst2OriginResource));
        when(relationsComparator.isRelationsChanged(resource1, resource2)).thenReturn(false);
        assertFalse(testInstance.isTopologyChanged(resource1, resource2).left().value());
    }
}