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