2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.context.schema.avro;
24 import static org.junit.Assert.assertEquals;
26 import java.io.IOException;
28 import org.apache.avro.generic.GenericRecord;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Ignore;
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.common.parameters.ParameterService;
43 import org.onap.policy.common.utils.resources.TextFileUtils;
46 * The Class TestAvroSchemaUnion.
48 * @author Liam Fallon (liam.fallon@ericsson.com)
51 public class AvroSchemaUnionTest {
52 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
53 private AxContextSchemas schemas;
54 private String uinionSchema;
59 * @throws IOException Signals that an I/O exception has occurred.
62 public void initTest() throws IOException {
63 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
64 ModelService.registerModel(AxContextSchemas.class, schemas);
65 uinionSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/UnionExample.avsc");
72 public void initContext() {
73 SchemaParameters schemaParameters = new SchemaParameters();
74 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
75 schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
76 ParameterService.register(schemaParameters);
84 public void clearContext() {
85 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
89 * Test union all fields.
91 * @throws IOException Signals that an I/O exception has occurred.
95 public void testUnionAllFields() throws IOException {
96 final AxContextSchema avroSchema =
97 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema);
99 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
100 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
102 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleAllFields.json");
103 final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString);
105 assertEquals("Ben", user.get("name").toString());
106 assertEquals(7, user.get("favourite_number"));
107 assertEquals("red", user.get("favourite_colour").toString());
111 * Test union optional field.
113 * @throws IOException Signals that an I/O exception has occurred.
117 public void testUnionOptionalField() throws IOException {
118 final AxContextSchema avroSchema =
119 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema);
121 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
122 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
124 final String inString =
125 TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleOptionalField.json");
126 final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString);
128 assertEquals("Ben", user.get("name").toString());
129 assertEquals(7, user.get("favourite_number"));
130 assertEquals("red", user.get("favourite_colour").toString());
134 * Test union null field.
136 * @throws IOException Signals that an I/O exception has occurred.
140 public void testUnionNullField() throws IOException {
141 final AxContextSchema avroSchema =
142 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema);
144 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
145 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
147 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleNullField.json");
148 final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString);
150 assertEquals("Ben", user.get("name").toString());
151 assertEquals(7, user.get("favourite_number"));
152 assertEquals("red", user.get("favourite_colour").toString());