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