2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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 PfValidationResult pfValidationResult = new PfValidationResult();
153 final PfConceptKey key2 = key;
154 assertThatThrownBy(() -> validated.validateNotNull(key2, null)).isInstanceOf(NullPointerException.class);
155 assertThatThrownBy(() -> validated.validateNotNull(null, pfValidationResult))
156 .isInstanceOf(NullPointerException.class);
160 public void testValidateText() {
161 PfValidationResult result = new PfValidationResult();
163 final PfValidationResult result2 = result;
164 assertThatThrownBy(() -> validated.validateText(null, VALID_FIELD, VALID_VALUE, WORD_PAT, result2))
165 .isInstanceOf(NullPointerException.class);
166 assertThatThrownBy(() -> validated.validateText(this, null, VALID_VALUE, WORD_PAT, result2))
167 .isInstanceOf(NullPointerException.class);
168 assertThatThrownBy(() -> validated.validateText(this, VALID_FIELD, VALID_VALUE, null, result2))
169 .isInstanceOf(NullPointerException.class);
170 assertThatThrownBy(() -> validated.validateText(this, VALID_FIELD, VALID_VALUE, WORD_PAT, null))
171 .isInstanceOf(NullPointerException.class);
174 result = validated.validateText(this, NULL_FIELD, null, WORD_PAT, result);
177 result = validated.validateText(this, INVALID_FIELD, "!!!", WORD_PAT, result);
180 result = validated.validateText(this, VALID_FIELD, VALID_VALUE, WORD_PAT, result);
182 assertFalse(result.isValid());
183 assertEquals(1, result.getMessageList().size());
185 // check result for invalid text
186 PfValidationMessage msg = result.getMessageList().get(0);
187 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
188 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
189 assertTrue(msg.getMessage().contains("invalidField invalid-parameter invalidField"));
193 public void testValidatePropertiesNotNull() {
194 PfValidationResult result = new PfValidationResult();
195 result = validated.validatePropertiesNotNull(this, "properties", null, result);
196 assertTrue(result.isValid());
197 assertEquals(0, result.getMessageList().size());
199 Map<String, Integer> map = new LinkedHashMap<>();
205 map.put("abc", null);
211 result = validated.validatePropertiesNotNull(this, PROPS_FIELD, map, result);
213 assertFalse(result.isValid());
214 assertEquals(2, result.getMessageList().size());
216 Iterator<PfValidationMessage> it = result.getMessageList().iterator();
219 PfValidationMessage msg = it.next();
220 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
221 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
222 assertTrue(msg.getMessage().contains("props.null invalid-null"));
226 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
227 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
228 assertTrue(msg.getMessage().contains("props.abc invalid-null"));
230 final PfValidationResult result2 = result;
231 assertThatThrownBy(() -> validated.validatePropertiesNotNull(null, PROPS_FIELD, map, result2))
232 .isInstanceOf(NullPointerException.class);
233 assertThatThrownBy(() -> validated.validatePropertiesNotNull(this, null, map, result2))
234 .isInstanceOf(NullPointerException.class);
235 assertThatThrownBy(() -> validated.validatePropertiesNotNull(this, PROPS_FIELD, map, null))
236 .isInstanceOf(NullPointerException.class);
240 public void testValidateCollectionNotNull() {
241 PfValidationResult result = new PfValidationResult();
242 result = validated.validateCollectionNotNull(this, COLLECTION_TEXT, null, result);
243 assertTrue(result.isValid());
244 assertEquals(0, result.getMessageList().size());
246 final List<String> lst = Arrays.asList("abc", null, "def", null);
248 result = validated.validateCollectionNotNull(this, COLLECTION_FIELD, lst, result);
250 assertFalse(result.isValid());
251 assertEquals(2, result.getMessageList().size());
253 Iterator<PfValidationMessage> it = result.getMessageList().iterator();
256 PfValidationMessage msg = it.next();
257 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
258 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
259 assertTrue(msg.getMessage().contains("coll.1 invalid-null"));
263 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
264 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
265 assertTrue(msg.getMessage().contains("coll.3 invalid-null"));
267 final PfValidationResult result2 = result;
268 assertThatThrownBy(() -> validated.validateCollectionNotNull(null, COLLECTION_FIELD, lst, result2))
269 .isInstanceOf(NullPointerException.class);
270 assertThatThrownBy(() -> validated.validateCollectionNotNull(this, null, lst, result2))
271 .isInstanceOf(NullPointerException.class);
272 assertThatThrownBy(() -> validated.validateCollectionNotNull(this, COLLECTION_FIELD, lst, null))
273 .isInstanceOf(NullPointerException.class);
277 public void testValidateCollection() {
278 PfValidationResult result = new PfValidationResult();
279 result = validated.validateCollection(this, COLLECTION_TEXT, null, result);
280 assertTrue(result.isValid());
281 assertEquals(0, result.getMessageList().size());
283 List<MyValid> lst = Arrays.asList(new MyValid(0, false), new MyValid(1, true), null, new MyValid(2, false),
284 new MyValid(3, true));
285 result = validated.validateCollection(this, COLLECTION_FIELD, lst, result);
287 assertFalse(result.isValid());
288 assertEquals(2, result.getMessageList().size());
290 Iterator<PfValidationMessage> it = result.getMessageList().iterator();
293 PfValidationMessage msg = it.next();
294 assertEquals(MyValid.class.getName(), msg.getObservedClass());
295 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
296 assertTrue(msg.getMessage().contains("index.0 invalid-wrong value"));
300 assertEquals(MyValid.class.getName(), msg.getObservedClass());
301 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
302 assertTrue(msg.getMessage().contains("index.2 invalid-wrong value"));
304 final PfValidationResult result2 = result;
305 assertThatThrownBy(() -> validated.validateCollection(null, COLLECTION_FIELD, lst, result2))
306 .isInstanceOf(NullPointerException.class);
307 assertThatThrownBy(() -> validated.validateCollection(this, null, lst, result2))
308 .isInstanceOf(NullPointerException.class);
309 assertThatThrownBy(() -> validated.validateCollection(this, COLLECTION_FIELD, lst, null))
310 .isInstanceOf(NullPointerException.class);
314 public void testValidateConceptCollection() {
315 PfValidationResult result = new PfValidationResult();
316 result = validated.validateConceptCollection(this, COLLECTION_TEXT, null, result);
317 assertTrue(result.isValid());
318 assertEquals(0, result.getMessageList().size());
320 List<MyConcept> lst = Arrays.asList(new MyConcept(0, false), new MyConcept(1, true), null,
321 new MyConcept(2, false), new MyConcept(3, true));
322 result = validated.validateConceptCollection(this, COLLECTION_FIELD, lst, result);
324 assertFalse(result.isValid());
325 assertEquals(2, result.getMessageList().size());
327 Iterator<PfValidationMessage> it = result.getMessageList().iterator();
330 PfValidationMessage msg = it.next();
331 assertEquals(MyConcept.class.getName(), msg.getObservedClass());
332 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
333 assertTrue(msg.getMessage().contains("index.0 invalid-wrong value"));
337 assertEquals(MyConcept.class.getName(), msg.getObservedClass());
338 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
339 assertTrue(msg.getMessage().contains("index.2 invalid-wrong value"));
341 final PfValidationResult result2 = result;
342 assertThatThrownBy(() -> validated.validateConceptCollection(null, COLLECTION_FIELD, lst, result2))
343 .isInstanceOf(NullPointerException.class);
344 assertThatThrownBy(() -> validated.validateConceptCollection(this, null, lst, result2))
345 .isInstanceOf(NullPointerException.class);
346 assertThatThrownBy(() -> validated.validateConceptCollection(this, COLLECTION_FIELD, lst, null))
347 .isInstanceOf(NullPointerException.class);
351 public void testAddError() {
352 final PfValidationResult result = new PfValidationResult();
353 final PfValidationResult result2 = result;
355 assertThatThrownBy(() -> validated.addError(null, VALID_FIELD, result2, ERROR_MESSAGE))
356 .isInstanceOf(NullPointerException.class);
357 assertThatThrownBy(() -> validated.addError(this, null, result2, ERROR_MESSAGE))
358 .isInstanceOf(NullPointerException.class);
359 assertThatThrownBy(() -> validated.addError(this, VALID_FIELD, null, ERROR_MESSAGE))
360 .isInstanceOf(NullPointerException.class);
362 validated.addError(this, VALID_FIELD, result, "error-A");
363 validated.addError(this, VALID_FIELD, result, null);
364 validated.addError(this, VALID_FIELD, result, "error-B");
366 assertFalse(result.isValid());
367 assertEquals(2, result.getMessageList().size());
369 Iterator<PfValidationMessage> it = result.getMessageList().iterator();
371 PfValidationMessage msg = it.next();
372 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
373 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
374 assertTrue(msg.getMessage().contains("validField invalid-error-A"));
377 assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
378 assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
379 assertTrue(msg.getMessage().contains("validField invalid-error-B"));
383 public void testMakeKey() {
384 assertThatThrownBy(() -> validated.makeKey(null)).isInstanceOf(NullPointerException.class);
386 PfKey key = validated.makeKey(this);
387 assertEquals(MY_TO_STRING, key.toString());
391 public String toString() {
395 private static class MyValid extends Validated {
396 private boolean valid;
399 public MyValid(int index, boolean valid) {
405 public PfValidationResult validate(PfValidationResult result) {
407 this.addError(this, "index." + index, result, "wrong value");
414 public String toString() {
419 private static class MyConcept extends PfConceptKey {
420 private static final long serialVersionUID = 1L;
422 private boolean valid;
425 public MyConcept(int index, boolean valid) {
431 public PfValidationResult validate(PfValidationResult result) {
433 new Validated().addError(this, "index." + index, result, "wrong value");
440 public String toString() {