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