64612238ba803dad0e62c01a36496c51048734dd
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / ValidatedTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
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;
29
30 import java.util.Arrays;
31 import java.util.Iterator;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Map;
35 import org.junit.Before;
36 import org.junit.Test;
37
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";
51
52     private Validated validated;
53
54     @Before
55     public void setUp() {
56         validated = new Validated();
57     }
58
59     @Test
60     public void testValidate() {
61         assertThatThrownBy(() -> validated.validate(null)).isInstanceOf(NullPointerException.class);
62
63         PfValidationResult result = new PfValidationResult();
64         assertSame(result, validated.validate(result));
65         assertTrue(result.isValid());
66         assertEquals(0, result.getMessageList().size());
67     }
68
69     @Test
70     public void testValidateNotNull() {
71         PfValidationResult result = new PfValidationResult();
72
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);
80
81         // null text
82         result = validated.validateNotNull(this, NULL_FIELD, null, result);
83
84         // invalid text
85         result = validated.validateNotNull(this, INVALID_FIELD, "!!!", result);
86
87         // valid text
88         result = validated.validateNotNull(this, VALID_FIELD, VALID_VALUE, result);
89
90         // different value
91         result = validated.validateNotNull(this, VALID_FIELD, Integer.valueOf(10), result);
92
93         assertFalse(result.isValid());
94         assertEquals(1, result.getMessageList().size());
95
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"));
101     }
102
103     @Test
104     public void testValidateNotNullConceptKey() {
105         PfValidationResult result = new PfValidationResult();
106
107         // null key
108         PfConceptKey key = new PfConceptKey();
109         key.setVersion(VERSION);
110         result = validated.validateNotNull(key, result);
111
112         // null value
113         key = new PfConceptKey();
114         key.setName(MY_NAME);
115         result = validated.validateNotNull(key, result);
116
117         // both null
118         key = new PfConceptKey();
119         result = validated.validateNotNull(key, result);
120
121         assertFalse(result.isValid());
122         assertEquals(4, result.getMessageList().size());
123
124         // valid key & value
125         key = new PfConceptKey();
126         key.setName(MY_NAME);
127         key.setVersion(VERSION);
128         result = validated.validateNotNull(key, result);
129
130         // no change
131         assertFalse(result.isValid());
132         assertEquals(4, result.getMessageList().size());
133
134         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
135
136         // check null key
137         PfValidationMessage msg = it.next();
138         assertEquals(PfConceptKey.class.getName(), msg.getObservedClass());
139         assertTrue(msg.getMessage().contains("name invalid-null"));
140
141         // check null value
142         msg = it.next();
143         assertEquals(PfConceptKey.class.getName(), msg.getObservedClass());
144         assertTrue(msg.getMessage().contains("version invalid-null"));
145
146         // check both null
147         msg = it.next();
148         assertEquals(PfConceptKey.class.getName(), msg.getObservedClass());
149         assertTrue(msg.getMessage().contains("name invalid-null"));
150         assertTrue(it.next().getMessage().contains("version invalid-null"));
151
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);
156     }
157
158     @Test
159     public void testValidateText() {
160         PfValidationResult result = new PfValidationResult();
161
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);
171
172         // null text
173         result = validated.validateText(this, NULL_FIELD, null, WORD_PAT, result);
174
175         // invalid text
176         result = validated.validateText(this, INVALID_FIELD, "!!!", WORD_PAT, result);
177
178         // valid text
179         result = validated.validateText(this, VALID_FIELD, VALID_VALUE, WORD_PAT, result);
180
181         assertFalse(result.isValid());
182         assertEquals(1, result.getMessageList().size());
183
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"));
189     }
190
191     @Test
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());
197
198         Map<String, Integer> map = new LinkedHashMap<>();
199
200         // null key
201         map.put(null, 10);
202
203         // null value
204         map.put("abc", null);
205
206         // valid key & value
207         map.put("def", 11);
208
209
210         result = validated.validatePropertiesNotNull(this, PROPS_FIELD, map, result);
211
212         assertFalse(result.isValid());
213         assertEquals(2, result.getMessageList().size());
214
215         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
216
217         // check null key
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"));
222
223         // check null value
224         msg = it.next();
225         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
226         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
227         assertTrue(msg.getMessage().contains("props.abc invalid-null"));
228
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);
236     }
237
238     @Test
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());
244
245         final List<String> lst = Arrays.asList("abc", null, "def", null);
246
247         result = validated.validateCollectionNotNull(this, COLLECTION_FIELD, lst, result);
248
249         assertFalse(result.isValid());
250         assertEquals(2, result.getMessageList().size());
251
252         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
253
254         // check first item
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"));
259
260         // check null value
261         msg = it.next();
262         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
263         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
264         assertTrue(msg.getMessage().contains("coll.3 invalid-null"));
265
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);
273     }
274
275     @Test
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());
281
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);
285
286         assertFalse(result.isValid());
287         assertEquals(2, result.getMessageList().size());
288
289         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
290
291         // check first item
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"));
296
297         // check null value
298         msg = it.next();
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"));
302
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);
310     }
311
312     @Test
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());
318
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);
322
323         assertFalse(result.isValid());
324         assertEquals(2, result.getMessageList().size());
325
326         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
327
328         // check first item
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"));
333
334         // check null value
335         msg = it.next();
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"));
339
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);
347     }
348
349     @Test
350     public void testAddError() {
351         final PfValidationResult result = new PfValidationResult();
352         final PfValidationResult result2 = result;
353
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);
360
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");
364
365         assertFalse(result.isValid());
366         assertEquals(2, result.getMessageList().size());
367
368         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
369
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"));
374
375         msg = it.next();
376         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
377         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
378         assertTrue(msg.getMessage().contains("validField invalid-error-B"));
379     }
380
381     @Test
382     public void testMakeKey() {
383         assertThatThrownBy(() -> validated.makeKey(null)).isInstanceOf(NullPointerException.class);
384
385         PfKey key = validated.makeKey(this);
386         assertEquals(MY_TO_STRING, key.toString());
387     }
388
389     @Override
390     public String toString() {
391         return MY_TO_STRING;
392     }
393
394     private static class MyValid extends Validated {
395         private boolean valid;
396         private int index;
397
398         public MyValid(int index, boolean valid) {
399             this.index = index;
400             this.valid = valid;
401         }
402
403         @Override
404         public PfValidationResult validate(PfValidationResult result) {
405             if (!valid) {
406                 this.addError(this, "index." + index, result, "wrong value");
407             }
408
409             return result;
410         }
411
412         @Override
413         public String toString() {
414             return MY_TO_STRING;
415         }
416     }
417
418     private static class MyConcept extends PfConceptKey {
419         private static final long serialVersionUID = 1L;
420
421         private boolean valid;
422         private int index;
423
424         public MyConcept(int index, boolean valid) {
425             this.index = index;
426             this.valid = valid;
427         }
428
429         @Override
430         public PfValidationResult validate(PfValidationResult result) {
431             if (!valid) {
432                 new Validated().addError(this, "index." + index, result, "wrong value");
433             }
434
435             return result;
436         }
437
438         @Override
439         public String toString() {
440             return MY_TO_STRING;
441         }
442     }
443 }