Support TOSCA functions in Node Filters
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / PropertyConstraintsUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.property;
22
23 import com.google.common.collect.Lists;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
27 import org.openecomp.sdc.be.config.ConfigurationManager;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.model.PropertyConstraint;
30 import org.openecomp.sdc.be.model.PropertyDefinition;
31 import org.openecomp.sdc.be.model.Resource;
32 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
33 import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint;
34 import org.openecomp.sdc.be.model.tosca.constraints.PatternConstraint;
35 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
36
37 import java.util.List;
38 import java.util.Optional;
39 import org.openecomp.sdc.common.impl.ExternalConfiguration;
40 import org.openecomp.sdc.common.impl.FSConfigurationSource;
41
42 import static org.assertj.core.api.Java6Assertions.assertThat;
43
44 public class PropertyConstraintsUtilsTest {
45
46     @Before
47     public void init(){
48         new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
49     }
50
51     @Test
52     public void mergePropertiesConstraintsDeletionNotPermittedTest(){
53         Resource newResource = new Resource();
54         Resource oldResource = new Resource();
55
56         PropertyDefinition prop1 = new PropertyDefinition();
57         prop1.setName("prop1");
58         ValidValuesConstraint vvConst = new ValidValuesConstraint();
59         vvConst.setValidValues(Lists.newArrayList("3","1","2"));
60         InRangeConstraint inRangeConst = new InRangeConstraint();
61         PatternConstraint patternConstraint  = new PatternConstraint();
62         prop1.setConstraints(Lists.newArrayList(vvConst, inRangeConst, patternConstraint));
63
64
65         PropertyDefinition prop1_update = new PropertyDefinition();
66         prop1_update.setName("prop1");
67         vvConst = new ValidValuesConstraint();
68         vvConst.setValidValues(Lists.newArrayList("1","2"));
69         inRangeConst = new InRangeConstraint();
70         patternConstraint  = new PatternConstraint();
71         prop1_update.setConstraints(Lists.newArrayList(vvConst, inRangeConst, patternConstraint));
72
73         List<PropertyDefinition> oldProperties = Lists.newArrayList(prop1);
74         List<PropertyDefinition> newProperties = Lists.newArrayList(prop1_update);
75         oldResource.setProperties(oldProperties);
76         newResource.setProperties(newProperties);
77         try {
78             PropertyConstraintsUtils.validatePropertiesConstraints(newResource, oldResource);
79         } catch (ComponentException e){
80             assertThat(e.getActionStatus())
81                     .isNotNull()
82                     .isEqualTo(ActionStatus.CANNOT_DELETE_VALID_VALUES);
83
84             assertThat(e.getParams())
85                     .containsExactlyInAnyOrder(ConstraintType.VALID_VALUES.name(),Lists.newArrayList("3").toString());
86
87         }
88     }
89
90     @Test
91     public void mergePropertiesConstraintsAdditionPermittedTest(){
92         Resource newResource = new Resource();
93         Resource oldResource = new Resource();
94
95         PropertyDefinition prop2 = new PropertyDefinition();
96         prop2.setName("prop2");
97         ValidValuesConstraint vvConst = new ValidValuesConstraint();
98         vvConst.setValidValues(Lists.newArrayList("def","abc"));
99         InRangeConstraint inRangeConst = new InRangeConstraint();
100         PatternConstraint patternConstraint  = new PatternConstraint();
101         prop2.setConstraints(Lists.newArrayList(vvConst, inRangeConst, patternConstraint));
102
103         PropertyDefinition prop2_update = new PropertyDefinition();
104         prop2_update.setName("prop2");
105         vvConst = new ValidValuesConstraint();
106         vvConst.setValidValues(Lists.newArrayList("ghi","def","abc"));
107         inRangeConst = new InRangeConstraint();
108         patternConstraint  = new PatternConstraint();
109         prop2_update.setConstraints(Lists.newArrayList(vvConst, inRangeConst, patternConstraint));
110
111         List<PropertyDefinition> oldProperties = Lists.newArrayList(prop2);
112         List<PropertyDefinition> newProperties = Lists.newArrayList(prop2_update);
113         oldResource.setProperties(oldProperties);
114         newResource.setProperties(newProperties);
115         PropertyConstraintsUtils.validatePropertiesConstraints(newResource, oldResource);
116
117         Optional<PropertyDefinition> prop_merged = newResource.getProperties().stream().filter(p -> p.getName().equals(prop2.getName())).findFirst();
118         assertThat(prop_merged.isPresent()).isTrue();
119         assertThat(prop_merged.get().getConstraints()).isNotEmpty();
120         assertThat(prop_merged.get().getConstraints().size()).isEqualTo(3);
121         Optional<PropertyConstraint> vvConst_merged = prop_merged.get().getConstraints()
122                 .stream()
123                 .filter(c -> c.getConstraintType() == ConstraintType.VALID_VALUES)
124                 .findFirst();
125         assertThat(vvConst_merged.isPresent()).isTrue();
126         assertThat(((ValidValuesConstraint)vvConst_merged.get()).getValidValues()).containsExactlyInAnyOrder("ghi","def","abc");
127     }
128
129     @Test
130     public void mergePropertiesConstraintsUpdateNotPermittedTest(){
131         Resource newResource = new Resource();
132         Resource oldResource = new Resource();
133
134         PropertyDefinition prop3 = new PropertyDefinition();
135         prop3.setName("prop3");
136         ValidValuesConstraint vvConst = new ValidValuesConstraint();
137         vvConst.setValidValues(Lists.newArrayList("a2","a3","a1"));
138         InRangeConstraint inRangeConst = new InRangeConstraint();
139         PatternConstraint patternConstraint  = new PatternConstraint();
140         prop3.setConstraints(Lists.newArrayList(vvConst, inRangeConst, patternConstraint));
141
142         PropertyDefinition prop3_update = new PropertyDefinition();
143         prop3_update.setName("prop3");
144         vvConst = new ValidValuesConstraint();
145         vvConst.setValidValues(Lists.newArrayList("a4","a2","a3"));
146         inRangeConst = new InRangeConstraint();
147         patternConstraint  = new PatternConstraint();
148         prop3_update.setConstraints(Lists.newArrayList(vvConst, inRangeConst, patternConstraint));
149
150         List<PropertyDefinition> oldProperties = Lists.newArrayList(prop3);
151         List<PropertyDefinition> newProperties = Lists.newArrayList(prop3_update);
152         oldResource.setProperties(oldProperties);
153         newResource.setProperties(newProperties);
154         try {
155             PropertyConstraintsUtils.validatePropertiesConstraints(newResource, oldResource);
156         } catch (ComponentException e){
157             assertThat(e.getActionStatus())
158                     .isNotNull()
159                     .isEqualTo(ActionStatus.CANNOT_DELETE_VALID_VALUES);
160
161             assertThat(e.getParams())
162                     .containsExactlyInAnyOrder(ConstraintType.VALID_VALUES.name(),Lists.newArrayList("a1").toString());
163
164         }
165
166     }
167 }