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.util.Utf8;
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 TestAvroSchemaMap {
50 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
51 private AxContextSchemas schemas;
52 private String longMapSchema;
53 private String addressMapSchema;
54 private String addressMapSchemaInvalidFields;
57 public void initTest() throws IOException {
58 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
59 ModelService.registerModel(AxContextSchemas.class, schemas);
60 longMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleLong.avsc");
61 addressMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleAddress.avsc");
62 addressMapSchemaInvalidFields =
63 TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleAddressInvalidFields.avsc");
67 public void initContext() {
68 SchemaParameters schemaParameters = new SchemaParameters();
69 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
70 schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
71 ParameterService.register(schemaParameters);
76 public void clearContext() {
77 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
81 public void testMapInit() throws IOException {
82 final AxContextSchema avroSchema =
83 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", addressMapSchema);
85 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
86 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
88 final HashMap<?, ?> newMapEmpty = (HashMap<?, ?>) schemaHelper.createNewInstance();
89 assertEquals(0, newMapEmpty.size());
91 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/MapExampleAddressFull.json");
92 final HashMap<?, ?> newMapFull = (HashMap<?, ?>) schemaHelper.createNewInstance(inString);
94 assertEquals("{\"streetaddress\": \"221 B Baker St.\", \"city\": \"London\"}",
95 newMapFull.get(new Utf8("address2")).toString());
99 public void testLongMapUnmarshalMarshal() throws IOException {
100 final AxContextSchema avroSchema =
101 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", longMapSchema);
103 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
104 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
106 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongNull.json");
107 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongFull.json");
111 public void testAddressMapUnmarshalMarshal() throws IOException {
112 final AxContextSchema avroSchema =
113 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", addressMapSchema);
115 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
116 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
118 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressNull.json");
119 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressFull.json");
123 public void testAddressMapUnmarshalMarshalInvalidFields() throws IOException {
124 final AxContextSchema avroSchema =
125 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", addressMapSchemaInvalidFields);
127 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
128 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
130 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressInvalidFields.json");
133 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
134 final String originalInString = TextFileUtils.getTextFileAsString(fileName);
135 final HashMap<?, ?> firstDecodedMap = (HashMap<?, ?>) schemaHelper.unmarshal(originalInString);
137 final String outString = schemaHelper.marshal2String(firstDecodedMap);
139 final File tempOutFile = File.createTempFile("ApexAvro", ".json");
140 TextFileUtils.putStringAsFile(outString, tempOutFile);
142 final String decodeEncodeInString = TextFileUtils.getTextFileAsString(fileName);
143 tempOutFile.delete();
145 final HashMap<?, ?> secondDecodedMap = (HashMap<?, ?>) schemaHelper.unmarshal(decodeEncodeInString);
147 // Now check that our doubly encoded map equals the first decoded map, Java map equals
148 // checks values and keys
149 assertEquals(firstDecodedMap, secondDecodedMap);