88668659313b4b221db786b3f44c61f43f286b90
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base.validation.annotations;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import jakarta.validation.Valid;
26 import jakarta.validation.constraints.NotNull;
27 import lombok.Getter;
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;
32
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";
38
39     @Test
40     void testStandardAnnotation() {
41         StdAnnotation data = new StdAnnotation();
42         data.strValue = STRING_VALUE;
43         assertThat(BeanValidator.validate("", data).getResult()).isNull();
44
45         data.strValue = null;
46         assertThat(BeanValidator.validate("", data).getResult()).contains("strValue", "null");
47     }
48
49     @Test
50     void testVerifyKey() {
51         FullKeyAnnot data = new FullKeyAnnot();
52
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);
57
58         // invalid version - should invoke cascade validation
59
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
62         try {
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);
68         }
69         assertThat(BeanValidator.validate("", data).getResult())
70             .contains(KEY_FIELD, "version", "xyzzy", "must match");
71
72         // null name
73         data.key = new PfConceptKey(PfKey.NULL_KEY_NAME, "2.3.4");
74         assertThat(BeanValidator.validate("", data).getResult()).contains(KEY_FIELD, "name", IS_NULL);
75
76         // null version
77         data.key = new PfConceptKey(STRING_VALUE, PfKey.NULL_KEY_VERSION);
78         assertThat(BeanValidator.validate("", data).getResult()).contains(KEY_FIELD, "version", IS_NULL);
79
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
83         try {
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);
89         }
90         assertThat(BeanValidator.validate("", data).getResult())
91             .contains(KEY_FIELD, "name", IS_NULL, "version", "xyzzy", "must match");
92     }
93
94     @Test
95     void testEmptyKeyAnnotation() {
96         EmptyKeyAnnot data = new EmptyKeyAnnot();
97         data.key = new PfConceptKey(); // totally invalid key
98
99         // should be ok, since no validations are performed
100         assertThat(BeanValidator.validate("", data).getResult()).isNull();
101     }
102
103     @Test
104     void testVerifyKeyOnGetters() {
105         GetterKeyAnnot data = new GetterKeyAnnot();
106
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");
112     }
113
114     public static class StdAnnotation {
115         @Getter
116         @NotNull
117         private String strValue;
118     }
119
120     public static class FullKeyAnnot {
121         @Getter
122         @Valid
123         @VerifyKey(keyNotNull = true, nameNotNull = true, versionNotNull = true)
124         private PfKey key;
125     }
126
127     public static class EmptyKeyAnnot {
128         @Getter
129         @Valid
130         @VerifyKey(keyNotNull = false, nameNotNull = false, versionNotNull = false)
131         private PfKey key;
132     }
133
134     public static class GetterKeyAnnot {
135         @Valid
136         @VerifyKey(versionNotNull = true)
137         public PfConceptKey getValidKey() {
138             return new PfConceptKey("validName", "1.0.0");
139         }
140
141         @Valid
142         @VerifyKey(versionNotNull = true)
143         public PfConceptKey getNullKey() {
144             return new PfConceptKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);
145         }
146
147         @Valid
148         @VerifyKey(versionNotNull = true)
149         public PfConceptKey getNullVersionKey() {
150             return new PfConceptKey("validName", PfKey.NULL_KEY_VERSION);
151         }
152     }
153 }