summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/input/InputsValuesMergingBusinessLogicTest.java
blob: 98fceab7a8d49b70365d96fee318d321f20d3459 (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
package org.openecomp.sdc.be.components.merge.input;

import org.junit.Before;
import org.junit.Test;
import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
import org.openecomp.sdc.be.components.utils.ResourceBuilder;
import org.openecomp.sdc.be.dao.utils.MapUtil;
import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.model.ComponentInstanceInput;
import org.openecomp.sdc.be.model.ComponentInstanceProperty;
import org.openecomp.sdc.be.model.InputDefinition;
import org.openecomp.sdc.be.model.Resource;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class InputsValuesMergingBusinessLogicTest {

    public static final String INPUT_DEFUALT_TYPE = "string";
    public static final String INPUT1_ID = "input1";
    public static final String INPUT2_ID = "input2";
    public static final String INPUT3_ID = "input3";
    public static final String INPUT4_ID = "input4";
    private InputsValuesMergingBusinessLogic testInstance;

    @Before
    public void setUp() throws Exception {
        testInstance = new InputsValuesMergingBusinessLogic();
    }

    @Test
    public void testMergeInputs_inputsOfDifferentType_dontCopyOldValue() throws Exception {
        InputDefinition oldInput = createUserDefinedInputDefinition(INPUT1_ID, "oldVal1");

        InputDefinition newInput = createInputDefinition(INPUT1_ID, null);
        newInput.setType("int");


        Map<String, InputDefinition> updatedInputs = Collections.singletonMap(newInput.getName(), newInput);
        Map<String, InputDefinition> oldInputs = Collections.singletonMap(oldInput.getName(), oldInput);
        testInstance.mergeComponentInputs(oldInputs, updatedInputs);

        assertNull(updatedInputs.get(INPUT1_ID).getDefaultValue());
    }

    @Test
    public void testMergeInputs_newInputsHaveNoValue_copyOldValues() throws Exception {
        InputDefinition oldInputWithCsarDefaultValue = createInputDefinition(INPUT1_ID, "oldVal1");
        InputDefinition oldInputWithUserDefinedValue = createUserDefinedInputDefinition(INPUT2_ID, "oldVal2");
        InputDefinition oldInputNotExistOnNew = createUserDefinedInputDefinition(INPUT3_ID, null);

        InputDefinition newInput1 = createInputDefinition(INPUT1_ID, "");
        InputDefinition newInput2 = createUserDefinedInputDefinition(INPUT2_ID, null);

        Map<String, InputDefinition> updatedInputs = mapInputsByName(Arrays.asList(newInput1, newInput2));
        Map<String, InputDefinition> oldInputs = mapInputsByName(Arrays.asList(oldInputWithCsarDefaultValue, oldInputWithUserDefinedValue, oldInputNotExistOnNew));
        testInstance.mergeComponentInputs(oldInputs, updatedInputs);

        assertEquals(oldInputWithCsarDefaultValue.getDefaultValue(), updatedInputs.get(INPUT1_ID).getDefaultValue());
        assertEquals(oldInputWithUserDefinedValue.getDefaultValue(), updatedInputs.get(INPUT2_ID).getDefaultValue());
        assertNull(updatedInputs.get(INPUT3_ID));
    }

    @Test
    public void testMergeInputs_newInputsHaveValue_dontOverrideNewValue() throws Exception {
        InputDefinition oldInputWithCsarDefaultValue = createInputDefinition(INPUT1_ID, "oldVal1");
        InputDefinition oldInputWithUserDefinedValue = createUserDefinedInputDefinition(INPUT2_ID, "oldVal2");
        InputDefinition oldInputWithNoValue = createUserDefinedInputDefinition(INPUT3_ID, null);

        InputDefinition newInput1 = createInputDefinition(INPUT1_ID, "newVal1");
        InputDefinition newInput2 = createUserDefinedInputDefinition(INPUT2_ID, "newVal2");
        InputDefinition newInput3 = createUserDefinedInputDefinition(INPUT3_ID, "newVal3");
        InputDefinition newInput4 = createUserDefinedInputDefinition(INPUT4_ID, "newVal4");

        Map<String, InputDefinition> updatedInputs = mapInputsByName(Arrays.asList(newInput1, newInput2, newInput3, newInput4));
        Map<String, InputDefinition> oldInputs = mapInputsByName(Arrays.asList(oldInputWithCsarDefaultValue, oldInputWithUserDefinedValue, oldInputWithNoValue));
        testInstance.mergeComponentInputs(oldInputs, updatedInputs);

        assertEquals(updatedInputs.get(INPUT1_ID).getDefaultValue(), newInput1.getDefaultValue());
        assertEquals(updatedInputs.get(INPUT2_ID).getDefaultValue(), newInput2.getDefaultValue());
        assertEquals(updatedInputs.get(INPUT3_ID).getDefaultValue(), newInput3.getDefaultValue());
        assertEquals(updatedInputs.get(INPUT4_ID).getDefaultValue(), newInput4.getDefaultValue());
    }

    @Test
    public void getPrevoislyDeclaredInputsToMerge() throws Exception {
        PropertyDataDefinition declaredInputProp1 = new PropertyDataDefinitionBuilder().addGetInputValue(INPUT1_ID).addGetInputValue(INPUT3_ID).setUniqueId("prevDeclaredPropId").build();
        PropertyDataDefinition declaredInputProp2 = new PropertyDataDefinitionBuilder().addGetInputValue(INPUT4_ID).setUniqueId("prevDeclaredPropId2").build();

        Resource prevResource = new ResourceBuilder().addInput(INPUT1_ID).addInput(INPUT2_ID).addInput(INPUT3_ID).addInput(INPUT4_ID).build();

        Resource currentResource = new ResourceBuilder()
                .addInput(INPUT2_ID)
                .addInstanceProperty("inst1", new ComponentInstanceProperty(declaredInputProp1))
                .addInstanceInput("inst2", new ComponentInstanceInput(declaredInputProp2))
                .build();

        List<InputDefinition> previouslyDeclaredInputs = testInstance.getPreviouslyDeclaredInputsToMerge(prevResource, currentResource);
        assertEquals(3, previouslyDeclaredInputs.size());

        assertInput(previouslyDeclaredInputs.get(0), INPUT1_ID, declaredInputProp1.getUniqueId(), "inst1");
        assertInput(previouslyDeclaredInputs.get(1), INPUT3_ID, declaredInputProp1.getUniqueId(), "inst1");
        assertInput(previouslyDeclaredInputs.get(2), INPUT4_ID, declaredInputProp2.getUniqueId(), "inst2");
    }

    private void assertInput(InputDefinition inputDefinition, String expectedInputId, String expectedPropertyId, String expectedInstanceUniqueId) {
        assertEquals(expectedInputId, inputDefinition.getUniqueId());
        assertEquals(expectedPropertyId, inputDefinition.getPropertyId());
        assertEquals(inputDefinition.getInstanceUniqueId(), expectedInstanceUniqueId);
    }

    private Map<String, InputDefinition> mapInputsByName(List<InputDefinition> inputs) {
        return MapUtil.toMap(inputs, InputDefinition::getName);
    }

    private InputDefinition createInputDefinition(String name, String value) {
        InputDefinition inputDef = new InputDefinition();
        inputDef.setName(name);
        inputDef.setDefaultValue(value);
        inputDef.setType(INPUT_DEFUALT_TYPE);
        return inputDef;
    }

    private InputDefinition createUserDefinedInputDefinition(String name, String value) {
        InputDefinition inputDef = createInputDefinition(name, value);
        inputDef.setOwnerId("owner");
        return inputDef;
    }
}