Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / input / DeclaredInputsResolverTest.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.input;
22
23 import com.google.common.collect.ImmutableMap;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
27 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
28 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
29 import org.openecomp.sdc.be.model.InputDefinition;
30 import org.openecomp.sdc.be.model.Resource;
31
32 import java.util.List;
33 import java.util.Map;
34
35 import static java.util.Arrays.asList;
36 import static java.util.Collections.emptyMap;
37 import static java.util.Collections.singletonList;
38 import static org.assertj.core.api.Assertions.assertThat;
39
40
41 public class DeclaredInputsResolverTest {
42
43     private DeclaredInputsResolver testInstance;
44     private Resource prevResource, currResource;
45
46     @Before
47     public void setUp() throws Exception {
48         testInstance = new DeclaredInputsResolver();
49         prevResource = new ResourceBuilder()
50                 .addInput("input1")
51                 .addInput("input2")
52                 .addInput("input3")
53                 .addInput("input4")
54                 .build();
55
56         currResource = new ResourceBuilder()
57                 .addInput("input1")
58                 .addInput("input3")
59                 .build();
60     }
61
62     @Test
63     public void whenPropertiesMapIsEmpty_returnEmptyList() {
64         List<InputDefinition> previouslyDeclaredInputsToMerge = testInstance.getPreviouslyDeclaredInputsToMerge(prevResource, currResource, emptyMap());
65         assertThat(previouslyDeclaredInputsToMerge).isEmpty();
66     }
67
68     @Test
69     public void whenPrevResourceHasNoInputs_returnEmptyList() {
70         List<InputDefinition> previouslyDeclaredInputsToMerge = testInstance.getPreviouslyDeclaredInputsToMerge(new Resource(), currResource, emptyMap());
71         assertThat(previouslyDeclaredInputsToMerge).isEmpty();
72     }
73
74     @Test
75     public void whenAllPropertiesNotReferencingInput_returnEmptyList() {
76         PropertyDataDefinition prop1 = createProperty("prop1");
77         PropertyDataDefinition prop2 = createProperty("prop2");
78         Map<String, List<PropertyDataDefinition>> props = ImmutableMap.of("inst1", singletonList(prop1), "inst2", singletonList(prop2));
79         List<InputDefinition> previouslyDeclaredInputsToMerge = testInstance.getPreviouslyDeclaredInputsToMerge(prevResource, currResource, props);
80         assertThat(previouslyDeclaredInputsToMerge).isEmpty();
81     }
82
83     @Test
84     public void doNotReturnReferencedInputIfAlreadyExistInNewResource() {
85         PropertyDataDefinition prop1 = createPropertyReferencingInput("prop1", "input1");
86         PropertyDataDefinition prop2 = createPropertyReferencingInput("prop2", "input3");
87         Map<String, List<PropertyDataDefinition>> props = ImmutableMap.of("inst1", singletonList(prop1), "inst2", singletonList(prop2));
88         List<InputDefinition> previouslyDeclaredInputsToMerge = testInstance.getPreviouslyDeclaredInputsToMerge(prevResource, currResource, props);
89         assertThat(previouslyDeclaredInputsToMerge).isEmpty();
90     }
91
92     @Test
93     public void returnAllInputsReferencedByPropertyAndNotExistInNewResource() {
94         PropertyDataDefinition prop1 = createPropertyReferencingInput("prop1", "input1");
95         PropertyDataDefinition prop2 = createPropertyReferencingInput("prop2", "input2");
96         PropertyDataDefinition prop3 = createPropertyReferencingInput("prop3", "input3");
97         PropertyDataDefinition prop4 = createPropertyReferencingInput("prop4", "input4");
98         PropertyDataDefinition prop5 = createProperty("prop5");
99         Map<String, List<PropertyDataDefinition>> props = ImmutableMap.of("inst1", asList(prop1, prop3), "inst2", singletonList(prop2), "group1", asList(prop4, prop5));
100         List<InputDefinition> previouslyDeclaredInputsToMerge = testInstance.getPreviouslyDeclaredInputsToMerge(prevResource, currResource, props);
101         assertThat(previouslyDeclaredInputsToMerge)
102                 .extracting("name")
103                 .containsExactlyInAnyOrder("input2", "input4");
104     }
105
106     private PropertyDataDefinition createPropertyReferencingInput(String propName, String referencingInputName) {
107         return new PropertyDataDefinitionBuilder()
108                 .setName(propName)
109                 .addGetInputValue(referencingInputName)
110                 .build();
111     }
112
113     private PropertyDataDefinition createProperty(String propName) {
114         return new PropertyDataDefinitionBuilder()
115                 .setName(propName)
116                 .build();
117     }
118
119
120
121 }