6d709645f084042f0d1a6de3c0fd470d8e1e5088
[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.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28
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.apex.model.utilities.TextFileUtils;
43 import org.onap.policy.common.parameters.ParameterService;
44
45 /**
46  * @author Liam Fallon (liam.fallon@ericsson.com)
47  * @version
48  */
49 public class TestAvroSchemaFixed {
50     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
51     private AxContextSchemas schemas;
52     private String fixedSchema;
53
54     @Before
55     public void initTest() throws IOException {
56         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
57         ModelService.registerModel(AxContextSchemas.class, schemas);
58         fixedSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/FixedSchema.avsc");
59     }
60
61     @Before
62     public void initContext() {
63         SchemaParameters schemaParameters = new SchemaParameters();
64         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
65         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
66         ParameterService.register(schemaParameters);
67         
68     }
69
70     @After
71     public void clearContext() {
72         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
73     }
74
75     @Test
76     public void testFixedInit() throws IOException {
77         final AxContextSchema avroSchema =
78                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", fixedSchema);
79
80         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
81         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
82
83         try {
84             schemaHelper.createNewInstance();
85             fail("Test should throw an exception here");
86         } catch (final Exception e) {
87             assertEquals(
88                     "AvroTest:0.0.1: could not create an instance of class \"org.apache.avro.generic.GenericData.Fixed\" using the default constructor \"Fixed()\"",
89                     e.getMessage());
90         }
91
92         final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/FixedExampleGood.json");
93         final Fixed newFixedFull = (Fixed) schemaHelper.createNewInstance(inString);
94         assertTrue(newFixedFull.toString().startsWith("[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65"));
95         assertTrue(newFixedFull.toString().endsWith("53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70]"));
96     }
97
98     @Test
99     public void testFixedUnmarshalMarshal() throws IOException {
100         final AxContextSchema avroSchema =
101                 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", fixedSchema);
102
103         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
104         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
105
106         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleGood.json");
107
108         try {
109             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json");
110             fail("This test should throw an exception here");
111         } catch (final Exception e) {
112             assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
113                     e.getMessage());
114         }
115         try {
116             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json");
117             fail("This test should throw an exception here");
118         } catch (final Exception e) {
119             assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
120                     e.getMessage());
121         }
122         try {
123             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad0.json");
124             fail("This test should throw an exception here");
125         } catch (final Exception e) {
126             assertEquals(
127                     "AvroTest:0.0.1: object \"\"BADBAD\"\" Avro unmarshalling failed: Expected fixed length 64, but got6",
128                     e.getMessage());
129         }
130         try {
131             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad1.json");
132             fail("This test should throw an exception here");
133         } catch (final Exception e) {
134             assertEquals(
135                     "AvroTest:0.0.1: object \"\"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0\"\" Avro unmarshalling failed: Expected fixed length 64, but got65",
136                     e.getMessage());
137         }
138     }
139
140     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
141         final String inString = TextFileUtils.getTextFileAsString(fileName);
142         final Fixed decodedObject = (Fixed) schemaHelper.unmarshal(inString);
143         final String outString = schemaHelper.marshal2String(decodedObject);
144         assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " "));
145     }
146 }