812916e3cf99c6196f8d70e7060b05cf13dea30b
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfValidatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.util.concurrent.atomic.AtomicBoolean;
28 import javax.validation.Valid;
29 import lombok.Getter;
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;
39
40 public class PfValidatorTest {
41     private static final String KEY_FIELD = "key";
42
43     private static final String STRING_VALUE = "abc";
44
45     private PfValidator validator;
46
47     @Before
48     public void setUp() {
49         validator = new PfValidator();
50     }
51
52     @Test
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();
58
59         data.strValue = null;
60         assertThat(validator.validateTop("", data).getResult()).contains("strValue", "null");
61     }
62
63     @Test
64     public void testVerPfMin() {
65         PfMinChecker data = new PfMinChecker();
66         data.intValue = 10;
67         assertThat(validator.validateTop("", data).getResult()).isNull();
68
69         data.intValue = -2;
70         assertThat(validator.validateTop("", data).getResult()).isNull();
71
72         data.intValue = null;
73         assertThat(validator.validateTop("", data).getResult()).isNull();
74
75         data.intValue = STRING_VALUE;
76         assertThat(validator.validateTop("", data).getResult()).isNull();
77
78         data.intValue = -1;
79         assertThat(validator.validateTop("", data).getResult()).contains("intValue", "-1");
80     }
81
82     @Test
83     public void testVerCascadeBeanValidationResultStringObject() {
84         CascadeChecker checker = new CascadeChecker();
85         checker.plain = new StdAnnotation();
86
87         // valid
88         checker.plain.strValue = STRING_VALUE;
89         BeanValidationResult result = new BeanValidationResult("", this);
90         assertThat(validator.verCascade(result, "", checker)).isTrue();
91
92         // invalid
93         checker.plain.strValue = null;
94
95         result = new BeanValidationResult("", this);
96         assertThat(validator.verCascade(result, "", checker.plain)).isFalse();
97         assertThat(result.getResult()).contains("null");
98
99         result = new BeanValidationResult("", this);
100         assertThat(validator.verCascade(result, "", checker)).isFalse();
101         assertThat(result.getResult()).contains("null").doesNotContain("plain");
102
103         // validator returns null result - should be treated as valid
104         checker = new CascadeChecker() {
105             @Override
106             public BeanValidationResult validate(@NonNull String fieldName) {
107                 return null;
108             }
109         };
110         checker.plain = new StdAnnotation();
111         result = new BeanValidationResult("", this);
112         assertThat(validator.verCascade(result, "", checker)).isTrue();
113     }
114
115     @Test
116     public void testVerKey() throws CoderException {
117         FullKeyAnnot data = new FullKeyAnnot();
118
119         // not a key
120         data.key = STRING_VALUE;
121         assertThat(validator.validateTop("", data).getResult()).isNull();
122
123         // null key
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");
127
128         // invalid version - should invoke verCascade() which will invoke key.validate()
129         data.key = new StandardCoder().decode("{'name':'abc', 'version':'xyzzy'}".replace('\'', '"'),
130                         PfConceptKey.class);
131         assertThat(validator.validateTop("", data).getResult())
132                         .contains(KEY_FIELD, "version", "xyzzy", "regular expression").doesNotContain("name");
133
134         // not a PfKeyImpl - should not check individual fields
135         PfKey pfkey = mock(PfKey.class);
136         data.key = pfkey;
137         assertThat(validator.validateTop("", data).getResult()).isNull();
138
139         when(pfkey.isNullKey()).thenReturn(true);
140         assertThat(validator.validateTop("", data).getResult()).contains(KEY_FIELD, Validated.IS_A_NULL_KEY);
141
142         // null name
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");
146
147         // null version
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);
151
152         // null name, invalid version - should get two messages
153         data.key = new StandardCoder().decode("{'name':'NULL', 'version':'xyzzy'}".replace('\'', '"'),
154                         PfConceptKey.class);
155         assertThat(validator.validateTop("", data).getResult()).contains(KEY_FIELD, "name", "null", "version", "xyzzy",
156                         "regular expression");
157
158         /*
159          * Tests with all flags set to "false" (i.e., no validations).
160          */
161
162         EmptyKeyAnnot data2 = new EmptyKeyAnnot();
163
164         // build a key that is totally invalid
165         AtomicBoolean called = new AtomicBoolean();
166
167         data2.key = new PfConceptKey() {
168             private static final long serialVersionUID = 1L;
169
170             @Override
171             public BeanValidationResult validate(@NonNull String fieldName) {
172                 called.set(true);
173                 return null;
174             }
175         };
176
177         // should be ok, since no validations are performed
178         assertThat(validator.validateTop("", data2).getResult()).isNull();
179         assertThat(called.get()).isFalse();
180     }
181
182     @Test
183     public void testXlateObject() {
184         assertThat(validator.xlate(null)).isNull();
185         assertThat(validator.xlate("hello")).isEqualTo("hello");
186
187         PfConceptKey key = new PfConceptKey("hello", "1.2.3");
188         assertThat(validator.xlate(key)).isEqualTo("hello:1.2.3");
189     }
190
191     public static class StdAnnotation {
192         @Getter
193         @NotNull
194         private String strValue;
195     }
196
197     public static class PfMinChecker {
198         @Getter
199         @PfMin(value = 5, allowed = -2)
200         private Object intValue;
201     }
202
203     public static class CascadeChecker extends Validated {
204         @Getter
205         @Valid
206         private StdAnnotation plain;
207
208         @Override
209         public BeanValidationResult validate(@NonNull String fieldName) {
210             // directly validates "plain"
211             return new PfValidator().validateTop(fieldName, plain);
212         }
213     }
214
215     public static class FullKeyAnnot {
216         @Getter
217         @VerifyKey(keyNotNull = true, nameNotNull = true, versionNotNull = true, valid = true)
218         private Object key;
219     }
220
221     public static class EmptyKeyAnnot {
222         @Getter
223         @VerifyKey(keyNotNull = false, nameNotNull = false, versionNotNull = false, valid = false)
224         private PfKey key;
225     }
226 }