b72b2ca102683bba773adaa2b041168e7cd54424
[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.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.apex.context.SchemaHelper;
32 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
33 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
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 import org.onap.policy.common.parameters.ParameterService;
42
43 /**
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  * @version
46  */
47 public class TestAvroSchemaArray {
48     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
49     private AxContextSchemas schemas;
50     private String longArraySchema;
51     private String addressArraySchema;
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         longArraySchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/ArrayExampleLong.avsc");
58         addressArraySchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/ArrayExampleAddress.avsc");
59     }
60
61     @Before
62     public void initContext() {
63         SchemaParameters schemaParameters = new SchemaParameters();
64         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
65         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
66         ParameterService.register(schemaParameters);
67         
68     }
69
70     @After
71     public void clearContext() {
72         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
73     }
74
75     @Test
76     public void testArrayInit() throws IOException {
77         final AxContextSchema avroSchema =
78                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", addressArraySchema);
79
80         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
81         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
82
83         final Array<?> newArrayEmpty = (Array<?>) schemaHelper.createNewInstance();
84         assertEquals(0, newArrayEmpty.size());
85
86         final String inString =
87                 TextFileUtils.getTextFileAsString("src/test/resources/data/ArrayExampleAddressFull.json");
88         final Array<?> newArrayFull = (Array<?>) schemaHelper.createNewInstance(inString);
89         assertEquals("{\"streetaddress\": \"1600 Pennsylvania Avenue\", \"city\": \"Washington DC\"}",
90                 newArrayFull.get(0).toString());
91     }
92
93     @Test
94     public void testLongArrayUnmarshalMarshal() throws IOException {
95         final AxContextSchema avroSchema =
96                 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", longArraySchema);
97
98         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
99         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
100
101         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleLongNull.json");
102         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleLongFull.json");
103     }
104
105     @Test
106     public void testAddressArrayUnmarshalMarshal() throws IOException {
107         final AxContextSchema avroSchema =
108                 new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", addressArraySchema);
109
110         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
111         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
112
113         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleAddressNull.json");
114         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleAddressFull.json");
115     }
116
117     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
118         final String inString = TextFileUtils.getTextFileAsString(fileName);
119         final Array<?> schemaObject = (Array<?>) schemaHelper.unmarshal(inString);
120         final String outString = schemaHelper.marshal2String(schemaObject);
121         assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));
122     }
123 }