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