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.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfKey;
33 class VerifyKeyValidatorTest {
34 private static final String IS_A_NULL_KEY = "is a null key";
35 private static final String IS_NULL = "is null";
36 private static final String KEY_FIELD = "key";
37 private static final String STRING_VALUE = "abc";
40 void testStandardAnnotation() {
41 StdAnnotation data = new StdAnnotation();
42 data.strValue = STRING_VALUE;
43 assertThat(BeanValidator.validate(data).getResult()).isNull();
46 assertThat(BeanValidator.validate(data).getResult()).contains("strValue", "null");
50 void testVerifyKey() {
51 FullKeyAnnot data = new FullKeyAnnot();
53 // null key - Jakarta validation will include all constraint violations
54 data.key = new PfConceptKey();
55 assertThat(BeanValidator.validate(data).getResult())
56 .contains(KEY_FIELD, IS_A_NULL_KEY);
58 // invalid version - should invoke cascade validation
60 // Create object with invalid version using reflection to bypass setter validation
61 data.key = new PfConceptKey("abc", "1.0.0"); // Create with valid version first
63 var versionField = PfConceptKey.class.getDeclaredField("version");
64 versionField.setAccessible(true);
65 versionField.set(data.key, "xyzzy"); // Set invalid version directly
66 } catch (Exception e) {
67 throw new RuntimeException("Failed to set invalid version for test", e);
69 assertThat(BeanValidator.validate("", data).getResult())
70 .contains(KEY_FIELD, "version", "xyzzy", "must match");
73 data.key = new PfConceptKey(PfKey.NULL_KEY_NAME, "2.3.4");
74 assertThat(BeanValidator.validate(data).getResult()).contains(KEY_FIELD, "name", IS_NULL);
77 data.key = new PfConceptKey(STRING_VALUE, PfKey.NULL_KEY_VERSION);
78 assertThat(BeanValidator.validate(data).getResult()).contains(KEY_FIELD, "version", IS_NULL);
80 // null name, invalid version - should get two messages
81 // Create object with invalid version using reflection to bypass setter validation
82 data.key = new PfConceptKey("NULL", "1.0.0"); // Create with valid version first
84 var versionField = PfConceptKey.class.getDeclaredField("version");
85 versionField.setAccessible(true);
86 versionField.set(data.key, "xyzzy"); // Set invalid version directly
87 } catch (Exception e) {
88 throw new RuntimeException("Failed to set invalid version for test", e);
90 assertThat(BeanValidator.validate("", data).getResult())
91 .contains(KEY_FIELD, "name", IS_NULL, "version", "xyzzy", "must match");
95 void testEmptyKeyAnnotation() {
96 EmptyKeyAnnot data = new EmptyKeyAnnot();
97 data.key = new PfConceptKey(); // totally invalid key
99 // should be ok, since no validations are performed
100 assertThat(BeanValidator.validate(data).getResult()).isNull();
104 void testVerifyKeyOnGetters() {
105 GetterKeyAnnot data = new GetterKeyAnnot();
107 var result = BeanValidator.validate(data);
108 assertThat(result.getResult())
109 .contains("nullKey", IS_A_NULL_KEY)
110 .contains("nullVersionKey", "version", IS_NULL)
111 .doesNotContain("validKey");
114 public static class StdAnnotation {
117 private String strValue;
120 public static class FullKeyAnnot {
123 @VerifyKey(keyNotNull = true, nameNotNull = true, versionNotNull = true)
127 public static class EmptyKeyAnnot {
130 @VerifyKey(keyNotNull = false, nameNotNull = false, versionNotNull = false)
134 public static class GetterKeyAnnot {
136 @VerifyKey(versionNotNull = true)
137 public PfConceptKey getValidKey() {
138 return new PfConceptKey("validName", "1.0.0");
142 @VerifyKey(versionNotNull = true)
143 public PfConceptKey getNullKey() {
144 return new PfConceptKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);
148 @VerifyKey(versionNotNull = true)
149 public PfConceptKey getNullVersionKey() {
150 return new PfConceptKey("validName", PfKey.NULL_KEY_VERSION);