Java 17 Upgrade
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfValidatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
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.assertThat;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import jakarta.validation.Valid;
29 import java.io.Serial;
30 import java.util.concurrent.atomic.AtomicBoolean;
31 import lombok.Getter;
32 import lombok.NonNull;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.parameters.BeanValidationResult;
36 import org.onap.policy.common.parameters.annotations.NotNull;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.models.base.validation.annotations.PfMin;
40 import org.onap.policy.models.base.validation.annotations.VerifyKey;
41
42 public class PfValidatorTest {
43     private static final String KEY_FIELD = "key";
44
45     private static final String STRING_VALUE = "abc";
46
47     private PfValidator validator;
48
49     @Before
50     public void setUp() {
51         validator = new PfValidator();
52     }
53
54     @Test
55     public void testAddValidatorsValueValidator() {
56         // verify that standard annotations work
57         StdAnnotation data = new StdAnnotation();
58         data.strValue = STRING_VALUE;
59         assertThat(validator.validateTop("", data).getResult()).isNull();
60
61         data.strValue = null;
62         assertThat(validator.validateTop("", data).getResult()).contains("strValue", "null");
63     }
64
65     @Test
66     public void testVerPfMin() {
67         PfMinChecker data = new PfMinChecker();
68         data.intValue = 10;
69         assertThat(validator.validateTop("", data).getResult()).isNull();
70
71         data.intValue = -2;
72         assertThat(validator.validateTop("", data).getResult()).isNull();
73
74         data.intValue = null;
75         assertThat(validator.validateTop("", data).getResult()).isNull();
76
77         data.intValue = STRING_VALUE;
78         assertThat(validator.validateTop("", data).getResult()).isNull();
79
80         data.intValue = -1;
81         assertThat(validator.validateTop("", data).getResult()).contains("intValue", "-1");
82     }
83
84     @Test
85     public void testVerCascadeBeanValidationResultStringObject() {
86         CascadeChecker checker = new CascadeChecker();
87         checker.plain = new StdAnnotation();
88
89         // valid
90         checker.plain.strValue = STRING_VALUE;
91         BeanValidationResult result = new BeanValidationResult("", this);
92         assertThat(validator.verCascade(result, "", checker)).isTrue();
93
94         // invalid
95         checker.plain.strValue = null;
96
97         result = new BeanValidationResult("", this);
98         assertThat(validator.verCascade(result, "", checker.plain)).isFalse();
99         assertThat(result.getResult()).contains("null");
100
101         result = new BeanValidationResult("", this);
102         assertThat(validator.verCascade(result, "", checker)).isFalse();
103         assertThat(result.getResult()).contains("null").doesNotContain("plain");
104
105         // validator returns null result - should be treated as valid
106         checker = new CascadeChecker() {
107             @Override
108             public BeanValidationResult validate(@NonNull String fieldName) {
109                 return null;
110             }
111         };
112         checker.plain = new StdAnnotation();
113         result = new BeanValidationResult("", this);
114         assertThat(validator.verCascade(result, "", checker)).isTrue();
115     }
116
117     @Test
118     public void testVerKey() throws CoderException {
119         FullKeyAnnot data = new FullKeyAnnot();
120
121         // not a key
122         data.key = STRING_VALUE;
123         assertThat(validator.validateTop("", data).getResult()).isNull();
124
125         // null key
126         data.key = new PfConceptKey();
127         assertThat(validator.validateTop("", data).getResult())
128             .contains(KEY_FIELD, "NULL:0.0.0", Validated.IS_A_NULL_KEY).doesNotContain("name", "version");
129
130         // invalid version - should invoke verCascade() which will invoke key.validate()
131         data.key = new StandardCoder().decode("{'name':'abc', 'version':'xyzzy'}".replace('\'', '"'),
132             PfConceptKey.class);
133         assertThat(validator.validateTop("", data).getResult())
134             .contains(KEY_FIELD, "version", "xyzzy", "regular expression").doesNotContain("name");
135
136         // not a PfKeyImpl - should not check individual fields
137         PfKey pfkey = mock(PfKey.class);
138         data.key = pfkey;
139         assertThat(validator.validateTop("", data).getResult()).isNull();
140
141         when(pfkey.isNullKey()).thenReturn(true);
142         assertThat(validator.validateTop("", data).getResult()).contains(KEY_FIELD, Validated.IS_A_NULL_KEY);
143
144         // null name
145         data.key = new PfConceptKey(PfKey.NULL_KEY_NAME, "2.3.4");
146         assertThat(validator.validateTop("", data).getResult()).contains(KEY_FIELD, "name", "null")
147             .doesNotContain("version", "2.3.4");
148
149         // null version
150         data.key = new PfConceptKey(STRING_VALUE, PfKey.NULL_KEY_VERSION);
151         assertThat(validator.validateTop("", data).getResult()).contains(KEY_FIELD, "version", "null")
152             .doesNotContain("name", STRING_VALUE);
153
154         // null name, invalid version - should get two messages
155         data.key = new StandardCoder().decode("{'name':'NULL', 'version':'xyzzy'}".replace('\'', '"'),
156             PfConceptKey.class);
157         assertThat(validator.validateTop("", data).getResult()).contains(KEY_FIELD, "name", "null", "version", "xyzzy",
158             "regular expression");
159
160         /*
161          * Tests with all flags set to "false" (i.e., no validations).
162          */
163
164         EmptyKeyAnnot data2 = new EmptyKeyAnnot();
165
166         // build a key that is totally invalid
167         AtomicBoolean called = new AtomicBoolean();
168
169         data2.key = new PfConceptKey() {
170             @Serial
171             private static final long serialVersionUID = 1L;
172
173             @Override
174             public BeanValidationResult validate(@NonNull String fieldName) {
175                 called.set(true);
176                 return null;
177             }
178         };
179
180         // should be ok, since no validations are performed
181         assertThat(validator.validateTop("", data2).getResult()).isNull();
182         assertThat(called.get()).isFalse();
183     }
184
185     @Test
186     public void testXlateObject() {
187         assertThat(validator.xlate(null)).isNull();
188         assertThat(validator.xlate("hello")).isEqualTo("hello");
189
190         PfConceptKey key = new PfConceptKey("hello", "1.2.3");
191         assertThat(validator.xlate(key)).isEqualTo("hello:1.2.3");
192     }
193
194     public static class StdAnnotation {
195         @Getter
196         @NotNull
197         private String strValue;
198     }
199
200     public static class PfMinChecker {
201         @Getter
202         @PfMin(value = 5, allowed = -2)
203         private Object intValue;
204     }
205
206     public static class CascadeChecker extends Validated {
207         @Getter
208         @Valid
209         private StdAnnotation plain;
210
211         @Override
212         public BeanValidationResult validate(@NonNull String fieldName) {
213             // directly validates "plain"
214             return new PfValidator().validateTop(fieldName, plain);
215         }
216     }
217
218     public static class FullKeyAnnot {
219         @Getter
220         @VerifyKey(keyNotNull = true, nameNotNull = true, versionNotNull = true, valid = true)
221         private Object key;
222     }
223
224     public static class EmptyKeyAnnot {
225         @Getter
226         @VerifyKey(keyNotNull = false, nameNotNull = false, versionNotNull = false, valid = false)
227         private PfKey key;
228     }
229 }