21fab66d9d1e7cc7f288a964abb2e86a758faaec
[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.IOException;
26
27 import org.apache.avro.generic.GenericData.Array;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.context.SchemaHelper;
31 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
32 import org.onap.policy.apex.context.parameters.SchemaParameters;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
35 import org.onap.policy.apex.model.basicmodel.service.ModelService;
36 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
38 import org.onap.policy.apex.model.utilities.TextFileUtils;
39
40 /**
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  * @version
43  */
44 public class TestAvroSchemaArray {
45     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
46     private AxContextSchemas schemas;
47     private String longArraySchema;
48     private String addressArraySchema;
49
50     @Before
51     public void initTest() throws IOException {
52         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
53         ModelService.registerModel(AxContextSchemas.class, schemas);
54         new SchemaParameters().getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
55         longArraySchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/ArrayExampleLong.avsc");
56         addressArraySchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/ArrayExampleAddress.avsc");
57     }
58
59     @Test
60     public void testArrayInit() throws IOException {
61         final AxContextSchema avroSchema =
62                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "Avro", addressArraySchema);
63
64         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
65         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
66
67         final Array<?> newArrayEmpty = (Array<?>) schemaHelper.createNewInstance();
68         assertEquals(0, newArrayEmpty.size());
69
70         final String inString =
71                 TextFileUtils.getTextFileAsString("src/test/resources/data/ArrayExampleAddressFull.json");
72         final Array<?> newArrayFull = (Array<?>) schemaHelper.createNewInstance(inString);
73         assertEquals("{\"streetaddress\": \"1600 Pennsylvania Avenue\", \"city\": \"Washington DC\"}",
74                 newArrayFull.get(0).toString());
75     }
76
77     @Test
78     public void testLongArrayUnmarshalMarshal() throws IOException {
79         final AxContextSchema avroSchema =
80                 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "Avro", longArraySchema);
81
82         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
83         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
84
85         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleLongNull.json");
86         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleLongFull.json");
87     }
88
89     @Test
90     public void testAddressArrayUnmarshalMarshal() throws IOException {
91         final AxContextSchema avroSchema =
92                 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "Avro", addressArraySchema);
93
94         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
95         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
96
97         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleAddressNull.json");
98         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleAddressFull.json");
99     }
100
101     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
102         final String inString = TextFileUtils.getTextFileAsString(fileName);
103         final Array<?> schemaObject = (Array<?>) schemaHelper.unmarshal(inString);
104         final String outString = schemaHelper.marshal2String(schemaObject);
105         assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));
106     }
107 }