000dcc9fd7b4e194c4cf018fda88e3574499bfd3
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.context.schema.avro;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.IOException;
26
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;
42
43 /**
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  * @version
46  */
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;
54
55     @Before
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");
64     }
65
66     @Before
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);
72         
73     }
74
75     @After
76     public void clearContext() {
77         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
78     }
79
80     @Test
81     public void testRecordInit() throws IOException {
82         final AxContextSchema avroSchema =
83                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
84
85         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
86         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
87
88         final GenericRecord newRecordEmpty = (GenericRecord) schemaHelper.createNewInstance();
89         assertEquals(null, newRecordEmpty.get("passwordHash"));
90
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());
94     }
95
96     @Test
97     public void testRecordUnmarshalMarshal() throws IOException {
98         final AxContextSchema avroSchema =
99                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
100
101         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
102         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
103
104         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleNull.json");
105         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleFull.json");
106     }
107
108     @Test
109     public void testRecordUnmarshalMarshalInvalid() throws IOException {
110         final AxContextSchema avroSchema =
111                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaInvalidFields);
112
113         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
114         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
115
116         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleInvalidFields.json");
117     }
118
119     @Test
120     public void testVPNRecordUnmarshalMarshal() throws IOException {
121         final AxContextSchema avroSchema =
122                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVPN);
123
124         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
125         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
126
127         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleVPNFull.json");
128     }
129
130     @Test
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);
135
136         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
137         new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
138     }
139
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+", ""));
145     }
146 }