Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / validation / NodeFilterValidationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.validation;
22
23 import fj.data.Either;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.openecomp.sdc.be.components.impl.utils.NodeFilterConstraintAction;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
33 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
34 import org.openecomp.sdc.be.impl.ComponentsUtils;
35 import org.openecomp.sdc.be.model.ComponentInstance;
36 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
37 import org.openecomp.sdc.be.model.PropertyDefinition;
38 import org.openecomp.sdc.be.model.Service;
39 import org.openecomp.sdc.exception.ResponseFormat;
40
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46
47 public class NodeFilterValidationTest {
48
49     private static final String UI_CONSTRAINT_STATIC = "Prop1: {equal: 'value'}";
50     private static final String INNER_SERVICE = "innerService";
51     private static final String PROPERTY_NAME = "Prop1";
52     private static final String VALUE = "value";
53     private static final String FLOAT_TYPE = "float";
54     private static final String STRING_TYPE = "string";
55     private static final String LIST_TYPE = "list";
56     private static final String COMPONENT1_ID = "component1";
57     private static final String INTEGER_TYPE = "integer";
58     private static final String PARENTSERVICE_ID = "parentservice";
59     private static final String COMPONENT2_ID = "component2";
60     private ComponentsUtils componentsUtils;
61
62     @InjectMocks
63     private NodeFilterValidator nodeFilterValidator;
64
65     @Before
66     public void setup() {
67         componentsUtils = Mockito.mock(ComponentsUtils.class);
68         MockitoAnnotations.initMocks(this);
69     }
70
71     @Test
72     public void testValidateNodeFilterStaticIncorrectPropertyTypeProvided() {
73         Service service = createService("booleanIncorrect");
74         Either<Boolean, ResponseFormat> either =
75                 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
76                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")),
77                         NodeFilterConstraintAction.ADD);
78
79         Assert.assertFalse(either.isLeft());
80     }
81
82     @Test
83     public void testValidateNodeFilterStaticIncorrectOperatorProvidedBoolean() {
84         Service service = createService("boolean");
85         Either<Boolean, ResponseFormat> either =
86                 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
87                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")
88                                 .replace("equal", "greater_than")),
89                         NodeFilterConstraintAction.ADD);
90
91         Assert.assertFalse(either.isLeft());
92     }
93
94     @Test
95     public void testValidateNodeFilterStaticIncorrectValueProvidedBoolean() {
96         Service service = createService("boolean");
97         Either<Boolean, ResponseFormat> either =
98                 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
99                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "trues")),
100                         NodeFilterConstraintAction.ADD);
101
102         Assert.assertFalse(either.isLeft());
103     }
104
105     @Test
106     public void testValidateNodeFilterStaticIncorrectOperatorProvidedString() {
107         Service service = createService(STRING_TYPE);
108         Either<Boolean, ResponseFormat> either =
109                 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
110                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")
111                                 .replace("equal", "greater_than")),
112                         NodeFilterConstraintAction.ADD);
113
114         Assert.assertTrue(either.isLeft());
115     }
116
117     @Test
118     public void testValidateNodeFilterIntegerValueSuccess() {
119         Service service = createService(INTEGER_TYPE);
120         Either<Boolean, ResponseFormat> either =
121                 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
122                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1")),
123                                 NodeFilterConstraintAction.ADD);
124
125         Assert.assertTrue(either.isLeft());
126     }
127
128     @Test
129     public void testValidateNodeFilterIntegerValueFail() {
130         Service service = createService(INTEGER_TYPE);
131
132         Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
133                 .thenReturn(new ResponseFormat());
134
135         Either<Boolean, ResponseFormat> either =
136                 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
137                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1.0")),
138                         NodeFilterConstraintAction.ADD);
139
140         Assert.assertTrue(either.isRight());
141     }
142
143     @Test
144     public void testValidateNodeFilterFloatValueSuccess() {
145         Service service = createService(FLOAT_TYPE);
146         Either<Boolean, ResponseFormat> either =
147                 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
148                         Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1.0")),
149                         NodeFilterConstraintAction.ADD);
150
151         Assert.assertTrue(either.isLeft());
152     }
153
154     @Test
155     public void testValidateNodeFilterFloatValueFail() {
156         Service service = createService(FLOAT_TYPE);
157
158         Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
159                 .thenReturn(new ResponseFormat());
160
161         Either<Boolean, ResponseFormat> either =
162                 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
163                         Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD);
164
165         Assert.assertTrue(either.isRight());
166     }
167
168     @Test
169     public void testValidateNodeFilterStringValueSuccess() {
170         Service service = createService(STRING_TYPE);
171         Either<Boolean, ResponseFormat> either =
172                 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
173                         Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD);
174
175         Assert.assertTrue(either.isLeft());
176     }
177
178     @Test
179     public void testValidatePropertyConstraintBrotherSuccess() {
180         Service service = createService(STRING_TYPE);
181         Either<Boolean, ResponseFormat> either =
182                 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
183                         + "  equal:  { get_property :[component2, Prop1]}\n"), NodeFilterConstraintAction.ADD);
184
185         Assert.assertTrue(either.isLeft());
186     }
187
188     @Test
189     public void testValidatePropertyConstraintParentSuccess() {
190         Service service = createService(STRING_TYPE);
191         Either<Boolean, ResponseFormat> either =
192                 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
193                         + "  equal:  { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
194
195         Assert.assertTrue(either.isLeft());
196     }
197
198     @Test
199     public void testValidatePropertyConstraintBrotherPropertyTypeMismatch() {
200         Service service = createService(STRING_TYPE);
201         service.getComponentInstancesProperties().get(COMPONENT2_ID).get(0).setType(INTEGER_TYPE);
202
203         Either<Boolean, ResponseFormat> either =
204                 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
205                         + "  equal: { get_property : [component2, Prop1]}\n"), NodeFilterConstraintAction.ADD);
206
207         Assert.assertFalse(either.isLeft());
208     }
209
210     @Test
211     public void testValidatePropertyConstraintParentPropertyTypeMismatch() {
212         Service service = createService(STRING_TYPE);
213         service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(INTEGER_TYPE);
214
215         Either<Boolean, ResponseFormat> either =
216                 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
217                         + "  equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
218
219         Assert.assertFalse(either.isLeft());
220     }
221
222     @Test
223     public void testValidatePropertyConstraintParentPropertyNotFound() {
224         Service service = createService(STRING_TYPE);
225         service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
226
227         Either<Boolean, ResponseFormat> either =
228                 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
229                         + "  equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
230
231         Assert.assertFalse(either.isLeft());
232     }
233
234     @Test
235     public void testvalidatePropertyConstraintBrotherPropertyNotFound() {
236         Service service = createService(STRING_TYPE);
237         service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
238
239         Either<Boolean, ResponseFormat> either =
240                 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
241                         + "  equal:  { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
242
243         Assert.assertFalse(either.isLeft());
244     }
245
246     @Test
247     public void testValidatePropertyConstraintParentPropertySchemaMismatch() {
248         Service service = createService(LIST_TYPE,STRING_TYPE);
249         service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(LIST_TYPE);
250
251         Either<Boolean, ResponseFormat> either =
252                 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
253                                                                                                                  + "  equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
254
255         Assert.assertFalse(either.isLeft());
256     }
257
258     private Service createService(String type) {
259         return createService(type, null);
260     }
261
262     private Service createService(String type, String schemaType) {
263         Service service = new Service();
264         service.setName(PARENTSERVICE_ID);
265
266         PropertyDefinition propertyDefinition = new PropertyDefinition();
267         propertyDefinition.setName(PROPERTY_NAME);
268         propertyDefinition.setType(type);
269         if (schemaType != null){
270             SchemaDefinition schemaDefinition = new SchemaDefinition();
271             PropertyDataDefinition schemaProperty = new PropertyDataDefinition();
272             schemaProperty.setType(schemaType);
273             schemaDefinition.setProperty(schemaProperty);
274             propertyDefinition.setSchema(schemaDefinition);
275         }
276         service.setProperties(Collections.singletonList(propertyDefinition));
277
278         ComponentInstance componentInstance = new ComponentInstance();
279         componentInstance.setUniqueId(COMPONENT1_ID);
280         componentInstance.setName(COMPONENT1_ID);
281
282         ComponentInstance componentInstance2 = new ComponentInstance();
283         componentInstance2.setUniqueId(COMPONENT2_ID);
284         componentInstance2.setName(COMPONENT2_ID);
285
286         service.setComponentInstances(Arrays.asList(componentInstance, componentInstance2));
287
288         ComponentInstanceProperty componentInstanceProperty  = new ComponentInstanceProperty();
289         componentInstanceProperty.setName(PROPERTY_NAME);
290         componentInstanceProperty.setType(type);
291
292         ComponentInstanceProperty componentInstanceProperty2  = new ComponentInstanceProperty();
293         componentInstanceProperty2.setName(PROPERTY_NAME);
294         componentInstanceProperty2.setType(type);
295
296         Map<String, List<ComponentInstanceProperty>> componentInstancePropertyMap = new HashMap<>();
297         componentInstancePropertyMap.put(componentInstance.getUniqueId(),
298                 Collections.singletonList(componentInstanceProperty));
299         componentInstancePropertyMap.put(componentInstance2.getUniqueId(),
300                 Collections.singletonList(componentInstanceProperty2));
301         componentInstancePropertyMap.put(INNER_SERVICE, Collections.singletonList(componentInstanceProperty));
302
303         service.setComponentInstancesProperties(componentInstancePropertyMap);
304
305         return service;
306     }
307
308 }