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