Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / RelationsComparatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.merge;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
26 import org.openecomp.sdc.be.components.utils.ObjectGenerator;
27 import org.openecomp.sdc.be.components.utils.RelationsBuilder;
28 import org.openecomp.sdc.be.model.ComponentInstance;
29 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
30 import org.openecomp.sdc.be.model.Resource;
31
32 import java.util.Arrays;
33 import java.util.LinkedList;
34
35 import static org.junit.Assert.assertFalse;
36 import static org.junit.Assert.assertTrue;
37
38 public class RelationsComparatorTest {
39
40     public static final String INSTANCE1 = "instance1";
41     public static final String INSTANCE2 = "instance2";
42     RelationsComparator testInstance;
43
44     private RequirementCapabilityRelDef relation1;
45     private RequirementCapabilityRelDef relation2;
46     private RequirementCapabilityRelDef relation3;
47     private RequirementCapabilityRelDef relation4;
48     private ComponentInstance componentInstance1;
49     private ComponentInstance componentInstance2;
50
51     @Before
52     public void setUp() {
53         testInstance = new RelationsComparator();
54         componentInstance1 =  new ComponentInstanceBuilder().setName(INSTANCE1).setId(INSTANCE1).build();
55         componentInstance2 =  new ComponentInstanceBuilder().setName(INSTANCE2).setId(INSTANCE2).build();
56         buildRelations();
57     }
58
59     @Test
60     public void isRelationsChanged_sameRelationships() throws Exception {
61         Resource oldResource = ObjectGenerator.buildResourceWithRelationships(relation1, relation2, relation3, relation4);
62         oldResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
63         Resource newResource = ObjectGenerator.buildResourceWithRelationships(relation4, relation3, relation1, relation2);
64         newResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
65         assertFalse(testInstance.isRelationsChanged(oldResource, newResource));
66     }
67     
68     @Test
69     public void isRelationsChanged_OldResourceNull() throws Exception {
70         Resource oldResource = ObjectGenerator.buildResourceWithRelationships();
71         oldResource.setComponentInstancesRelations(new LinkedList<>());
72         oldResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
73         Resource newResource = ObjectGenerator.buildResourceWithRelationships(relation4, relation3, relation1, relation2);
74         newResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
75         assertTrue(testInstance.isRelationsChanged(oldResource, newResource));
76     }
77     
78     @Test
79     public void isRelationsChanged_notSameAmountOfRelations()  {
80         Resource oldResource = ObjectGenerator.buildResourceWithRelationships(relation1, relation2);
81         oldResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
82         Resource newResource = ObjectGenerator.buildResourceWithRelationships(relation1, relation2, relation3);
83         newResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
84         assertTrue(testInstance.isRelationsChanged(oldResource, newResource));
85     }
86
87     @Test
88     public void isRelationsChanged_notSameFromNode() throws Exception {
89         RequirementCapabilityRelDef relation2DifType = buildRelation("2", INSTANCE1);
90         relation2DifType.setFromNode(INSTANCE2);
91         isRelationsChangedTest(relation2DifType);
92     }
93
94     @Test
95     public void isRelationsChanged_notSameType() throws Exception {
96         RequirementCapabilityRelDef relation2DifType = buildRelation("2", INSTANCE1);
97         relation2DifType.resolveSingleRelationship().getRelation().getRelationship().setType("someDiffType");
98         isRelationsChangedTest(relation2DifType);
99     }
100
101     @Test
102     public void isRelationsChanged_notSameCapability() throws Exception {
103         RequirementCapabilityRelDef relation2DifType = buildRelation("2", INSTANCE1);
104         relation2DifType.resolveSingleRelationship().getRelation().setCapabilityUid("someDiffUid");
105         isRelationsChangedTest(relation2DifType);
106     }
107
108     @Test
109     public void isRelationsChanged_notSameReqName() throws Exception {
110         RequirementCapabilityRelDef relation2DifType = buildRelation("2", INSTANCE1);
111         relation2DifType.resolveSingleRelationship().getRelation().setRequirement("someDiffReq");
112         isRelationsChangedTest(relation2DifType);
113     }
114
115     @Test
116     public void isRelationsChanged_notSameToNode() throws Exception {
117         RequirementCapabilityRelDef relation2DifType = buildRelation("2", INSTANCE1);
118         relation2DifType.setToNode("someDiffNode");
119         isRelationsChangedTest(relation2DifType);
120     }
121
122     private void isRelationsChangedTest(RequirementCapabilityRelDef relation2DifType) {
123         Resource oldResource = ObjectGenerator.buildResourceWithRelationships(relation1, relation2);
124         oldResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
125         Resource newResource = ObjectGenerator.buildResourceWithRelationships(relation1, relation2DifType);
126         newResource.setComponentInstances(Arrays.asList(componentInstance1, componentInstance2));
127         assertTrue(testInstance.isRelationsChanged(oldResource, newResource));
128     }
129
130
131     private void buildRelations() {
132         relation1 = buildRelation("1", INSTANCE1);
133         relation2 = buildRelation("2", INSTANCE1);
134         relation3 = buildRelation("3", INSTANCE2);
135         relation4 = buildRelation("4", INSTANCE2);
136     }
137
138     private RequirementCapabilityRelDef buildRelation(String postFix, String instance) {
139         return new RelationsBuilder()
140                 .setFromNode(instance)
141                 .setCapabilityUID("cap" + postFix)
142                 .setRelationType("type" + postFix)
143                 .setRequirementName("req" + postFix)
144                 .setToNode(instance)
145                 .build();
146     }
147 }