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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.be.datatypes.elements;
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;
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;
41 class PropertyFilterConstraintDataDefinitionJsonDeserializerTest {
42 private static final Path TEST_RESOURCES_PATH = Path.of("src/test/resources/propertyFilterConstraintDataDefinitionDeserializer");
45 void testStaticPropertyFilter() throws IOException {
47 final String propertyFilterAsString = Files.readString(TEST_RESOURCES_PATH.resolve("filter-constraint-static.json"));
49 final PropertyFilterConstraintDataDefinition actualPropertyFilterConstraint = parseToscaFunction(propertyFilterAsString);
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());
60 void testGetInputToscaFunction() throws IOException {
62 final String toscaGetInputFunction = Files.readString(TEST_RESOURCES_PATH.resolve("filter-constraint-get-input.json"));
64 final PropertyFilterConstraintDataDefinition actualPropertyFilterConstraint = parseToscaFunction(toscaGetInputFunction);
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());
84 void testLegacyPropertyFilter() throws IOException {
86 final String legacyPropertyFilter = Files.readString(TEST_RESOURCES_PATH.resolve("filter-constraint-legacy.txt"));
88 final PropertyFilterConstraintDataDefinition actualPropertyFilterConstraint = parseToscaFunction(legacyPropertyFilter);
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());
98 private PropertyFilterConstraintDataDefinition parseToscaFunction(final String propertyFilterConstraintAsJson) throws JsonProcessingException {
99 return new ObjectMapper().readValue(propertyFilterConstraintAsJson, PropertyFilterConstraintDataDefinition.class);