Catalog alignment
[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.Test;
25 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.model.PropertyConstraint;
28 import org.openecomp.sdc.be.model.PropertyDefinition;
29 import org.openecomp.sdc.be.model.Resource;
30 import org.openecomp.sdc.be.model.tosca.constraints.ConstraintType;
31 import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint;
32 import org.openecomp.sdc.be.model.tosca.constraints.PatternConstraint;
33 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
34
35 import java.util.List;
36 import java.util.Optional;
37
38 import static org.assertj.core.api.Java6Assertions.assertThat;
39
40 public class PropertyConstraintsUtilsTest {
41
42     @Test
43     public void mergePropertiesConstraintsDeletionNotPermittedTest(){
44         Resource newResource = new Resource();
45         Resource oldResource = new Resource();
46
47         PropertyDefinition prop1 = new PropertyDefinition();
48         prop1.setName("prop1");
49         ValidValuesConstraint vvConst = new ValidValuesConstraint();
50         vvConst.setValidValues(Lists.newArrayList("3","1","2"));
51         InRangeConstraint inRangeConst = new InRangeConstraint();
52         PatternConstraint patternConstraint  = new PatternConstraint();
53         prop1.setConstraints(Lists.newArrayList(vvConst, inRangeConst, patternConstraint));
54
55
56         PropertyDefinition prop1_update = new PropertyDefinition();
57         prop1_update.setName("prop1");
58         vvConst = new ValidValuesConstraint();
59         vvConst.setValidValues(Lists.newArrayList("1","2"));
60         inRangeConst = new InRangeConstraint();
61         patternConstraint  = new PatternConstraint();
62         prop1_update.setConstraints(Lists.newArrayList(vvConst, inRangeConst, patternConstraint));
63
64         List<PropertyDefinition> oldProperties = Lists.newArrayList(prop1);
65         List<PropertyDefinition> newProperties = Lists.newArrayList(prop1_update);
66         oldResource.setProperties(oldProperties);
67         newResource.setProperties(newProperties);
68         try {
69             PropertyConstraintsUtils.validatePropertiesConstraints(newResource, oldResource);
70         } catch (ComponentException e){
71             assertThat(e.getActionStatus())
72                     .isNotNull()
73                     .isEqualTo(ActionStatus.CANNOT_DELETE_VALID_VALUES);
74
75             assertThat(e.getParams())
76                     .containsExactlyInAnyOrder(ConstraintType.VALID_VALUES.name(),Lists.newArrayList("3").toString());
77
78         }
79     }
80
81     @Test
82     public void mergePropertiesConstraintsAdditionPermittedTest(){
83         Resource newResource = new Resource();
84         Resource oldResource = new Resource();
85
86         PropertyDefinition prop2 = new PropertyDefinition();
87         prop2.setName("prop2");
88         ValidValuesConstraint vvConst = new ValidValuesConstraint();
89         vvConst.setValidValues(Lists.newArrayList("def","abc"));
90         InRangeConstraint inRangeConst = new InRangeConstraint();
91         PatternConstraint patternConstraint  = new PatternConstraint();
92         prop2.setConstraints(Lists.newArrayList(vvConst, inRangeConst, patternConstraint));
93
94         PropertyDefinition prop2_update = new PropertyDefinition();
95         prop2_update.setName("prop2");
96         vvConst = new ValidValuesConstraint();
97         vvConst.setValidValues(Lists.newArrayList("ghi","def","abc"));
98         inRangeConst = new InRangeConstraint();
99         patternConstraint  = new PatternConstraint();
100         prop2_update.setConstraints(Lists.newArrayList(vvConst, inRangeConst, patternConstraint));
101
102         List<PropertyDefinition> oldProperties = Lists.newArrayList(prop2);
103         List<PropertyDefinition> newProperties = Lists.newArrayList(prop2_update);
104         oldResource.setProperties(oldProperties);
105         newResource.setProperties(newProperties);
106         PropertyConstraintsUtils.validatePropertiesConstraints(newResource, oldResource);
107
108         Optional<PropertyDefinition> prop_merged = newResource.getProperties().stream().filter(p -> p.getName().equals(prop2.getName())).findFirst();
109         assertThat(prop_merged.isPresent()).isTrue();
110         assertThat(prop_merged.get().getConstraints()).isNotEmpty();
111         assertThat(prop_merged.get().getConstraints().size()).isEqualTo(3);
112         Optional<PropertyConstraint> vvConst_merged = prop_merged.get().getConstraints()
113                 .stream()
114                 .filter(c -> c.getConstraintType() == ConstraintType.VALID_VALUES)
115                 .findFirst();
116         assertThat(vvConst_merged.isPresent()).isTrue();
117         assertThat(((ValidValuesConstraint)vvConst_merged.get()).getValidValues()).containsExactlyInAnyOrder("ghi","def","abc");
118     }
119
120     @Test
121     public void mergePropertiesConstraintsUpdateNotPermittedTest(){
122         Resource newResource = new Resource();
123         Resource oldResource = new Resource();
124
125         PropertyDefinition prop3 = new PropertyDefinition();
126         prop3.setName("prop3");
127         ValidValuesConstraint vvConst = new ValidValuesConstraint();
128         vvConst.setValidValues(Lists.newArrayList("a2","a3","a1"));
129         InRangeConstraint inRangeConst = new InRangeConstraint();
130         PatternConstraint patternConstraint  = new PatternConstraint();
131         prop3.setConstraints(Lists.newArrayList(vvConst, inRangeConst, patternConstraint));
132
133         PropertyDefinition prop3_update = new PropertyDefinition();
134         prop3_update.setName("prop3");
135         vvConst = new ValidValuesConstraint();
136         vvConst.setValidValues(Lists.newArrayList("a4","a2","a3"));
137         inRangeConst = new InRangeConstraint();
138         patternConstraint  = new PatternConstraint();
139         prop3_update.setConstraints(Lists.newArrayList(vvConst, inRangeConst, patternConstraint));
140
141         List<PropertyDefinition> oldProperties = Lists.newArrayList(prop3);
142         List<PropertyDefinition> newProperties = Lists.newArrayList(prop3_update);
143         oldResource.setProperties(oldProperties);
144         newResource.setProperties(newProperties);
145         try {
146             PropertyConstraintsUtils.validatePropertiesConstraints(newResource, oldResource);
147         } catch (ComponentException e){
148             assertThat(e.getActionStatus())
149                     .isNotNull()
150                     .isEqualTo(ActionStatus.CANNOT_DELETE_VALID_VALUES);
151
152             assertThat(e.getParams())
153                     .containsExactlyInAnyOrder(ConstraintType.VALID_VALUES.name(),Lists.newArrayList("a1").toString());
154
155         }
156
157     }
158 }