4bbaef8c96921bed940119681b4d796b9c5f07a5
[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 / AvroSchemaArrayTest.java
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.GenericData.Array;
29 import org.junit.After;
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.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.common.parameters.ParameterService;
42 import org.onap.policy.common.utils.resources.TextFileUtils;
43
44 // TODO: Auto-generated Javadoc
45 /**
46  * The Class TestAvroSchemaArray.
47  *
48  * @author Liam Fallon (liam.fallon@ericsson.com)
49  * @version
50  */
51 public class AvroSchemaArrayTest {
52     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
53     private AxContextSchemas schemas;
54     private String longArraySchema;
55     private String addressArraySchema;
56
57     /**
58      * Inits the test.
59      *
60      * @throws IOException Signals that an I/O exception has occurred.
61      */
62     @Before
63     public void initTest() throws IOException {
64         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
65         ModelService.registerModel(AxContextSchemas.class, schemas);
66         longArraySchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/ArrayExampleLong.avsc");
67         addressArraySchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/ArrayExampleAddress.avsc");
68     }
69
70     /**
71      * Inits the context.
72      */
73     @Before
74     public void initContext() {
75         SchemaParameters schemaParameters = new SchemaParameters();
76         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
77         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
78         ParameterService.register(schemaParameters);
79
80     }
81
82     /**
83      * Clear context.
84      */
85     @After
86     public void clearContext() {
87         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
88     }
89
90     /**
91      * Test array init.
92      *
93      * @throws IOException Signals that an I/O exception has occurred.
94      */
95     @Test
96     public void testArrayInit() throws IOException {
97         final AxContextSchema avroSchema =
98                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", addressArraySchema);
99
100         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
101         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
102
103         final Array<?> newArrayEmpty = (Array<?>) schemaHelper.createNewInstance();
104         assertEquals(0, newArrayEmpty.size());
105
106         final String inString =
107                 TextFileUtils.getTextFileAsString("src/test/resources/data/ArrayExampleAddressFull.json");
108         final Array<?> newArrayFull = (Array<?>) schemaHelper.createNewInstance(inString);
109         assertEquals("{\"streetaddress\": \"1600 Pennsylvania Avenue\", \"city\": \"Washington DC\"}",
110                 newArrayFull.get(0).toString());
111     }
112
113     /**
114      * Test long array unmarshal marshal.
115      *
116      * @throws IOException Signals that an I/O exception has occurred.
117      */
118     @Test
119     public void testLongArrayUnmarshalMarshal() throws IOException {
120         final AxContextSchema avroSchema =
121                 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", longArraySchema);
122
123         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
124         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
125
126         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleLongNull.json");
127         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleLongFull.json");
128     }
129
130     /**
131      * Test address array unmarshal marshal.
132      *
133      * @throws IOException Signals that an I/O exception has occurred.
134      */
135     @Test
136     public void testAddressArrayUnmarshalMarshal() throws IOException {
137         final AxContextSchema avroSchema =
138                 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", addressArraySchema);
139
140         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
141         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
142
143         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleAddressNull.json");
144         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleAddressFull.json");
145     }
146
147     /**
148      * Test unmarshal marshal.
149      *
150      * @param schemaHelper the schema helper
151      * @param fileName the file name
152      * @throws IOException Signals that an I/O exception has occurred.
153      */
154     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
155         final String inString = TextFileUtils.getTextFileAsString(fileName);
156         final Array<?> schemaObject = (Array<?>) schemaHelper.unmarshal(inString);
157         final String outString = schemaHelper.marshal2String(schemaObject);
158         assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));
159     }
160 }