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 / AvroSchemaEnumTest.java
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.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import org.apache.avro.generic.GenericData.EnumSymbol;
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 // TODO: Auto-generated Javadoc
45 /**
46  * The Class TestAvroSchemaEnum.
47  *
48  * @author Liam Fallon (liam.fallon@ericsson.com)
49  * @version
50  */
51 public class AvroSchemaEnumTest {
52     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
53     private AxContextSchemas schemas;
54     private String enumSchema;
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         enumSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/EnumSchema.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 enum init.
90      *
91      * @throws IOException Signals that an I/O exception has occurred.
92      */
93     @Test
94     public void testEnumInit() throws IOException {
95         final AxContextSchema avroSchema =
96                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", enumSchema);
97
98         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
99         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
100
101         final EnumSymbol newEnumEmpty = (EnumSymbol) schemaHelper.createNewInstance();
102         assertEquals("SPADES", newEnumEmpty.toString());
103
104         final EnumSymbol newEnumFull = (EnumSymbol) schemaHelper.createNewInstance("\"HEARTS\"");
105         assertEquals("HEARTS", newEnumFull.toString());
106     }
107
108     /**
109      * Test enum unmarshal marshal.
110      *
111      * @throws IOException Signals that an I/O exception has occurred.
112      */
113     @Test
114     public void testEnumUnmarshalMarshal() throws IOException {
115         final AxContextSchema avroSchema =
116                 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", enumSchema);
117
118         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
119         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
120
121         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleHearts.json");
122
123         try {
124             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json");
125             fail("This test should throw an exception here");
126         } catch (final Exception e) {
127             assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
128                     e.getMessage());
129         }
130         try {
131             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json");
132             fail("This test should throw an exception here");
133         } catch (final Exception e) {
134             assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
135                     e.getMessage());
136         }
137         try {
138             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad0.json");
139             fail("This test should throw an exception here");
140         } catch (final Exception e) {
141             assertEquals("AvroTest:0.0.1: object \"\"TWEED\"\" Avro unmarshalling failed: Unknown symbol in enum TWEED",
142                     e.getMessage());
143         }
144         try {
145             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad1.json");
146             fail("This test should throw an exception here");
147         } catch (final Exception e) {
148             assertEquals(
149                     "AvroTest:0.0.1: object \"\"Hearts\"\" Avro unmarshalling failed: Unknown symbol in enum Hearts",
150                     e.getMessage());
151         }
152     }
153
154     /**
155      * Test unmarshal marshal.
156      *
157      * @param schemaHelper the schema helper
158      * @param fileName the file name
159      * @throws IOException Signals that an I/O exception has occurred.
160      */
161     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
162         final String inString = TextFileUtils.getTextFileAsString(fileName);
163         final EnumSymbol decodedObject = (EnumSymbol) schemaHelper.unmarshal(inString);
164         final String outString = schemaHelper.marshal2String(decodedObject);
165         assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " "));
166     }
167 }