6b1d09eb6ce660223f42248b0e2b446602696055
[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
25 import java.io.IOException;
26
27 import org.apache.avro.generic.GenericRecord;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.apex.context.SchemaHelper;
32 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
33 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
34 import org.onap.policy.apex.context.parameters.SchemaParameters;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
37 import org.onap.policy.apex.model.basicmodel.service.ModelService;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
40 import org.onap.policy.apex.model.utilities.TextFileUtils;
41 import org.onap.policy.common.parameters.ParameterService;
42
43 // TODO: Auto-generated Javadoc
44 /**
45  * The Class TestAvroSchemaRecord.
46  *
47  * @author Liam Fallon (liam.fallon@ericsson.com)
48  * @version 
49  */
50 public class TestAvroSchemaRecord {
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 unmarshal marshal invalid.
134      *
135      * @throws IOException Signals that an I/O exception has occurred.
136      */
137     @Test
138     public void testRecordUnmarshalMarshalInvalid() throws IOException {
139         final AxContextSchema avroSchema =
140                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaInvalidFields);
141
142         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
143         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
144
145         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleInvalidFields.json");
146     }
147
148     /**
149      * Test VPN record unmarshal marshal.
150      *
151      * @throws IOException Signals that an I/O exception has occurred.
152      */
153     @Test
154     public void testVpnRecordUnmarshalMarshal() throws IOException {
155         final AxContextSchema avroSchema =
156                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpn);
157
158         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
159         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
160
161         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleVPNFull.json");
162     }
163
164     /**
165      * Test VPN record reuse.
166      *
167      * @throws IOException Signals that an I/O exception has occurred.
168      */
169     @Test
170     public void testVpnRecordReuse() throws IOException {
171         final AxContextSchema avroSchema =
172                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", recordSchemaVpnReuse);
173         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
174
175         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
176         new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
177     }
178
179     /**
180      * Test unmarshal marshal.
181      *
182      * @param schemaHelper the schema helper
183      * @param fileName the file name
184      * @throws IOException Signals that an I/O exception has occurred.
185      */
186     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
187         final String inString = TextFileUtils.getTextFileAsString(fileName);
188         final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString);
189         final String outString = schemaHelper.marshal2String(decodedObject);
190         assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));
191     }
192 }