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 static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import fj.data.Either;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.LinkedList;
31 import java.util.List;
33 import org.junit.Assert;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mockito;
38 import org.mockito.MockitoAnnotations;
39 import org.openecomp.sdc.be.components.impl.utils.NodeFilterConstraintAction;
40 import org.openecomp.sdc.be.config.ConfigurationManager;
41 import org.openecomp.sdc.be.dao.api.ActionStatus;
42 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
43 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
44 import org.openecomp.sdc.be.datatypes.enums.NodeFilterConstraintType;
45 import org.openecomp.sdc.be.impl.ComponentsUtils;
46 import org.openecomp.sdc.be.model.ComponentInstance;
47 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
48 import org.openecomp.sdc.be.model.PropertyDefinition;
49 import org.openecomp.sdc.be.model.Service;
50 import org.openecomp.sdc.common.impl.ExternalConfiguration;
51 import org.openecomp.sdc.common.impl.FSConfigurationSource;
52 import org.openecomp.sdc.exception.ResponseFormat;
54 public class NodeFilterValidationTest {
56 private static final String UI_CONSTRAINT_STATIC = "Prop1: {equal: 'value'}";
57 private static final String INNER_SERVICE = "innerService";
58 private static final String PROPERTY_NAME = "Prop1";
59 private static final String VALUE = "value";
60 private static final String FLOAT_TYPE = "float";
61 private static final String STRING_TYPE = "string";
62 private static final String LIST_TYPE = "list";
63 private static final String COMPONENT1_ID = "component1";
64 private static final String INTEGER_TYPE = "integer";
65 private static final String PARENTSERVICE_ID = "parentservice";
66 private static final String COMPONENT2_ID = "component2";
67 private ComponentsUtils componentsUtils;
70 private NodeFilterValidator nodeFilterValidator;
74 componentsUtils = Mockito.mock(ComponentsUtils.class);
75 MockitoAnnotations.openMocks(this);
76 new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
80 public void testValidateComponentInstanceExist() {
81 Either<Boolean, ResponseFormat> either =
82 nodeFilterValidator.validateComponentInstanceExist(null, INNER_SERVICE);
83 assertTrue(either.isRight());
84 assertEquals("Error: Internal Server Error. Please try again later.", either.right().value().getText());
85 assertEquals(500, either.right().value().getStatus());
87 Service service = createService("booleanIncorrect");
88 either = nodeFilterValidator.validateComponentInstanceExist(service, INNER_SERVICE);
89 assertTrue(either.isRight());
90 assertEquals("Error: Internal Server Error. Please try again later.", either.right().value().getText());
91 assertEquals(500, either.right().value().getStatus());
93 List<ComponentInstance> list = new LinkedList<>();
94 ComponentInstance instance = new ComponentInstance();
95 instance.setUniqueId("uniqueId");
97 service.setComponentInstances(list);
98 either = nodeFilterValidator.validateComponentInstanceExist(service, "uniqueId");
99 assertTrue(either.isLeft());
103 public void testValidateNodeFilterStaticIncorrectPropertyTypeProvided() {
104 Service service = createService("booleanIncorrect");
105 Either<Boolean, ResponseFormat> either =
106 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
107 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")),
108 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
109 assertTrue(either.isRight());
112 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
113 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")),
114 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.CAPABILITIES);
115 assertTrue(either.isRight());
119 public void testValidateComponentFilter() {
120 Service service = createService("booleanIncorrect");
121 String property = "Prop1: {equal: {get_property: ['test','test2']}}";
122 Either<Boolean, ResponseFormat> either =
123 nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(property),
124 NodeFilterConstraintAction.ADD);
125 assertTrue(either.isRight());
127 property = "Prop1: {equal: {get_property: ['parentservice','Prop1']}}";
129 nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(property),
130 NodeFilterConstraintAction.ADD);
131 assertTrue(either.isLeft());
133 String staticStr = "Prop1: {equal: 1}";
134 either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
135 NodeFilterConstraintAction.ADD);
136 assertTrue(either.isLeft());
137 assertTrue(either.left().value());
139 staticStr = "Prop1: {equal: 'true'}";
140 either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
141 NodeFilterConstraintAction.ADD);
142 assertTrue(either.isRight());
144 staticStr = "Prop1: {greater_than: '3'}";
145 either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
146 NodeFilterConstraintAction.ADD);
147 assertTrue(either.isRight());
149 staticStr = "test: {greater_than: '3'}";
150 either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
151 NodeFilterConstraintAction.ADD);
152 assertTrue(either.isRight());
156 public void testValidateNodeFilterStaticIncorrectOperatorProvidedBoolean() {
157 Service service = createService("boolean");
158 Either<Boolean, ResponseFormat> either =
159 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
160 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")
161 .replace("equal", "greater_than")),
162 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
164 Assert.assertFalse(either.isLeft());
168 public void testValidateNodeFilterStaticIncorrectValueProvidedBoolean() {
169 Service service = createService("boolean");
170 Either<Boolean, ResponseFormat> either =
171 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
172 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "trues")),
173 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
175 Assert.assertFalse(either.isLeft());
179 public void testValidateNodeFilterStaticIncorrectOperatorProvidedString() {
180 Service service = createService(STRING_TYPE);
181 Either<Boolean, ResponseFormat> either =
182 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
183 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")
184 .replace("equal", "greater_than")),
185 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
187 Assert.assertTrue(either.isLeft());
191 public void testValidateNodeFilterIntegerValueSuccess() {
192 Service service = createService(INTEGER_TYPE);
193 Either<Boolean, ResponseFormat> either =
194 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
195 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1")),
196 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
198 Assert.assertTrue(either.isLeft());
202 public void testValidateNodeFilterIntegerValueFail() {
203 Service service = createService(INTEGER_TYPE);
205 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
206 .thenReturn(new ResponseFormat());
208 Either<Boolean, ResponseFormat> either =
209 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
210 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1.0")),
211 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
213 Assert.assertTrue(either.isRight());
217 public void testValidateNodeFilterFloatValueSuccess() {
218 Service service = createService(FLOAT_TYPE);
219 Either<Boolean, ResponseFormat> either =
220 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
221 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1.0")),
222 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES);
224 Assert.assertTrue(either.isLeft());
228 public void testValidateNodeFilterFloatValueFail() {
229 Service service = createService(FLOAT_TYPE);
231 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
232 .thenReturn(new ResponseFormat());
234 Either<Boolean, ResponseFormat> either =
235 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
236 Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD,
237 NodeFilterConstraintType.PROPERTIES);
239 Assert.assertTrue(either.isRight());
243 public void testValidateNodeFilterStringValueSuccess() {
244 Service service = createService(STRING_TYPE);
245 Either<Boolean, ResponseFormat> either =
246 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
247 Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD,
248 NodeFilterConstraintType.PROPERTIES);
250 Assert.assertTrue(either.isLeft());
254 public void testValidatePropertyConstraintBrotherSuccess() {
255 Service service = createService(STRING_TYPE);
256 Either<Boolean, ResponseFormat> either =
257 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
258 + " equal: { get_property :[component2, Prop1]}\n"), NodeFilterConstraintAction.ADD,
259 NodeFilterConstraintType.PROPERTIES);
261 Assert.assertTrue(either.isLeft());
265 public void testValidatePropertyConstraintParentSuccess() {
266 Service service = createService(STRING_TYPE);
267 Either<Boolean, ResponseFormat> either =
268 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
269 + " equal: { get_property : [SELF, Prop1]}\n"), NodeFilterConstraintAction.ADD,
270 NodeFilterConstraintType.PROPERTIES);
272 Assert.assertTrue(either.isLeft());
276 public void testValidatePropertyConstraintBrotherPropertyTypeMismatch() {
277 Service service = createService(STRING_TYPE);
278 service.getComponentInstancesProperties().get(COMPONENT2_ID).get(0).setType(INTEGER_TYPE);
280 Either<Boolean, ResponseFormat> either =
281 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
282 + " equal: { get_property : [component2, Prop1]}\n"), NodeFilterConstraintAction.ADD,
283 NodeFilterConstraintType.PROPERTIES);
285 Assert.assertFalse(either.isLeft());
289 public void testValidatePropertyConstraintParentPropertyTypeMismatch() {
290 Service service = createService(STRING_TYPE);
291 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(INTEGER_TYPE);
293 Either<Boolean, ResponseFormat> either =
294 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
295 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
296 NodeFilterConstraintType.PROPERTIES);
298 Assert.assertFalse(either.isLeft());
302 public void testValidatePropertyConstraintParentPropertyNotFound() {
303 Service service = createService(STRING_TYPE);
304 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
306 Either<Boolean, ResponseFormat> either =
307 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
308 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
309 NodeFilterConstraintType.PROPERTIES);
311 Assert.assertFalse(either.isLeft());
315 public void testvalidatePropertyConstraintBrotherPropertyNotFound() {
316 Service service = createService(STRING_TYPE);
317 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
319 Either<Boolean, ResponseFormat> either =
320 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
321 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
322 NodeFilterConstraintType.PROPERTIES);
324 Assert.assertFalse(either.isLeft());
328 public void testValidatePropertyConstraintParentPropertySchemaMismatch() {
329 Service service = createService(LIST_TYPE,STRING_TYPE);
330 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(LIST_TYPE);
332 Either<Boolean, ResponseFormat> either =
333 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
334 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
335 NodeFilterConstraintType.PROPERTIES);
337 Assert.assertFalse(either.isLeft());
340 private Service createService(String type) {
341 return createService(type, null);
344 private Service createService(String type, String schemaType) {
345 Service service = new Service();
346 service.setName(PARENTSERVICE_ID);
348 PropertyDefinition propertyDefinition = new PropertyDefinition();
349 propertyDefinition.setName(PROPERTY_NAME);
350 propertyDefinition.setType(type);
351 if (schemaType != null){
352 SchemaDefinition schemaDefinition = new SchemaDefinition();
353 PropertyDataDefinition schemaProperty = new PropertyDataDefinition();
354 schemaProperty.setType(schemaType);
355 schemaDefinition.setProperty(schemaProperty);
356 propertyDefinition.setSchema(schemaDefinition);
358 service.setProperties(Collections.singletonList(propertyDefinition));
360 ComponentInstance componentInstance = new ComponentInstance();
361 componentInstance.setUniqueId(COMPONENT1_ID);
362 componentInstance.setName(COMPONENT1_ID);
364 ComponentInstance componentInstance2 = new ComponentInstance();
365 componentInstance2.setUniqueId(COMPONENT2_ID);
366 componentInstance2.setName(COMPONENT2_ID);
368 service.setComponentInstances(Arrays.asList(componentInstance, componentInstance2));
370 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
371 componentInstanceProperty.setName(PROPERTY_NAME);
372 componentInstanceProperty.setType(type);
374 ComponentInstanceProperty componentInstanceProperty2 = new ComponentInstanceProperty();
375 componentInstanceProperty2.setName(PROPERTY_NAME);
376 componentInstanceProperty2.setType(type);
378 Map<String, List<ComponentInstanceProperty>> componentInstancePropertyMap = new HashMap<>();
379 componentInstancePropertyMap.put(componentInstance.getUniqueId(),
380 Collections.singletonList(componentInstanceProperty));
381 componentInstancePropertyMap.put(componentInstance2.getUniqueId(),
382 Collections.singletonList(componentInstanceProperty2));
383 componentInstancePropertyMap.put(INNER_SERVICE, Collections.singletonList(componentInstanceProperty));
385 service.setComponentInstancesProperties(componentInstancePropertyMap);