Changes for checkstyle 8.32
[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 import org.apache.avro.generic.GenericRecord;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.context.ContextRuntimeException;
33 import org.onap.policy.apex.context.SchemaHelper;
34 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
35 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
36 import org.onap.policy.apex.context.parameters.SchemaParameters;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
39 import org.onap.policy.apex.model.basicmodel.service.ModelService;
40 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
42 import org.onap.policy.common.parameters.ParameterService;
43 import org.onap.policy.common.utils.resources.TextFileUtils;
44
45 /**
46  * The Class TestAvroSchemaRecord.
47  *
48  * @author Liam Fallon (liam.fallon@ericsson.com)
49  * @version
50  */
51 public class AvroSchemaRecordTest {
52     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
53     private AxContextSchemas schemas;
54     private String recordSchema;
55     private String recordSchemaVpn;
56     private String recordSchemaVpnReuse;
57     private String recordSchemaInvalidFields;
58
59     /**
60      * Inits the test.
61      *
62      * @throws IOException Signals that an I/O exception has occurred.
63      */
64     @Before
65     public void initTest() throws IOException {
66         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
67         ModelService.registerModel(AxContextSchemas.class, schemas);
68         recordSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExample.avsc");
69         recordSchemaVpn = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPN.avsc");
70         recordSchemaVpnReuse = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPNReuse.avsc");
71         recordSchemaInvalidFields =
72                 TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleInvalidFields.avsc");
73     }
74
75     /**
76      * Inits the context.
77      */
78     @Before
79     public void initContext() {
80         SchemaParameters schemaParameters = new SchemaParameters();
81         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
82         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
83         ParameterService.register(schemaParameters);
84
85     }
86
87     /**
88      * Clear context.
89      */
90     @After
91     public void clearContext() {
92         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
93     }
94
95     /**
96      * Test record init.
97      *
98      * @throws IOException Signals that an I/O exception has occurred.
99      */
100     @Test
101     public void testRecordInit() throws IOException {
102         final AxContextSchema avroSchema =
103                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
104
105         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
106         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
107
108         final GenericRecord newRecordEmpty = (GenericRecord) schemaHelper.createNewInstance();
109         assertEquals(null, newRecordEmpty.get("passwordHash"));
110
111         final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/RecordExampleFull.json");
112         final GenericRecord newRecordFull = (GenericRecord) schemaHelper.createNewInstance(inString);
113         assertEquals("gobbledygook", newRecordFull.get("passwordHash").toString());
114     }
115
116     /**
117      * Test record unmarshal marshal.
118      *
119      * @throws IOException Signals that an I/O exception has occurred.
120      */
121     @Test
122     public void testRecordUnmarshalMarshal() throws IOException {
123         final AxContextSchema avroSchema =
124                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
125
126         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
127         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
128
129         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleNull.json");
130         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleFull.json");
131     }
132
133     /**
134      * Test record create.
135      *
136      * @throws IOException Signals that an I/O exception has occurred.
137      */
138     @Test
139     public void testRecordCreateRecord() throws IOException {
140         final AxContextSchema avroSchema =
141                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchema);
142
143         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
144         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
145
146         GenericRecord subRecord = (GenericRecord) schemaHelper.createNewSubInstance("AddressUSRecord");
147         assertEquals(null, subRecord.get("streetAddress"));
148
149         subRecord = (GenericRecord) schemaHelper.createNewSubInstance("EmailAddress");
150         assertEquals(null, subRecord.get("address"));
151
152         try {
153             subRecord = (GenericRecord) schemaHelper.createNewSubInstance("IDontExist");
154             fail("test should throw an exception here");
155         } catch (ContextRuntimeException cre) {
156             assertEquals("AvroTest:0.0.1: the schema \"User\" does not have a subtype of type \"IDontExist\"",
157                     cre.getMessage());
158         }
159     }
160
161     /**
162      * Test record unmarshal marshal invalid.
163      *
164      * @throws IOException Signals that an I/O exception has occurred.
165      */
166     @Test
167     public void testRecordUnmarshalMarshalInvalid() throws IOException {
168         final AxContextSchema avroSchema =
169                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaInvalidFields);
170
171         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
172         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
173
174         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleInvalidFields.json");
175     }
176
177     /**
178      * Test VPN record unmarshal marshal.
179      *
180      * @throws IOException Signals that an I/O exception has occurred.
181      */
182     @Test
183     public void testVpnRecordUnmarshalMarshal() throws IOException {
184         final AxContextSchema avroSchema =
185                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpn);
186
187         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
188         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
189
190         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleVPNFull.json");
191     }
192
193     /**
194      * Test VPN record reuse.
195      *
196      * @throws IOException Signals that an I/O exception has occurred.
197      */
198     @Test
199     public void testVpnRecordReuse() throws IOException {
200         final AxContextSchema avroSchema =
201                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpnReuse);
202         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
203
204         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
205         new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
206     }
207
208     /**
209      * Test unmarshal marshal.
210      *
211      * @param schemaHelper the schema helper
212      * @param fileName the file name
213      * @throws IOException Signals that an I/O exception has occurred.
214      */
215     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
216         final String inString = TextFileUtils.getTextFileAsString(fileName);
217         final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString);
218         final String outString = schemaHelper.marshal2String(decodedObject);
219         assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));
220     }
221 }