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;
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;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.HashMap;
45 import java.util.List;
48 public class NodeFilterValidationTest {
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;
64 private NodeFilterValidator nodeFilterValidator;
68 componentsUtils = Mockito.mock(ComponentsUtils.class);
69 MockitoAnnotations.initMocks(this);
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);
80 Assert.assertFalse(either.isLeft());
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);
92 Assert.assertFalse(either.isLeft());
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);
103 Assert.assertFalse(either.isLeft());
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);
115 Assert.assertTrue(either.isLeft());
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);
126 Assert.assertTrue(either.isLeft());
130 public void testValidateNodeFilterIntegerValueFail() {
131 Service service = createService(INTEGER_TYPE);
133 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
134 .thenReturn(new ResponseFormat());
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);
141 Assert.assertTrue(either.isRight());
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);
152 Assert.assertTrue(either.isLeft());
156 public void testValidateNodeFilterFloatValueFail() {
157 Service service = createService(FLOAT_TYPE);
159 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
160 .thenReturn(new ResponseFormat());
162 Either<Boolean, ResponseFormat> either =
163 nodeFilterValidator.validateFilter(service, INNER_SERVICE,
164 Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD,
165 NodeFilterConstraintType.PROPERTIES);
167 Assert.assertTrue(either.isRight());
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);
178 Assert.assertTrue(either.isLeft());
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);
189 Assert.assertTrue(either.isLeft());
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);
200 Assert.assertTrue(either.isLeft());
204 public void testValidatePropertyConstraintBrotherPropertyTypeMismatch() {
205 Service service = createService(STRING_TYPE);
206 service.getComponentInstancesProperties().get(COMPONENT2_ID).get(0).setType(INTEGER_TYPE);
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);
213 Assert.assertFalse(either.isLeft());
217 public void testValidatePropertyConstraintParentPropertyTypeMismatch() {
218 Service service = createService(STRING_TYPE);
219 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(INTEGER_TYPE);
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);
226 Assert.assertFalse(either.isLeft());
230 public void testValidatePropertyConstraintParentPropertyNotFound() {
231 Service service = createService(STRING_TYPE);
232 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
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);
239 Assert.assertFalse(either.isLeft());
243 public void testvalidatePropertyConstraintBrotherPropertyNotFound() {
244 Service service = createService(STRING_TYPE);
245 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
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);
252 Assert.assertFalse(either.isLeft());
256 public void testValidatePropertyConstraintParentPropertySchemaMismatch() {
257 Service service = createService(LIST_TYPE,STRING_TYPE);
258 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(LIST_TYPE);
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);
265 Assert.assertFalse(either.isLeft());
268 private Service createService(String type) {
269 return createService(type, null);
272 private Service createService(String type, String schemaType) {
273 Service service = new Service();
274 service.setName(PARENTSERVICE_ID);
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);
286 service.setProperties(Collections.singletonList(propertyDefinition));
288 ComponentInstance componentInstance = new ComponentInstance();
289 componentInstance.setUniqueId(COMPONENT1_ID);
290 componentInstance.setName(COMPONENT1_ID);
292 ComponentInstance componentInstance2 = new ComponentInstance();
293 componentInstance2.setUniqueId(COMPONENT2_ID);
294 componentInstance2.setName(COMPONENT2_ID);
296 service.setComponentInstances(Arrays.asList(componentInstance, componentInstance2));
298 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
299 componentInstanceProperty.setName(PROPERTY_NAME);
300 componentInstanceProperty.setType(type);
302 ComponentInstanceProperty componentInstanceProperty2 = new ComponentInstanceProperty();
303 componentInstanceProperty2.setName(PROPERTY_NAME);
304 componentInstanceProperty2.setType(type);
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));
313 service.setComponentInstancesProperties(componentInstancePropertyMap);