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;
26 import java.io.IOException;
27 import java.util.HashMap;
29 import org.apache.avro.generic.GenericRecord;
30 import org.apache.avro.util.Utf8;
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.apex.model.utilities.TextFileUtils;
44 import org.onap.policy.common.parameters.ParameterService;
47 * The Class TestAvroSchemaMap.
49 * @author Liam Fallon (liam.fallon@ericsson.com)
52 public class AvroSchemaMapTest {
53 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
54 private AxContextSchemas schemas;
55 private String longMapSchema;
56 private String addressMapSchema;
57 private String addressMapSchemaInvalidFields;
62 * @throws IOException Signals that an I/O exception has occurred.
65 public void initTest() throws IOException {
66 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
67 ModelService.registerModel(AxContextSchemas.class, schemas);
68 longMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleLong.avsc");
69 addressMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleAddress.avsc");
70 addressMapSchemaInvalidFields = TextFileUtils
71 .getTextFileAsString("src/test/resources/avsc/MapExampleAddressInvalidFields.avsc");
78 public void initContext() {
79 SchemaParameters schemaParameters = new SchemaParameters();
80 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
81 schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
82 ParameterService.register(schemaParameters);
90 public void clearContext() {
91 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
97 * @throws IOException Signals that an I/O exception has occurred.
100 public void testMapInit() throws IOException {
101 final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO",
104 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
105 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
107 final HashMap<?, ?> newMapEmpty = (HashMap<?, ?>) schemaHelper.createNewInstance();
108 assertEquals(0, newMapEmpty.size());
110 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/MapExampleAddressFull.json");
111 final HashMap<?, ?> newMapFull = (HashMap<?, ?>) schemaHelper.createNewInstance(inString);
113 assertEquals("{\"streetaddress\": \"221 B Baker St.\", \"city\": \"London\"}",
114 newMapFull.get(new Utf8("address2")).toString());
118 * Test long map unmarshal marshal.
120 * @throws IOException Signals that an I/O exception has occurred.
123 public void testLongMapUnmarshalMarshal() throws IOException {
124 final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO",
127 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
128 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
130 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongNull.json");
131 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongFull.json");
135 * Test address map unmarshal marshal.
137 * @throws IOException Signals that an I/O exception has occurred.
140 public void testAddressMapUnmarshalMarshal() throws IOException {
141 final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO",
144 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
145 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
147 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressNull.json");
148 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressFull.json");
152 * Test sub record create.
154 * @throws IOException Signals that an I/O exception has occurred.
157 public void testSubRecordCreateRecord() throws IOException {
158 final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO",
161 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
162 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
164 GenericRecord subRecord = (GenericRecord) schemaHelper.createNewSubInstance("AddressUSRecord");
165 assertEquals(null, subRecord.get("streetAddress"));
169 * Test address map unmarshal marshal invalid fields.
171 * @throws IOException Signals that an I/O exception has occurred.
174 public void testAddressMapUnmarshalMarshalInvalidFields() throws IOException {
175 final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO",
176 addressMapSchemaInvalidFields);
178 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
179 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
181 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressInvalidFields.json");
185 * Test unmarshal marshal.
187 * @param schemaHelper the schema helper
188 * @param fileName the file name
189 * @throws IOException Signals that an I/O exception has occurred.
191 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
192 final String originalInString = TextFileUtils.getTextFileAsString(fileName);
193 final HashMap<?, ?> firstDecodedMap = (HashMap<?, ?>) schemaHelper.unmarshal(originalInString);
195 final String outString = schemaHelper.marshal2String(firstDecodedMap);
197 final File tempOutFile = File.createTempFile("ApexAvro", ".json");
198 TextFileUtils.putStringAsFile(outString, tempOutFile);
200 final String decodeEncodeInString = TextFileUtils.getTextFileAsString(fileName);
201 tempOutFile.delete();
203 final HashMap<?, ?> secondDecodedMap = (HashMap<?, ?>) schemaHelper.unmarshal(decodeEncodeInString);
205 // Now check that our doubly encoded map equals the first decoded map, Java map equals
206 // checks values and keys
207 assertEquals(firstDecodedMap, secondDecodedMap);