aboutsummaryrefslogtreecommitdiffstats
path: root/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValueValidator.java
blob: dcf0869556ec75271047bf3371ba0a2895a095c8 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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 java.lang.annotation.Annotation;
import java.util.concurrent.atomic.AtomicBoolean;
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 TestValueValidator extends ValidatorUtil {

    private ValueValidator validator;

    // these fields just provide place-holders for annotations

    @NotNull
    @NotBlank
    private final int annotField = 1;


    @Before
    public void setUp() {
        validator = new MyValueValidator();
    }

    @Test
    public void testIsEmpty() {
        assertThat(validator.isEmpty()).isTrue();

        validator.addAnnotation(NotNull.class, (result2, fieldName, value) -> true);
        assertThat(validator.isEmpty()).isFalse();
    }

    @Test
    public void testValidateValue_NullValue() {
        BeanValidationResult result = new BeanValidationResult(MY_NAME, this);

        validator.validateValue(result, MY_FIELD, null);
        assertThat(result.getResult()).isNull();

        validator.addAnnotation(NotNull.class,
            (result2, fieldName, value) -> result2.validateNotNull(fieldName, value));
        validator.validateValue(result, MY_FIELD, null);
        assertThat(result.getResult()).contains(MY_FIELD, "null");
    }

    @Test
    public void testValidateValue_NotNullValue() {
        BeanValidationResult result = new BeanValidationResult(MY_NAME, this);

        validator.validateValue(result, MY_FIELD, HELLO);
        assertThat(result.getResult()).isNull();

        validator.addAnnotation(NotNull.class,
            (result2, fieldName, value) -> result2.validateNotNull(fieldName, value));
        validator.validateValue(result, MY_FIELD, HELLO);
        assertThat(result.getResult()).isNull();
    }

    @Test
    public void testAddAnnotationClassOfTChecker() {
        // the field does not have this annotation
        validator.addAnnotation(Min.class, (result2, fieldName, value) -> true);
        assertThat(validator.isEmpty()).isTrue();

        // "null" flag should stay true with this annotation
        assertThat(validator.isNullAllowed()).isTrue();
        validator.addAnnotation(NotBlank.class, (result2, fieldName, value) -> true);
        assertThat(validator.isNullAllowed()).isTrue();

        // "null" flag should become false with this annotation
        assertThat(validator.isNullAllowed()).isTrue();
        validator.addAnnotation(NotNull.class, (result2, fieldName, value) -> true);
        assertThat(validator.isNullAllowed()).isFalse();
    }

    @Test
    public void testAddAnnotationClassOfTCheckerWithAnnotOfT() {
        // the field does not have this annotation
        validator.addAnnotation(Min.class, (result2, fieldName, annot, value) -> true);
        assertThat(validator.isEmpty()).isTrue();

        // indicates the annotation value
        AtomicBoolean wasNull = new AtomicBoolean(false);

        // the field DOES have this annotation
        validator.addAnnotation(NotNull.class, (result2, fieldName, annot, value) -> {
            wasNull.set(annot instanceof NotNull);
            return result2.validateNotNull(fieldName, value);
        });
        assertThat(validator.isEmpty()).isFalse();

        // ensure that the checker is invoked
        BeanValidationResult result = new BeanValidationResult(MY_NAME, this);
        validator.validateValue(result, MY_FIELD, HELLO);
        assertThat(result.getResult()).isNull();

        assertThat(wasNull.get()).isTrue();
    }

    @Test
    public void testGetAnnotation() {
        assertThat(new ValueValidator().getAnnotation(NotNull.class)).isNull();
    }

    /**
     * Checks for annotations on the "annotField" field.
     */
    private static class MyValueValidator extends ValueValidator {
        @Override
        public <T extends Annotation> T getAnnotation(Class<T> annotClass) {
            try {
                return TestValueValidator.class.getDeclaredField("annotField").getAnnotation(annotClass);
            } catch (NoSuchFieldException | SecurityException e) {
                throw new RuntimeException(e);
            }
        }
    }
}