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;
27 import java.io.IOException;
28 import java.util.HashMap;
30 import org.apache.avro.generic.GenericRecord;
31 import org.apache.avro.util.Utf8;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.apex.context.SchemaHelper;
36 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
37 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
38 import org.onap.policy.apex.context.parameters.SchemaParameters;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
41 import org.onap.policy.apex.model.basicmodel.service.ModelService;
42 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
43 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
44 import org.onap.policy.common.parameters.ParameterService;
45 import org.onap.policy.common.utils.resources.TextFileUtils;
48 * The Class TestAvroSchemaMap.
50 * @author Liam Fallon (liam.fallon@ericsson.com)
53 public class AvroSchemaMapTest {
54 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
55 private AxContextSchemas schemas;
56 private String longMapSchema;
57 private String addressMapSchema;
58 private String addressMapSchemaInvalidFields;
63 * @throws IOException Signals that an I/O exception has occurred.
66 public void initTest() throws IOException {
67 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
68 ModelService.registerModel(AxContextSchemas.class, schemas);
69 longMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleLong.avsc");
70 addressMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleAddress.avsc");
71 addressMapSchemaInvalidFields =
72 TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleAddressInvalidFields.avsc");
79 public void initContext() {
80 SchemaParameters schemaParameters = new SchemaParameters();
81 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
82 schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
83 ParameterService.register(schemaParameters);
91 public void clearContext() {
92 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
98 * @throws IOException Signals that an I/O exception has occurred.
101 public void testMapInit() throws IOException {
102 final AxContextSchema avroSchema =
103 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", addressMapSchema);
105 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
106 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
108 final HashMap<?, ?> newMapEmpty = (HashMap<?, ?>) schemaHelper.createNewInstance();
109 assertEquals(0, newMapEmpty.size());
111 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/MapExampleAddressFull.json");
112 final HashMap<?, ?> newMapFull = (HashMap<?, ?>) schemaHelper.createNewInstance(inString);
114 assertEquals("{\"streetaddress\": \"221 B Baker St.\", \"city\": \"London\"}",
115 newMapFull.get(new Utf8("address2")).toString());
119 * Test long map unmarshal marshal.
121 * @throws IOException Signals that an I/O exception has occurred.
124 public void testLongMapUnmarshalMarshal() throws IOException {
125 final AxContextSchema avroSchema =
126 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", longMapSchema);
128 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
129 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
131 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongNull.json");
132 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongFull.json");
136 * Test address map unmarshal marshal.
138 * @throws IOException Signals that an I/O exception has occurred.
141 public void testAddressMapUnmarshalMarshal() throws IOException {
142 final AxContextSchema avroSchema =
143 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", addressMapSchema);
145 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
146 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
148 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressNull.json");
149 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressFull.json");
153 * Test sub record create.
155 * @throws IOException Signals that an I/O exception has occurred.
158 public void testSubRecordCreateRecord() throws IOException {
159 final AxContextSchema avroSchema =
160 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", addressMapSchema);
162 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
163 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
165 GenericRecord subRecord = (GenericRecord) schemaHelper.createNewSubInstance("AddressUSRecord");
166 assertEquals(null, subRecord.get("streetAddress"));
170 * Test address map unmarshal marshal invalid fields.
172 * @throws IOException Signals that an I/O exception has occurred.
175 public void testAddressMapUnmarshalMarshalInvalidFields() throws IOException {
176 final AxContextSchema avroSchema =
177 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", addressMapSchemaInvalidFields);
179 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
180 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
182 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressInvalidFields.json");
186 * Test unmarshal marshal.
188 * @param schemaHelper the schema helper
189 * @param fileName the file name
190 * @throws IOException Signals that an I/O exception has occurred.
192 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
193 final String originalInString = TextFileUtils.getTextFileAsString(fileName);
194 final HashMap<?, ?> firstDecodedMap = (HashMap<?, ?>) schemaHelper.unmarshal(originalInString);
196 final String outString = schemaHelper.marshal2String(firstDecodedMap);
198 final File tempOutFile = File.createTempFile("ApexAvro", ".json");
199 TextFileUtils.putStringAsFile(outString, tempOutFile);
201 final String decodeEncodeInString = TextFileUtils.getTextFileAsString(fileName);
202 tempOutFile.delete();
204 final HashMap<?, ?> secondDecodedMap = (HashMap<?, ?>) schemaHelper.unmarshal(decodeEncodeInString);
206 // Now check that our doubly encoded map equals the first decoded map, Java map equals
207 // checks values and keys
208 assertEquals(firstDecodedMap, secondDecodedMap);