a6a3f662dc952a7d8a330d59297d4fd093c85f39
[policy/apex-pdp.git] / plugins / plugins-context / plugins-context-schema / plugins-context-schema-avro / src / test / java / org / onap / policy / apex / plugins / context / schema / avro / AvroSchemaRecordTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.context.schema.avro;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28
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;
45
46 /**
47  * The Class TestAvroSchemaRecord.
48  *
49  * @author Liam Fallon (liam.fallon@ericsson.com)
50  * @version
51  */
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;
59
60     /**
61      * Inits the test.
62      *
63      * @throws IOException Signals that an I/O exception has occurred.
64      */
65     @Before
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");
74     }
75
76     /**
77      * Inits the context.
78      */
79     @Before
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);
85
86     }
87
88     /**
89      * Clear context.
90      */
91     @After
92     public void clearContext() {
93         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
94     }
95
96     /**
97      * Test record init.
98      *
99      * @throws IOException Signals that an I/O exception has occurred.
100      */
101     @Test
102     public void testRecordInit() throws IOException {
103         final AxContextSchema avroSchema =
104                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
105
106         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
107         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
108
109         final GenericRecord newRecordEmpty = (GenericRecord) schemaHelper.createNewInstance();
110         assertEquals(null, newRecordEmpty.get("passwordHash"));
111
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());
115     }
116
117     /**
118      * Test record unmarshal marshal.
119      *
120      * @throws IOException Signals that an I/O exception has occurred.
121      */
122     @Test
123     public void testRecordUnmarshalMarshal() throws IOException {
124         final AxContextSchema avroSchema =
125                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
126
127         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
128         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
129
130         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleNull.json");
131         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleFull.json");
132     }
133
134     /**
135      * Test record create.
136      *
137      * @throws IOException Signals that an I/O exception has occurred.
138      */
139     @Test
140     public void testRecordCreateRecord() throws IOException {
141         final AxContextSchema avroSchema =
142                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
143
144         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
145         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
146
147         GenericRecord subRecord = (GenericRecord) schemaHelper.createNewSubInstance("AddressUSRecord");
148         assertEquals(null, subRecord.get("streetAddress"));
149
150         subRecord = (GenericRecord) schemaHelper.createNewSubInstance("EmailAddress");
151         assertEquals(null, subRecord.get("address"));
152
153         try {
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\"",
158                     cre.getMessage());
159         }
160     }
161
162     /**
163      * Test record unmarshal marshal invalid.
164      *
165      * @throws IOException Signals that an I/O exception has occurred.
166      */
167     @Test
168     public void testRecordUnmarshalMarshalInvalid() throws IOException {
169         final AxContextSchema avroSchema =
170                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaInvalidFields);
171
172         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
173         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
174
175         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleInvalidFields.json");
176     }
177
178     /**
179      * Test VPN record unmarshal marshal.
180      *
181      * @throws IOException Signals that an I/O exception has occurred.
182      */
183     @Test
184     public void testVpnRecordUnmarshalMarshal() throws IOException {
185         final AxContextSchema avroSchema =
186                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpn);
187
188         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
189         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
190
191         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleVPNFull.json");
192     }
193
194     /**
195      * Test VPN record reuse.
196      *
197      * @throws IOException Signals that an I/O exception has occurred.
198      */
199     @Test
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);
204
205         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
206         new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
207     }
208
209     /**
210      * Test unmarshal marshal.
211      *
212      * @param schemaHelper the schema helper
213      * @param fileName the file name
214      * @throws IOException Signals that an I/O exception has occurred.
215      */
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+", ""));
221     }
222 }