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.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.apex.context.SchemaHelper;
34 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
35 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
36 import org.onap.policy.apex.context.parameters.SchemaParameters;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
39 import org.onap.policy.apex.model.basicmodel.service.ModelService;
40 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
42 import org.onap.policy.apex.model.utilities.TextFileUtils;
43 import org.onap.policy.common.parameters.ParameterService;
46 * @author Liam Fallon (liam.fallon@ericsson.com)
49 public class TestAvroSchemaFixed {
50 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
51 private AxContextSchemas schemas;
52 private String fixedSchema;
55 public void initTest() throws IOException {
56 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
57 ModelService.registerModel(AxContextSchemas.class, schemas);
58 fixedSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/FixedSchema.avsc");
62 public void initContext() {
63 SchemaParameters schemaParameters = new SchemaParameters();
64 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
65 schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
66 ParameterService.register(schemaParameters);
71 public void clearContext() {
72 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
76 public void testFixedInit() throws IOException {
77 final AxContextSchema avroSchema =
78 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", fixedSchema);
80 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
81 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
84 schemaHelper.createNewInstance();
85 fail("Test should throw an exception here");
86 } catch (final Exception e) {
88 "AvroTest:0.0.1: could not create an instance of class \"org.apache.avro.generic.GenericData.Fixed\" using the default constructor \"Fixed()\"",
92 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/FixedExampleGood.json");
93 final Fixed newFixedFull = (Fixed) schemaHelper.createNewInstance(inString);
94 assertTrue(newFixedFull.toString().startsWith("[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65"));
95 assertTrue(newFixedFull.toString().endsWith("53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70]"));
99 public void testFixedUnmarshalMarshal() throws IOException {
100 final AxContextSchema avroSchema =
101 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", fixedSchema);
103 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
104 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
106 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleGood.json");
109 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json");
110 fail("This test should throw an exception here");
111 } catch (final Exception e) {
112 assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
116 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json");
117 fail("This test should throw an exception here");
118 } catch (final Exception e) {
119 assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
123 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad0.json");
124 fail("This test should throw an exception here");
125 } catch (final Exception e) {
127 "AvroTest:0.0.1: object \"\"BADBAD\"\" Avro unmarshalling failed: Expected fixed length 64, but got6",
131 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad1.json");
132 fail("This test should throw an exception here");
133 } catch (final Exception e) {
135 "AvroTest:0.0.1: object \"\"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0\"\" Avro unmarshalling failed: Expected fixed length 64, but got65",
140 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
141 final String inString = TextFileUtils.getTextFileAsString(fileName);
142 final Fixed decodedObject = (Fixed) schemaHelper.unmarshal(inString);
143 final String outString = schemaHelper.marshal2String(decodedObject);
144 assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " "));