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.impl.ComponentsUtils;
35 import org.openecomp.sdc.be.model.ComponentInstance;
36 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
37 import org.openecomp.sdc.be.model.PropertyDefinition;
38 import org.openecomp.sdc.be.model.Service;
39 import org.openecomp.sdc.exception.ResponseFormat;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.HashMap;
44 import java.util.List;
47 public class NodeFilterValidationTest {
49 private static final String UI_CONSTRAINT_STATIC = "Prop1: {equal: 'value'}";
50 private static final String INNER_SERVICE = "innerService";
51 private static final String PROPERTY_NAME = "Prop1";
52 private static final String VALUE = "value";
53 private static final String FLOAT_TYPE = "float";
54 private static final String STRING_TYPE = "string";
55 private static final String LIST_TYPE = "list";
56 private static final String COMPONENT1_ID = "component1";
57 private static final String INTEGER_TYPE = "integer";
58 private static final String PARENTSERVICE_ID = "parentservice";
59 private static final String COMPONENT2_ID = "component2";
60 private ComponentsUtils componentsUtils;
63 private NodeFilterValidator nodeFilterValidator;
67 componentsUtils = Mockito.mock(ComponentsUtils.class);
68 MockitoAnnotations.initMocks(this);
72 public void testValidateNodeFilterStaticIncorrectPropertyTypeProvided() {
73 Service service = createService("booleanIncorrect");
74 Either<Boolean, ResponseFormat> either =
75 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
76 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")),
77 NodeFilterConstraintAction.ADD);
79 Assert.assertFalse(either.isLeft());
83 public void testValidateNodeFilterStaticIncorrectOperatorProvidedBoolean() {
84 Service service = createService("boolean");
85 Either<Boolean, ResponseFormat> either =
86 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
87 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")
88 .replace("equal", "greater_than")),
89 NodeFilterConstraintAction.ADD);
91 Assert.assertFalse(either.isLeft());
95 public void testValidateNodeFilterStaticIncorrectValueProvidedBoolean() {
96 Service service = createService("boolean");
97 Either<Boolean, ResponseFormat> either =
98 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
99 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "trues")),
100 NodeFilterConstraintAction.ADD);
102 Assert.assertFalse(either.isLeft());
106 public void testValidateNodeFilterStaticIncorrectOperatorProvidedString() {
107 Service service = createService(STRING_TYPE);
108 Either<Boolean, ResponseFormat> either =
109 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
110 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "true")
111 .replace("equal", "greater_than")),
112 NodeFilterConstraintAction.ADD);
114 Assert.assertTrue(either.isLeft());
118 public void testValidateNodeFilterIntegerValueSuccess() {
119 Service service = createService(INTEGER_TYPE);
120 Either<Boolean, ResponseFormat> either =
121 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
122 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1")),
123 NodeFilterConstraintAction.ADD);
125 Assert.assertTrue(either.isLeft());
129 public void testValidateNodeFilterIntegerValueFail() {
130 Service service = createService(INTEGER_TYPE);
132 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
133 .thenReturn(new ResponseFormat());
135 Either<Boolean, ResponseFormat> either =
136 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
137 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1.0")),
138 NodeFilterConstraintAction.ADD);
140 Assert.assertTrue(either.isRight());
144 public void testValidateNodeFilterFloatValueSuccess() {
145 Service service = createService(FLOAT_TYPE);
146 Either<Boolean, ResponseFormat> either =
147 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
148 Collections.singletonList(UI_CONSTRAINT_STATIC.replace(VALUE, "1.0")),
149 NodeFilterConstraintAction.ADD);
151 Assert.assertTrue(either.isLeft());
155 public void testValidateNodeFilterFloatValueFail() {
156 Service service = createService(FLOAT_TYPE);
158 Mockito.when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, "param1"))
159 .thenReturn(new ResponseFormat());
161 Either<Boolean, ResponseFormat> either =
162 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
163 Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD);
165 Assert.assertTrue(either.isRight());
169 public void testValidateNodeFilterStringValueSuccess() {
170 Service service = createService(STRING_TYPE);
171 Either<Boolean, ResponseFormat> either =
172 nodeFilterValidator.validateNodeFilter(service, INNER_SERVICE,
173 Collections.singletonList(UI_CONSTRAINT_STATIC), NodeFilterConstraintAction.ADD);
175 Assert.assertTrue(either.isLeft());
179 public void testValidatePropertyConstraintBrotherSuccess() {
180 Service service = createService(STRING_TYPE);
181 Either<Boolean, ResponseFormat> either =
182 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
183 + " equal: { get_property :[component2, Prop1]}\n"), NodeFilterConstraintAction.ADD);
185 Assert.assertTrue(either.isLeft());
189 public void testValidatePropertyConstraintParentSuccess() {
190 Service service = createService(STRING_TYPE);
191 Either<Boolean, ResponseFormat> either =
192 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
193 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
195 Assert.assertTrue(either.isLeft());
199 public void testValidatePropertyConstraintBrotherPropertyTypeMismatch() {
200 Service service = createService(STRING_TYPE);
201 service.getComponentInstancesProperties().get(COMPONENT2_ID).get(0).setType(INTEGER_TYPE);
203 Either<Boolean, ResponseFormat> either =
204 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
205 + " equal: { get_property : [component2, Prop1]}\n"), NodeFilterConstraintAction.ADD);
207 Assert.assertFalse(either.isLeft());
211 public void testValidatePropertyConstraintParentPropertyTypeMismatch() {
212 Service service = createService(STRING_TYPE);
213 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(INTEGER_TYPE);
215 Either<Boolean, ResponseFormat> either =
216 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
217 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
219 Assert.assertFalse(either.isLeft());
223 public void testValidatePropertyConstraintParentPropertyNotFound() {
224 Service service = createService(STRING_TYPE);
225 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
227 Either<Boolean, ResponseFormat> either =
228 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
229 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
231 Assert.assertFalse(either.isLeft());
235 public void testvalidatePropertyConstraintBrotherPropertyNotFound() {
236 Service service = createService(STRING_TYPE);
237 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2");
239 Either<Boolean, ResponseFormat> either =
240 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
241 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
243 Assert.assertFalse(either.isLeft());
247 public void testValidatePropertyConstraintParentPropertySchemaMismatch() {
248 Service service = createService(LIST_TYPE,STRING_TYPE);
249 service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(LIST_TYPE);
251 Either<Boolean, ResponseFormat> either =
252 nodeFilterValidator.validateNodeFilter(service, COMPONENT1_ID, Collections.singletonList("Prop1:\n"
253 + " equal: { get_property : [parentservice, Prop1]}\n"), NodeFilterConstraintAction.ADD);
255 Assert.assertFalse(either.isLeft());
258 private Service createService(String type) {
259 return createService(type, null);
262 private Service createService(String type, String schemaType) {
263 Service service = new Service();
264 service.setName(PARENTSERVICE_ID);
266 PropertyDefinition propertyDefinition = new PropertyDefinition();
267 propertyDefinition.setName(PROPERTY_NAME);
268 propertyDefinition.setType(type);
269 if (schemaType != null){
270 SchemaDefinition schemaDefinition = new SchemaDefinition();
271 PropertyDataDefinition schemaProperty = new PropertyDataDefinition();
272 schemaProperty.setType(schemaType);
273 schemaDefinition.setProperty(schemaProperty);
274 propertyDefinition.setSchema(schemaDefinition);
276 service.setProperties(Collections.singletonList(propertyDefinition));
278 ComponentInstance componentInstance = new ComponentInstance();
279 componentInstance.setUniqueId(COMPONENT1_ID);
280 componentInstance.setName(COMPONENT1_ID);
282 ComponentInstance componentInstance2 = new ComponentInstance();
283 componentInstance2.setUniqueId(COMPONENT2_ID);
284 componentInstance2.setName(COMPONENT2_ID);
286 service.setComponentInstances(Arrays.asList(componentInstance, componentInstance2));
288 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
289 componentInstanceProperty.setName(PROPERTY_NAME);
290 componentInstanceProperty.setType(type);
292 ComponentInstanceProperty componentInstanceProperty2 = new ComponentInstanceProperty();
293 componentInstanceProperty2.setName(PROPERTY_NAME);
294 componentInstanceProperty2.setType(type);
296 Map<String, List<ComponentInstanceProperty>> componentInstancePropertyMap = new HashMap<>();
297 componentInstancePropertyMap.put(componentInstance.getUniqueId(),
298 Collections.singletonList(componentInstanceProperty));
299 componentInstancePropertyMap.put(componentInstance2.getUniqueId(),
300 Collections.singletonList(componentInstanceProperty2));
301 componentInstancePropertyMap.put(INNER_SERVICE, Collections.singletonList(componentInstanceProperty));
303 service.setComponentInstancesProperties(componentInstancePropertyMap);