2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.base;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
30 import java.util.Arrays;
31 import java.util.Iterator;
32 import java.util.LinkedHashMap;
33 import java.util.List;
35 import org.junit.Before;
36 import org.junit.Test;
38 public class ValidatedTest {
39 private static final String COLLECTION_TEXT = "collection";
40 private static final String ERROR_MESSAGE = "error message";
41 private static final String COLLECTION_FIELD = "coll";
42 private static final String VALID_VALUE = "abc123";
43 private static final String PROPS_FIELD = "props";
44 private static final String MY_NAME = "my.name";
45 private static final String VALID_FIELD = "validField";
46 private static final String INVALID_FIELD = "invalidField";
47 private static final String NULL_FIELD = "nullField";
48 private static final String WORD_PAT = "\\w*";
49 private static final String MY_TO_STRING = "[some text]";
50 private static final String VERSION = "1.2.3";
52 private Validated validated;
56 validated = new Validated();
60 public void testValidate() {
61 assertThatThrownBy(() -> validated.validate(null)).isInstanceOf(NullPointerException.class);
63 PfValidationResult result = new PfValidationResult();
64 assertSame(result, validated.validate(result));
65 assertTrue(result.isValid());
66 assertEquals(0, result.getMessageList().size());
70 public void testValidateNotNull() {
71 PfValidationResult result = new PfValidationResult();
73 final PfValidationResult result2 = result;
74 assertThatThrownBy(() -> validated.validateNotNull(null, VALID_FIELD, VALID_VALUE, result2))
75 .isInstanceOf(NullPointerException.class);
76 assertThatThrownBy(() -> validated.validateNotNull(this, null, VALID_VALUE, result2))
77 .isInstanceOf(NullPointerException.class);
78 assertThatThrownBy(() -> validated.validateNotNull(this, VALID_FIELD, VALID_VALUE, null))
79 .isInstanceOf(NullPointerException.class);
82 result = validated.validateNotNull(this, NULL_FIELD, null, result);
85 result = validated.validateNotNull(this, INVALID_FIELD, "!!!", result);
88 result = validated.validateNotNull(this, VALID_FIELD, VALID_VALUE, result);
91 result = validated.validateNotNull(this, VALID_FIELD, Integer.valueOf(10), result);
93 assertFalse(result.isValid());
94 assertEquals(1, result.getMessageList().size());
96 // check result for null text
97 PfValidationMessage msg = result.getMessageList().get(0);
98 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
99 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
100 assertTrue(msg.getMessage().contains("nullField invalid-null"));
104 public void testValidateNotNullConceptKey() {
105 PfValidationResult result = new PfValidationResult();
108 PfConceptKey key = new PfConceptKey();
109 key.setVersion(VERSION);
110 result = validated.validateNotNull(key, result);
113 key = new PfConceptKey();
114 key.setName(MY_NAME);
115 result = validated.validateNotNull(key, result);
118 key = new PfConceptKey();
119 result = validated.validateNotNull(key, result);
121 assertFalse(result.isValid());
122 assertEquals(4, result.getMessageList().size());
125 key = new PfConceptKey();
126 key.setName(MY_NAME);
127 key.setVersion(VERSION);
128 result = validated.validateNotNull(key, result);
131 assertFalse(result.isValid());
132 assertEquals(4, result.getMessageList().size());
134 Iterator<PfValidationMessage> it = result.getMessageList().iterator();
137 PfValidationMessage msg = it.next();
138 assertEquals(PfConceptKey.class.getName(), msg.getObservedClass());
139 assertTrue(msg.getMessage().contains("name invalid-null"));
143 assertEquals(PfConceptKey.class.getName(), msg.getObservedClass());
144 assertTrue(msg.getMessage().contains("version invalid-null"));
148 assertEquals(PfConceptKey.class.getName(), msg.getObservedClass());
149 assertTrue(msg.getMessage().contains("name invalid-null"));
150 assertTrue(it.next().getMessage().contains("version invalid-null"));
152 final PfConceptKey key2 = key;
153 assertThatThrownBy(() -> validated.validateNotNull(key2, null)).isInstanceOf(NullPointerException.class);
154 assertThatThrownBy(() -> validated.validateNotNull(null, new PfValidationResult()))
155 .isInstanceOf(NullPointerException.class);
159 public void testValidateText() {
160 PfValidationResult result = new PfValidationResult();
162 final PfValidationResult result2 = result;
163 assertThatThrownBy(() -> validated.validateText(null, VALID_FIELD, VALID_VALUE, WORD_PAT, result2))
164 .isInstanceOf(NullPointerException.class);
165 assertThatThrownBy(() -> validated.validateText(this, null, VALID_VALUE, WORD_PAT, result2))
166 .isInstanceOf(NullPointerException.class);
167 assertThatThrownBy(() -> validated.validateText(this, VALID_FIELD, VALID_VALUE, null, result2))
168 .isInstanceOf(NullPointerException.class);
169 assertThatThrownBy(() -> validated.validateText(this, VALID_FIELD, VALID_VALUE, WORD_PAT, null))
170 .isInstanceOf(NullPointerException.class);
173 result = validated.validateText(this, NULL_FIELD, null, WORD_PAT, result);
176 result = validated.validateText(this, INVALID_FIELD, "!!!", WORD_PAT, result);
179 result = validated.validateText(this, VALID_FIELD, VALID_VALUE, WORD_PAT, result);
181 assertFalse(result.isValid());
182 assertEquals(1, result.getMessageList().size());
184 // check result for invalid text
185 PfValidationMessage msg = result.getMessageList().get(0);
186 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
187 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
188 assertTrue(msg.getMessage().contains("invalidField invalid-parameter invalidField"));
192 public void testValidatePropertiesNotNull() {
193 PfValidationResult result = new PfValidationResult();
194 result = validated.validatePropertiesNotNull(this, "properties", null, result);
195 assertTrue(result.isValid());
196 assertEquals(0, result.getMessageList().size());
198 Map<String, Integer> map = new LinkedHashMap<>();
204 map.put("abc", null);
210 result = validated.validatePropertiesNotNull(this, PROPS_FIELD, map, result);
212 assertFalse(result.isValid());
213 assertEquals(2, result.getMessageList().size());
215 Iterator<PfValidationMessage> it = result.getMessageList().iterator();
218 PfValidationMessage msg = it.next();
219 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
220 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
221 assertTrue(msg.getMessage().contains("props.null invalid-null"));
225 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
226 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
227 assertTrue(msg.getMessage().contains("props.abc invalid-null"));
229 final PfValidationResult result2 = result;
230 assertThatThrownBy(() -> validated.validatePropertiesNotNull(null, PROPS_FIELD, map, result2))
231 .isInstanceOf(NullPointerException.class);
232 assertThatThrownBy(() -> validated.validatePropertiesNotNull(this, null, map, result2))
233 .isInstanceOf(NullPointerException.class);
234 assertThatThrownBy(() -> validated.validatePropertiesNotNull(this, PROPS_FIELD, map, null))
235 .isInstanceOf(NullPointerException.class);
239 public void testValidateCollectionNotNull() {
240 PfValidationResult result = new PfValidationResult();
241 result = validated.validateCollectionNotNull(this, COLLECTION_TEXT, null, result);
242 assertTrue(result.isValid());
243 assertEquals(0, result.getMessageList().size());
245 final List<String> lst = Arrays.asList("abc", null, "def", null);
247 result = validated.validateCollectionNotNull(this, COLLECTION_FIELD, lst, result);
249 assertFalse(result.isValid());
250 assertEquals(2, result.getMessageList().size());
252 Iterator<PfValidationMessage> it = result.getMessageList().iterator();
255 PfValidationMessage msg = it.next();
256 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
257 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
258 assertTrue(msg.getMessage().contains("coll.1 invalid-null"));
262 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
263 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
264 assertTrue(msg.getMessage().contains("coll.3 invalid-null"));
266 final PfValidationResult result2 = result;
267 assertThatThrownBy(() -> validated.validateCollectionNotNull(null, COLLECTION_FIELD, lst, result2))
268 .isInstanceOf(NullPointerException.class);
269 assertThatThrownBy(() -> validated.validateCollectionNotNull(this, null, lst, result2))
270 .isInstanceOf(NullPointerException.class);
271 assertThatThrownBy(() -> validated.validateCollectionNotNull(this, COLLECTION_FIELD, lst, null))
272 .isInstanceOf(NullPointerException.class);
276 public void testValidateCollection() {
277 PfValidationResult result = new PfValidationResult();
278 result = validated.validateCollection(this, COLLECTION_TEXT, null, result);
279 assertTrue(result.isValid());
280 assertEquals(0, result.getMessageList().size());
282 List<MyValid> lst = Arrays.asList(new MyValid(0, false), new MyValid(1, true), null, new MyValid(2, false),
283 new MyValid(3, true));
284 result = validated.validateCollection(this, COLLECTION_FIELD, lst, result);
286 assertFalse(result.isValid());
287 assertEquals(2, result.getMessageList().size());
289 Iterator<PfValidationMessage> it = result.getMessageList().iterator();
292 PfValidationMessage msg = it.next();
293 assertEquals(MyValid.class.getName(), msg.getObservedClass());
294 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
295 assertTrue(msg.getMessage().contains("index.0 invalid-wrong value"));
299 assertEquals(MyValid.class.getName(), msg.getObservedClass());
300 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
301 assertTrue(msg.getMessage().contains("index.2 invalid-wrong value"));
303 final PfValidationResult result2 = result;
304 assertThatThrownBy(() -> validated.validateCollection(null, COLLECTION_FIELD, lst, result2))
305 .isInstanceOf(NullPointerException.class);
306 assertThatThrownBy(() -> validated.validateCollection(this, null, lst, result2))
307 .isInstanceOf(NullPointerException.class);
308 assertThatThrownBy(() -> validated.validateCollection(this, COLLECTION_FIELD, lst, null))
309 .isInstanceOf(NullPointerException.class);
313 public void testValidateConceptCollection() {
314 PfValidationResult result = new PfValidationResult();
315 result = validated.validateConceptCollection(this, COLLECTION_TEXT, null, result);
316 assertTrue(result.isValid());
317 assertEquals(0, result.getMessageList().size());
319 List<MyConcept> lst = Arrays.asList(new MyConcept(0, false), new MyConcept(1, true), null,
320 new MyConcept(2, false), new MyConcept(3, true));
321 result = validated.validateConceptCollection(this, COLLECTION_FIELD, lst, result);
323 assertFalse(result.isValid());
324 assertEquals(2, result.getMessageList().size());
326 Iterator<PfValidationMessage> it = result.getMessageList().iterator();
329 PfValidationMessage msg = it.next();
330 assertEquals(MyConcept.class.getName(), msg.getObservedClass());
331 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
332 assertTrue(msg.getMessage().contains("index.0 invalid-wrong value"));
336 assertEquals(MyConcept.class.getName(), msg.getObservedClass());
337 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
338 assertTrue(msg.getMessage().contains("index.2 invalid-wrong value"));
340 final PfValidationResult result2 = result;
341 assertThatThrownBy(() -> validated.validateConceptCollection(null, COLLECTION_FIELD, lst, result2))
342 .isInstanceOf(NullPointerException.class);
343 assertThatThrownBy(() -> validated.validateConceptCollection(this, null, lst, result2))
344 .isInstanceOf(NullPointerException.class);
345 assertThatThrownBy(() -> validated.validateConceptCollection(this, COLLECTION_FIELD, lst, null))
346 .isInstanceOf(NullPointerException.class);
350 public void testAddError() {
351 final PfValidationResult result = new PfValidationResult();
352 final PfValidationResult result2 = result;
354 assertThatThrownBy(() -> validated.addError(null, VALID_FIELD, result2, ERROR_MESSAGE))
355 .isInstanceOf(NullPointerException.class);
356 assertThatThrownBy(() -> validated.addError(this, null, result2, ERROR_MESSAGE))
357 .isInstanceOf(NullPointerException.class);
358 assertThatThrownBy(() -> validated.addError(this, VALID_FIELD, null, ERROR_MESSAGE))
359 .isInstanceOf(NullPointerException.class);
361 validated.addError(this, VALID_FIELD, result, "error-A");
362 validated.addError(this, VALID_FIELD, result, null);
363 validated.addError(this, VALID_FIELD, result, "error-B");
365 assertFalse(result.isValid());
366 assertEquals(2, result.getMessageList().size());
368 Iterator<PfValidationMessage> it = result.getMessageList().iterator();
370 PfValidationMessage msg = it.next();
371 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
372 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
373 assertTrue(msg.getMessage().contains("validField invalid-error-A"));
376 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
377 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
378 assertTrue(msg.getMessage().contains("validField invalid-error-B"));
382 public void testMakeKey() {
383 assertThatThrownBy(() -> validated.makeKey(null)).isInstanceOf(NullPointerException.class);
385 PfKey key = validated.makeKey(this);
386 assertEquals(MY_TO_STRING, key.toString());
390 public String toString() {
394 private static class MyValid extends Validated {
395 private boolean valid;
398 public MyValid(int index, boolean valid) {
404 public PfValidationResult validate(PfValidationResult result) {
406 this.addError(this, "index." + index, result, "wrong value");
413 public String toString() {
418 private static class MyConcept extends PfConceptKey {
419 private static final long serialVersionUID = 1L;
421 private boolean valid;
424 public MyConcept(int index, boolean valid) {
430 public PfValidationResult validate(PfValidationResult result) {
432 new Validated().addError(this, "index." + index, result, "wrong value");
439 public String toString() {