Sonar Fixes policy/models, removing model-yaml
[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-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
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         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);
157     }
158
159     @Test
160     public void testValidateText() {
161         PfValidationResult result = new PfValidationResult();
162
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);
172
173         // null text
174         result = validated.validateText(this, NULL_FIELD, null, WORD_PAT, result);
175
176         // invalid text
177         result = validated.validateText(this, INVALID_FIELD, "!!!", WORD_PAT, result);
178
179         // valid text
180         result = validated.validateText(this, VALID_FIELD, VALID_VALUE, WORD_PAT, result);
181
182         assertFalse(result.isValid());
183         assertEquals(1, result.getMessageList().size());
184
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"));
190     }
191
192     @Test
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());
198
199         Map<String, Integer> map = new LinkedHashMap<>();
200
201         // null key
202         map.put(null, 10);
203
204         // null value
205         map.put("abc", null);
206
207         // valid key & value
208         map.put("def", 11);
209
210
211         result = validated.validatePropertiesNotNull(this, PROPS_FIELD, map, result);
212
213         assertFalse(result.isValid());
214         assertEquals(2, result.getMessageList().size());
215
216         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
217
218         // check null key
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"));
223
224         // check null value
225         msg = it.next();
226         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
227         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
228         assertTrue(msg.getMessage().contains("props.abc invalid-null"));
229
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);
237     }
238
239     @Test
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());
245
246         final List<String> lst = Arrays.asList("abc", null, "def", null);
247
248         result = validated.validateCollectionNotNull(this, COLLECTION_FIELD, lst, result);
249
250         assertFalse(result.isValid());
251         assertEquals(2, result.getMessageList().size());
252
253         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
254
255         // check first item
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"));
260
261         // check null value
262         msg = it.next();
263         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
264         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
265         assertTrue(msg.getMessage().contains("coll.3 invalid-null"));
266
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);
274     }
275
276     @Test
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());
282
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);
286
287         assertFalse(result.isValid());
288         assertEquals(2, result.getMessageList().size());
289
290         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
291
292         // check first item
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"));
297
298         // check null value
299         msg = it.next();
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"));
303
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);
311     }
312
313     @Test
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());
319
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);
323
324         assertFalse(result.isValid());
325         assertEquals(2, result.getMessageList().size());
326
327         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
328
329         // check first item
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"));
334
335         // check null value
336         msg = it.next();
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"));
340
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);
348     }
349
350     @Test
351     public void testAddError() {
352         final PfValidationResult result = new PfValidationResult();
353         final PfValidationResult result2 = result;
354
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);
361
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");
365
366         assertFalse(result.isValid());
367         assertEquals(2, result.getMessageList().size());
368
369         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
370
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"));
375
376         msg = it.next();
377         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
378         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
379         assertTrue(msg.getMessage().contains("validField invalid-error-B"));
380     }
381
382     @Test
383     public void testMakeKey() {
384         assertThatThrownBy(() -> validated.makeKey(null)).isInstanceOf(NullPointerException.class);
385
386         PfKey key = validated.makeKey(this);
387         assertEquals(MY_TO_STRING, key.toString());
388     }
389
390     @Override
391     public String toString() {
392         return MY_TO_STRING;
393     }
394
395     private static class MyValid extends Validated {
396         private boolean valid;
397         private int index;
398
399         public MyValid(int index, boolean valid) {
400             this.index = index;
401             this.valid = valid;
402         }
403
404         @Override
405         public PfValidationResult validate(PfValidationResult result) {
406             if (!valid) {
407                 this.addError(this, "index." + index, result, "wrong value");
408             }
409
410             return result;
411         }
412
413         @Override
414         public String toString() {
415             return MY_TO_STRING;
416         }
417     }
418
419     private static class MyConcept extends PfConceptKey {
420         private static final long serialVersionUID = 1L;
421
422         private boolean valid;
423         private int index;
424
425         public MyConcept(int index, boolean valid) {
426             this.index = index;
427             this.valid = valid;
428         }
429
430         @Override
431         public PfValidationResult validate(PfValidationResult result) {
432             if (!valid) {
433                 new Validated().addError(this, "index." + index, result, "wrong value");
434             }
435
436             return result;
437         }
438
439         @Override
440         public String toString() {
441             return MY_TO_STRING;
442         }
443     }
444 }