2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2026 OpenInfra Foundation Europe. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base.validation.annotations;
23 import static org.assertj.core.api.Assertions.assertThat;
25 import jakarta.validation.Valid;
26 import jakarta.validation.constraints.NotNull;
28 import org.junit.jupiter.api.Test;
29 import org.onap.policy.common.parameters.BeanValidator;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.models.base.PfConceptKey;
33 import org.onap.policy.models.base.PfKey;
35 class VerifyKeyValidatorTest {
36 private static final String IS_A_NULL_KEY = "is a null key";
37 private static final String IS_NULL = "is null";
38 private static final String KEY_FIELD = "key";
39 private static final String STRING_VALUE = "abc";
42 void testStandardAnnotation() {
43 StdAnnotation data = new StdAnnotation();
44 data.strValue = STRING_VALUE;
45 assertThat(BeanValidator.validate("", data).getResult()).isNull();
48 assertThat(BeanValidator.validate("", data).getResult()).contains("strValue", "null");
52 void testVerifyKey() throws CoderException {
53 FullKeyAnnot data = new FullKeyAnnot();
55 // null key - Jakarta validation will include all constraint violations
56 data.key = new PfConceptKey();
57 assertThat(BeanValidator.validate("", data).getResult())
58 .contains(KEY_FIELD, IS_A_NULL_KEY);
60 // invalid version - should invoke cascade validation
61 data.key = new StandardCoder().decode("{'name':'abc', 'version':'xyzzy'}".replace('\'', '"'),
63 assertThat(BeanValidator.validate("", data).getResult())
64 .contains(KEY_FIELD, "version", "xyzzy", "must match");
67 data.key = new PfConceptKey(PfKey.NULL_KEY_NAME, "2.3.4");
68 assertThat(BeanValidator.validate("", data).getResult()).contains(KEY_FIELD, "name", IS_NULL);
71 data.key = new PfConceptKey(STRING_VALUE, PfKey.NULL_KEY_VERSION);
72 assertThat(BeanValidator.validate("", data).getResult()).contains(KEY_FIELD, "version", IS_NULL);
74 // null name, invalid version - should get two messages
75 data.key = new StandardCoder().decode("{'name':'NULL', 'version':'xyzzy'}".replace('\'', '"'),
77 assertThat(BeanValidator.validate("", data).getResult())
78 .contains(KEY_FIELD, "name", IS_NULL, "version", "xyzzy", "must match");
82 void testEmptyKeyAnnotation() {
83 EmptyKeyAnnot data = new EmptyKeyAnnot();
84 data.key = new PfConceptKey(); // totally invalid key
86 // should be ok, since no validations are performed
87 assertThat(BeanValidator.validate("", data).getResult()).isNull();
91 void testVerifyKeyOnGetters() {
92 GetterKeyAnnot data = new GetterKeyAnnot();
94 var result = BeanValidator.validate("", data);
95 assertThat(result.getResult())
96 .contains("nullKey", IS_A_NULL_KEY)
97 .contains("nullVersionKey", "version", IS_NULL)
98 .doesNotContain("validKey");
101 public static class StdAnnotation {
104 private String strValue;
107 public static class FullKeyAnnot {
110 @VerifyKey(keyNotNull = true, nameNotNull = true, versionNotNull = true)
114 public static class EmptyKeyAnnot {
117 @VerifyKey(keyNotNull = false, nameNotNull = false, versionNotNull = false)
121 public static class GetterKeyAnnot {
123 @VerifyKey(versionNotNull = true)
124 public PfConceptKey getValidKey() {
125 return new PfConceptKey("validName", "1.0.0");
129 @VerifyKey(versionNotNull = true)
130 public PfConceptKey getNullKey() {
131 return new PfConceptKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);
135 @VerifyKey(versionNotNull = true)
136 public PfConceptKey getNullVersionKey() {
137 return new PfConceptKey("validName", PfKey.NULL_KEY_VERSION);