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 EMPTY_STR = "";
57 private static final String UI_CONSTRAINT_STATIC = "Prop1: {equal: 'value'}";
58 private static final String INNER_SERVICE = "innerService";
59 private static final String PROPERTY_NAME = "Prop1";
60 private static final String VALUE = "value";
61 private static final String FLOAT_TYPE = "float";
62 private static final String STRING_TYPE = "string";
63 private static final String LIST_TYPE = "list";
64 private static final String COMPONENT1_ID = "component1";
65 private static final String INTEGER_TYPE = "integer";
66 private static final String PARENTSERVICE_ID = "parentservice";
67 private static final String COMPONENT2_ID = "component2";
68 private ComponentsUtils componentsUtils;
71 private NodeFilterValidator nodeFilterValidator;
75 componentsUtils = Mockito.mock(ComponentsUtils.class);
76 MockitoAnnotations.openMocks(this);
77 new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
81 public void testValidateComponentInstanceExist() {
82 Either<Boolean, ResponseFormat> either =
83 nodeFilterValidator.validateComponentInstanceExist(null, INNER_SERVICE);
84 assertTrue(either.isRight());
85 assertEquals("Error: Internal Server Error. Please try again later.", either.right().value().getText());
86 assertEquals(500, either.right().value().getStatus());
88 Service service = createService("booleanIncorrect");
89 either = nodeFilterValidator.validateComponentInstanceExist(service, INNER_SERVICE);
90 assertTrue(either.isRight());
91 assertEquals("Error: Internal Server Error. Please try again later.", either.right().value().getText());
92 assertEquals(500, either.right().value().getStatus());
94 List<ComponentInstance> list = new LinkedList<>();
95 ComponentInstance instance = new ComponentInstance();
96 instance.setUniqueId("uniqueId");
98 service.setComponentInstances(list);
99 either = nodeFilterValidator.validateComponentInstanceExist(service, "uniqueId");
100 assertTrue(either.isLeft());
104 public void testValidateNodeFilterStaticIncorrectPropertyTypeProvided() {
105 Service service = createService("booleanIncorrect");
106 Either<Boolean, ResponseFormat> either =
107 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
108 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")),
109 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
110 assertTrue(either.isRight());
113 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
114 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")),
115 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.CAPABILITIES, EMPTY_STR);
116 assertTrue(either.isRight());
120 public void testValidateComponentFilter() {
121 Service service = createService("booleanIncorrect");
122 String property = "Prop1: {equal: {get_property: ['test','test2']}}";
123 Either<Boolean, ResponseFormat> either =
124 nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(property),
125 NodeFilterConstraintAction.ADD);
126 assertTrue(either.isRight());
128 property = "Prop1: {equal: {get_property: ['parentservice','Prop1']}}";
130 nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(property),
131 NodeFilterConstraintAction.ADD);
132 assertTrue(either.isLeft());
134 String staticStr = "Prop1: {equal: 1}";
135 either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
136 NodeFilterConstraintAction.ADD);
137 assertTrue(either.isLeft());
138 assertTrue(either.left().value());
140 staticStr = "Prop1: {equal: 'true'}";
141 either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
142 NodeFilterConstraintAction.ADD);
143 assertTrue(either.isRight());
145 staticStr = "Prop1: {greater_than: '3'}";
146 either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
147 NodeFilterConstraintAction.ADD);
148 assertTrue(either.isRight());
150 staticStr = "test: {greater_than: '3'}";
151 either = nodeFilterValidator.validateComponentFilter(service, Collections.singletonList(staticStr),
152 NodeFilterConstraintAction.ADD);
153 assertTrue(either.isRight());
157 public void testValidateNodeFilterStaticIncorrectOperatorProvidedBoolean() {
158 Service service = createService("boolean");
159 Either<Boolean, ResponseFormat> either =
160 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
161 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")
162 .replace("equal", "greater_than")),
163 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
165 Assert.assertFalse(either.isLeft());
169 public void testValidateNodeFilterStaticIncorrectValueProvidedBoolean() {
170 Service service = createService("boolean");
171 Either<Boolean, ResponseFormat> either =
172 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
173 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "trues")),
174 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
176 Assert.assertFalse(either.isLeft());
180 public void testValidateNodeFilterStaticIncorrectOperatorProvidedString() {
181 Service service = createService(STRING_TYPE);
182 Either<Boolean, ResponseFormat> either =
183 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
184 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")
185 .replace("equal", "greater_than")),
186 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
188 Assert.assertTrue(either.isLeft());
192 public void testValidateNodeFilterIntegerValueSuccess() {
193 Service service = createService(INTEGER_TYPE);
194 Either<Boolean, ResponseFormat> either =
195 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
196 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1")),
197 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
199 Assert.assertTrue(either.isLeft());
203 public void testValidateNodeFilterIntegerValueFail() {
204 Service service = createService(INTEGER_TYPE);
206 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
207 .thenReturn(new ResponseFormat());
209 Either<Boolean, ResponseFormat> either =
210 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
211 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1.0")),
212 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
214 Assert.assertTrue(either.isRight());
218 public void testValidateNodeFilterFloatValueSuccess() {
219 Service service = createService(FLOAT_TYPE);
220 Either<Boolean, ResponseFormat> either =
221 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
222 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1.0")),
223 NodeFilterConstraintAction.ADD, NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
225 Assert.assertTrue(either.isLeft());
229 public void testValidateNodeFilterFloatValueFail() {
230 Service service = createService(FLOAT_TYPE);
232 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
233 .thenReturn(new ResponseFormat());
235 Either<Boolean, ResponseFormat> either =
236 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
237 Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD,
238 NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
240 Assert.assertTrue(either.isRight());
244 public void testValidateNodeFilterStringValueSuccess() {
245 Service service = createService(STRING_TYPE);
246 Either<Boolean, ResponseFormat> either =
247 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
248 Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD,
249 NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
251 Assert.assertTrue(either.isLeft());
255 public void testValidatePropertyConstraintBrotherSuccess() {
256 Service service = createService(STRING_TYPE);
257 Either<Boolean, ResponseFormat> either =
258 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
259 + " equal: { get_property :[component2, Prop1]}\n"), NodeFilterConstraintAction.ADD,
260 NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
262 Assert.assertTrue(either.isLeft());
266 public void testValidatePropertyConstraintParentSuccess() {
267 Service service = createService(STRING_TYPE);
268 Either<Boolean, ResponseFormat> either =
269 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
270 + " equal: { get_property : [SELF, Prop1]}\n"), NodeFilterConstraintAction.ADD,
271 NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
273 Assert.assertTrue(either.isLeft());
277 public void testValidatePropertyConstraintBrotherPropertyTypeMismatch() {
278 Service service = createService(STRING_TYPE);
279 service.getComponentInstancesProperties().get(COMPONENT2_ID).get(0).setType(INTEGER_TYPE);
281 Either<Boolean, ResponseFormat> either =
282 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
283 + " equal: { get_property : [component2, Prop1]}\n"), NodeFilterConstraintAction.ADD,
284 NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
286 Assert.assertFalse(either.isLeft());
290 public void testValidatePropertyConstraintParentPropertyTypeMismatch() {
291 Service service = createService(STRING_TYPE);
292 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(INTEGER_TYPE);
294 Either<Boolean, ResponseFormat> either =
295 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
296 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
297 NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
299 Assert.assertFalse(either.isLeft());
303 public void testValidatePropertyConstraintParentPropertyNotFound() {
304 Service service = createService(STRING_TYPE);
305 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
307 Either<Boolean, ResponseFormat> either =
308 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
309 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
310 NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
312 Assert.assertFalse(either.isLeft());
316 public void testvalidatePropertyConstraintBrotherPropertyNotFound() {
317 Service service = createService(STRING_TYPE);
318 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
320 Either<Boolean, ResponseFormat> either =
321 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
322 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
323 NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
325 Assert.assertFalse(either.isLeft());
329 public void testValidatePropertyConstraintParentPropertySchemaMismatch() {
330 Service service = createService(LIST_TYPE,STRING_TYPE);
331 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(LIST_TYPE);
333 Either<Boolean, ResponseFormat> either =
334 nodeFilterValidator.validateFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
335 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD,
336 NodeFilterConstraintType.PROPERTIES, EMPTY_STR);
338 Assert.assertFalse(either.isLeft());
341 private Service createService(String type) {
342 return createService(type, null);
345 private Service createService(String type, String schemaType) {
346 Service service = new Service();
347 service.setName(PARENTSERVICE_ID);
349 PropertyDefinition propertyDefinition = new PropertyDefinition();
350 propertyDefinition.setName(PROPERTY_NAME);
351 propertyDefinition.setType(type);
352 if (schemaType != null){
353 SchemaDefinition schemaDefinition = new SchemaDefinition();
354 PropertyDataDefinition schemaProperty = new PropertyDataDefinition();
355 schemaProperty.setType(schemaType);
356 schemaDefinition.setProperty(schemaProperty);
357 propertyDefinition.setSchema(schemaDefinition);
359 service.setProperties(Collections.singletonList(propertyDefinition));
361 ComponentInstance componentInstance = new ComponentInstance();
362 componentInstance.setUniqueId(COMPONENT1_ID);
363 componentInstance.setName(COMPONENT1_ID);
365 ComponentInstance componentInstance2 = new ComponentInstance();
366 componentInstance2.setUniqueId(COMPONENT2_ID);
367 componentInstance2.setName(COMPONENT2_ID);
369 service.setComponentInstances(Arrays.asList(componentInstance, componentInstance2));
371 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
372 componentInstanceProperty.setName(PROPERTY_NAME);
373 componentInstanceProperty.setType(type);
375 ComponentInstanceProperty componentInstanceProperty2 = new ComponentInstanceProperty();
376 componentInstanceProperty2.setName(PROPERTY_NAME);
377 componentInstanceProperty2.setType(type);
379 Map<String, List<ComponentInstanceProperty>> componentInstancePropertyMap = new HashMap<>();
380 componentInstancePropertyMap.put(componentInstance.getUniqueId(),
381 Collections.singletonList(componentInstanceProperty));
382 componentInstancePropertyMap.put(componentInstance2.getUniqueId(),
383 Collections.singletonList(componentInstanceProperty2));
384 componentInstancePropertyMap.put(INNER_SERVICE, Collections.singletonList(componentInstanceProperty));
386 service.setComponentInstancesProperties(componentInstancePropertyMap);