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