33ca512b96fa0d6ae82b3db580e8a1e9e9d1a348
[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.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.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.apex.model.utilities.TextFileUtils;
41
42 /**
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  * @version
45  */
46 public class TestAvroSchemaMap {
47     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
48     private AxContextSchemas schemas;
49     private String longMapSchema;
50     private String addressMapSchema;
51     private String addressMapSchemaInvalidFields;
52
53     @Before
54     public void initTest() throws IOException {
55         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
56         ModelService.registerModel(AxContextSchemas.class, schemas);
57         new SchemaParameters().getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
58         longMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleLong.avsc");
59         addressMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleAddress.avsc");
60         addressMapSchemaInvalidFields =
61                 TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleAddressInvalidFields.avsc");
62     }
63
64     @Test
65     public void testMapInit() throws IOException {
66         final AxContextSchema avroSchema =
67                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "Avro", addressMapSchema);
68
69         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
70         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
71
72         final HashMap<?, ?> newMapEmpty = (HashMap<?, ?>) schemaHelper.createNewInstance();
73         assertEquals(0, newMapEmpty.size());
74
75         final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/MapExampleAddressFull.json");
76         final HashMap<?, ?> newMapFull = (HashMap<?, ?>) schemaHelper.createNewInstance(inString);
77
78         assertEquals("{\"streetaddress\": \"221 B Baker St.\", \"city\": \"London\"}",
79                 newMapFull.get(new Utf8("address2")).toString());
80     }
81
82     @Test
83     public void testLongMapUnmarshalMarshal() throws IOException {
84         final AxContextSchema avroSchema =
85                 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "Avro", longMapSchema);
86
87         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
88         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
89
90         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongNull.json");
91         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongFull.json");
92     }
93
94     @Test
95     public void testAddressMapUnmarshalMarshal() throws IOException {
96         final AxContextSchema avroSchema =
97                 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "Avro", addressMapSchema);
98
99         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
100         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
101
102         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressNull.json");
103         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressFull.json");
104     }
105
106     @Test
107     public void testAddressMapUnmarshalMarshalInvalidFields() throws IOException {
108         final AxContextSchema avroSchema =
109                 new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "Avro", addressMapSchemaInvalidFields);
110
111         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
112         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
113
114         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressInvalidFields.json");
115     }
116
117     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
118         final String originalInString = TextFileUtils.getTextFileAsString(fileName);
119         final HashMap<?, ?> firstDecodedMap = (HashMap<?, ?>) schemaHelper.unmarshal(originalInString);
120
121         final String outString = schemaHelper.marshal2String(firstDecodedMap);
122
123         final File tempOutFile = File.createTempFile("ApexAvro", ".json");
124         TextFileUtils.putStringAsFile(outString, tempOutFile);
125
126         final String decodeEncodeInString = TextFileUtils.getTextFileAsString(fileName);
127         tempOutFile.delete();
128
129         final HashMap<?, ?> secondDecodedMap = (HashMap<?, ?>) schemaHelper.unmarshal(decodeEncodeInString);
130
131         // Now check that our doubly encoded map equals the first decoded map, Java map equals
132         // checks values and keys
133         assertEquals(firstDecodedMap, secondDecodedMap);
134     }
135 }