2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.validation;
23 import fj.data.Either;
24 import org.junit.Assert;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mockito;
30 import org.mockito.MockitoAnnotations;
31 import org.openecomp.sdc.be.components.impl.utils.NodeFilterConstraintAction;
32 import org.openecomp.sdc.be.config.ConfigurationManager;
33 import org.openecomp.sdc.be.dao.api.ActionStatus;
34 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
35 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
36 import org.openecomp.sdc.be.datatypes.enums.NodeFilterConstraintType;
37 import org.openecomp.sdc.be.impl.ComponentsUtils;
38 import org.openecomp.sdc.be.model.ComponentInstance;
39 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
40 import org.openecomp.sdc.be.model.PropertyDefinition;
41 import org.openecomp.sdc.be.model.Service;
42 import org.openecomp.sdc.common.impl.ExternalConfiguration;
43 import org.openecomp.sdc.common.impl.FSConfigurationSource;
44 import org.openecomp.sdc.exception.ResponseFormat;
48 import static org.junit.jupiter.api.Assertions.assertEquals;
49 import static org.junit.jupiter.api.Assertions.assertTrue;
50 import static org.junit.jupiter.api.Assertions.assertFalse;
52 public class NodeFilterValidationTest {
54 private static final String UI_CONSTRAINT_STATIC = "Prop1: {equal: 'value'}";
55 private static final String INNER_SERVICE = "innerService";
56 private static final String PROPERTY_NAME = "Prop1";
57 private static final String VALUE = "value";
58 private static final String FLOAT_TYPE = "float";
59 private static final String STRING_TYPE = "string";
60 private static final String LIST_TYPE = "list";
61 private static final String COMPONENT1_ID = "component1";
62 private static final String INTEGER_TYPE = "integer";
63 private static final String PARENTSERVICE_ID = "parentservice";
64 private static final String COMPONENT2_ID = "component2";
65 private ComponentsUtils componentsUtils;
68 private NodeFilterValidator nodeFilterValidator;
72 componentsUtils = Mockito.mock(ComponentsUtils.class);
73 MockitoAnnotations.initMocks(this);
74 new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
78 public void testValidateComponentInstanceExist() {
79 Either<Boolean, ResponseFormat> either =
80 nodeFilterValidator.validateComponentInstanceExist(null, INNER_SERVICE);
81 assertTrue(either.isRight());
82 assertEquals("Error: Internal Server Error. Please try again later.", either.right().value().getText());
83 assertEquals(500, either.right().value().getStatus());
85 Service service = createService("booleanIncorrect");
86 either = nodeFilterValidator.validateComponentInstanceExist(service, INNER_SERVICE);
87 assertTrue(either.isRight());
88 assertEquals("Error: Internal Server Error. Please try again later.", either.right().value().getText());
89 assertEquals(500, either.right().value().getStatus());
91 List<ComponentInstance> list = new LinkedList<>();
92 ComponentInstance instance = new ComponentInstance();
93 instance.setUniqueId("uniqueId");
95 service.setComponentInstances(list);
96 either = nodeFilterValidator.validateComponentInstanceExist(service, "uniqueId");
97 assertTrue(either.isLeft());
101 public void testValidateNodeFilterStaticIncorrectPropertyTypeProvided() {
102 Service service = createService("booleanIncorrect");
103 Either<Boolean, ResponseFormat> either =
104 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
105 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")),
106 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
107 assertTrue(either.isRight());
110 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
111 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")),
112 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.CAPABILITIES);
113 assertTrue(either.isRight());
117 public void testValidateComponentFilter() {
118 Service service = createService("booleanIncorrect");
119 String property = "Prop1: {equal: {get_property: ['test','test2']}}";
120 Either<Boolean, ResponseFormat> either =
121 nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(property),
122 NodeFilterConstraintAction.ADD);
123 assertTrue(either.isRight());
125 property = "Prop1: {equal: {get_property: ['parentservice','Prop1']}}";
127 nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(property),
128 NodeFilterConstraintAction.ADD);
129 assertTrue(either.isLeft());
131 String staticStr = "Prop1: {equal: 1}";
132 either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
133 NodeFilterConstraintAction.ADD);
134 assertTrue(either.isLeft());
135 assertTrue(either.left().value());
137 staticStr = "Prop1: {equal: 'true'}";
138 either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
139 NodeFilterConstraintAction.ADD);
140 assertTrue(either.isRight());
142 staticStr = "Prop1: {greater_than: '3'}";
143 either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
144 NodeFilterConstraintAction.ADD);
145 assertTrue(either.isRight());
147 staticStr = "test: {greater_than: '3'}";
148 either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
149 NodeFilterConstraintAction.ADD);
150 assertTrue(either.isRight());
154 public void testValidateNodeFilterStaticIncorrectOperatorProvidedBoolean() {
155 Service service = createService("boolean");
156 Either<Boolean, ResponseFormat> either =
157 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
158 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")
159 .replace("equal", "greater_than")),
160 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
162 Assert.assertFalse(either.isLeft());
166 public void testValidateNodeFilterStaticIncorrectValueProvidedBoolean() {
167 Service service = createService("boolean");
168 Either<Boolean, ResponseFormat> either =
169 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
170 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "trues")),
171 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
173 Assert.assertFalse(either.isLeft());
177 public void testValidateNodeFilterStaticIncorrectOperatorProvidedString() {
178 Service service = createService(STRING_TYPE);
179 Either<Boolean, ResponseFormat> either =
180 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
181 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")
182 .replace("equal", "greater_than")),
183 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
185 Assert.assertTrue(either.isLeft());
189 public void testValidateNodeFilterIntegerValueSuccess() {
190 Service service = createService(INTEGER_TYPE);
191 Either<Boolean, ResponseFormat> either =
192 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
193 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1")),
194 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
196 Assert.assertTrue(either.isLeft());
200 public void testValidateNodeFilterIntegerValueFail() {
201 Service service = createService(INTEGER_TYPE);
203 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
204 .thenReturn(new ResponseFormat());
206 Either<Boolean, ResponseFormat> either =
207 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
208 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1.0")),
209 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
211 Assert.assertTrue(either.isRight());
215 public void testValidateNodeFilterFloatValueSuccess() {
216 Service service = createService(FLOAT_TYPE);
217 Either<Boolean, ResponseFormat> either =
218 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
219 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1.0")),
220 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
222 Assert.assertTrue(either.isLeft());
226 public void testValidateNodeFilterFloatValueFail() {
227 Service service = createService(FLOAT_TYPE);
229 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
230 .thenReturn(new ResponseFormat());
232 Either<Boolean, ResponseFormat> either =
233 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
234 Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD,
235 NodeFilterConstraintType.PROPERTIES);
237 Assert.assertTrue(either.isRight());
241 public void testValidateNodeFilterStringValueSuccess() {
242 Service service = createService(STRING_TYPE);
243 Either<Boolean, ResponseFormat> either =
244 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
245 Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD,
246 NodeFilterConstraintType.PROPERTIES);
248 Assert.assertTrue(either.isLeft());
252 public void testValidatePropertyConstraintBrotherSuccess() {
253 Service service = createService(STRING_TYPE);
254 Either<Boolean, ResponseFormat> either =
255 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
256 + " equal: { get_property :[component2, Prop1]}\n"), NodeFilterConstraintAction.ADD,
257 NodeFilterConstraintType.PROPERTIES);
259 Assert.assertTrue(either.isLeft());
263 public void testValidatePropertyConstraintParentSuccess() {
264 Service service = createService(STRING_TYPE);
265 Either<Boolean, ResponseFormat> either =
266 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
267 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
268 NodeFilterConstraintType.PROPERTIES);
270 Assert.assertTrue(either.isLeft());
274 public void testValidatePropertyConstraintBrotherPropertyTypeMismatch() {
275 Service service = createService(STRING_TYPE);
276 service.getComponentInstancesProperties().get(COMPONENT2_ID).get(0).setType(INTEGER_TYPE);
278 Either<Boolean, ResponseFormat> either =
279 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
280 + " equal: { get_property : [component2, Prop1]}\n"), NodeFilterConstraintAction.ADD,
281 NodeFilterConstraintType.PROPERTIES);
283 Assert.assertFalse(either.isLeft());
287 public void testValidatePropertyConstraintParentPropertyTypeMismatch() {
288 Service service = createService(STRING_TYPE);
289 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(INTEGER_TYPE);
291 Either<Boolean, ResponseFormat> either =
292 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
293 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
294 NodeFilterConstraintType.PROPERTIES);
296 Assert.assertFalse(either.isLeft());
300 public void testValidatePropertyConstraintParentPropertyNotFound() {
301 Service service = createService(STRING_TYPE);
302 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
304 Either<Boolean, ResponseFormat> either =
305 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
306 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
307 NodeFilterConstraintType.PROPERTIES);
309 Assert.assertFalse(either.isLeft());
313 public void testvalidatePropertyConstraintBrotherPropertyNotFound() {
314 Service service = createService(STRING_TYPE);
315 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
317 Either<Boolean, ResponseFormat> either =
318 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
319 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
320 NodeFilterConstraintType.PROPERTIES);
322 Assert.assertFalse(either.isLeft());
326 public void testValidatePropertyConstraintParentPropertySchemaMismatch() {
327 Service service = createService(LIST_TYPE,STRING_TYPE);
328 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(LIST_TYPE);
330 Either<Boolean, ResponseFormat> either =
331 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
332 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
333 NodeFilterConstraintType.PROPERTIES);
335 Assert.assertFalse(either.isLeft());
338 private Service createService(String type) {
339 return createService(type, null);
342 private Service createService(String type, String schemaType) {
343 Service service = new Service();
344 service.setName(PARENTSERVICE_ID);
346 PropertyDefinition propertyDefinition = new PropertyDefinition();
347 propertyDefinition.setName(PROPERTY_NAME);
348 propertyDefinition.setType(type);
349 if (schemaType != null){
350 SchemaDefinition schemaDefinition = new SchemaDefinition();
351 PropertyDataDefinition schemaProperty = new PropertyDataDefinition();
352 schemaProperty.setType(schemaType);
353 schemaDefinition.setProperty(schemaProperty);
354 propertyDefinition.setSchema(schemaDefinition);
356 service.setProperties(Collections.singletonList(propertyDefinition));
358 ComponentInstance componentInstance = new ComponentInstance();
359 componentInstance.setUniqueId(COMPONENT1_ID);
360 componentInstance.setName(COMPONENT1_ID);
362 ComponentInstance componentInstance2 = new ComponentInstance();
363 componentInstance2.setUniqueId(COMPONENT2_ID);
364 componentInstance2.setName(COMPONENT2_ID);
366 service.setComponentInstances(Arrays.asList(componentInstance, componentInstance2));
368 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
369 componentInstanceProperty.setName(PROPERTY_NAME);
370 componentInstanceProperty.setType(type);
372 ComponentInstanceProperty componentInstanceProperty2 = new ComponentInstanceProperty();
373 componentInstanceProperty2.setName(PROPERTY_NAME);
374 componentInstanceProperty2.setType(type);
376 Map<String, List<ComponentInstanceProperty>> componentInstancePropertyMap = new HashMap<>();
377 componentInstancePropertyMap.put(componentInstance.getUniqueId(),
378 Collections.singletonList(componentInstanceProperty));
379 componentInstancePropertyMap.put(componentInstance2.getUniqueId(),
380 Collections.singletonList(componentInstanceProperty2));
381 componentInstancePropertyMap.put(INNER_SERVICE, Collections.singletonList(componentInstanceProperty));
383 service.setComponentInstancesProperties(componentInstancePropertyMap);