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;
44 * @author Liam Fallon (liam.fallon@ericsson.com)
47 public class TestAvroSchemaRecord {
48 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
49 private AxContextSchemas schemas;
50 private String recordSchema;
51 private String recordSchemaVPN;
52 private String recordSchemaVPNReuse;
53 private String recordSchemaInvalidFields;
56 public void initTest() throws IOException {
57 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
58 ModelService.registerModel(AxContextSchemas.class, schemas);
59 recordSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExample.avsc");
60 recordSchemaVPN = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPN.avsc");
61 recordSchemaVPNReuse = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPNReuse.avsc");
62 recordSchemaInvalidFields =
63 TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleInvalidFields.avsc");
67 public void initContext() {
68 SchemaParameters schemaParameters = new SchemaParameters();
69 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
70 schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
71 ParameterService.register(schemaParameters);
76 public void clearContext() {
77 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
81 public void testRecordInit() throws IOException {
82 final AxContextSchema avroSchema =
83 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
85 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
86 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
88 final GenericRecord newRecordEmpty = (GenericRecord) schemaHelper.createNewInstance();
89 assertEquals(null, newRecordEmpty.get("passwordHash"));
91 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/RecordExampleFull.json");
92 final GenericRecord newRecordFull = (GenericRecord) schemaHelper.createNewInstance(inString);
93 assertEquals("gobbledygook", newRecordFull.get("passwordHash").toString());
97 public void testRecordUnmarshalMarshal() throws IOException {
98 final AxContextSchema avroSchema =
99 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
101 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
102 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
104 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleNull.json");
105 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleFull.json");
109 public void testRecordUnmarshalMarshalInvalid() throws IOException {
110 final AxContextSchema avroSchema =
111 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaInvalidFields);
113 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
114 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
116 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleInvalidFields.json");
120 public void testVPNRecordUnmarshalMarshal() throws IOException {
121 final AxContextSchema avroSchema =
122 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVPN);
124 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
125 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
127 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleVPNFull.json");
131 public void testVPNRecordReuse() throws IOException {
132 final AxContextSchema avroSchema =
133 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVPNReuse);
134 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
136 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
137 new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
140 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
141 final String inString = TextFileUtils.getTextFileAsString(fileName);
142 final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString);
143 final String outString = schemaHelper.marshal2String(decodedObject);
144 assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));