Add PolicyIdentOptVersion
[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  * ================================================================================
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.Arrays;
30 import java.util.Iterator;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Map;
34 import org.junit.Before;
35 import org.junit.Test;
36
37 public class ValidatedTest {
38     private static final String ERROR_MESSAGE = "error message";
39     private static final String COLLECTION_FIELD = "coll";
40     private static final String VALID_VALUE = "abc123";
41     private static final String PROPS_FIELD = "props";
42     private static final String MY_NAME = "my.name";
43     private static final String VALID_FIELD = "validField";
44     private static final String INVALID_FIELD = "invalidField";
45     private static final String NULL_FIELD = "nullField";
46     private static final String WORD_PAT = "\\w*";
47     private static final String MY_TO_STRING = "[some text]";
48     private static final String VERSION = "1.2.3";
49
50     private Validated validated;
51
52     @Before
53     public void setUp() {
54         validated = new Validated();
55     }
56
57     @Test
58     public void testValidate() {
59         assertThatThrownBy(() -> validated.validate(null)).isInstanceOf(NullPointerException.class);
60
61         PfValidationResult result = new PfValidationResult();
62         assertSame(result, validated.validate(result));
63         assertTrue(result.isValid());
64         assertEquals(0, result.getMessageList().size());
65     }
66
67     @Test
68     public void testValidateNotNull() {
69         PfValidationResult result = new PfValidationResult();
70
71         final PfValidationResult result2 = result;
72         assertThatThrownBy(() -> validated.validateNotNull(null, VALID_FIELD, VALID_VALUE, result2))
73                         .isInstanceOf(NullPointerException.class);
74         assertThatThrownBy(() -> validated.validateNotNull(this, null, VALID_VALUE, result2))
75                         .isInstanceOf(NullPointerException.class);
76         assertThatThrownBy(() -> validated.validateNotNull(this, VALID_FIELD, VALID_VALUE, null))
77                         .isInstanceOf(NullPointerException.class);
78
79         // null text
80         result = validated.validateNotNull(this, NULL_FIELD, null, result);
81
82         // invalid text
83         result = validated.validateNotNull(this, INVALID_FIELD, "!!!", result);
84
85         // valid text
86         result = validated.validateNotNull(this, VALID_FIELD, VALID_VALUE, result);
87
88         // different value
89         result = validated.validateNotNull(this, VALID_FIELD, Integer.valueOf(10), result);
90
91         assertFalse(result.isValid());
92         assertEquals(1, result.getMessageList().size());
93
94         // check result for null text
95         PfValidationMessage msg = result.getMessageList().get(0);
96         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
97         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
98         assertTrue(msg.getMessage().contains("nullField invalid-null"));
99     }
100
101     @Test
102     public void testValidateNotNullConceptKey() {
103         PfValidationResult result = new PfValidationResult();
104
105         // null key
106         PfConceptKey key = new PfConceptKey();
107         key.setVersion(VERSION);
108         result = validated.validateNotNull(key, result);
109
110         // null value
111         key = new PfConceptKey();
112         key.setName(MY_NAME);
113         result = validated.validateNotNull(key, result);
114
115         // both null
116         key = new PfConceptKey();
117         result = validated.validateNotNull(key, result);
118
119         assertFalse(result.isValid());
120         assertEquals(4, result.getMessageList().size());
121
122         // valid key & value
123         key = new PfConceptKey();
124         key.setName(MY_NAME);
125         key.setVersion(VERSION);
126         result = validated.validateNotNull(key, result);
127
128         // no change
129         assertFalse(result.isValid());
130         assertEquals(4, result.getMessageList().size());
131
132         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
133
134         // check null key
135         PfValidationMessage msg = it.next();
136         assertEquals(PfConceptKey.class.getName(), msg.getObservedClass());
137         assertTrue(msg.getMessage().contains("name invalid-null"));
138
139         // check null value
140         msg = it.next();
141         assertEquals(PfConceptKey.class.getName(), msg.getObservedClass());
142         assertTrue(msg.getMessage().contains("version invalid-null"));
143
144         // check both null
145         msg = it.next();
146         assertEquals(PfConceptKey.class.getName(), msg.getObservedClass());
147         assertTrue(msg.getMessage().contains("name invalid-null"));
148         assertTrue(it.next().getMessage().contains("version invalid-null"));
149
150         final PfConceptKey key2 = key;
151         assertThatThrownBy(() -> validated.validateNotNull(key2, null)).isInstanceOf(NullPointerException.class);
152         assertThatThrownBy(() -> validated.validateNotNull(null, new PfValidationResult()))
153                         .isInstanceOf(NullPointerException.class);
154     }
155
156     @Test
157     public void testValidateText() {
158         PfValidationResult result = new PfValidationResult();
159
160         final PfValidationResult result2 = result;
161         assertThatThrownBy(() -> validated.validateText(null, VALID_FIELD, VALID_VALUE, WORD_PAT, result2))
162                         .isInstanceOf(NullPointerException.class);
163         assertThatThrownBy(() -> validated.validateText(this, null, VALID_VALUE, WORD_PAT, result2))
164                         .isInstanceOf(NullPointerException.class);
165         assertThatThrownBy(() -> validated.validateText(this, VALID_FIELD, VALID_VALUE, null, result2))
166                         .isInstanceOf(NullPointerException.class);
167         assertThatThrownBy(() -> validated.validateText(this, VALID_FIELD, VALID_VALUE, WORD_PAT, null))
168                         .isInstanceOf(NullPointerException.class);
169
170         // null text
171         result = validated.validateText(this, NULL_FIELD, null, WORD_PAT, result);
172
173         // invalid text
174         result = validated.validateText(this, INVALID_FIELD, "!!!", WORD_PAT, result);
175
176         // valid text
177         result = validated.validateText(this, VALID_FIELD, VALID_VALUE, WORD_PAT, result);
178
179         assertFalse(result.isValid());
180         assertEquals(1, result.getMessageList().size());
181
182         // check result for invalid text
183         PfValidationMessage msg = result.getMessageList().get(0);
184         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
185         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
186         assertTrue(msg.getMessage().contains("invalidField invalid-parameter invalidField"));
187     }
188
189     @Test
190     public void testValidatePropertiesNotNull() {
191         PfValidationResult result = new PfValidationResult();
192         result = validated.validatePropertiesNotNull(this, "properties", null, result);
193         assertTrue(result.isValid());
194         assertEquals(0, result.getMessageList().size());
195
196         Map<String, Integer> map = new LinkedHashMap<>();
197
198         // null key
199         map.put(null, 10);
200
201         // null value
202         map.put("abc", null);
203
204         // valid key & value
205         map.put("def", 11);
206
207
208         result = validated.validatePropertiesNotNull(this, PROPS_FIELD, map, result);
209
210         assertFalse(result.isValid());
211         assertEquals(2, result.getMessageList().size());
212
213         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
214
215         // check null key
216         PfValidationMessage msg = it.next();
217         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
218         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
219         assertTrue(msg.getMessage().contains("props.null invalid-null"));
220
221         // check null value
222         msg = it.next();
223         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
224         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
225         assertTrue(msg.getMessage().contains("props.abc invalid-null"));
226
227         final PfValidationResult result2 = result;
228         assertThatThrownBy(() -> validated.validatePropertiesNotNull(null, PROPS_FIELD, map, result2))
229                         .isInstanceOf(NullPointerException.class);
230         assertThatThrownBy(() -> validated.validatePropertiesNotNull(this, null, map, result2))
231                         .isInstanceOf(NullPointerException.class);
232         assertThatThrownBy(() -> validated.validatePropertiesNotNull(this, PROPS_FIELD, map, null))
233                         .isInstanceOf(NullPointerException.class);
234     }
235
236     @Test
237     public void testValidateCollectionNotNull() {
238         PfValidationResult result = new PfValidationResult();
239         result = validated.validateCollectionNotNull(this, "collection", null, result);
240         assertTrue(result.isValid());
241         assertEquals(0, result.getMessageList().size());
242
243         final List<String> lst = Arrays.asList("abc", null, "def", null);
244
245         result = validated.validateCollectionNotNull(this, COLLECTION_FIELD, lst, result);
246
247         assertFalse(result.isValid());
248         assertEquals(2, result.getMessageList().size());
249
250         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
251
252         // check first item
253         PfValidationMessage msg = it.next();
254         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
255         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
256         assertTrue(msg.getMessage().contains("coll.1 invalid-null"));
257
258         // check null value
259         msg = it.next();
260         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
261         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
262         assertTrue(msg.getMessage().contains("coll.3 invalid-null"));
263
264         final PfValidationResult result2 = result;
265         assertThatThrownBy(() -> validated.validateCollectionNotNull(null, COLLECTION_FIELD, lst, result2))
266                         .isInstanceOf(NullPointerException.class);
267         assertThatThrownBy(() -> validated.validateCollectionNotNull(this, null, lst, result2))
268                         .isInstanceOf(NullPointerException.class);
269         assertThatThrownBy(() -> validated.validateCollectionNotNull(this, COLLECTION_FIELD, lst, null))
270                         .isInstanceOf(NullPointerException.class);
271     }
272
273     @Test
274     public void testValidateCollection() {
275         PfValidationResult result = new PfValidationResult();
276         result = validated.validateCollection(this, "collection", null, result);
277         assertTrue(result.isValid());
278         assertEquals(0, result.getMessageList().size());
279
280         List<MyValid> lst = Arrays.asList(new MyValid(0, false), new MyValid(1, true), null, new MyValid(2, false),
281                         new MyValid(3, true));
282         result = validated.validateCollection(this, COLLECTION_FIELD, lst, result);
283
284         assertFalse(result.isValid());
285         assertEquals(2, result.getMessageList().size());
286
287         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
288
289         // check first item
290         PfValidationMessage msg = it.next();
291         assertEquals(MyValid.class.getName().replace('$', '.'), msg.getObservedClass());
292         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
293         assertTrue(msg.getMessage().contains("index.0 invalid-wrong value"));
294
295         // check null value
296         msg = it.next();
297         assertEquals(MyValid.class.getName().replace('$', '.'), msg.getObservedClass());
298         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
299         assertTrue(msg.getMessage().contains("index.2 invalid-wrong value"));
300
301         final PfValidationResult result2 = result;
302         assertThatThrownBy(() -> validated.validateCollection(null, COLLECTION_FIELD, lst, result2))
303                         .isInstanceOf(NullPointerException.class);
304         assertThatThrownBy(() -> validated.validateCollection(this, null, lst, result2))
305                         .isInstanceOf(NullPointerException.class);
306         assertThatThrownBy(() -> validated.validateCollection(this, COLLECTION_FIELD, lst, null))
307                         .isInstanceOf(NullPointerException.class);
308     }
309
310     @Test
311     public void testValidateConceptCollection() {
312         PfValidationResult result = new PfValidationResult();
313         result = validated.validateConceptCollection(this, "collection", null, result);
314         assertTrue(result.isValid());
315         assertEquals(0, result.getMessageList().size());
316
317         List<MyConcept> lst = Arrays.asList(new MyConcept(0, false), new MyConcept(1, true), null,
318                         new MyConcept(2, false), new MyConcept(3, true));
319         result = validated.validateConceptCollection(this, COLLECTION_FIELD, lst, result);
320
321         assertFalse(result.isValid());
322         assertEquals(2, result.getMessageList().size());
323
324         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
325
326         // check first item
327         PfValidationMessage msg = it.next();
328         assertEquals(MyConcept.class.getName().replace('$', '.'), msg.getObservedClass());
329         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
330         assertTrue(msg.getMessage().contains("index.0 invalid-wrong value"));
331
332         // check null value
333         msg = it.next();
334         assertEquals(MyConcept.class.getName().replace('$', '.'), msg.getObservedClass());
335         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
336         assertTrue(msg.getMessage().contains("index.2 invalid-wrong value"));
337
338         final PfValidationResult result2 = result;
339         assertThatThrownBy(() -> validated.validateConceptCollection(null, COLLECTION_FIELD, lst, result2))
340                         .isInstanceOf(NullPointerException.class);
341         assertThatThrownBy(() -> validated.validateConceptCollection(this, null, lst, result2))
342                         .isInstanceOf(NullPointerException.class);
343         assertThatThrownBy(() -> validated.validateConceptCollection(this, COLLECTION_FIELD, lst, null))
344                         .isInstanceOf(NullPointerException.class);
345     }
346
347     @Test
348     public void testAddError() {
349         final PfValidationResult result = new PfValidationResult();
350         final PfValidationResult result2 = result;
351
352         assertThatThrownBy(() -> validated.addError(null, VALID_FIELD, result2, ERROR_MESSAGE))
353                         .isInstanceOf(NullPointerException.class);
354         assertThatThrownBy(() -> validated.addError(this, null, result2, ERROR_MESSAGE))
355                         .isInstanceOf(NullPointerException.class);
356         assertThatThrownBy(() -> validated.addError(this, VALID_FIELD, null, ERROR_MESSAGE))
357                         .isInstanceOf(NullPointerException.class);
358
359         validated.addError(this, VALID_FIELD, result, "error-A");
360         validated.addError(this, VALID_FIELD, result, null);
361         validated.addError(this, VALID_FIELD, result, "error-B");
362
363         assertFalse(result.isValid());
364         assertEquals(2, result.getMessageList().size());
365
366         Iterator<PfValidationMessage> it = result.getMessageList().iterator();
367
368         PfValidationMessage msg = it.next();
369         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
370         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
371         assertTrue(msg.getMessage().contains("validField invalid-error-A"));
372
373         msg = it.next();
374         assertEquals(ValidatedTest.class.getName(), msg.getObservedClass());
375         assertEquals(MY_TO_STRING, msg.getObservedKey().toString());
376         assertTrue(msg.getMessage().contains("validField invalid-error-B"));
377     }
378
379     @Test
380     public void testMakeKey() {
381         assertThatThrownBy(() -> validated.makeKey(null)).isInstanceOf(NullPointerException.class);
382
383         PfKey key = validated.makeKey(this);
384         assertEquals(MY_TO_STRING, key.toString());
385     }
386
387     @Override
388     public String toString() {
389         return MY_TO_STRING;
390     }
391
392     private static class MyValid extends Validated {
393         private boolean valid;
394         private int index;
395
396         public MyValid(int index, boolean valid) {
397             this.index = index;
398             this.valid = valid;
399         }
400
401         @Override
402         public PfValidationResult validate(PfValidationResult result) {
403             if (!valid) {
404                 this.addError(this, "index." + index, result, "wrong value");
405             }
406
407             return result;
408         }
409
410         @Override
411         public String toString() {
412             return MY_TO_STRING;
413         }
414     }
415
416     private static class MyConcept extends PfConceptKey {
417         private static final long serialVersionUID = 1L;
418
419         private boolean valid;
420         private int index;
421
422         public MyConcept(int index, boolean valid) {
423             this.index = index;
424             this.valid = valid;
425         }
426
427         @Override
428         public PfValidationResult validate(PfValidationResult result) {
429             if (!valid) {
430                 new Validated().addError(this, "index." + index, result, "wrong value");
431             }
432
433             return result;
434         }
435
436         @Override
437         public String toString() {
438             return MY_TO_STRING;
439         }
440     }
441 }