2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.context.schema.avro;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
27 import java.io.IOException;
29 import org.apache.avro.generic.GenericData.Fixed;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.context.SchemaHelper;
33 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
34 import org.onap.policy.apex.context.parameters.SchemaParameters;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
37 import org.onap.policy.apex.model.basicmodel.service.ModelService;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
40 import org.onap.policy.apex.model.utilities.TextFileUtils;
43 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class TestAvroSchemaFixed {
47 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
48 private AxContextSchemas schemas;
49 private String fixedSchema;
52 public void initTest() throws IOException {
53 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
54 ModelService.registerModel(AxContextSchemas.class, schemas);
55 new SchemaParameters().getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
56 fixedSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/FixedSchema.avsc");
60 public void testFixedInit() throws IOException {
61 final AxContextSchema avroSchema =
62 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "Avro", fixedSchema);
64 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
65 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
68 schemaHelper.createNewInstance();
69 fail("Test should throw an exception here");
70 } catch (final Exception e) {
72 "AvroTest:0.0.1: could not create an instance of class \"org.apache.avro.generic.GenericData.Fixed\" using the default constructor \"Fixed()\"",
76 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/FixedExampleGood.json");
77 final Fixed newFixedFull = (Fixed) schemaHelper.createNewInstance(inString);
78 assertTrue(newFixedFull.toString().startsWith("[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65"));
79 assertTrue(newFixedFull.toString().endsWith("53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70]"));
83 public void testFixedUnmarshalMarshal() throws IOException {
84 final AxContextSchema avroSchema =
85 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "Avro", fixedSchema);
87 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
88 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
90 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleGood.json");
93 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json");
94 fail("This test should throw an exception here");
95 } catch (final Exception e) {
96 assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
100 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json");
101 fail("This test should throw an exception here");
102 } catch (final Exception e) {
103 assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
107 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad0.json");
108 fail("This test should throw an exception here");
109 } catch (final Exception e) {
111 "AvroTest:0.0.1: object \"\"BADBAD\"\" Avro unmarshalling failed: Expected fixed length 64, but got6",
115 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad1.json");
116 fail("This test should throw an exception here");
117 } catch (final Exception e) {
119 "AvroTest:0.0.1: object \"\"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0\"\" Avro unmarshalling failed: Expected fixed length 64, but got65",
124 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
125 final String inString = TextFileUtils.getTextFileAsString(fileName);
126 final Fixed decodedObject = (Fixed) schemaHelper.unmarshal(inString);
127 final String outString = schemaHelper.marshal2String(decodedObject);
128 assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " "));