aboutsummaryrefslogtreecommitdiffstats
path: root/common-parameters/src/test/java/org/onap/policy/common/parameters/TestFieldValidator.java
blob: 81a7b8ff5eab7a716d06c95b6e57c07a016c764d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.common.parameters;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.google.gson.annotations.SerializedName;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import lombok.Getter;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.common.parameters.annotations.Min;
import org.onap.policy.common.parameters.annotations.NotBlank;
import org.onap.policy.common.parameters.annotations.NotNull;

public class TestFieldValidator extends ValidatorUtil {
    private static final String INT_LIST_FIELD = "intList";
    private static final String INT_MAP_FIELD = "intMap";
    private static final String UNANNOTATED_FIELD = "unannotated";
    private static final String INT_FIELD = "intValue";
    private static final int VALID_INT = 10;
    private static final int INVALID_INT = -10;

    @Getter
    private int unannotated;

    @Min(0)
    @Getter
    private int intValue;

    @Getter
    private List<@Min(1) Integer> intList;

    @Getter
    private Map<@NotBlank String, @Min(1) Integer> intMap;

    @SerializedName("annotated_key_map")
    @Getter
    private Map<@NotBlank String, Integer> annotatedKeyMap;

    @Getter
    private Map<String, @Min(1) Integer> annotatedValueMap;

    @Getter
    private List<Integer> unannotatedList;

    @Getter
    private Map<String, Integer> unannotatedMap;

    @NotNull
    @Getter
    private boolean boolValue;

    @NotNull
    @Getter
    private String notNullValue;

    @Min(0)
    @Getter
    private static int staticField;

    /**
     * Has no accessor.
     */
    @Min(0)
    private int noMethod;

    /**
     * Accessor is {@link #getStaticMethod()}, which is static.
     */
    @Min(0)
    private int staticMethod;

    /**
     * Accessor is {@link #getVoidMethod()}, which returns a void.
     */
    @Min(0)
    private int voidMethod;

    /**
     * Accessor is {@link #getParameterizedMethod()}, which requires a parameter.
     */
    @Min(0)
    private int parameterizedMethod;

    /**
     * Accessor is {@link #getExMethod()}, which throws an exception.
     */
    @Min(0)
    private int exMethod;


    @Before
    public void setUp() {
        bean = new BeanValidator();
    }

