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