a39455a7a6fa3592a685e77e828e155bc3efb48b
[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.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;
34
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";
40
41     @Test
42     void testStandardAnnotation() {
43         StdAnnotation data = new StdAnnotation();
44         data.strValue = STRING_VALUE;
45         assertThat(BeanValidator.validate("", data).getResult()).isNull();
46
47         data.strValue = null;
48         assertThat(BeanValidator.validate("", data).getResult()).contains("strValue", "null");
49     }
50
51     @Test
52     void testVerifyKey() throws CoderException {
53         FullKeyAnnot data = new FullKeyAnnot();
54
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);
59
60         // invalid version - should invoke cascade validation
61         data.key = new StandardCoder().decode("{'name':'abc', 'version':'xyzzy'}".replace('\'', '"'),
62             PfConceptKey.class);
63         assertThat(BeanValidator.validate("", data).getResult())
64             .contains(KEY_FIELD, "version", "xyzzy", "must match");
65
66         // null name
67         data.key = new PfConceptKey(PfKey.NULL_KEY_NAME, "2.3.4");
68         assertThat(BeanValidator.validate("", data).getResult()).contains(KEY_FIELD, "name", IS_NULL);
69
70         // null version
71         data.key = new PfConceptKey(STRING_VALUE, PfKey.NULL_KEY_VERSION);
72         assertThat(BeanValidator.validate("", data).getResult()).contains(KEY_FIELD, "version", IS_NULL);
73
74         // null name, invalid version - should get two messages
75         data.key = new StandardCoder().decode("{'name':'NULL', 'version':'xyzzy'}".replace('\'', '"'),
76             PfConceptKey.class);
77         assertThat(BeanValidator.validate("", data).getResult())
78             .contains(KEY_FIELD, "name", IS_NULL, "version", "xyzzy", "must match");
79     }
80
81     @Test
82     void testEmptyKeyAnnotation() {
83         EmptyKeyAnnot data = new EmptyKeyAnnot();
84         data.key = new PfConceptKey(); // totally invalid key
85
86         // should be ok, since no validations are performed
87         assertThat(BeanValidator.validate("", data).getResult()).isNull();
88     }
89
90     @Test
91     void testVerifyKeyOnGetters() {
92         GetterKeyAnnot data = new GetterKeyAnnot();
93
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");
99     }
100
101     public static class StdAnnotation {
102         @Getter
103         @NotNull
104         private String strValue;
105     }
106
107     public static class FullKeyAnnot {
108         @Getter
109         @Valid
110         @VerifyKey(keyNotNull = true, nameNotNull = true, versionNotNull = true)
111         private PfKey key;
112     }
113
114     public static class EmptyKeyAnnot {
115         @Getter
116         @Valid
117         @VerifyKey(keyNotNull = false, nameNotNull = false, versionNotNull = false)
118         private PfKey key;
119     }
120
121     public static class GetterKeyAnnot {
122         @Valid
123         @VerifyKey(versionNotNull = true)
124         public PfConceptKey getValidKey() {
125             return new PfConceptKey("validName", "1.0.0");
126         }
127
128         @Valid
129         @VerifyKey(versionNotNull = true)
130         public PfConceptKey getNullKey() {
131             return new PfConceptKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);
132         }
133
134         @Valid
135         @VerifyKey(versionNotNull = true)
136         public PfConceptKey getNullVersionKey() {
137             return new PfConceptKey("validName", PfKey.NULL_KEY_VERSION);
138         }
139     }
140 }