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 java.util.Arrays;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
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;
46 public class NodeFilterValidationTest {
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;
62 private NodeFilterValidator nodeFilterValidator;
66 componentsUtils = Mockito.mock(ComponentsUtils.class);
67 MockitoAnnotations.initMocks(this);
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);
78 Assert.assertFalse(either.isLeft());
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);
90 Assert.assertFalse(either.isLeft());
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);
101 Assert.assertFalse(either.isLeft());
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);
113 Assert.assertTrue(either.isLeft());
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);
124 Assert.assertTrue(either.isLeft());
128 public void testValidateNodeFilterIntegerValueFail() {
129 Service service = createService(INTEGER_TYPE);
131 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
132 .thenReturn(new ResponseFormat());
134 Either<Boolean, ResponseFormat> either =
135 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
136 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1.0")),
137 NodeFilterConstraintAction.ADD);
139 Assert.assertTrue(either.isRight());
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);
150 Assert.assertTrue(either.isLeft());
154 public void testValidateNodeFilterFloatValueFail() {
155 Service service = createService(FLOAT_TYPE);
157 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
158 .thenReturn(new ResponseFormat());
160 Either<Boolean, ResponseFormat> either =
161 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
162 Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD);
164 Assert.assertTrue(either.isRight());
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);
174 Assert.assertTrue(either.isLeft());
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);
184 Assert.assertTrue(either.isLeft());
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);
194 Assert.assertTrue(either.isLeft());
198 public void testValidatePropertyConstraintBrotherPropertyTypeMismatch() {
199 Service service = createService(STRING_TYPE);
200 service.getComponentInstancesProperties().get(COMPONENT2_ID).get(0).setType(INTEGER_TYPE);
202 Either<Boolean, ResponseFormat> either =
203 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
204 + " equal: { get_property : [component2, Prop1]}\n"), NodeFilterConstraintAction.ADD);
206 Assert.assertFalse(either.isLeft());
210 public void testValidatePropertyConstraintParentPropertyTypeMismatch() {
211 Service service = createService(STRING_TYPE);
212 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(INTEGER_TYPE);
214 Either<Boolean, ResponseFormat> either =
215 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
216 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
218 Assert.assertFalse(either.isLeft());
222 public void testValidatePropertyConstraintParentPropertyNotFound() {
223 Service service = createService(STRING_TYPE);
224 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
226 Either<Boolean, ResponseFormat> either =
227 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
228 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
230 Assert.assertFalse(either.isLeft());
234 public void testvalidatePropertyConstraintBrotherPropertyNotFound() {
235 Service service = createService(STRING_TYPE);
236 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
238 Either<Boolean, ResponseFormat> either =
239 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
240 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
242 Assert.assertFalse(either.isLeft());
246 public void testValidatePropertyConstraintParentPropertySchemaMismatch() {
247 Service service = createService(LIST_TYPE,STRING_TYPE);
248 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(LIST_TYPE);
250 Either<Boolean, ResponseFormat> either =
251 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
252 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
254 Assert.assertFalse(either.isLeft());
257 private Service createService(String type) {
258 return createService(type, null);
261 private Service createService(String type, String schemaType) {
262 Service service = new Service();
263 service.setName(PARENTSERVICE_ID);
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);
275 service.setProperties(Collections.singletonList(propertyDefinition));
277 ComponentInstance componentInstance = new ComponentInstance();
278 componentInstance.setUniqueId(COMPONENT1_ID);
279 componentInstance.setName(COMPONENT1_ID);
281 ComponentInstance componentInstance2 = new ComponentInstance();
282 componentInstance2.setUniqueId(COMPONENT2_ID);
283 componentInstance2.setName(COMPONENT2_ID);
285 service.setComponentInstances(Arrays.asList(componentInstance, componentInstance2));
287 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
288 componentInstanceProperty.setName(PROPERTY_NAME);
289 componentInstanceProperty.setType(type);
291 ComponentInstanceProperty componentInstanceProperty2 = new ComponentInstanceProperty();
292 componentInstanceProperty2.setName(PROPERTY_NAME);
293 componentInstanceProperty2.setType(type);
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));
302 service.setComponentInstancesProperties(componentInstancePropertyMap);