e67fdc0f337e3c43e825d24892b33f0a72128f5b
[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.List;
29 import java.util.Map;
30 import java.util.Optional;
31 import org.junit.jupiter.api.Test;
32 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
33 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
34 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
35
36 class FilterConstraintDtoTest {
37
38     @Test
39     void isCapabilityPropertyFilter() {
40         var filterConstraintDto = new FilterConstraintDto();
41         assertFalse(filterConstraintDto.isCapabilityPropertyFilter());
42         filterConstraintDto.setCapabilityName("aCapability");
43         assertTrue(filterConstraintDto.isCapabilityPropertyFilter());
44     }
45
46     @Test
47     void readGetFunctionWithToscaGetFunctionInstanceAsValue() {
48         final var filterConstraintDto = new FilterConstraintDto();
49         final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
50         toscaGetFunction.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
51         filterConstraintDto.setValue(toscaGetFunction);
52         final Optional<ToscaGetFunctionDataDefinition> readGetFunctionOpt = filterConstraintDto.getAsToscaGetFunction();
53         assertTrue(readGetFunctionOpt.isPresent());
54         assertEquals(toscaGetFunction, readGetFunctionOpt.get());
55     }
56
57     @Test
58     void readGetFunctionWithInvalidGetFunctionValue() {
59         final var filterConstraintDto = new FilterConstraintDto();
60         filterConstraintDto.setValue("not a ToscaGetFunctionDataDefinition");
61         final Optional<ToscaGetFunctionDataDefinition> readGetFunctionOpt = filterConstraintDto.getAsToscaGetFunction();
62         assertTrue(readGetFunctionOpt.isEmpty());
63     }
64
65     @Test
66     void readGetFunctionWithGetFunctionValueAsMap() {
67         //given
68         final List<String> propertyPathFromSource = List.of("input", "path");
69         final String propertyUniqueId = "propertyUniqueIdValue";
70         final String propertyName = "propertyNameValue";
71         final String sourceUniqueId = "sourceUniqueIdValue";
72         final String sourceName = "sourceNameValue";
73         final Map<String, Object> toscaGetFunctionAsMap = Map.of(
74             "propertyUniqueId", propertyUniqueId,
75             "propertyName", propertyName,
76             "propertySource", PropertySource.SELF.getName(),
77             "sourceUniqueId", sourceUniqueId,
78             "sourceName", sourceName,
79             "functionType", ToscaGetFunctionType.GET_INPUT.getFunctionName(),
80             "propertyPathFromSource", propertyPathFromSource
81         );
82
83         final var filterConstraintDto = new FilterConstraintDto();
84         filterConstraintDto.setValue(toscaGetFunctionAsMap);
85         //when
86         final Optional<ToscaGetFunctionDataDefinition> readGetFunctionOpt = filterConstraintDto.getAsToscaGetFunction();
87         //then
88         assertTrue(readGetFunctionOpt.isPresent());
89         final ToscaGetFunctionDataDefinition toscaGetFunctionDataDefinition = readGetFunctionOpt.get();
90         assertEquals(toscaGetFunctionDataDefinition.getPropertyUniqueId(), propertyUniqueId);
91         assertEquals(toscaGetFunctionDataDefinition.getPropertyName(), propertyName);
92         assertEquals(toscaGetFunctionDataDefinition.getPropertySource(), PropertySource.SELF);
93         assertEquals(toscaGetFunctionDataDefinition.getSourceUniqueId(), sourceUniqueId);
94         assertEquals(toscaGetFunctionDataDefinition.getSourceName(), sourceName);
95         assertEquals(toscaGetFunctionDataDefinition.getFunctionType(), ToscaGetFunctionType.GET_INPUT);
96         assertEquals(toscaGetFunctionDataDefinition.getPropertyPathFromSource().size(), 2);
97         assertEquals(toscaGetFunctionDataDefinition.getPropertyPathFromSource().get(0), propertyPathFromSource.get(0));
98         assertEquals(toscaGetFunctionDataDefinition.getPropertyPathFromSource().get(1), propertyPathFromSource.get(1));
99     }
100
101 }