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