Support TOSCA functions in Node Filters
[sdc.git] / common-be / src / test / java / org / openecomp / sdc / be / datatypes / elements / PropertyFilterConstraintDataDefinitionJsonDeserializerTest.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.datatypes.elements;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.util.List;
34 import org.junit.jupiter.api.Test;
35 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
36 import org.openecomp.sdc.be.datatypes.enums.FilterValueType;
37 import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType;
38 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
39 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
40
41 class PropertyFilterConstraintDataDefinitionJsonDeserializerTest {
42     private static final Path TEST_RESOURCES_PATH = Path.of("src/test/resources/propertyFilterConstraintDataDefinitionDeserializer");
43
44     @Test
45     void testStaticPropertyFilter() throws IOException {
46         //given
47         final String propertyFilterAsString = Files.readString(TEST_RESOURCES_PATH.resolve("filter-constraint-static.json"));
48         //when
49         final PropertyFilterConstraintDataDefinition actualPropertyFilterConstraint = parseToscaFunction(propertyFilterAsString);
50         //then
51         assertEquals(FilterValueType.STATIC, actualPropertyFilterConstraint.getValueType());
52         assertEquals(ConstraintType.EQUAL, actualPropertyFilterConstraint.getOperator());
53         assertEquals(PropertyFilterTargetType.CAPABILITY, actualPropertyFilterConstraint.getTargetType());
54         assertEquals("aCapability", actualPropertyFilterConstraint.getCapabilityName());
55         assertEquals("aProperty", actualPropertyFilterConstraint.getPropertyName());
56         assertEquals("aStaticValue", actualPropertyFilterConstraint.getValue());
57     }
58
59     @Test
60     void testGetInputToscaFunction() throws IOException {
61         //given
62         final String toscaGetInputFunction = Files.readString(TEST_RESOURCES_PATH.resolve("filter-constraint-get-input.json"));
63         //when
64         final PropertyFilterConstraintDataDefinition actualPropertyFilterConstraint = parseToscaFunction(toscaGetInputFunction);
65         //then
66         assertEquals(FilterValueType.GET_INPUT, actualPropertyFilterConstraint.getValueType());
67         assertEquals(ConstraintType.GREATER_THAN, actualPropertyFilterConstraint.getOperator());
68         assertEquals(PropertyFilterTargetType.PROPERTY, actualPropertyFilterConstraint.getTargetType());
69         assertNull(actualPropertyFilterConstraint.getCapabilityName());
70         assertEquals("aProperty", actualPropertyFilterConstraint.getPropertyName());
71         assertTrue(actualPropertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition);
72         final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) actualPropertyFilterConstraint.getValue();
73         assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunction.getType());
74         assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType());
75         assertEquals("aPropertyId", toscaGetFunction.getPropertyUniqueId());
76         assertEquals("aProperty", toscaGetFunction.getPropertyName());
77         assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource());
78         assertEquals("aServiceId", toscaGetFunction.getSourceUniqueId());
79         assertEquals("aService", toscaGetFunction.getSourceName());
80         assertEquals(List.of("input", "subProperty"), toscaGetFunction.getPropertyPathFromSource());
81     }
82
83     @Test
84     void testLegacyPropertyFilter() throws IOException {
85         //given
86         final String legacyPropertyFilter = Files.readString(TEST_RESOURCES_PATH.resolve("filter-constraint-legacy.txt"));
87         //when
88         final PropertyFilterConstraintDataDefinition actualPropertyFilterConstraint = parseToscaFunction(legacyPropertyFilter);
89         //then
90         assertEquals(FilterValueType.STATIC, actualPropertyFilterConstraint.getValueType());
91         assertEquals(ConstraintType.EQUAL, actualPropertyFilterConstraint.getOperator());
92         assertEquals(PropertyFilterTargetType.PROPERTY, actualPropertyFilterConstraint.getTargetType());
93         assertNull(actualPropertyFilterConstraint.getCapabilityName());
94         assertEquals("propertyName", actualPropertyFilterConstraint.getPropertyName());
95         assertEquals("aValue", actualPropertyFilterConstraint.getValue());
96     }
97
98     private PropertyFilterConstraintDataDefinition parseToscaFunction(final String propertyFilterConstraintAsJson) throws JsonProcessingException {
99         return new ObjectMapper().readValue(propertyFilterConstraintAsJson, PropertyFilterConstraintDataDefinition.class);
100     }
101 }