summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/RelationsComparatorTest.java
blob: bea1ff0acf44956f98bc7349d07a3b9bf95abc6e (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
137
138
139
140
141
142
143
144
145
146
147
/*-
 * ============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 org.junit.Before;
import org.junit.Test;
import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
import org.openecomp.sdc.be.components.utils.ObjectGenerator;
import org.openecomp.sdc.be.components.utils.RelationsBuilder;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
import org.openecomp.sdc.be.model.Resource;

import java.util.Arrays;
import java.util.LinkedList;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class RelationsComparatorTest {

    public static final String INSTANCE1 = "instance1";
    public static final String INSTANCE2 = "instance2";
    RelationsComparator testInstance;

    private RequirementCapabilityRelDef relation1;
    private RequirementCapabilityRelDef relation2;
    private RequirementCapabilityRelDef relation3;
    private RequirementCapabilityRelDef relation4;
    private ComponentInstance componentInstance1;
    private ComponentInstance componentInstance2;

    @Before
    public void setUp() {
        testInstance = new RelationsComparator();
        componentInstance1 =  new ComponentInstanceBuilder().setName(INSTANCE1).setId(INSTANCE1).build();
        componentInstance2 =  new ComponentInstanceBuilder().setName(INSTANCE2).setId(INSTANCE2).build();
        buildRelations();
    }

    @Test
    public void isRelationsChanged_sameRelationships() throws Exception {
        Resource oldResource = ObjectGenerator.buildResourceWithRelationships(relation1, relation2, relation3, relation4);
        oldResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
        Resource newResource = ObjectGenerator.buildResourceWithRelationships(relation4, relation3, relation1, relation2);
        newResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
        assertFalse(testInstance.isRelationsChanged(oldResource, newResource));
    }
    
    @Test
    public void isRelationsChanged_OldResourceNull() throws Exception {
        Resource oldResource = ObjectGenerator.buildResourceWithRelationships();
        oldResource.setComponentInstancesRelations(new LinkedList<>());
        oldResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
        Resource newResource = ObjectGenerator.buildResourceWithRelationships(relation4, relation3, relation1, relation2);
        newResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
        assertTrue(testInstance.isRelationsChanged(oldResource, newResource));
    }
    
    @Test
    public void isRelationsChanged_notSameAmountOfRelations()  {
        Resource oldResource = ObjectGenerator.buildResourceWithRelationships(relation1, relation2);
        oldResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
        Resource newResource = ObjectGenerator.buildResourceWithRelationships(relation1, relation2, relation3);
        newResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
        assertTrue(testInstance.isRelationsChanged(oldResource, newResource));
    }

    @Test
    public void isRelationsChanged_notSameFromNode() throws Exception {
        RequirementCapabilityRelDef relation2DifType = buildRelation("2", INSTANCE1);
        relation2DifType.setFromNode(INSTANCE2);
        isRelationsChangedTest(relation2DifType);
    }

    @Test
    public void isRelationsChanged_notSameType() throws Exception {
        RequirementCapabilityRelDef relation2DifType = buildRelation("2", INSTANCE1);
        relation2DifType.resolveSingleRelationship().getRelation().getRelationship().setType("someDiffType");
        isRelationsChangedTest(relation2DifType);
    }

    @Test
    public void isRelationsChanged_notSameCapability() throws Exception {
        RequirementCapabilityRelDef relation2DifType = buildRelation("2", INSTANCE1);
        relation2DifType.resolveSingleRelationship().getRelation().setCapabilityUid("someDiffUid");
        isRelationsChangedTest(relation2DifType);
    }

    @Test
    public void isRelationsChanged_notSameReqName() throws Exception {
        RequirementCapabilityRelDef relation2DifType = buildRelation("2", INSTANCE1);
        relation2DifType.resolveSingleRelationship().getRelation().setRequirement("someDiffReq");
        isRelationsChangedTest(relation2DifType);
    }

    @Test
    public void isRelationsChanged_notSameToNode() throws Exception {
        RequirementCapabilityRelDef relation2DifType = buildRelation("2", INSTANCE1);
        relation2DifType.setToNode("someDiffNode");
        isRelationsChangedTest(relation2DifType);
    }

    private void isRelationsChangedTest(RequirementCapabilityRelDef relation2DifType) {
        Resource oldResource = ObjectGenerator.buildResourceWithRelationships(relation1, relation2);
        oldResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
        Resource newResource = ObjectGenerator.buildResourceWithRelationships(relation1, relation2DifType);
        newResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
        assertTrue(testInstance.isRelationsChanged(oldResource, newResource));
    }


    private void buildRelations() {
        relation1 = buildRelation("1", INSTANCE1);
        relation2 = buildRelation("2", INSTANCE1);
        relation3 = buildRelation("3", INSTANCE2);
        relation4 = buildRelation("4", INSTANCE2);
    }

    private RequirementCapabilityRelDef buildRelation(String postFix, String instance) {
        return new RelationsBuilder()
                .setFromNode(instance)
                .setCapabilityUID("cap" + postFix)
                .setRelationType("type" + postFix)
                .setRequirementName("req" + postFix)
                .setToNode(instance)
                .build();
    }
}