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