Fix incorrect naming on context plugins
[policy/apex-pdp.git] / plugins / plugins-context / plugins-context-schema / plugins-context-schema-avro / src / test / java / org / onap / policy / apex / plugins / context / schema / avro / TestAvroSchemaUnion.java
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.Before;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.onap.policy.apex.context.SchemaHelper;
32 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
33 import org.onap.policy.apex.context.parameters.SchemaParameters;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
36 import org.onap.policy.apex.model.basicmodel.service.ModelService;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
39 import org.onap.policy.apex.model.utilities.TextFileUtils;
40
41 /**
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  * @version
44  */
45 public class TestAvroSchemaUnion {
46     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
47     private AxContextSchemas schemas;
48     private String uinionSchema;
49
50     @Before
51     public void initTest() throws IOException {
52         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
53         ModelService.registerModel(AxContextSchemas.class, schemas);
54         new SchemaParameters().getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
55         uinionSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/UnionExample.avsc");
56     }
57
58     @Ignore
59     @Test
60     public void testUnionAllFields() throws IOException {
61         final AxContextSchema avroSchema =
62                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "Avro", uinionSchema);
63
64         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
65         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
66
67         final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleAllFields.json");
68         final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString);
69
70         assertEquals("Ben", user.get("name").toString());
71         assertEquals(7, user.get("favourite_number"));
72         assertEquals("red", user.get("favourite_colour").toString());
73     }
74
75     @Ignore
76     @Test
77     public void testUnionOptionalField() throws IOException {
78         final AxContextSchema avroSchema =
79                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "Avro", uinionSchema);
80
81         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
82         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
83
84         final String inString =
85                 TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleOptionalField.json");
86         final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString);
87
88         assertEquals("Ben", user.get("name").toString());
89         assertEquals(7, user.get("favourite_number"));
90         assertEquals("red", user.get("favourite_colour").toString());
91     }
92
93     @Ignore
94     @Test
95     public void testUnionNullField() 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/UnionExampleNullField.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 }