    @Test
    public void testGetAnnotation() {
        // field-level annotation
        assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_FIELD)).isEmpty()).isFalse();

        // class-level annotation
        assertThat(new FieldValidator(bean, ClassAnnot.class, getField(ClassAnnot.class, "text")).isEmpty()).isFalse();
    }

    @Test
    public void testFieldValidator() throws NoSuchFieldException, SecurityException {
        /*
         * Note: nested classes contain fields like "$this", thus the check for "$" in the
         * variable name is already covered by the other tests.
         */

        /*
         * Class with no annotations.
         */
        @NotNull
        class NoAnnotations {
            @SuppressWarnings("unused")
            String strValue;
        }

        Field field = NoAnnotations.class.getDeclaredField("this$0");

        assertThat(new FieldValidator(bean, NoAnnotations.class, field).isEmpty()).isTrue();

        // unannotated
        assertThat(new FieldValidator(bean, TestFieldValidator.class, getField("unannotated")).isEmpty()).isTrue();

        // these are invalid for various reasons

        Field staticField2 = getField("staticField");
        assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, staticField2))
                        .isInstanceOf(IllegalArgumentException.class);

        Field noMethodField = getField("noMethod");
        assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, noMethodField))
                        .isInstanceOf(IllegalArgumentException.class);

        // annotated
        assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_FIELD)).isEmpty()).isFalse();
    }

    @Test
    public void testFieldValidator_SetNullAllowed() {
        // default - null is allowed
        assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_FIELD)).isNullAllowed()).isTrue();

        // field-level NotNull
        assertThat(new FieldValidator(bean, TestFieldValidator.class, getField("notNullValue")).isNullAllowed())
                        .isFalse();

        // class-level NotNull
        assertThat(new FieldValidator(bean, ClassAnnot.class, getField(ClassAnnot.class, "noMethod")).isNullAllowed())
                        .isFalse();
    }

    @Test
    public void testAddListValidator() {

        // unannotated
        assertThat(new FieldValidator(bean, TestFieldValidator.class, getField("unannotatedList")).isEmpty()).isTrue();

        // annotated
        assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_LIST_FIELD)).isEmpty()).isFalse();
    }

    @Test
    public void testAddMapValidator() {

        // unannotated
        assertThat(new FieldValidator(bean, TestFieldValidator.class, getField("unannotatedMap")).isEmpty()).isTrue();

        // annotated
        assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_MAP_FIELD)).isEmpty()).isFalse();

        // only the key is annotated
        FieldValidator validator = new FieldValidator(bean, TestFieldValidator.class, getField("annotatedKeyMap"));
        assertThat(validator.isEmpty()).isFalse();

        BeanValidationResult result = new BeanValidationResult(MY_NAME, this);
        annotatedKeyMap = Map.of("abc", -10);
        validator.validateField(result, this);
        assertThat(result.getResult()).isNull();

        annotatedKeyMap = Map.of(" ", -10);
        validator.validateField(result, this);
        assertThat(result.getResult()).contains("annotated_key_map", "blank").doesNotContain("-10");

        // only the value is annotated
        validator = new FieldValidator(bean, TestFieldValidator.class, getField("annotatedValueMap"));
        assertThat(validator.isEmpty()).isFalse();

        result = new BeanValidationResult(MY_NAME, this);
        annotatedValueMap = Map.of(" ", 10);
        validator.validateField(result, this);
        assertThat(result.getResult()).isNull();

        annotatedValueMap = Map.of(" ", -10);
        validator.validateField(result, this);
        assertThat(result.getResult()).doesNotContain("blank").contains("annotatedValueMap", "\" \"", "-10");
    }

    @SuppressWarnings("deprecation")
    @Test
    public void testValidateField_testGetValue() {
        // unannotated
        BeanValidationResult result = new BeanValidationResult(MY_NAME, this);
        new FieldValidator(bean, getClass(), getField(UNANNOTATED_FIELD)).validateField(result, this);
        assertThat(result.getResult()).isNull();

        // valid
        intValue = VALID_INT;
        result = new BeanValidationResult(MY_NAME, this);
        new FieldValidator(bean, getClass(), getField(INT_FIELD)).validateField(result, this);
        assertThat(result.getResult()).isNull();

        // invalid
        intValue = INVALID_INT;
        result = new BeanValidationResult(MY_NAME, this);
        new FieldValidator(bean, getClass(), getField(INT_FIELD)).validateField(result, this);
        assertThat(result.getResult()).contains(INT_FIELD);

        // throws an exception
        FieldValidator validator = new FieldValidator(bean, TestFieldValidator.class, getField("exMethod"));
        BeanValidationResult result2 = new BeanValidationResult(MY_NAME, this);
        assertThatThrownBy(() -> validator.validateField(result2, this)).isInstanceOf(IllegalArgumentException.class)
                        .getCause().isInstanceOf(InvocationTargetException.class).getCause()
                        .hasMessage("expected exception");
    }

    @Test
    public void testValidateField_testGetValue_ListField() {
        // valid
        BeanValidationResult result = new BeanValidationResult(MY_NAME, this);
        intList = List.of(10, 20, 30, 40);
        new FieldValidator(bean, getClass(), getField(INT_LIST_FIELD)).validateField(result, this);
        assertThat(result.getResult()).isNull();

        // invalid
        result = new BeanValidationResult(MY_NAME, this);
        intList = List.of(9, -8, 7, -6);
        new FieldValidator(bean, getClass(), getField(INT_LIST_FIELD)).validateField(result, this);
        assertThat(result.getResult()).doesNotContain("0", "9").contains("1", "-8").doesNotContain("2", "7")
                        .contains("3", "-6");
    }

    @Test
    public void testValidateField_testGetValue_MapField() {
        // valid
        BeanValidationResult result = new BeanValidationResult(MY_NAME, this);
        intMap = Map.of("ten", 10, "twenty", 20, "thirty", 30, "forty", 40);
        new FieldValidator(bean, getClass(), getField(INT_MAP_FIELD)).validateField(result, this);
        assertThat(result.getResult()).isNull();

        // invalid
        result = new BeanValidationResult(MY_NAME, this);
        intMap = Map.of("ten", 9, "twenty", -8, "thirty", 7, "forty", -6);
        new FieldValidator(bean, getClass(), getField(INT_MAP_FIELD)).validateField(result, this);
        assertThat(result.getResult()).doesNotContain("ten", "9").contains("twenty", "-8").doesNotContain("thirty", "7")
                        .contains("forty", "-6");
    }

    @Test
    public void testClassOnly() {
        // class-level annotation has no bearing on a static field
        assertThat(new FieldValidator(bean, ClassAnnot.class, getField(ClassAnnot.class, "staticValue")).isEmpty())
                        .isTrue();

        // field-level annotation on a static field
        Field staticField2 = getField("staticField");
        assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, staticField2))
                        .isInstanceOf(IllegalArgumentException.class);
    }

    @Test
    public void testGetAccessor() {
        // uses "getXxx"
        assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_FIELD)).isEmpty()).isFalse();

        // uses "isXxx"
        assertThat(new FieldValidator(bean, TestFieldValidator.class, getField("boolValue")).isEmpty()).isFalse();
    }

    @Test
    public void testGetMethod() {
        assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_FIELD)).isEmpty()).isFalse();

        // these are invalid for various reasons

        Field noMethodField = getField("noMethod");
        assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, noMethodField))
                        .isInstanceOf(IllegalArgumentException.class);

        Field staticMethodField = getField("staticMethod");
        assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, staticMethodField))
                        .isInstanceOf(IllegalArgumentException.class);
    }

    @Test
    public void testValidMethod() {
        assertThat(new FieldValidator(bean, TestFieldValidator.class, getField(INT_FIELD)).isEmpty()).isFalse();

        // these are invalid for various reasons

        Field staticMethodField = getField("staticMethod");
        assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, staticMethodField))
                        .isInstanceOf(IllegalArgumentException.class);

        Field voidMethodField = getField("voidMethod");
        assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, voidMethodField))
                        .isInstanceOf(IllegalArgumentException.class);

        Field paramMethodField = getField("parameterizedMethod");
        assertThatThrownBy(() -> new FieldValidator(bean, TestFieldValidator.class, paramMethodField))
                        .isInstanceOf(IllegalArgumentException.class);
    }

    @Test
    public void testIsFieldAnnotated_testSetFieldAnnotated() {
        // annotated at the field level
        assertThat(new FieldValidator(bean, getClass(), getField(INT_FIELD)).isFieldAnnotated()).isTrue();

        // unannotated
        assertThat(new FieldValidator(bean, getClass(), getField(UNANNOTATED_FIELD)).isFieldAnnotated()).isFalse();
    }

    public static int getStaticMethod() {
        return -1000;
    }

    public void getVoidMethod() {
        // do nothing
    }

    public int getParameterizedMethod(boolean flag) {
        return 0;
    }

    public int getExMethod() {
        throw new RuntimeException("expected exception");
    }

    @NotNull
    public static class ClassAnnot {
        @Getter
        private String text;

        // no "get" method
        @SuppressWarnings("unused")
        private String noMethod;

        @Getter
        private static int staticValue;
    }
}