f4906e5df03bceac574d9254ed5ae92065d687a6
[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.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.SchemaParameters;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
37 import org.onap.policy.apex.model.basicmodel.service.ModelService;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
40 import org.onap.policy.apex.model.utilities.TextFileUtils;
41
42 /**
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  * @version
45  */
46 public class TestAvroSchemaFixed {
47     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
48     private AxContextSchemas schemas;
49     private String fixedSchema;
50
51     @Before
52     public void initTest() throws IOException {
53         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
54         ModelService.registerModel(AxContextSchemas.class, schemas);
55         new SchemaParameters().getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
56         fixedSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/FixedSchema.avsc");
57     }
58
59     @Test
60     public void testFixedInit() throws IOException {
61         final AxContextSchema avroSchema =
62                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "Avro", fixedSchema);
63
64         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
65         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
66
67         try {
68             schemaHelper.createNewInstance();
69             fail("Test should throw an exception here");
70         } catch (final Exception e) {
71             assertEquals(
72                     "AvroTest:0.0.1: could not create an instance of class \"org.apache.avro.generic.GenericData.Fixed\" using the default constructor \"Fixed()\"",
73                     e.getMessage());
74         }
75
76         final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/FixedExampleGood.json");
77         final Fixed newFixedFull = (Fixed) schemaHelper.createNewInstance(inString);
78         assertTrue(newFixedFull.toString().startsWith("[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65"));
79         assertTrue(newFixedFull.toString().endsWith("53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70]"));
80     }
81
82     @Test
83     public void testFixedUnmarshalMarshal() throws IOException {
84         final AxContextSchema avroSchema =
85                 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "Avro", fixedSchema);
86
87         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
88         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
89
90         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleGood.json");
91
92         try {
93             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json");
94             fail("This test should throw an exception here");
95         } catch (final Exception e) {
96             assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL",
97                     e.getMessage());
98         }
99         try {
100             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.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/FixedExampleBad0.json");
108             fail("This test should throw an exception here");
109         } catch (final Exception e) {
110             assertEquals(
111                     "AvroTest:0.0.1: object \"\"BADBAD\"\" Avro unmarshalling failed: Expected fixed length 64, but got6",
112                     e.getMessage());
113         }
114         try {
115             testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad1.json");
116             fail("This test should throw an exception here");
117         } catch (final Exception e) {
118             assertEquals(
119                     "AvroTest:0.0.1: object \"\"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0\"\" Avro unmarshalling failed: Expected fixed length 64, but got65",
120                     e.getMessage());
121         }
122     }
123
124     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
125         final String inString = TextFileUtils.getTextFileAsString(fileName);
126         final Fixed decodedObject = (Fixed) schemaHelper.unmarshal(inString);
127         final String outString = schemaHelper.marshal2Json(decodedObject);
128         assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " "));
129     }
130 }