2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.models.base;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
27 import java.util.concurrent.atomic.AtomicBoolean;
28 import javax.validation.Valid;
30 import lombok.NonNull;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.common.parameters.BeanValidationResult;
34 import org.onap.policy.common.parameters.annotations.NotNull;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.models.base.validation.annotations.PfMin;
38 import org.onap.policy.models.base.validation.annotations.VerifyKey;
40 public class PfValidatorTest {
41 private static final String KEY_FIELD = "key";
43 private static final String STRING_VALUE = "abc";
45 private PfValidator validator;
49 validator = new PfValidator();
53 public void testAddValidatorsValueValidator() {
54 // verify that standard annotations work
55 StdAnnotation data = new StdAnnotation();
56 data.strValue = STRING_VALUE;
57 assertThat(validator.validateTop("", data).getResult()).isNull();
60 assertThat(validator.validateTop("", data).getResult()).contains("strValue", "null");
64 public void testVerPfMin() {
65 PfMinChecker data = new PfMinChecker();
67 assertThat(validator.validateTop("", data).getResult()).isNull();
70 assertThat(validator.validateTop("", data).getResult()).isNull();
73 assertThat(validator.validateTop("", data).getResult()).isNull();
75 data.intValue = STRING_VALUE;
76 assertThat(validator.validateTop("", data).getResult()).isNull();
79 assertThat(validator.validateTop("", data).getResult()).contains("intValue", "-1");
83 public void testVerCascadeBeanValidationResultStringObject() {
84 CascadeChecker checker = new CascadeChecker();
85 checker.plain = new StdAnnotation();
88 checker.plain.strValue = STRING_VALUE;
89 BeanValidationResult result = new BeanValidationResult("", this);
90 assertThat(validator.verCascade(result, "", checker)).isTrue();
93 checker.plain.strValue = null;
95 result = new BeanValidationResult("", this);
96 assertThat(validator.verCascade(result, "", checker.plain)).isFalse();
97 assertThat(result.getResult()).contains("null");
99 result = new BeanValidationResult("", this);
100 assertThat(validator.verCascade(result, "", checker)).isFalse();
101 assertThat(result.getResult()).contains("null").doesNotContain("plain");
103 // validator returns null result - should be treated as valid
104 checker = new CascadeChecker() {
106 public BeanValidationResult validate(@NonNull String fieldName) {
110 checker.plain = new StdAnnotation();
111 result = new BeanValidationResult("", this);
112 assertThat(validator.verCascade(result, "", checker)).isTrue();
116 public void testVerKey() throws CoderException {
117 FullKeyAnnot data = new FullKeyAnnot();
120 data.key = STRING_VALUE;
121 assertThat(validator.validateTop("", data).getResult()).isNull();
124 data.key = new PfConceptKey();
125 assertThat(validator.validateTop("", data).getResult())
126 .contains(KEY_FIELD, "NULL:0.0.0", Validated.IS_A_NULL_KEY).doesNotContain("name", "version");
128 // invalid version - should invoke verCascade() which will invoke key.validate()
129 data.key = new StandardCoder().decode("{'name':'abc', 'version':'xyzzy'}".replace('\'', '"'),
131 assertThat(validator.validateTop("", data).getResult())
132 .contains(KEY_FIELD, "version", "xyzzy", "regular expression").doesNotContain("name");
134 // not a PfKeyImpl - should not check individual fields
135 PfKey pfkey = mock(PfKey.class);
137 assertThat(validator.validateTop("", data).getResult()).isNull();
139 when(pfkey.isNullKey()).thenReturn(true);
140 assertThat(validator.validateTop("", data).getResult()).contains(KEY_FIELD, Validated.IS_A_NULL_KEY);
143 data.key = new PfConceptKey(PfKey.NULL_KEY_NAME, "2.3.4");
144 assertThat(validator.validateTop("", data).getResult()).contains(KEY_FIELD, "name", "null")
145 .doesNotContain("version", "2.3.4");
148 data.key = new PfConceptKey(STRING_VALUE, PfKey.NULL_KEY_VERSION);
149 assertThat(validator.validateTop("", data).getResult()).contains(KEY_FIELD, "version", "null")
150 .doesNotContain("name", STRING_VALUE);
152 // null name, invalid version - should get two messages
153 data.key = new StandardCoder().decode("{'name':'NULL', 'version':'xyzzy'}".replace('\'', '"'),
155 assertThat(validator.validateTop("", data).getResult()).contains(KEY_FIELD, "name", "null", "version", "xyzzy",
156 "regular expression");
159 * Tests with all flags set to "false" (i.e., no validations).
162 EmptyKeyAnnot data2 = new EmptyKeyAnnot();
164 // build a key that is totally invalid
165 AtomicBoolean called = new AtomicBoolean();
167 data2.key = new PfConceptKey() {
168 private static final long serialVersionUID = 1L;
171 public BeanValidationResult validate(@NonNull String fieldName) {
177 // should be ok, since no validations are performed
178 assertThat(validator.validateTop("", data2).getResult()).isNull();
179 assertThat(called.get()).isFalse();
183 public void testXlateObject() {
184 assertThat(validator.xlate(null)).isNull();
185 assertThat(validator.xlate("hello")).isEqualTo("hello");
187 PfConceptKey key = new PfConceptKey("hello", "1.2.3");
188 assertThat(validator.xlate(key)).isEqualTo("hello:1.2.3");
191 public static class StdAnnotation {
194 private String strValue;
197 public static class PfMinChecker {
199 @PfMin(value = 5, allowed = -2)
200 private Object intValue;
203 public static class CascadeChecker extends Validated {
206 private StdAnnotation plain;
209 public BeanValidationResult validate(@NonNull String fieldName) {
210 // directly validates "plain"
211 return new PfValidator().validateTop(fieldName, plain);
215 public static class FullKeyAnnot {
217 @VerifyKey(keyNotNull = true, nameNotNull = true, versionNotNull = true, valid = true)
221 public static class EmptyKeyAnnot {
223 @VerifyKey(keyNotNull = false, nameNotNull = false, versionNotNull = false, valid = false)