47b1593fa4defbad8f88d01ba59ed6561e721d38
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.context.schema.avro;
23
24 import static org.junit.Assert.assertEquals;
25
26 import java.io.IOException;
27
28 import org.apache.avro.generic.GenericRecord;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Ignore;
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.common.parameters.ParameterService;
43 import org.onap.policy.common.utils.resources.TextFileUtils;
44
45 /**
46  * The Class TestAvroSchemaUnion.
47  *
48  * @author Liam Fallon (liam.fallon@ericsson.com)
49  * @version
50  */
51 public class AvroSchemaUnionTest {
52     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
53     private AxContextSchemas schemas;
54     private String uinionSchema;
55
56     /**
57      * Inits the test.
58      *
59      * @throws IOException Signals that an I/O exception has occurred.
60      */
61     @Before
62     public void initTest() throws IOException {
63         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
64         ModelService.registerModel(AxContextSchemas.class, schemas);
65         uinionSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/UnionExample.avsc");
66     }
67
68     /**
69      * Inits the context.
70      */
71     @Before
72     public void initContext() {
73         SchemaParameters schemaParameters = new SchemaParameters();
74         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
75         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
76         ParameterService.register(schemaParameters);
77
78     }
79
80     /**
81      * Clear context.
82      */
83     @After
84     public void clearContext() {
85         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
86     }
87
88     /**
89      * Test union all fields.
90      *
91      * @throws IOException Signals that an I/O exception has occurred.
92      */
93     @Ignore
94     @Test
95     public void testUnionAllFields() throws IOException {
96         final AxContextSchema avroSchema =
97                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema);
98
99         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
100         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
101
102         final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleAllFields.json");
103         final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString);
104
105         assertEquals("Ben", user.get("name").toString());
106         assertEquals(7, user.get("favourite_number"));
107         assertEquals("red", user.get("favourite_colour").toString());
108     }
109
110     /**
111      * Test union optional field.
112      *
113      * @throws IOException Signals that an I/O exception has occurred.
114      */
115     @Ignore
116     @Test
117     public void testUnionOptionalField() throws IOException {
118         final AxContextSchema avroSchema =
119                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema);
120
121         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
122         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
123
124         final String inString =
125                 TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleOptionalField.json");
126         final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString);
127
128         assertEquals("Ben", user.get("name").toString());
129         assertEquals(7, user.get("favourite_number"));
130         assertEquals("red", user.get("favourite_colour").toString());
131     }
132
133     /**
134      * Test union null field.
135      *
136      * @throws IOException Signals that an I/O exception has occurred.
137      */
138     @Ignore
139     @Test
140     public void testUnionNullField() throws IOException {
141         final AxContextSchema avroSchema =
142                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema);
143
144         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
145         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
146
147         final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleNullField.json");
148         final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString);
149
150         assertEquals("Ben", user.get("name").toString());
151         assertEquals(7, user.get("favourite_number"));
152         assertEquals("red", user.get("favourite_colour").toString());
153     }
154 }