47a5c969cea86c32a273737fe31fcb4686d37ef1
[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.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.apex.model.utilities.TextFileUtils;
42 import org.onap.policy.common.parameters.ParameterService;
43
44 /**
45  * @author Liam Fallon (liam.fallon@ericsson.com)
46  * @version
47  */
48 public class TestAvroSchemaEnum {
49     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
50     private AxContextSchemas schemas;
51     private String enumSchema;
52
53     @Before
54     public void initTest() throws IOException {
55         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
56         ModelService.registerModel(AxContextSchemas.class, schemas);
57         enumSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/EnumSchema.avsc");
58     }
59
60     @Before
61     public void initContext() {
62         SchemaParameters schemaParameters = new SchemaParameters();
63         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
64         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
65         ParameterService.register(schemaParameters);
66         
67     }
68
69     @After
70     public void clearContext() {
71         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
72     }
73
74     @Test
75     public void testEnumInit() throws IOException {
76         final AxContextSchema avroSchema =
77                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", enumSchema);
78
79         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
80         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
81
82         final EnumSymbol newEnumEmpty = (EnumSymbol) schemaHelper.createNewInstance();
83         assertEquals("SPADES", newEnumEmpty.toString());
84
85         final EnumSymbol newEnumFull = (EnumSymbol) schemaHelper.createNewInstance("\"HEARTS\"");
86         assertEquals("HEARTS", newEnumFull.toString());
87     }
88
89     @Test
90     public void testEnumUnmarshalMarshal() throws IOException {
91         final AxContextSchema avroSchema =
92                 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", enumSchema);
93
94         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
95         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
96
97         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleHearts.json");
98
99         try {
100             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json");
101             fail("This test should throw an exception here");
102         } catch (final Exception e) {
103             assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
104                     e.getMessage());
105         }
106         try {
107             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json");
108             fail("This test should throw an exception here");
109         } catch (final Exception e) {
110             assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
111                     e.getMessage());
112         }
113         try {
114             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad0.json");
115             fail("This test should throw an exception here");
116         } catch (final Exception e) {
117             assertEquals("AvroTest:0.0.1: object \"\"TWEED\"\" Avro unmarshalling failed: Unknown symbol in enum TWEED",
118                     e.getMessage());
119         }
120         try {
121             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad1.json");
122             fail("This test should throw an exception here");
123         } catch (final Exception e) {
124             assertEquals(
125                     "AvroTest:0.0.1: object \"\"Hearts\"\" Avro unmarshalling failed: Unknown symbol in enum Hearts",
126                     e.getMessage());
127         }
128     }
129
130     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
131         final String inString = TextFileUtils.getTextFileAsString(fileName);
132         final EnumSymbol decodedObject = (EnumSymbol) schemaHelper.unmarshal(inString);
133         final String outString = schemaHelper.marshal2String(decodedObject);
134         assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " "));
135     }
136 }