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 / AvroSchemaMapTest.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.File;
27 import java.io.IOException;
28 import java.util.HashMap;
29 import org.apache.avro.generic.GenericRecord;
30 import org.apache.avro.util.Utf8;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.context.SchemaHelper;
35 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
36 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
37 import org.onap.policy.apex.context.parameters.SchemaParameters;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
40 import org.onap.policy.apex.model.basicmodel.service.ModelService;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
42 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
43 import org.onap.policy.common.parameters.ParameterService;
44 import org.onap.policy.common.utils.resources.TextFileUtils;
45
46 /**
47  * The Class TestAvroSchemaMap.
48  *
49  * @author Liam Fallon (liam.fallon@ericsson.com)
50  * @version
51  */
52 public class AvroSchemaMapTest {
53     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
54     private AxContextSchemas schemas;
55     private String longMapSchema;
56     private String addressMapSchema;
57     private String addressMapSchemaInvalidFields;
58
59     /**
60      * Inits the test.
61      *
62      * @throws IOException Signals that an I/O exception has occurred.
63      */
64     @Before
65     public void initTest() throws IOException {
66         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
67         ModelService.registerModel(AxContextSchemas.class, schemas);
68         longMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleLong.avsc");
69         addressMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleAddress.avsc");
70         addressMapSchemaInvalidFields =
71                 TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleAddressInvalidFields.avsc");
72     }
73
74     /**
75      * Inits the context.
76      */
77     @Before
78     public void initContext() {
79         SchemaParameters schemaParameters = new SchemaParameters();
80         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
81         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
82         ParameterService.register(schemaParameters);
83
84     }
85
86     /**
87      * Clear context.
88      */
89     @After
90     public void clearContext() {
91         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
92     }
93
94     /**
95      * Test map init.
96      *
97      * @throws IOException Signals that an I/O exception has occurred.
98      */
99     @Test
100     public void testMapInit() throws IOException {
101         final AxContextSchema avroSchema =
102                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", addressMapSchema);
103
104         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
105         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
106
107         final HashMap<?, ?> newMapEmpty = (HashMap<?, ?>) schemaHelper.createNewInstance();
108         assertEquals(0, newMapEmpty.size());
109
110         final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/MapExampleAddressFull.json");
111         final HashMap<?, ?> newMapFull = (HashMap<?, ?>) schemaHelper.createNewInstance(inString);
112
113         assertEquals("{\"streetaddress\": \"221 B Baker St.\", \"city\": \"London\"}",
114                 newMapFull.get(new Utf8("address2")).toString());
115     }
116
117     /**
118      * Test long map unmarshal marshal.
119      *
120      * @throws IOException Signals that an I/O exception has occurred.
121      */
122     @Test
123     public void testLongMapUnmarshalMarshal() throws IOException {
124         final AxContextSchema avroSchema =
125                 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", longMapSchema);
126
127         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
128         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
129
130         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongNull.json");
131         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongFull.json");
132     }
133
134     /**
135      * Test address map unmarshal marshal.
136      *
137      * @throws IOException Signals that an I/O exception has occurred.
138      */
139     @Test
140     public void testAddressMapUnmarshalMarshal() throws IOException {
141         final AxContextSchema avroSchema =
142                 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", addressMapSchema);
143
144         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
145         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
146
147         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressNull.json");
148         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressFull.json");
149     }
150
151     /**
152      * Test sub record create.
153      *
154      * @throws IOException Signals that an I/O exception has occurred.
155      */
156     @Test
157     public void testSubRecordCreateRecord() throws IOException {
158         final AxContextSchema avroSchema =
159                 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", addressMapSchema);
160
161         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
162         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
163
164         GenericRecord subRecord = (GenericRecord) schemaHelper.createNewSubInstance("AddressUSRecord");
165         assertEquals(null, subRecord.get("streetAddress"));
166     }
167
168     /**
169      * Test address map unmarshal marshal invalid fields.
170      *
171      * @throws IOException Signals that an I/O exception has occurred.
172      */
173     @Test
174     public void testAddressMapUnmarshalMarshalInvalidFields() throws IOException {
175         final AxContextSchema avroSchema =
176                 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", addressMapSchemaInvalidFields);
177
178         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
179         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
180
181         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressInvalidFields.json");
182     }
183
184     /**
185      * Test unmarshal marshal.
186      *
187      * @param schemaHelper the schema helper
188      * @param fileName the file name
189      * @throws IOException Signals that an I/O exception has occurred.
190      */
191     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
192         final String originalInString = TextFileUtils.getTextFileAsString(fileName);
193         final HashMap<?, ?> firstDecodedMap = (HashMap<?, ?>) schemaHelper.unmarshal(originalInString);
194
195         final String outString = schemaHelper.marshal2String(firstDecodedMap);
196
197         final File tempOutFile = File.createTempFile("ApexAvro", ".json");
198         TextFileUtils.putStringAsFile(outString, tempOutFile);
199
200         final String decodeEncodeInString = TextFileUtils.getTextFileAsString(fileName);
201         tempOutFile.delete();
202
203         final HashMap<?, ?> secondDecodedMap = (HashMap<?, ?>) schemaHelper.unmarshal(decodeEncodeInString);
204
205         // Now check that our doubly encoded map equals the first decoded map, Java map equals
206         // checks values and keys
207         assertEquals(firstDecodedMap, secondDecodedMap);
208     }
209 }