summaryrefslogtreecommitdiffstats
path: root/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperUnmarshal.java
blob: 725ebb22139e8f4a7b76823bdcb0810a56cde128 (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. 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.apex.plugins.context.schema.avro;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.apache.avro.util.Utf8;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.apex.context.SchemaHelper;
import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
import org.onap.policy.apex.context.parameters.SchemaParameters;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
import org.onap.policy.apex.model.basicmodel.service.ModelService;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;

/**
 * @author Liam Fallon (liam.fallon@ericsson.com)
 * @version
 */
public class TestAvroSchemaHelperUnmarshal {
    private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
    private AxContextSchemas schemas;

    @Before
    public void initTest() {
        schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
        ModelService.registerModel(AxContextSchemas.class, schemas);
        new SchemaParameters().getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
    }

    // @Test
    public void testNullUnmarshal() {
        final AxContextSchema avroNullSchema =
                new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "Avro", "{\"type\": \"null\"}");

        schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema);
        final SchemaHelper schemaHelper0 =
                new SchemaHelperFactory().createSchemaHelper(testKey, avroNullSchema.getKey());

        try {
            schemaHelper0.createNewInstance();
            fail("test should throw an exception here");
        } catch (final Exception e) {
            assertEquals("AvroTest:0.0.1: could not create an instance, schema class for the schema is null",
                    e.getMessage());
        }

        assertEquals(null, schemaHelper0.unmarshal("null"));

        try {
            schemaHelper0.unmarshal("123");
            fail("test should throw an exception here");
        } catch (final Exception e) {
            assertEquals(
                    "AvroTest:0.0.1: object \"123\" Avro unmarshalling failed: Expected null. Got VALUE_NUMBER_INT",
                    e.getMessage());
        }
    }

    // @Test
    public void testBooleanUnmarshal() {
        final AxContextSchema avroBooleanSchema =
                new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "Avro", "{\"type\": \"boolean\"}");

        schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema);
        final SchemaHelper schemaHelper1 =
                new SchemaHelperFactory().createSchemaHelper(testKey, avroBooleanSchema.getKey());

        try {
            schemaHelper1.createNewInstance();
            fail("test should throw an exception here");
        } catch (final Exception e) {
            assertEquals(
                    "AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\" using the default constructor \"Boolean()\"",
                    e.getMessage());
        }
        assertEquals(true, schemaHelper1.createNewInstance("true"));

        assertEquals(true, schemaHelper1.unmarshal("true"));
        assertEquals(false, schemaHelper1.unmarshal("false"));
        try {
            schemaHelper1.unmarshal(0);
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertEquals(
                    "AvroTest:0.0.1: object \"0\" Avro unmarshalling failed: Expected boolean. Got VALUE_NUMBER_INT",
                    e.getMessage());
        }
    }

    // @Test
    public void testIntUnmarshal() {
        final AxContextSchema avroIntSchema =
                new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "Avro", "{\"type\": \"int\"}");

        schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema);
        final SchemaHelper schemaHelper2 =
                new SchemaHelperFactory().createSchemaHelper(testKey, avroIntSchema.getKey());

        try {
            schemaHelper2.createNewInstance();
            fail("test should throw an exception here");
        } catch (final Exception e) {
            assertEquals(
                    "AvroTest:0.0.1: could not create an instance of class \"java.lang.Integer\" using the default constructor \"Integer()\"",
                    e.getMessage());
        }
        assertEquals(123, schemaHelper2.createNewInstance("123"));

        assertEquals(0, schemaHelper2.unmarshal("0"));
        assertEquals(1, schemaHelper2.unmarshal("1"));
        assertEquals(-1, schemaHelper2.unmarshal("-1"));
        assertEquals(1, schemaHelper2.unmarshal("1.23"));
        assertEquals(-1, schemaHelper2.unmarshal("-1.23"));
        assertEquals(2147483647, schemaHelper2.unmarshal("2147483647"));
        assertEquals(-2147483648, schemaHelper2.unmarshal("-2147483648"));
        try {
            schemaHelper2.unmarshal("2147483648");
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertTrue(e.getMessage().startsWith(
                    "AvroTest:0.0.1: object \"2147483648\" Avro unmarshalling failed: Numeric value (2147483648) out of range of int"));
        }
        try {
            schemaHelper2.unmarshal("-2147483649");
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertTrue(e.getMessage().startsWith(
                    "AvroTest:0.0.1: object \"-2147483649\" Avro unmarshalling failed: Numeric value (-2147483649) out of range of int"));
        }
        try {
            schemaHelper2.unmarshal(null);
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertTrue(e.getMessage().equals(
                    "AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: String to read from cannot be null!"));
        }
    }

    // @Test
    public void testLongUnmarshal() {
        final AxContextSchema avroLongSchema =
                new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "Avro", "{\"type\": \"long\"}");

        schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema);
        final SchemaHelper schemaHelper3 =
                new SchemaHelperFactory().createSchemaHelper(testKey, avroLongSchema.getKey());

        try {
            schemaHelper3.createNewInstance();
            fail("test should throw an exception here");
        } catch (final Exception e) {
            assertEquals(
                    "AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" using the default constructor \"Long()\"",
                    e.getMessage());
        }
        assertEquals(123456789L, schemaHelper3.createNewInstance("123456789"));

        assertEquals(0L, schemaHelper3.unmarshal("0"));
        assertEquals(1L, schemaHelper3.unmarshal("1"));
        assertEquals(-1L, schemaHelper3.unmarshal("-1"));
        assertEquals(1L, schemaHelper3.unmarshal("1.23"));
        assertEquals(-1L, schemaHelper3.unmarshal("-1.23"));
        assertEquals(9223372036854775807L, schemaHelper3.unmarshal("9223372036854775807"));
        assertEquals(-9223372036854775808L, schemaHelper3.unmarshal("-9223372036854775808"));
        try {
            schemaHelper3.unmarshal("9223372036854775808");
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertTrue(e.getMessage().startsWith(
                    "AvroTest:0.0.1: object \"9223372036854775808\" Avro unmarshalling failed: Numeric value (9223372036854775808) out of range of long"));
        }
        try {
            schemaHelper3.unmarshal("-9223372036854775809");
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertTrue(e.getMessage().startsWith(
                    "AvroTest:0.0.1: object \"-9223372036854775809\" Avro unmarshalling failed: Numeric value (-9223372036854775809) out of range of long"));
        }
        try {
            schemaHelper3.unmarshal("\"Hello\"");
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertTrue(e.getMessage().equals(
                    "AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: Expected long. Got VALUE_STRING"));
        }
        try {
            schemaHelper3.unmarshal(null);
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertTrue(e.getMessage().equals(
                    "AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: String to read from cannot be null!"));
        }
    }

    // @Test
    public void testFloatUnmarshal() {
        final AxContextSchema avroFloatSchema =
                new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "Avro", "{\"type\": \"float\"}");

        schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema);
        final SchemaHelper schemaHelper4 =
                new SchemaHelperFactory().createSchemaHelper(testKey, avroFloatSchema.getKey());

        try {
            schemaHelper4.createNewInstance();
            fail("test should throw an exception here");
        } catch (final Exception e) {
            assertEquals(
                    "AvroTest:0.0.1: could not create an instance of class \"java.lang.Float\" using the default constructor \"Float()\"",
                    e.getMessage());
        }
        assertEquals(1.2345F, schemaHelper4.createNewInstance("1.2345"));

        assertEquals(0.0F, schemaHelper4.unmarshal("0"));
        assertEquals(1.0F, schemaHelper4.unmarshal("1"));
        assertEquals(-1.0F, schemaHelper4.unmarshal("-1"));
        assertEquals(1.23F, schemaHelper4.unmarshal("1.23"));
        assertEquals(-1.23F, schemaHelper4.unmarshal("-1.23"));
        assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775807"));
        assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775808"));
        assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775808"));
        assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775809"));
        try {
            schemaHelper4.unmarshal("\"Hello\"");
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertTrue(e.getMessage().equals(
                    "AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: Expected float. Got VALUE_STRING"));
        }
        try {
            schemaHelper4.unmarshal(null);
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertTrue(e.getMessage().equals(
                    "AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: String to read from cannot be null!"));
        }
    }

    // @Test
    public void testDoubleUnmarshal() {
        final AxContextSchema avroDoubleSchema =
                new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "Avro", "{\"type\": \"double\"}");

        schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema);
        final SchemaHelper schemaHelper5 =
                new SchemaHelperFactory().createSchemaHelper(testKey, avroDoubleSchema.getKey());

        try {
            schemaHelper5.createNewInstance();
            fail("test should throw an exception here");
        } catch (final Exception e) {
            assertEquals(
                    "AvroTest:0.0.1: could not create an instance of class \"java.lang.Double\" using the default constructor \"Double()\"",
                    e.getMessage());
        }
        assertEquals(1.2345E06, schemaHelper5.createNewInstance("1.2345E06"));

        assertEquals(0.0, schemaHelper5.unmarshal("0"));
        assertEquals(1.0, schemaHelper5.unmarshal("1"));
        assertEquals(-1.0, schemaHelper5.unmarshal("-1"));
        assertEquals(1.23, schemaHelper5.unmarshal("1.23"));
        assertEquals(-1.23, schemaHelper5.unmarshal("-1.23"));
        assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775807"));
        assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775808"));
        assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775808"));
        assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775809"));
        try {
            schemaHelper5.unmarshal("\"Hello\"");
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertTrue(e.getMessage().equals(
                    "AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: Expected double. Got VALUE_STRING"));
        }
        try {
            schemaHelper5.unmarshal(null);
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertTrue(e.getMessage().equals(
                    "AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: String to read from cannot be null!"));
        }
    }

    @Test
    public void testStringUnmarshal() {
        final AxContextSchema avroStringSchema =
                new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "Avro", "{\"type\": \"string\"}");

        schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema);
        final SchemaHelper schemaHelper7 =
                new SchemaHelperFactory().createSchemaHelper(testKey, avroStringSchema.getKey());

        assertEquals("", schemaHelper7.createNewInstance(""));
        assertEquals("1.2345E06", schemaHelper7.createNewInstance("1.2345E06"));

        assertEquals("0", schemaHelper7.unmarshal("0"));
        assertEquals("1", schemaHelper7.unmarshal("1"));
        assertEquals("-1", schemaHelper7.unmarshal("-1"));
        assertEquals("1.23", schemaHelper7.unmarshal("1.23"));
        assertEquals("-1.23", schemaHelper7.unmarshal("-1.23"));
        assertEquals("9223372036854775807", schemaHelper7.unmarshal("9223372036854775807"));
        assertEquals("-9223372036854775808", schemaHelper7.unmarshal("-9223372036854775808"));
        assertEquals("9223372036854775808", schemaHelper7.unmarshal("9223372036854775808"));
        assertEquals("-9223372036854775809", schemaHelper7.unmarshal("-9223372036854775809"));
        assertEquals("Hello", schemaHelper7.unmarshal("Hello"));
        assertEquals("Hello", schemaHelper7.unmarshal(new Utf8("Hello")));
        try {
            schemaHelper7.unmarshal(null);
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertTrue(e.getMessage().equals(
                    "AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: String to read from cannot be null!"));
        }
    }

    @Test
    public void testBytesUnmarshal() {
        final AxContextSchema avroSchema =
                new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "Avro", "{\"type\": \"bytes\"}");

        schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
        final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());

        try {
            schemaHelper.createNewInstance();
            fail("test should throw an exception here");
        } catch (final Exception e) {
            assertEquals(
                    "AvroTest:0.0.1: could not create an instance of class \"java.lang.Byte[]\" using the default constructor \"Byte[]()\"",
                    e.getMessage());
        }
        final byte[] newBytes = (byte[]) schemaHelper.createNewInstance("\"hello\"");
        assertEquals(5, newBytes.length);
        assertEquals(104, newBytes[0]);
        assertEquals(101, newBytes[1]);
        assertEquals(108, newBytes[2]);
        assertEquals(108, newBytes[3]);
        assertEquals(111, newBytes[4]);

        try {
            schemaHelper.unmarshal(null);
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertTrue(e.getMessage().equals(
                    "AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: String to read from cannot be null!"));
        }
    }
}