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;
29 import org.apache.avro.generic.GenericRecord;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.apex.context.ContextRuntimeException;
34 import org.onap.policy.apex.context.SchemaHelper;
35 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
36 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
37 import org.onap.policy.apex.context.parameters.SchemaParameters;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
40 import org.onap.policy.apex.model.basicmodel.service.ModelService;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
42 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
43 import org.onap.policy.common.parameters.ParameterService;
44 import org.onap.policy.common.utils.resources.TextFileUtils;
47 * The Class TestAvroSchemaRecord.
49 * @author Liam Fallon (liam.fallon@ericsson.com)
52 public class AvroSchemaRecordTest {
53 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
54 private AxContextSchemas schemas;
55 private String recordSchema;
56 private String recordSchemaVpn;
57 private String recordSchemaVpnReuse;
58 private String recordSchemaInvalidFields;
63 * @throws IOException Signals that an I/O exception has occurred.
66 public void initTest() throws IOException {
67 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
68 ModelService.registerModel(AxContextSchemas.class, schemas);
69 recordSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExample.avsc");
70 recordSchemaVpn = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPN.avsc");
71 recordSchemaVpnReuse = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPNReuse.avsc");
72 recordSchemaInvalidFields =
73 TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleInvalidFields.avsc");
80 public void initContext() {
81 SchemaParameters schemaParameters = new SchemaParameters();
82 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
83 schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
84 ParameterService.register(schemaParameters);
92 public void clearContext() {
93 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
99 * @throws IOException Signals that an I/O exception has occurred.
102 public void testRecordInit() throws IOException {
103 final AxContextSchema avroSchema =
104 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
106 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
107 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
109 final GenericRecord newRecordEmpty = (GenericRecord) schemaHelper.createNewInstance();
110 assertEquals(null, newRecordEmpty.get("passwordHash"));
112 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/RecordExampleFull.json");
113 final GenericRecord newRecordFull = (GenericRecord) schemaHelper.createNewInstance(inString);
114 assertEquals("gobbledygook", newRecordFull.get("passwordHash").toString());
118 * Test record unmarshal marshal.
120 * @throws IOException Signals that an I/O exception has occurred.
123 public void testRecordUnmarshalMarshal() throws IOException {
124 final AxContextSchema avroSchema =
125 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
127 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
128 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
130 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleNull.json");
131 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleFull.json");
135 * Test record create.
137 * @throws IOException Signals that an I/O exception has occurred.
140 public void testRecordCreateRecord() throws IOException {
141 final AxContextSchema avroSchema =
142 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
144 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
145 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
147 GenericRecord subRecord = (GenericRecord) schemaHelper.createNewSubInstance("AddressUSRecord");
148 assertEquals(null, subRecord.get("streetAddress"));
150 subRecord = (GenericRecord) schemaHelper.createNewSubInstance("EmailAddress");
151 assertEquals(null, subRecord.get("address"));
154 subRecord = (GenericRecord) schemaHelper.createNewSubInstance("IDontExist");
155 fail("test should throw an exception here");
156 } catch (ContextRuntimeException cre) {
157 assertEquals("AvroTest:0.0.1: the schema \"User\" does not have a subtype of type \"IDontExist\"",
163 * Test record unmarshal marshal invalid.
165 * @throws IOException Signals that an I/O exception has occurred.
168 public void testRecordUnmarshalMarshalInvalid() throws IOException {
169 final AxContextSchema avroSchema =
170 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaInvalidFields);
172 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
173 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
175 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleInvalidFields.json");
179 * Test VPN record unmarshal marshal.
181 * @throws IOException Signals that an I/O exception has occurred.
184 public void testVpnRecordUnmarshalMarshal() throws IOException {
185 final AxContextSchema avroSchema =
186 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpn);
188 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
189 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
191 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleVPNFull.json");
195 * Test VPN record reuse.
197 * @throws IOException Signals that an I/O exception has occurred.
200 public void testVpnRecordReuse() throws IOException {
201 final AxContextSchema avroSchema =
202 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpnReuse);
203 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
205 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
206 new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
210 * Test unmarshal marshal.
212 * @param schemaHelper the schema helper
213 * @param fileName the file name
214 * @throws IOException Signals that an I/O exception has occurred.
216 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
217 final String inString = TextFileUtils.getTextFileAsString(fileName);
218 final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString);
219 final String outString = schemaHelper.marshal2String(decodedObject);
220 assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));