3574c547401d7e9f95c474095f4ddcfb9ab11a27
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / ui / mapper / FilterConstraintMapperTest.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.ui.mapper;
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 java.util.Map;
29 import java.util.Optional;
30 import org.junit.jupiter.api.Test;
31 import org.openecomp.sdc.be.datatypes.elements.PropertyFilterConstraintDataDefinition;
32 import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction;
33 import org.openecomp.sdc.be.datatypes.elements.ToscaFunction;
34 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
35 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
36 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
37 import org.openecomp.sdc.be.datatypes.enums.FilterValueType;
38 import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType;
39 import org.openecomp.sdc.be.model.dto.FilterConstraintDto;
40 import org.openecomp.sdc.be.ui.model.UIConstraint;
41
42 class FilterConstraintMapperTest {
43
44     private final FilterConstraintMapper filterConstraintMapper = new FilterConstraintMapper();
45
46     @Test
47     void mapFromUIConstraintTest() {
48         //given
49         final var uIConstraint = new UIConstraint();
50         final FilterValueType filterValueType = FilterValueType.STATIC;
51         uIConstraint.setSourceType(filterValueType.getName());
52         final String capabilityName = "aCapability";
53         uIConstraint.setCapabilityName(capabilityName);
54         final String propertyName = "aProperty";
55         uIConstraint.setServicePropertyName(propertyName);
56         final ConstraintType operator = ConstraintType.GREATER_OR_EQUAL;
57         uIConstraint.setConstraintOperator(operator.getType());
58         final ToscaFunctionType expectedValueToscaFunctionType = ToscaFunctionType.GET_INPUT;
59         uIConstraint.setValue(Map.of("type", expectedValueToscaFunctionType.getName()));
60         //when
61         final FilterConstraintDto filterConstraintDto = filterConstraintMapper.mapFrom(uIConstraint);
62         //then
63         assertEquals(PropertyFilterTargetType.CAPABILITY, filterConstraintDto.getTargetType());
64         assertEquals(propertyName, filterConstraintDto.getPropertyName());
65         assertEquals(capabilityName, filterConstraintDto.getCapabilityName());
66         assertEquals(filterValueType, filterConstraintDto.getValueType());
67         assertTrue(filterConstraintDto.getValue() instanceof ToscaGetFunctionDataDefinition);
68         assertEquals(expectedValueToscaFunctionType, ((ToscaGetFunctionDataDefinition) filterConstraintDto.getValue()).getType());
69         assertEquals(operator, filterConstraintDto.getOperator());
70         //when
71         final UIConstraint actualUiConstraint = filterConstraintMapper.mapToUiConstraint(filterConstraintDto);
72         //then
73         assertEquals(propertyName, actualUiConstraint.getServicePropertyName());
74         assertEquals(capabilityName, actualUiConstraint.getCapabilityName());
75         assertEquals(filterValueType.getName(), actualUiConstraint.getSourceType());
76         assertEquals(filterValueType.getName(), actualUiConstraint.getSourceName());
77         assertTrue(actualUiConstraint.getValue() instanceof ToscaGetFunctionDataDefinition);
78         assertEquals(expectedValueToscaFunctionType, ((ToscaGetFunctionDataDefinition) actualUiConstraint.getValue()).getType());
79         assertEquals(operator.getType(), actualUiConstraint.getConstraintOperator());
80     }
81
82     @Test
83     void mapFromPropertyFilterConstraintDataDefinitionTest() {
84         //given
85         final var propertyFilterConstraintDataDefinition = new PropertyFilterConstraintDataDefinition();
86         propertyFilterConstraintDataDefinition.setTargetType(PropertyFilterTargetType.CAPABILITY);
87         final String capabilityName = "aCapability";
88         propertyFilterConstraintDataDefinition.setCapabilityName(capabilityName);
89         final String propertyName = "aProperty";
90         propertyFilterConstraintDataDefinition.setPropertyName(propertyName);
91         final ConstraintType operator = ConstraintType.GREATER_OR_EQUAL;
92         propertyFilterConstraintDataDefinition.setOperator(operator);
93         final FilterValueType filterValueType = FilterValueType.STATIC;
94         propertyFilterConstraintDataDefinition.setValueType(filterValueType);
95         final String value = "aStaticValue";
96         propertyFilterConstraintDataDefinition.setValue(value);
97         //when
98         final FilterConstraintDto filterConstraintDto = filterConstraintMapper.mapFrom(propertyFilterConstraintDataDefinition);
99         //then
100         assertEquals(PropertyFilterTargetType.CAPABILITY, filterConstraintDto.getTargetType());
101         assertEquals(propertyName, filterConstraintDto.getPropertyName());
102         assertEquals(capabilityName, filterConstraintDto.getCapabilityName());
103         assertEquals(filterValueType, filterConstraintDto.getValueType());
104         assertEquals(value, filterConstraintDto.getValue());
105         assertEquals(operator, filterConstraintDto.getOperator());
106         //when
107         final PropertyFilterConstraintDataDefinition actualPropertyFilterConstraint =
108             filterConstraintMapper.mapTo(filterConstraintDto);
109         assertEquals(PropertyFilterTargetType.CAPABILITY, actualPropertyFilterConstraint.getTargetType());
110         assertEquals(propertyName, actualPropertyFilterConstraint.getPropertyName());
111         assertEquals(capabilityName, actualPropertyFilterConstraint.getCapabilityName());
112         assertEquals(filterValueType, actualPropertyFilterConstraint.getValueType());
113         assertEquals(value, actualPropertyFilterConstraint.getValue());
114         assertEquals(operator, actualPropertyFilterConstraint.getOperator());
115     }
116
117     @Test
118     void parseValueToToscaFunctionTest() {
119         //given
120         final ToscaConcatFunction expectedValue = new ToscaConcatFunction();
121         //when
122         Optional<ToscaFunction> actualToscaFunction = filterConstraintMapper.parseValueToToscaFunction(expectedValue);
123         //then
124         assertTrue(actualToscaFunction.isPresent());
125         assertEquals(expectedValue, actualToscaFunction.get());
126         //when
127         actualToscaFunction = filterConstraintMapper.parseValueToToscaFunction("not a tosca function");
128         //then
129         assertTrue(actualToscaFunction.isEmpty());
130         //given
131         final Map<String, Object> value = Map.of("type", ToscaFunctionType.CONCAT.getName());
132         //when
133         actualToscaFunction = filterConstraintMapper.parseValueToToscaFunction(value);
134         //then
135         assertTrue(actualToscaFunction.isPresent());
136         assertTrue(actualToscaFunction.get() instanceof ToscaConcatFunction);
137         //when
138         actualToscaFunction = filterConstraintMapper.parseValueToToscaFunction(Map.of("type", 1));
139         //then
140         assertTrue(actualToscaFunction.isEmpty());
141     }
142 }