720b5046d72af07e901f8aa94ac3942572cd5737
[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
25 import java.io.IOException;
26
27 import org.apache.avro.generic.GenericRecord;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Ignore;
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  * The Class TestAvroSchemaUnion.
46  *
47  * @author Liam Fallon (liam.fallon@ericsson.com)
48  * @version 
49  */
50 public class AvroSchemaUnionTest {
51     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
52     private AxContextSchemas schemas;
53     private String uinionSchema;
54
55     /**
56      * Inits the test.
57      *
58      * @throws IOException Signals that an I/O exception has occurred.
59      */
60     @Before
61     public void initTest() throws IOException {
62         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
63         ModelService.registerModel(AxContextSchemas.class, schemas);
64         uinionSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/UnionExample.avsc");
65     }
66
67     /**
68      * Inits the context.
69      */
70     @Before
71     public void initContext() {
72         SchemaParameters schemaParameters = new SchemaParameters();
73         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
74         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
75         ParameterService.register(schemaParameters);
76
77     }
78
79     /**
80      * Clear context.
81      */
82     @After
83     public void clearContext() {
84         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
85     }
86
87     /**
88      * Test union all fields.
89      *
90      * @throws IOException Signals that an I/O exception has occurred.
91      */
92     @Ignore
93     @Test
94     public void testUnionAllFields() throws IOException {
95         final AxContextSchema avroSchema =
96                         new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema);
97
98         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
99         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
100
101         final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleAllFields.json");
102         final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString);
103
104         assertEquals("Ben", user.get("name").toString());
105         assertEquals(7, user.get("favourite_number"));
106         assertEquals("red", user.get("favourite_colour").toString());
107     }
108
109     /**
110      * Test union optional field.
111      *
112      * @throws IOException Signals that an I/O exception has occurred.
113      */
114     @Ignore
115     @Test
116     public void testUnionOptionalField() throws IOException {
117         final AxContextSchema avroSchema =
118                         new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema);
119
120         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
121         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
122
123         final String inString =
124                         TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleOptionalField.json");
125         final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString);
126
127         assertEquals("Ben", user.get("name").toString());
128         assertEquals(7, user.get("favourite_number"));
129         assertEquals("red", user.get("favourite_colour").toString());
130     }
131
132     /**
133      * Test union null field.
134      *
135      * @throws IOException Signals that an I/O exception has occurred.
136      */
137     @Ignore
138     @Test
139     public void testUnionNullField() throws IOException {
140         final AxContextSchema avroSchema =
141                         new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema);
142
143         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
144         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
145
146         final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleNullField.json");
147         final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString);
148
149         assertEquals("Ben", user.get("name").toString());
150         assertEquals(7, user.get("favourite_number"));
151         assertEquals("red", user.get("favourite_colour").toString());
152     }
153 }