Increase unit test coverage for backend
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / dto / FilterConstraintDtoTest.java
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.be.model.dto;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Optional;
32 import org.junit.jupiter.api.Test;
33 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
34 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
35 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
36
37 class FilterConstraintDtoTest {
38
39     @Test
40     void isCapabilityPropertyFilter() {
41         var filterConstraintDto = new FilterConstraintDto();
42         assertFalse(filterConstraintDto.isCapabilityPropertyFilter());
43         filterConstraintDto.setCapabilityName("aCapability");
44         assertTrue(filterConstraintDto.isCapabilityPropertyFilter());
45     }
46
47     @Test
48     void readGetFunctionWithToscaGetFunctionInstanceAsValue() {
49         final var filterConstraintDto = new FilterConstraintDto();
50         final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
51         toscaGetFunction.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
52         filterConstraintDto.setValue(toscaGetFunction);
53         final Optional<ToscaGetFunctionDataDefinition> readGetFunctionOpt = filterConstraintDto.getAsToscaGetFunction();
54         assertTrue(readGetFunctionOpt.isPresent());
55         assertEquals(toscaGetFunction, readGetFunctionOpt.get());
56     }
57
58     @Test
59     void readGetFunctionWithInvalidGetFunctionValue() {
60         final var filterConstraintDto = new FilterConstraintDto();
61         filterConstraintDto.setValue("not a ToscaGetFunctionDataDefinition");
62         final Optional<ToscaGetFunctionDataDefinition> readGetFunctionOpt = filterConstraintDto.getAsToscaGetFunction();
63         assertTrue(readGetFunctionOpt.isEmpty());
64     }
65
66     @Test
67     void readGetFunctionWithGetFunctionValueAsMap() {
68         //given
69         final List<String> propertyPathFromSource = List.of("input", "path");
70         final String propertyUniqueId = "propertyUniqueIdValue";
71         final String propertyName = "propertyNameValue";
72         final String sourceUniqueId = "sourceUniqueIdValue";
73         final String sourceName = "sourceNameValue";
74         final Map<String, Object> toscaGetFunctionAsMap = Map.of(
75             "propertyUniqueId", propertyUniqueId,
76             "propertyName", propertyName,
77             "propertySource", PropertySource.SELF.getName(),
78             "sourceUniqueId", sourceUniqueId,
79             "sourceName", sourceName,
80             "functionType", ToscaGetFunctionType.GET_INPUT.getFunctionName(),
81             "propertyPathFromSource", propertyPathFromSource
82         );
83
84         final var filterConstraintDto = new FilterConstraintDto();
85         filterConstraintDto.setValue(toscaGetFunctionAsMap);
86         //when
87         final Optional<ToscaGetFunctionDataDefinition> readGetFunctionOpt = filterConstraintDto.getAsToscaGetFunction();
88         //then
89         assertTrue(readGetFunctionOpt.isPresent());
90         final ToscaGetFunctionDataDefinition toscaGetFunctionDataDefinition = readGetFunctionOpt.get();
91         assertEquals(toscaGetFunctionDataDefinition.getPropertyUniqueId(), propertyUniqueId);
92         assertEquals(toscaGetFunctionDataDefinition.getPropertyName(), propertyName);
93         assertEquals(PropertySource.SELF, toscaGetFunctionDataDefinition.getPropertySource());
94         assertEquals(toscaGetFunctionDataDefinition.getSourceUniqueId(), sourceUniqueId);
95         assertEquals(toscaGetFunctionDataDefinition.getSourceName(), sourceName);
96         assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunctionDataDefinition.getFunctionType());
97         assertEquals(2, toscaGetFunctionDataDefinition.getPropertyPathFromSource().size());
98         assertEquals(toscaGetFunctionDataDefinition.getPropertyPathFromSource().get(0), propertyPathFromSource.get(0));
99         assertEquals(toscaGetFunctionDataDefinition.getPropertyPathFromSource().get(1), propertyPathFromSource.get(1));
100     }
101
102     @Test
103     void readGetFunctionWithGetFunctionAsListValueAsMap() {
104         //given
105         final List<String> propertyPathFromSource1 = List.of("input1", "path1");
106         final String propertyUniqueId1 = "propertyUniqueIdValue1";
107         final String propertyName1 = "propertyNameValue1";
108         final String sourceUniqueId1 = "sourceUniqueIdValue1";
109         final String sourceName1 = "sourceNameValue1";
110         final Map<String, Object> toscaGetFunctionAsMap1 = Map.of(
111             "propertyUniqueId", propertyUniqueId1,
112             "propertyName", propertyName1,
113             "propertySource", PropertySource.SELF.getName(),
114             "sourceUniqueId", sourceUniqueId1,
115             "sourceName", sourceName1,
116             "functionType", ToscaGetFunctionType.GET_INPUT.getFunctionName(),
117             "propertyPathFromSource", propertyPathFromSource1
118         );
119         final List<String> propertyPathFromSource2 = List.of("input2", "path2");
120         final String propertyUniqueId2 = "propertyUniqueIdValue2";
121         final String propertyName2 = "propertyNameValue2";
122         final String sourceUniqueId2 = "sourceUniqueIdValue2";
123         final String sourceName2 = "sourceNameValue2";
124         final Map<String, Object> toscaGetFunctionAsMap2 = Map.of(
125             "propertyUniqueId", propertyUniqueId2,
126             "propertyName", propertyName2,
127             "propertySource", PropertySource.SELF.getName(),
128             "sourceUniqueId", sourceUniqueId2,
129             "sourceName", sourceName2,
130             "functionType", ToscaGetFunctionType.GET_INPUT.getFunctionName(),
131             "propertyPathFromSource", propertyPathFromSource2
132         );
133
134         List<Object> toscaGetFunctionDataDefinitionList = new ArrayList<>();
135         toscaGetFunctionDataDefinitionList.add(toscaGetFunctionAsMap1);
136         toscaGetFunctionDataDefinitionList.add(toscaGetFunctionAsMap2);
137
138         final var filterConstraintDto = new FilterConstraintDto();
139         filterConstraintDto.setValue(toscaGetFunctionDataDefinitionList);
140         //when
141         final Optional<List<ToscaGetFunctionDataDefinition>> readListGetFunctionOpt = filterConstraintDto.getAsListToscaGetFunction();
142         //then
143         assertTrue(readListGetFunctionOpt.isPresent());
144         final List<ToscaGetFunctionDataDefinition> toscaGetFunctionDataDefinition = readListGetFunctionOpt.get();
145         assertEquals(toscaGetFunctionDataDefinition.get(0).getPropertyUniqueId(), propertyUniqueId1);
146         assertEquals(toscaGetFunctionDataDefinition.get(1).getPropertyUniqueId(), propertyUniqueId2);
147         assertEquals(toscaGetFunctionDataDefinition.get(0).getPropertyName(), propertyName1);
148         assertEquals(toscaGetFunctionDataDefinition.get(1).getPropertyName(), propertyName2);
149         assertEquals(PropertySource.SELF, toscaGetFunctionDataDefinition.get(0).getPropertySource());
150         assertEquals(PropertySource.SELF, toscaGetFunctionDataDefinition.get(1).getPropertySource());
151         assertEquals(toscaGetFunctionDataDefinition.get(0).getSourceUniqueId(), sourceUniqueId1);
152         assertEquals(toscaGetFunctionDataDefinition.get(1).getSourceUniqueId(), sourceUniqueId2);
153         assertEquals(toscaGetFunctionDataDefinition.get(0).getSourceName(), sourceName1);
154         assertEquals(toscaGetFunctionDataDefinition.get(1).getSourceName(), sourceName2);
155         assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunctionDataDefinition.get(0).getFunctionType());
156         assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunctionDataDefinition.get(1).getFunctionType());
157         assertEquals(2, toscaGetFunctionDataDefinition.get(0).getPropertyPathFromSource().size());
158         assertEquals(2, toscaGetFunctionDataDefinition.get(1).getPropertyPathFromSource().size());
159         assertEquals(toscaGetFunctionDataDefinition.get(0).getPropertyPathFromSource().get(0), propertyPathFromSource1.get(0));
160         assertEquals(toscaGetFunctionDataDefinition.get(1).getPropertyPathFromSource().get(0), propertyPathFromSource2.get(0));
161         assertEquals(toscaGetFunctionDataDefinition.get(0).getPropertyPathFromSource().get(1), propertyPathFromSource1.get(1));
162         assertEquals(toscaGetFunctionDataDefinition.get(1).getPropertyPathFromSource().get(1), propertyPathFromSource2.get(1));
163     }
164 }