8fa757a00eb07fbd902a5fcb685a003dc2e1e53e
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / ValidatedTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 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.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28
29 import lombok.AllArgsConstructor;
30 import lombok.NonNull;
31 import org.junit.Test;
32 import org.onap.policy.common.parameters.BeanValidationResult;
33 import org.onap.policy.common.parameters.ObjectValidationResult;
34 import org.onap.policy.common.parameters.ValidationResult;
35 import org.onap.policy.common.parameters.ValidationStatus;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38
39 public class ValidatedTest {
40     private static final @NonNull String MY_FIELD = "myField";
41     private static final @NonNull String Q_KEY = "\"" + Validated.KEY_TOKEN + "\"";
42     private static final @NonNull String Q_VALUE = "\"" + Validated.VALUE_TOKEN + "\"";
43     private static final String NOT_SAME = "not same";
44     private static final String TEXT = "some text";
45     private static final String OTHER = "other text";
46     private static final String NAME = "myKey";
47     private static final String VERSION = "1.0.0";
48
49     @Test
50     public void testAddResult() {
51         BeanValidationResult result = new BeanValidationResult("", this);
52         Validated.addResult(result, MY_FIELD, TEXT, "some message");
53         assertThat(result.getResult()).contains(MY_FIELD).contains(TEXT).contains("some message");
54
55         assertThatThrownBy(() -> Validated.addResult(null, MY_FIELD, TEXT, OTHER))
56                         .isInstanceOf(NullPointerException.class);
57
58         assertThatThrownBy(() -> Validated.addResult(result, null, TEXT, OTHER))
59                         .isInstanceOf(NullPointerException.class);
60
61         assertThatCode(() -> Validated.addResult(result, MY_FIELD, null, OTHER)).doesNotThrowAnyException();
62
63         assertThatThrownBy(() -> Validated.addResult(result, MY_FIELD, TEXT, null))
64                         .isInstanceOf(NullPointerException.class);
65     }
66
67     @Test
68     public void testMakeNullResult() {
69         ValidationResult rnull = Validated.makeNullResult(MY_FIELD, TEXT);
70         assertEquals(MY_FIELD, rnull.getName());
71         assertThat(rnull.getResult()).contains(MY_FIELD).contains(TEXT).contains(Validated.IS_NULL);
72         assertFalse(rnull.isValid());
73
74         assertThatThrownBy(() -> Validated.makeNullResult(null, TEXT)).isInstanceOf(NullPointerException.class);
75
76         assertThatCode(() -> Validated.makeNullResult(MY_FIELD, null)).doesNotThrowAnyException();
77     }
78
79     @Test
80     public void testValidateKeyNotNull() throws CoderException {
81         BeanValidationResult result = new BeanValidationResult("", this);
82         Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey(NAME, VERSION));
83         assertThat(result.getResult()).isNull();
84
85         result = new BeanValidationResult("", this);
86         Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey(NAME, PfConceptKey.NULL_KEY_VERSION));
87         assertThat(result.getResult()).isNull();
88
89         result = new BeanValidationResult("", this);
90         Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey(PfConceptKey.NULL_KEY_NAME, VERSION));
91         assertThat(result.getResult()).isNull();
92
93         // key is null
94         result = new BeanValidationResult("", this);
95         Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey());
96         assertThat(result.getResult()).contains(MY_FIELD, Validated.IS_A_NULL_KEY)
97                         .doesNotContain("\"name\"", "\"version\"");
98
99         /*
100          * Key is not null, but key.validate() should fail due to an invalid version.
101          * Note: have to create the key by decoding from json, as the class will prevent
102          * an invalid version from being assigned.
103          */
104         PfConceptKey key = new StandardCoder().decode("{'name':'myKey','version':'bogus'}".replace('\'', '"'),
105                         PfConceptKey.class);
106         result = new BeanValidationResult("", this);
107         Validated.validateKeyNotNull(result, MY_FIELD, key);
108         assertThat(result.getResult()).contains(MY_FIELD, "version", "does not match regular expression");
109
110         BeanValidationResult result2 = new BeanValidationResult("", this);
111
112         // null parameter tests
113         PfConceptKey conceptKey = new PfConceptKey();
114         assertThatThrownBy(() -> Validated.validateKeyNotNull(result2, null, conceptKey))
115                         .isInstanceOf(NullPointerException.class);
116
117         assertThatCode(() -> Validated.validateKeyNotNull(result2, MY_FIELD, null)).doesNotThrowAnyException();
118     }
119
120     @Test
121     public void testValidateKeyVersionNotNull() {
122         BeanValidationResult result = new BeanValidationResult("", this);
123         Validated.validateKeyVersionNotNull(result, MY_FIELD, null);
124         assertThat(result.getResult()).isNull();
125
126         result = new BeanValidationResult("", this);
127         Validated.validateKeyVersionNotNull(result, MY_FIELD, new PfConceptKey(NAME, VERSION));
128         assertThat(result.getResult()).isNull();
129
130         result = new BeanValidationResult("", this);
131         Validated.validateKeyVersionNotNull(result, MY_FIELD, new PfConceptKey(NAME, PfConceptKey.NULL_KEY_VERSION));
132         assertThat(result.getResult()).contains(MY_FIELD).contains("version").contains(Validated.IS_NULL);
133
134         BeanValidationResult result2 = new BeanValidationResult("", this);
135         PfConceptKey conceptKey = new PfConceptKey();
136         assertThatThrownBy(() -> Validated.validateKeyVersionNotNull(result2, null, conceptKey))
137                         .isInstanceOf(NullPointerException.class);
138
139         assertThatCode(() -> Validated.validateKeyVersionNotNull(result2, MY_FIELD, null)).doesNotThrowAnyException();
140     }
141
142     @Test
143     public void testGetKeyId() {
144         // not a key field - should just use the given value
145         BeanValidationResult result = new BeanValidationResult("", this);
146         Validated.addResult(result, MY_FIELD, TEXT, "some message");
147         assertThat(result.getResult()).contains(MY_FIELD).contains(TEXT).contains("some message");
148
149         // repeat with a key field - should use the key's ID
150         result = new BeanValidationResult("", this);
151         Validated.addResult(result, MY_FIELD, new PfConceptKey(NAME, VERSION), "some message");
152         assertThat(result.getResult()).contains(MY_FIELD).contains("myKey:1.0.0").contains("some message");
153     }
154
155     @AllArgsConstructor
156     private static class MyString extends Validated {
157         private final String text;
158
159         @Override
160         public BeanValidationResult validate(String fieldName) {
161             if (TEXT.equals(text)) {
162                 return null;
163             }
164
165             BeanValidationResult result = new BeanValidationResult(fieldName, this);
166             result.addResult(new ObjectValidationResult(fieldName, text, ValidationStatus.INVALID, NOT_SAME));
167             return result;
168         }
169     }
170 }