e14236064da95d43c415b702ca9d17a5ddfcb7a9
[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.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.context.SchemaHelper;
31 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
32 import org.onap.policy.apex.context.parameters.SchemaParameters;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
35 import org.onap.policy.apex.model.basicmodel.service.ModelService;
36 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
38 import org.onap.policy.apex.model.utilities.TextFileUtils;
39
40 /**
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  * @version
43  */
44 public class TestAvroSchemaRecord {
45     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
46     private AxContextSchemas schemas;
47     private String recordSchema;
48     private String recordSchemaVPN;
49     private String recordSchemaVPNReuse;
50     private String recordSchemaInvalidFields;
51
52     @Before
53     public void initTest() throws IOException {
54         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
55         ModelService.registerModel(AxContextSchemas.class, schemas);
56         new SchemaParameters().getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
57         recordSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExample.avsc");
58         recordSchemaVPN = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPN.avsc");
59         recordSchemaVPNReuse = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPNReuse.avsc");
60         recordSchemaInvalidFields =
61                 TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleInvalidFields.avsc");
62     }
63
64     @Test
65     public void testRecordInit() throws IOException {
66         final AxContextSchema avroSchema =
67                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "Avro", recordSchema);
68
69         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
70         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
71
72         final GenericRecord newRecordEmpty = (GenericRecord) schemaHelper.createNewInstance();
73         assertEquals(null, newRecordEmpty.get("passwordHash"));
74
75         final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/RecordExampleFull.json");
76         final GenericRecord newRecordFull = (GenericRecord) schemaHelper.createNewInstance(inString);
77         assertEquals("gobbledygook", newRecordFull.get("passwordHash").toString());
78     }
79
80     @Test
81     public void testRecordUnmarshalMarshal() 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         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleNull.json");
89         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleFull.json");
90     }
91
92     @Test
93     public void testRecordUnmarshalMarshalInvalid() throws IOException {
94         final AxContextSchema avroSchema =
95                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "Avro", recordSchemaInvalidFields);
96
97         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
98         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
99
100         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleInvalidFields.json");
101     }
102
103     @Test
104     public void testVPNRecordUnmarshalMarshal() throws IOException {
105         final AxContextSchema avroSchema =
106                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "Avro", recordSchemaVPN);
107
108         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
109         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
110
111         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleVPNFull.json");
112     }
113
114     @Test
115     public void testVPNRecordReuse() throws IOException {
116         final AxContextSchema avroSchema =
117                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "Avro", recordSchemaVPNReuse);
118         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
119
120         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
121         new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
122     }
123
124     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
125         final String inString = TextFileUtils.getTextFileAsString(fileName);
126         final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString);
127         final String outString = schemaHelper.marshal2String(decodedObject);
128         assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));
129     }
130 }