2977059c63cf17af0d4cb524f1b0a744b57b53d1
[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,2023 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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
28 import java.io.IOException;
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.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         final GenericRecord subRecord0 = (GenericRecord) schemaHelper.createNewSubInstance("AddressUSRecord");
147         assertThatThrownBy(() -> subRecord0.get("address")).hasMessage("Not a valid schema field: address");
148
149         final GenericRecord subRecord1 = (GenericRecord) schemaHelper.createNewSubInstance("EmailAddress");
150         assertThatThrownBy(() -> subRecord0.get("address")).hasMessage("Not a valid schema field: address");
151
152         assertThatThrownBy(() -> schemaHelper.createNewSubInstance("IDontExist"))
153             .hasMessage("AvroTest:0.0.1: the schema \"User\" does not have a subtype of type \"IDontExist\"");
154     }
155
156     /**
157      * Test record unmarshal marshal invalid.
158      *
159      * @throws IOException Signals that an I/O exception has occurred.
160      */
161     @Test
162     public void testRecordUnmarshalMarshalInvalid() throws IOException {
163         final AxContextSchema avroSchema =
164                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaInvalidFields);
165
166         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
167         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
168
169         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleInvalidFields.json");
170     }
171
172     /**
173      * Test VPN record unmarshal marshal.
174      *
175      * @throws IOException Signals that an I/O exception has occurred.
176      */
177     @Test
178     public void testVpnRecordUnmarshalMarshal() throws IOException {
179         final AxContextSchema avroSchema =
180                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpn);
181
182         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
183         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
184
185         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleVPNFull.json");
186     }
187
188     /**
189      * Test VPN record reuse.
190      *
191      * @throws IOException Signals that an I/O exception has occurred.
192      */
193     @Test
194     public void testVpnRecordReuse() throws IOException {
195         final AxContextSchema avroSchema =
196                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpnReuse);
197         assertNotNull(avroSchema);
198         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
199
200         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
201         new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
202     }
203
204     /**
205      * Test unmarshal marshal.
206      *
207      * @param schemaHelper the schema helper
208      * @param fileName the file name
209      * @throws IOException Signals that an I/O exception has occurred.
210      */
211     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
212         final String inString = TextFileUtils.getTextFileAsString(fileName);
213         final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString);
214         final String outString = schemaHelper.marshal2String(decodedObject);
215         assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));
216     }
217 }