35e305734b7a232179491899e6744f02e5338b5d
[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 import static org.junit.Assert.fail;
25
26 import java.io.IOException;
27
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.apex.model.utilities.TextFileUtils;
43 import org.onap.policy.common.parameters.ParameterService;
44
45 // TODO: Auto-generated Javadoc
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 = TextFileUtils
73                         .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 = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO",
104                         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 = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO",
125                         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 = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO",
142                         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 = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO",
170                         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 = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO",
186                         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 = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO",
202                         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 }