a23aac910ec127ddae1847fa873d262f25542ab5
[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 / AvroSchemaFixedTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-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.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.IOException;
29
30 import org.apache.avro.generic.GenericData.Fixed;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.context.SchemaHelper;
35 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
36 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
37 import org.onap.policy.apex.context.parameters.SchemaParameters;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
40 import org.onap.policy.apex.model.basicmodel.service.ModelService;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
42 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
43 import org.onap.policy.common.parameters.ParameterService;
44 import org.onap.policy.common.utils.resources.TextFileUtils;
45
46 /**
47  * The Class TestAvroSchemaFixed.
48  *
49  * @author Liam Fallon (liam.fallon@ericsson.com)
50  * @version
51  */
52 public class AvroSchemaFixedTest {
53     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
54     private AxContextSchemas schemas;
55     private String fixedSchema;
56
57     /**
58      * Inits the test.
59      *
60      * @throws IOException Signals that an I/O exception has occurred.
61      */
62     @Before
63     public void initTest() throws IOException {
64         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
65         ModelService.registerModel(AxContextSchemas.class, schemas);
66         fixedSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/FixedSchema.avsc");
67     }
68
69     /**
70      * Inits the context.
71      */
72     @Before
73     public void initContext() {
74         SchemaParameters schemaParameters = new SchemaParameters();
75         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
76         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
77         ParameterService.register(schemaParameters);
78
79     }
80
81     /**
82      * Clear context.
83      */
84     @After
85     public void clearContext() {
86         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
87     }
88
89     /**
90      * Test fixed init.
91      *
92      * @throws IOException Signals that an I/O exception has occurred.
93      */
94     @Test
95     public void testFixedInit() throws IOException {
96         final AxContextSchema avroSchema =
97                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", fixedSchema);
98
99         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
100         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
101
102         try {
103             schemaHelper.createNewInstance();
104             fail("Test should throw an exception here");
105         } catch (final Exception e) {
106             assertEquals("AvroTest:0.0.1: could not create an instance "
107                     + "of class \"org.apache.avro.generic.GenericData$Fixed\" "
108                     + "using the default constructor \"Fixed()\"", e.getMessage());
109         }
110
111         final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/FixedExampleGood.json");
112         final Fixed newFixedFull = (Fixed) schemaHelper.createNewInstance(inString);
113         assertTrue(newFixedFull.toString().startsWith("[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65"));
114         assertTrue(newFixedFull.toString().endsWith("53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70]"));
115     }
116
117     /**
118      * Test fixed unmarshal marshal.
119      *
120      * @throws IOException Signals that an I/O exception has occurred.
121      */
122     @Test
123     public void testFixedUnmarshalMarshal() throws IOException {
124         final AxContextSchema avroSchema =
125                 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", fixedSchema);
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/FixedExampleGood.json");
131
132         try {
133             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json");
134             fail("This test should throw an exception here");
135         } catch (final Exception e) {
136             assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
137                     e.getMessage());
138         }
139         try {
140             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json");
141             fail("This test should throw an exception here");
142         } catch (final Exception e) {
143             assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
144                     e.getMessage());
145         }
146         try {
147             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad0.json");
148             fail("This test should throw an exception here");
149         } catch (final Exception e) {
150             assertEquals("AvroTest:0.0.1: object \"\"BADBAD\"\" "
151                     + "Avro unmarshalling failed: Expected fixed length 64, but got6", e.getMessage());
152         }
153         try {
154             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad1.json");
155             fail("This test should throw an exception here");
156         } catch (final Exception e) {
157             assertEquals("AvroTest:0.0.1: object "
158                     + "\"\"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0\"\" "
159                     + "Avro unmarshalling failed: Expected fixed length 64, but got65", e.getMessage());
160         }
161     }
162
163     /**
164      * Test unmarshal marshal.
165      *
166      * @param schemaHelper the schema helper
167      * @param fileName the file name
168      * @throws IOException Signals that an I/O exception has occurred.
169      */
170     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
171         final String inString = TextFileUtils.getTextFileAsString(fileName);
172         final Fixed decodedObject = (Fixed) schemaHelper.unmarshal(inString);
173         final String outString = schemaHelper.marshal2String(decodedObject);
174         assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " "));
175     }
176 }