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;
25 import java.io.IOException;
27 import org.apache.avro.generic.GenericRecord;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.apex.context.SchemaHelper;
32 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
33 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
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;
41 import org.onap.policy.common.parameters.ParameterService;
43 // TODO: Auto-generated Javadoc
45 * The Class TestAvroSchemaRecord.
47 * @author Liam Fallon (liam.fallon@ericsson.com)
50 public class TestAvroSchemaRecord {
51 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
52 private AxContextSchemas schemas;
53 private String recordSchema;
54 private String recordSchemaVpn;
55 private String recordSchemaVpnReuse;
56 private String recordSchemaInvalidFields;
61 * @throws IOException Signals that an I/O exception has occurred.
64 public void initTest() throws IOException {
65 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
66 ModelService.registerModel(AxContextSchemas.class, schemas);
67 recordSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExample.avsc");
68 recordSchemaVpn = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPN.avsc");
69 recordSchemaVpnReuse = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPNReuse.avsc");
70 recordSchemaInvalidFields =
71 TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleInvalidFields.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 testRecordInit() throws IOException {
101 final AxContextSchema avroSchema =
102 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
104 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
105 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
107 final GenericRecord newRecordEmpty = (GenericRecord) schemaHelper.createNewInstance();
108 assertEquals(null, newRecordEmpty.get("passwordHash"));
110 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/RecordExampleFull.json");
111 final GenericRecord newRecordFull = (GenericRecord) schemaHelper.createNewInstance(inString);
112 assertEquals("gobbledygook", newRecordFull.get("passwordHash").toString());
116 * Test record unmarshal marshal.
118 * @throws IOException Signals that an I/O exception has occurred.
121 public void testRecordUnmarshalMarshal() throws IOException {
122 final AxContextSchema avroSchema =
123 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
125 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
126 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
128 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleNull.json");
129 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleFull.json");
133 * Test record unmarshal marshal invalid.
135 * @throws IOException Signals that an I/O exception has occurred.
138 public void testRecordUnmarshalMarshalInvalid() throws IOException {
139 final AxContextSchema avroSchema =
140 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaInvalidFields);
142 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
143 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
145 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleInvalidFields.json");
149 * Test VPN record unmarshal marshal.
151 * @throws IOException Signals that an I/O exception has occurred.
154 public void testVpnRecordUnmarshalMarshal() throws IOException {
155 final AxContextSchema avroSchema =
156 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpn);
158 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
159 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
161 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleVPNFull.json");
165 * Test VPN record reuse.
167 * @throws IOException Signals that an I/O exception has occurred.
170 public void testVpnRecordReuse() throws IOException {
171 final AxContextSchema avroSchema =
172 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpnReuse);
173 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
175 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
176 new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
180 * Test unmarshal marshal.
182 * @param schemaHelper the schema helper
183 * @param fileName the file name
184 * @throws IOException Signals that an I/O exception has occurred.
186 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
187 final String inString = TextFileUtils.getTextFileAsString(fileName);
188 final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString);
189 final String outString = schemaHelper.marshal2String(decodedObject);
190 assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));