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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
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.SchemaHelper;
33 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
34 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
35 import org.onap.policy.apex.context.parameters.SchemaParameters;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
38 import org.onap.policy.apex.model.basicmodel.service.ModelService;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
40 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
41 import org.onap.policy.common.parameters.ParameterService;
42 import org.onap.policy.common.utils.resources.TextFileUtils;
45 * The Class TestAvroSchemaRecord.
47 * @author Liam Fallon (liam.fallon@ericsson.com)
50 public class AvroSchemaRecordTest {
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 create.
135 * @throws IOException Signals that an I/O exception has occurred.
138 public void testRecordCreateRecord() throws IOException {
139 final AxContextSchema avroSchema =
140 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
142 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
143 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
145 GenericRecord subRecord = (GenericRecord) schemaHelper.createNewSubInstance("AddressUSRecord");
146 assertEquals(null, subRecord.get("streetAddress"));
148 subRecord = (GenericRecord) schemaHelper.createNewSubInstance("EmailAddress");
149 assertEquals(null, subRecord.get("address"));
151 assertThatThrownBy(() -> schemaHelper.createNewSubInstance("IDontExist"))
152 .hasMessage("AvroTest:0.0.1: the schema \"User\" does not have a subtype of type \"IDontExist\"");
156 * Test record unmarshal marshal invalid.
158 * @throws IOException Signals that an I/O exception has occurred.
161 public void testRecordUnmarshalMarshalInvalid() throws IOException {
162 final AxContextSchema avroSchema =
163 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaInvalidFields);
165 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
166 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
168 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleInvalidFields.json");
172 * Test VPN record unmarshal marshal.
174 * @throws IOException Signals that an I/O exception has occurred.
177 public void testVpnRecordUnmarshalMarshal() throws IOException {
178 final AxContextSchema avroSchema =
179 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpn);
181 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
182 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
184 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleVPNFull.json");
188 * Test VPN record reuse.
190 * @throws IOException Signals that an I/O exception has occurred.
193 public void testVpnRecordReuse() throws IOException {
194 final AxContextSchema avroSchema =
195 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpnReuse);
196 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
198 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
199 new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
203 * Test unmarshal marshal.
205 * @param schemaHelper the schema helper
206 * @param fileName the file name
207 * @throws IOException Signals that an I/O exception has occurred.
209 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
210 final String inString = TextFileUtils.getTextFileAsString(fileName);
211 final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString);
212 final String outString = schemaHelper.marshal2String(decodedObject);
213 assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));