2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.plugins.context.schema.avro;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
29 import java.io.IOException;
30 import org.apache.avro.generic.GenericData.Fixed;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.context.SchemaHelper;
35 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
36 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
37 import org.onap.policy.apex.context.parameters.SchemaParameters;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
40 import org.onap.policy.apex.model.basicmodel.service.ModelService;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
42 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
43 import org.onap.policy.common.parameters.ParameterService;
44 import org.onap.policy.common.utils.resources.TextFileUtils;
47 * The Class TestAvroSchemaFixed.
49 * @author Liam Fallon (liam.fallon@ericsson.com)
52 public class AvroSchemaFixedTest {
53 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
54 private AxContextSchemas schemas;
55 private String fixedSchema;
60 * @throws IOException Signals that an I/O exception has occurred.
63 public void initTest() throws IOException {
64 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
65 ModelService.registerModel(AxContextSchemas.class, schemas);
66 fixedSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/FixedSchema.avsc");
73 public void initContext() {
74 SchemaParameters schemaParameters = new SchemaParameters();
75 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
76 schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
77 ParameterService.register(schemaParameters);
85 public void clearContext() {
86 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
92 * @throws IOException Signals that an I/O exception has occurred.
95 public void testFixedInit() throws IOException {
96 final AxContextSchema avroSchema =
97 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", fixedSchema);
99 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
100 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
102 assertThatThrownBy(() -> schemaHelper.createNewInstance())
103 .hasMessage("AvroTest:0.0.1: could not create an instance "
104 + "of class \"org.apache.avro.generic.GenericData$Fixed\" "
105 + "using the default constructor \"Fixed()\"");
106 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/FixedExampleGood.json");
107 final Fixed newFixedFull = (Fixed) schemaHelper.createNewInstance(inString);
108 assertTrue(newFixedFull.toString().startsWith("[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65"));
109 assertTrue(newFixedFull.toString().endsWith("53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70]"));
113 * Test fixed unmarshal marshal.
115 * @throws IOException Signals that an I/O exception has occurred.
118 public void testFixedUnmarshalMarshal() throws IOException {
119 final AxContextSchema avroSchema =
120 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", fixedSchema);
122 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
123 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
125 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleGood.json");
127 assertThatThrownBy(() -> testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json"))
128 .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed.");
129 assertThatThrownBy(() -> testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json"))
130 .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed.");
131 assertThatThrownBy(() -> testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad0.json"))
132 .hasMessage("AvroTest:0.0.1: object \"\"BADBAD\"\" " + "Avro unmarshalling failed.");
133 assertThatThrownBy(() -> testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad1.json"))
135 "AvroTest:0.0.1: object " + "\"\"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0\"\" "
136 + "Avro unmarshalling failed.");
140 * Test unmarshal marshal.
142 * @param schemaHelper the schema helper
143 * @param fileName the file name
144 * @throws IOException Signals that an I/O exception has occurred.
146 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
147 final String inString = TextFileUtils.getTextFileAsString(fileName);
148 final Fixed decodedObject = (Fixed) schemaHelper.unmarshal(inString);
149 final String outString = schemaHelper.marshal2String(decodedObject);
150 assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " "));