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.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.context.SchemaHelper;
33 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
34 import org.onap.policy.apex.context.parameters.SchemaParameters;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
37 import org.onap.policy.apex.model.basicmodel.service.ModelService;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
40 import org.onap.policy.apex.model.utilities.TextFileUtils;
43 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class TestAvroSchemaMap {
47 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
48 private AxContextSchemas schemas;
49 private String longMapSchema;
50 private String addressMapSchema;
51 private String addressMapSchemaInvalidFields;
54 public void initTest() throws IOException {
55 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
56 ModelService.registerModel(AxContextSchemas.class, schemas);
57 new SchemaParameters().getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
58 longMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleLong.avsc");
59 addressMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleAddress.avsc");
60 addressMapSchemaInvalidFields =
61 TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleAddressInvalidFields.avsc");
65 public void testMapInit() throws IOException {
66 final AxContextSchema avroSchema =
67 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "Avro", addressMapSchema);
69 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
70 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
72 final HashMap<?, ?> newMapEmpty = (HashMap<?, ?>) schemaHelper.createNewInstance();
73 assertEquals(0, newMapEmpty.size());
75 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/MapExampleAddressFull.json");
76 final HashMap<?, ?> newMapFull = (HashMap<?, ?>) schemaHelper.createNewInstance(inString);
78 assertEquals("{\"streetaddress\": \"221 B Baker St.\", \"city\": \"London\"}",
79 newMapFull.get(new Utf8("address2")).toString());
83 public void testLongMapUnmarshalMarshal() throws IOException {
84 final AxContextSchema avroSchema =
85 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "Avro", longMapSchema);
87 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
88 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
90 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongNull.json");
91 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongFull.json");
95 public void testAddressMapUnmarshalMarshal() throws IOException {
96 final AxContextSchema avroSchema =
97 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "Avro", addressMapSchema);
99 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
100 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
102 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressNull.json");
103 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressFull.json");
107 public void testAddressMapUnmarshalMarshalInvalidFields() throws IOException {
108 final AxContextSchema avroSchema =
109 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "Avro", addressMapSchemaInvalidFields);
111 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
112 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
114 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressInvalidFields.json");
117 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
118 final String originalInString = TextFileUtils.getTextFileAsString(fileName);
119 final HashMap<?, ?> firstDecodedMap = (HashMap<?, ?>) schemaHelper.unmarshal(originalInString);
121 final String outString = schemaHelper.marshal2String(firstDecodedMap);
123 final File tempOutFile = File.createTempFile("ApexAvro", ".json");
124 TextFileUtils.putStringAsFile(outString, tempOutFile);
126 final String decodeEncodeInString = TextFileUtils.getTextFileAsString(fileName);
127 tempOutFile.delete();
129 final HashMap<?, ?> secondDecodedMap = (HashMap<?, ?>) schemaHelper.unmarshal(decodeEncodeInString);
131 // Now check that our doubly encoded map equals the first decoded map, Java map equals
132 // checks values and keys
133 assertEquals(firstDecodedMap, secondDecodedMap);