aboutsummaryrefslogtreecommitdiffstats
path: root/utils/src/test/java/org/onap/policy/common/utils/coder/StandardValCoderTest.java
blob: 2fcdb0ddba340a96ab279e150d56a28d3a2f1b30 (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
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2020 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.common.utils.coder;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.worldturner.medeia.api.ValidationFailedException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Before;
import org.junit.Test;

public class StandardValCoderTest {
    private String jsonSchema;
    private String validJson;
    private String missingReqJson;
    private String badRegexJson;

    @Data
    @NoArgsConstructor
    public static class ValOuter {
        @Data
        @NoArgsConstructor
        public static class ValInner {
            public String subItemString;
            public Integer subItemInteger;
        }

        public String aaString;
        public int anInteger;
        public boolean aaBoolean;
        public List<ValInner> aaCollection;
    }

    @Before
    public void testSetUp() throws Exception {
        jsonSchema = getJson("src/test/resources/org/onap/policy/common/utils/coder/test.schema.json");
        validJson = getJson("src/test/resources/org/onap/policy/common/utils/coder/valid.json");
        missingReqJson = getJson("src/test/resources/org/onap/policy/common/utils/coder/missing-required.json");
        badRegexJson = getJson("src/test/resources/org/onap/policy/common/utils/coder/bad-regex.json");
    }

    @Test
    public void testDecode() throws CoderException {
        StandardValCoder valCoder = new StandardValCoder(jsonSchema, "test-schema");

        ValOuter valOuter = valCoder.decode(validJson, ValOuter.class);
        assertValidJson(valOuter);

        StringReader reader = new StringReader(validJson);
        valOuter = valCoder.decode(reader, ValOuter.class);
        assertValidJson(valOuter);

        try {
            valCoder.decode(missingReqJson, ValOuter.class);
            fail("missing required field should have been flagged by the schema validation");
        } catch (CoderException e) {
            assertEquals("required", ((ValidationFailedException) e.getCause()).getFailures().get(0).getRule());
            assertEquals("aaCollection",
                ((ValidationFailedException) e.getCause()).getFailures().get(0).getProperty());
            assertEquals("Required property aaCollection is missing from object",
                ((ValidationFailedException) e.getCause()).getFailures().get(0).getMessage());
        }

        try {
            valCoder.decode(badRegexJson, ValOuter.class);
            fail("bad regex should have been flagged by the schema validation");
        } catch (CoderException e) {
            assertEquals("properties", ((ValidationFailedException) e.getCause()).getFailures().get(0).getRule());
            assertEquals("aaString",
                    ((ValidationFailedException) e.getCause()).getFailures().get(0).getProperty());
            assertEquals("Property validation failed",
                    ((ValidationFailedException) e.getCause()).getFailures().get(0).getMessage());
            assertEquals("pattern",
                    ((ValidationFailedException) e.getCause()).getFailures()
                            .get(0).getDetails().iterator().next().getRule());
            assertEquals("Pattern ^([a-z]*)$ is not contained in text",
                    ((ValidationFailedException) e.getCause()).getFailures()
                            .get(0).getDetails().iterator().next().getMessage());
        }
    }

    @Test
    public void testEncode() throws CoderException {
        StandardValCoder valCoder = new StandardValCoder(jsonSchema, "test-schema");
        ValOuter valOuter = valCoder.decode(validJson, ValOuter.class);

        String valOuterJson = valCoder.encode(valOuter);
        assertEquals(valOuter, valCoder.decode(valOuterJson, ValOuter.class));
        assertValidJson(valOuter);

        StringWriter writer = new StringWriter();
        valCoder.encode(writer, valOuter);
        assertEquals(valOuterJson, writer.toString());

        // test exception case with an empty object
        assertThatThrownBy(() -> valCoder.encode(new ValOuter())).isInstanceOf(CoderException.class);
    }

    @Test
    public void testPretty() throws CoderException {
        StandardValCoder valCoder = new StandardValCoder(jsonSchema, "test-schema");
        ValOuter valOuter = valCoder.decode(validJson, ValOuter.class);

        String valOuterJson = valCoder.encode(valOuter);
        assertEquals(valOuterJson, valCoder.encode(valOuter, false));
        String prettyValOuterJson = valCoder.encode(valOuter, true);
        assertNotEquals(valOuterJson, prettyValOuterJson);

        assertEquals(valOuter, valCoder.decode(prettyValOuterJson, ValOuter.class));

        // test exception cases with an empty object
        assertThatThrownBy(() -> valCoder.encode(new ValOuter(), false)).isInstanceOf(CoderException.class);
        assertThatThrownBy(() -> valCoder.encode(new ValOuter(), true)).isInstanceOf(CoderException.class);
    }

    @Test
    public void testConformance() {
        StandardValCoder valCoder = new StandardValCoder(jsonSchema, "test-schema");
        assertTrue(valCoder.isConformant(validJson));
        assertFalse(valCoder.isConformant(missingReqJson));
        assertFalse(valCoder.isConformant(badRegexJson));
    }

    private void assertValidJson(ValOuter valOuter) {
        assertEquals("abcd", valOuter.getAaString());
        assertEquals(90, valOuter.getAnInteger());
        assertTrue(valOuter.isAaBoolean());
        assertEquals("defg", valOuter.getAaCollection().get(0).getSubItemString());
        assertEquals(Integer.valueOf(1200), valOuter.getAaCollection().get(0).getSubItemInteger());
    }

    private String getJson(String filePath) throws IOException {
        return new String(Files.readAllBytes(Paths.get(filePath)));
    }
}