aboutsummaryrefslogtreecommitdiffstats
path: root/common-parameters/src/test/java/org/onap/policy/common/parameters/TestBeanValidationResult.java
blob: 12cd80cb73bd14330689df55d06ae93fac065c17 (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
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2019 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.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;

public class TestBeanValidationResult {
    private static final String MY_LIST = "my-list";
    private static final String OBJECT = "an object";
    private static final String INITIAL_INDENT = "xx ";
    private static final String NEXT_INDENT = "yy ";
    private static final String MID_INDENT = "xx yy ";
    private static final String NAME = "my-name";
    private static final String MY_LIST_INVALID = "  'my-list' INVALID, item has status INVALID\n    ";
    private static final String BEAN_INVALID_MSG = requote("'my-name' INVALID, item has status INVALID\n");

    private String cleanMsg;
    private String invalidMsg;

    private BeanValidationResult bean;
    private ObjectValidationResult clean;
    private ObjectValidationResult invalid;

    /**
     * Sets up.
     */
    @Before
    public void setUp() {
        clean = new ObjectValidationResult("abc", 10);
        cleanMsg = clean.getResult("", "", true);

        invalid = new ObjectValidationResult("def", 20);
        invalid.setResult(ValidationStatus.INVALID, "invalid");
        invalidMsg = invalid.getResult();

        bean = new BeanValidationResult(NAME, OBJECT);
    }

    @Test
    public void testBeanValidationResult() {
        assertTrue(bean.isValid());
        assertNull(bean.getResult());
    }

    @Test
    public void testAddResult_testGetResult() {
        // null should be ok
        assertTrue(bean.addResult(null));

        assertTrue(bean.addResult(clean));
        assertTrue(bean.isValid());
        assertNull(bean.getResult());

        assertFalse(bean.addResult(invalid));
        assertFalse(bean.isValid());
        assertEquals(BEAN_INVALID_MSG + "  " + invalidMsg, bean.getResult());

        assertEquals(INITIAL_INDENT + BEAN_INVALID_MSG + MID_INDENT + cleanMsg + MID_INDENT + invalidMsg,
                        bean.getResult(INITIAL_INDENT, NEXT_INDENT, true));
    }

    @Test
    public void testValidateNotNull() {
        assertTrue(bean.validateNotNull("sub-name", "sub-object"));
        assertTrue(bean.isValid());
        assertNull(bean.getResult());

        assertFalse(bean.validateNotNull("sub-name", null));
        assertFalse(bean.isValid());
        assertEquals(requote(BEAN_INVALID_MSG + "  item 'sub-name' value 'null' INVALID, is null\n"), bean.getResult());
    }

    @Test
    public void testValidateNotNullList() {
        List<ValidationResult> list = Arrays.asList(clean);
        assertTrue(bean.validateNotNullList(MY_LIST, list, item -> item));
        assertTrue(bean.isValid());
        assertNull(bean.getResult());

        list = Arrays.asList(invalid, invalid);
        assertFalse(bean.validateNotNullList(MY_LIST, list, item -> item));
        assertFalse(bean.isValid());
        assertEquals(requote(BEAN_INVALID_MSG + MY_LIST_INVALID + invalidMsg
                        + "    " + invalidMsg), bean.getResult());
    }

    @Test
    public void testValidateNotNullList_NullList() {
        List<ValidationResult> list = null;
        assertFalse(bean.validateNotNullList(MY_LIST, list, item -> item));
        assertFalse(bean.isValid());
        assertEquals(requote(BEAN_INVALID_MSG + "  item 'my-list' value 'null' INVALID, is null\n"), bean.getResult());

    }

    @Test
    public void testValidateList() {
        List<ValidationResult> list = null;
        bean = new BeanValidationResult(NAME, OBJECT);
        assertTrue(bean.validateList(MY_LIST, list, item -> item));
        assertTrue(bean.isValid());
        assertNull(bean.getResult());

        list = Arrays.asList(clean);
        bean = new BeanValidationResult(NAME, OBJECT);
        assertTrue(bean.validateList(MY_LIST, list, item -> item));
        assertTrue(bean.isValid());
        assertNull(bean.getResult());

        // null item in the list
        list = Arrays.asList(clean, null);
        bean = new BeanValidationResult(NAME, OBJECT);
        assertFalse(bean.validateList(MY_LIST, list, item -> item));
        assertFalse(bean.isValid());
        assertEquals(requote(BEAN_INVALID_MSG + MY_LIST_INVALID
                        + "item 'item' value 'null' INVALID, null\n"), bean.getResult());

        list = Arrays.asList(invalid, invalid);
        bean = new BeanValidationResult(NAME, OBJECT);
        assertFalse(bean.validateList(MY_LIST, list, item -> item));
        assertFalse(bean.isValid());
        assertEquals(requote(BEAN_INVALID_MSG + MY_LIST_INVALID + invalidMsg
                        + "    " + invalidMsg), bean.getResult());

    }

    private static String requote(String text) {
        return text.replace('\'', '"');
    }
}