2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.models.base;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
28 import jakarta.validation.Valid;
29 import java.io.Serial;
30 import java.util.concurrent.atomic.AtomicBoolean;
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;
42 public class PfValidatorTest {
43 private static final String KEY_FIELD = "key";
45 private static final String STRING_VALUE = "abc";
47 private PfValidator validator;
51 validator = new PfValidator();
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();
62 assertThat(validator.validateTop("", data).getResult()).contains("strValue", "null");
66 public void testVerPfMin() {
67 PfMinChecker data = new PfMinChecker();
69 assertThat(validator.validateTop("", data).getResult()).isNull();
72 assertThat(validator.validateTop("", data).getResult()).isNull();
75 assertThat(validator.validateTop("", data).getResult()).isNull();
77 data.intValue = STRING_VALUE;
78 assertThat(validator.validateTop("", data).getResult()).isNull();
81 assertThat(validator.validateTop("", data).getResult()).contains("intValue", "-1");
85 public void testVerCascadeBeanValidationResultStringObject() {
86 CascadeChecker checker = new CascadeChecker();
87 checker.plain = new StdAnnotation();
90 checker.plain.strValue = STRING_VALUE;
91 BeanValidationResult result = new BeanValidationResult("", this);
92 assertThat(validator.verCascade(result, "", checker)).isTrue();
95 checker.plain.strValue = null;
97 result = new BeanValidationResult("", this);
98 assertThat(validator.verCascade(result, "", checker.plain)).isFalse();
99 assertThat(result.getResult()).contains("null");
101 result = new BeanValidationResult("", this);
102 assertThat(validator.verCascade(result, "", checker)).isFalse();
103 assertThat(result.getResult()).contains("null").doesNotContain("plain");
105 // validator returns null result - should be treated as valid
106 checker = new CascadeChecker() {
108 public BeanValidationResult validate(@NonNull String fieldName) {
112 checker.plain = new StdAnnotation();
113 result = new BeanValidationResult("", this);
114 assertThat(validator.verCascade(result, "", checker)).isTrue();
118 public void testVerKey() throws CoderException {
119 FullKeyAnnot data = new FullKeyAnnot();
122 data.key = STRING_VALUE;
123 assertThat(validator.validateTop("", data).getResult()).isNull();
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");
130 // invalid version - should invoke verCascade() which will invoke key.validate()
131 data.key = new StandardCoder().decode("{'name':'abc', 'version':'xyzzy'}".replace('\'', '"'),
133 assertThat(validator.validateTop("", data).getResult())
134 .contains(KEY_FIELD, "version", "xyzzy", "regular expression").doesNotContain("name");
136 // not a PfKeyImpl - should not check individual fields
137 PfKey pfkey = mock(PfKey.class);
139 assertThat(validator.validateTop("", data).getResult()).isNull();
141 when(pfkey.isNullKey()).thenReturn(true);
142 assertThat(validator.validateTop("", data).getResult()).contains(KEY_FIELD, Validated.IS_A_NULL_KEY);
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");
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);
154 // null name, invalid version - should get two messages
155 data.key = new StandardCoder().decode("{'name':'NULL', 'version':'xyzzy'}".replace('\'', '"'),
157 assertThat(validator.validateTop("", data).getResult()).contains(KEY_FIELD, "name", "null", "version", "xyzzy",
158 "regular expression");
161 * Tests with all flags set to "false" (i.e., no validations).
164 EmptyKeyAnnot data2 = new EmptyKeyAnnot();
166 // build a key that is totally invalid
167 AtomicBoolean called = new AtomicBoolean();
169 data2.key = new PfConceptKey() {
171 private static final long serialVersionUID = 1L;
174 public BeanValidationResult validate(@NonNull String fieldName) {
180 // should be ok, since no validations are performed
181 assertThat(validator.validateTop("", data2).getResult()).isNull();
182 assertThat(called.get()).isFalse();
186 public void testXlateObject() {
187 assertThat(validator.xlate(null)).isNull();
188 assertThat(validator.xlate("hello")).isEqualTo("hello");
190 PfConceptKey key = new PfConceptKey("hello", "1.2.3");
191 assertThat(validator.xlate(key)).isEqualTo("hello:1.2.3");
194 public static class StdAnnotation {
197 private String strValue;
200 public static class PfMinChecker {
202 @PfMin(value = 5, allowed = -2)
203 private Object intValue;
206 public static class CascadeChecker extends Validated {
209 private StdAnnotation plain;
212 public BeanValidationResult validate(@NonNull String fieldName) {
213 // directly validates "plain"
214 return new PfValidator().validateTop(fieldName, plain);
218 public static class FullKeyAnnot {
220 @VerifyKey(keyNotNull = true, nameNotNull = true, versionNotNull = true, valid = true)
224 public static class EmptyKeyAnnot {
226 @VerifyKey(keyNotNull = false, nameNotNull = false, versionNotNull = false, valid = false)