aboutsummaryrefslogtreecommitdiffstats
path: root/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java
blob: 3add149e23b9f41ae48e4c69aa69c0dc65b478f0 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * -
 *  ============LICENSE_START=======================================================
 *  Copyright (C) 2022 Nordix Foundation.
 *  ================================================================================
 *  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.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.datatypes.elements;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.openecomp.sdc.be.datatypes.enums.PropertySource;
import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;

class ToscaGetFunctionDataDefinitionTest {

    @Test
    void isSubPropertyTest() {
        final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
        assertFalse(toscaGetFunction.isSubProperty());
        toscaGetFunction.setPropertyPathFromSource(List.of("property1"));
        assertFalse(toscaGetFunction.isSubProperty());
        toscaGetFunction.setPropertyPathFromSource(List.of("property1", "subProperty1"));
        assertTrue(toscaGetFunction.isSubProperty());
    }

    @Test
    void generateGetInputSinglePropertyValueTest() {
        //given
        final String propertyName = "property";
        final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, List.of(propertyName), null);
        //when
        final String actualValue = toscaGetFunction.generatePropertyValue();
        //then
        final Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
        assertTrue(getInputJsonAsMap.containsKey(ToscaGetFunctionType.GET_INPUT.getFunctionName()));
        final Object value = getInputJsonAsMap.get(ToscaGetFunctionType.GET_INPUT.getFunctionName());
        assertTrue(value instanceof List);
        assertEquals(((List<String>)value).get(0), propertyName);
    }

    @Test
    void generateGetInputMultiplePropertyValueTest() {
        //given
        final var toscaGetFunction = createGetFunction(
            ToscaGetFunctionType.GET_INPUT,
            null,
            List.of("property", "subProperty", "subSubProperty"),
            null
        );
        //when
        final String actualValue = toscaGetFunction.generatePropertyValue();
        //then
        final Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
        assertTrue(getInputJsonAsMap.containsKey(ToscaGetFunctionType.GET_INPUT.getFunctionName()));
        final Object value = getInputJsonAsMap.get(ToscaGetFunctionType.GET_INPUT.getFunctionName());
        assertTrue(value instanceof List);
        assertEquals(value, toscaGetFunction.getPropertyPathFromSource());
    }

    @ParameterizedTest
    @EnumSource(value =  ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
    void generateValueForGetFunctionWithSelfAsSourceTest(final ToscaGetFunctionType toscaFunction) {
        //given
        final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.SELF, List.of("property"), null);
        //when
        String actualValue = toscaGetFunction.generatePropertyValue();
        //then
        Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
        assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
        Object actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
        List<String> expectedGetPropertyValue = Stream.concat(
                Stream.of(PropertySource.SELF.getName()),
                toscaGetFunction.getPropertyPathFromSource().stream())
            .collect(Collectors.toList());
        assertEquals(expectedGetPropertyValue, actualGetPropertyValue);

        //given a sub property path
        toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
        //when
        actualValue = toscaGetFunction.generatePropertyValue();
        //then
        getInputJsonAsMap = convertJsonStringToMap(actualValue);
        assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
        actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
        expectedGetPropertyValue = Stream.concat(
                Stream.of(PropertySource.SELF.getName()),
                toscaGetFunction.getPropertyPathFromSource().stream())
            .collect(Collectors.toList());
        assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
    }

    @ParameterizedTest
    @EnumSource(value =  ToscaGetFunctionType.class, names = {"GET_ATTRIBUTE", "GET_PROPERTY"})
    void generateValueForGetFunctionWithInstanceAsSourceTest(final ToscaGetFunctionType toscaFunction) {
        //given
        final var toscaGetFunction = createGetFunction(toscaFunction, PropertySource.INSTANCE, List.of("property"), "sourceName");
        //when
        String actualValue = toscaGetFunction.generatePropertyValue();
        //then
        Map<?, ?> getInputJsonAsMap = convertJsonStringToMap(actualValue);
        assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
        Object actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
        List<String> expectedGetPropertyValue = Stream.concat(
                Stream.of(toscaGetFunction.getSourceName()),
                toscaGetFunction.getPropertyPathFromSource().stream())
            .collect(Collectors.toList());
        assertEquals(expectedGetPropertyValue, actualGetPropertyValue);

        //given a sub property path
        toscaGetFunction.setPropertyPathFromSource(List.of("property", "subProperty", "subSubProperty"));
        //when
        actualValue = toscaGetFunction.generatePropertyValue();
        //then
        getInputJsonAsMap = convertJsonStringToMap(actualValue);
        assertTrue(getInputJsonAsMap.containsKey(toscaFunction.getFunctionName()));
        actualGetPropertyValue = getInputJsonAsMap.get(toscaFunction.getFunctionName());
        expectedGetPropertyValue = Stream.concat(
                Stream.of(toscaGetFunction.getSourceName()),
                toscaGetFunction.getPropertyPathFromSource().stream())
            .collect(Collectors.toList());
        assertEquals(expectedGetPropertyValue, actualGetPropertyValue);
    }

    @Test
    void generateValueFunctionTypeIsRequiredTest() {
        final var toscaGetFunction = createGetFunction(null, null, List.of("property"), null);
        toscaGetFunction.setPropertyPathFromSource(List.of("property"));
        final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
        assertEquals("functionType is required in order to generate the get function value", actualException.getMessage());
    }

    @Test
    void generateValuePropertyPathIsRequiredTest() {
        final var toscaGetFunction = createGetFunction(ToscaGetFunctionType.GET_INPUT, null, null, null);
        final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
        assertEquals("propertyPathFromSource is required in order to generate the get function value", actualException.getMessage());
    }

    @Test
    void generateValuePropertySourceIsRequiredForGetPropertyTest() {
        final var toscaGetFunction = createGetFunction(
            ToscaGetFunctionType.GET_PROPERTY,
            null,
            List.of("property"),
            null);
        final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);
        assertEquals("propertySource is required in order to generate the get_property value", actualException.getMessage());
    }

    @Test
    void generateValueSourceNameIsRequiredForGetInstancePropertyTest() {
        final ToscaGetFunctionDataDefinition toscaGetFunction = createGetFunction(
            ToscaGetFunctionType.GET_PROPERTY,
            PropertySource.INSTANCE,
            List.of("property"),
            null);
        final IllegalStateException actualException = assertThrows(IllegalStateException.class, toscaGetFunction::generatePropertyValue);

        assertEquals("sourceName is required in order to generate the get_property from INSTANCE value", actualException.getMessage());
    }

    @Test
    void getTypeTest() {
        final ToscaGetFunctionDataDefinition toscaGetFunctionDataDefinition = new ToscaGetFunctionDataDefinition();
        assertNull(toscaGetFunctionDataDefinition.getType());
        toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_INPUT);
        assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunctionDataDefinition.getType());
        toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_ATTRIBUTE);
        assertEquals(ToscaFunctionType.GET_ATTRIBUTE, toscaGetFunctionDataDefinition.getType());
        toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
        assertEquals(ToscaFunctionType.GET_PROPERTY, toscaGetFunctionDataDefinition.getType());
    }

    private ToscaGetFunctionDataDefinition createGetFunction(final ToscaGetFunctionType toscaGetFunctionType,
                                                             final PropertySource propertySource,
                                                             final List<String> propertyPath, String sourceName) {
        final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
        toscaGetFunction.setFunctionType(toscaGetFunctionType);
        toscaGetFunction.setPropertySource(propertySource);
        toscaGetFunction.setPropertyPathFromSource(propertyPath);
        toscaGetFunction.setSourceName(sourceName);
        toscaGetFunction.setToscaIndexList(new ArrayList<>());
        return toscaGetFunction;
    }

    private Map<?, ?> convertJsonStringToMap(final String actualValue) {
        final Gson gson = new Gson();
        return gson.fromJson(actualValue, Map.class);
    }
}