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;
25 import static org.junit.Assert.fail;
27 import java.io.IOException;
28 import org.apache.avro.generic.GenericRecord;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.context.ContextRuntimeException;
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.common.parameters.ParameterService;
43 import org.onap.policy.common.utils.resources.TextFileUtils;
46 * The Class TestAvroSchemaRecord.
48 * @author Liam Fallon (liam.fallon@ericsson.com)
51 public class AvroSchemaRecordTest {
52 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
53 private AxContextSchemas schemas;
54 private String recordSchema;
55 private String recordSchemaVpn;
56 private String recordSchemaVpnReuse;
57 private String recordSchemaInvalidFields;
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 recordSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExample.avsc");
69 recordSchemaVpn = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPN.avsc");
70 recordSchemaVpnReuse = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPNReuse.avsc");
71 recordSchemaInvalidFields =
72 TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleInvalidFields.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 testRecordInit() throws IOException {
102 final AxContextSchema avroSchema =
103 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
105 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
106 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
108 final GenericRecord newRecordEmpty = (GenericRecord) schemaHelper.createNewInstance();
109 assertEquals(null, newRecordEmpty.get("passwordHash"));
111 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/RecordExampleFull.json");
112 final GenericRecord newRecordFull = (GenericRecord) schemaHelper.createNewInstance(inString);
113 assertEquals("gobbledygook", newRecordFull.get("passwordHash").toString());
117 * Test record unmarshal marshal.
119 * @throws IOException Signals that an I/O exception has occurred.
122 public void testRecordUnmarshalMarshal() throws IOException {
123 final AxContextSchema avroSchema =
124 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
126 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
127 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
129 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleNull.json");
130 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleFull.json");
134 * Test record create.
136 * @throws IOException Signals that an I/O exception has occurred.
139 public void testRecordCreateRecord() throws IOException {
140 final AxContextSchema avroSchema =
141 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
143 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
144 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
146 GenericRecord subRecord = (GenericRecord) schemaHelper.createNewSubInstance("AddressUSRecord");
147 assertEquals(null, subRecord.get("streetAddress"));
149 subRecord = (GenericRecord) schemaHelper.createNewSubInstance("EmailAddress");
150 assertEquals(null, subRecord.get("address"));
153 subRecord = (GenericRecord) schemaHelper.createNewSubInstance("IDontExist");
154 fail("test should throw an exception here");
155 } catch (ContextRuntimeException cre) {
156 assertEquals("AvroTest:0.0.1: the schema \"User\" does not have a subtype of type \"IDontExist\"",
162 * Test record unmarshal marshal invalid.
164 * @throws IOException Signals that an I/O exception has occurred.
167 public void testRecordUnmarshalMarshalInvalid() throws IOException {
168 final AxContextSchema avroSchema =
169 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaInvalidFields);
171 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
172 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
174 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleInvalidFields.json");
178 * Test VPN record unmarshal marshal.
180 * @throws IOException Signals that an I/O exception has occurred.
183 public void testVpnRecordUnmarshalMarshal() throws IOException {
184 final AxContextSchema avroSchema =
185 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpn);
187 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
188 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
190 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleVPNFull.json");
194 * Test VPN record reuse.
196 * @throws IOException Signals that an I/O exception has occurred.
199 public void testVpnRecordReuse() throws IOException {
200 final AxContextSchema avroSchema =
201 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpnReuse);
202 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
204 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
205 new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
209 * Test unmarshal marshal.
211 * @param schemaHelper the schema helper
212 * @param fileName the file name
213 * @throws IOException Signals that an I/O exception has occurred.
215 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
216 final String inString = TextFileUtils.getTextFileAsString(fileName);
217 final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString);
218 final String outString = schemaHelper.marshal2String(decodedObject);
219 assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));