ae19cd31a7efae570800be65a407fe51a4bb9e97
[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 import static org.junit.Assert.fail;
25
26 import java.io.IOException;
27
28 import org.apache.avro.generic.GenericData.EnumSymbol;
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.SchemaParameters;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
36 import org.onap.policy.apex.model.basicmodel.service.ModelService;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
39 import org.onap.policy.apex.model.utilities.TextFileUtils;
40
41 /**
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  * @version
44  */
45 public class TestAvroSchemaEnum {
46     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
47     private AxContextSchemas schemas;
48     private String enumSchema;
49
50     @Before
51     public void initTest() throws IOException {
52         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
53         ModelService.registerModel(AxContextSchemas.class, schemas);
54         new SchemaParameters().getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
55         enumSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/EnumSchema.avsc");
56     }
57
58     @Test
59     public void testEnumInit() throws IOException {
60         final AxContextSchema avroSchema =
61                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "Avro", enumSchema);
62
63         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
64         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
65
66         final EnumSymbol newEnumEmpty = (EnumSymbol) schemaHelper.createNewInstance();
67         assertEquals("SPADES", newEnumEmpty.toString());
68
69         final EnumSymbol newEnumFull = (EnumSymbol) schemaHelper.createNewInstance("\"HEARTS\"");
70         assertEquals("HEARTS", newEnumFull.toString());
71     }
72
73     @Test
74     public void testEnumUnmarshalMarshal() throws IOException {
75         final AxContextSchema avroSchema =
76                 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "Avro", enumSchema);
77
78         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
79         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
80
81         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleHearts.json");
82
83         try {
84             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json");
85             fail("This test should throw an exception here");
86         } catch (final Exception e) {
87             assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
88                     e.getMessage());
89         }
90         try {
91             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json");
92             fail("This test should throw an exception here");
93         } catch (final Exception e) {
94             assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
95                     e.getMessage());
96         }
97         try {
98             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad0.json");
99             fail("This test should throw an exception here");
100         } catch (final Exception e) {
101             assertEquals("AvroTest:0.0.1: object \"\"TWEED\"\" Avro unmarshalling failed: Unknown symbol in enum TWEED",
102                     e.getMessage());
103         }
104         try {
105             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad1.json");
106             fail("This test should throw an exception here");
107         } catch (final Exception e) {
108             assertEquals(
109                     "AvroTest:0.0.1: object \"\"Hearts\"\" Avro unmarshalling failed: Unknown symbol in enum Hearts",
110                     e.getMessage());
111         }
112     }
113
114     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
115         final String inString = TextFileUtils.getTextFileAsString(fileName);
116         final EnumSymbol decodedObject = (EnumSymbol) schemaHelper.unmarshal(inString);
117         final String outString = schemaHelper.marshal2String(decodedObject);
118         assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " "));
119     }
120 }