2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.context.schema.avro;
24 import static org.junit.Assert.assertEquals;
26 import java.io.IOException;
27 import org.apache.avro.generic.GenericData.Array;
28 import org.apache.avro.generic.GenericRecord;
29 import org.junit.After;
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.ContextParameterConstants;
35 import org.onap.policy.apex.context.parameters.SchemaParameters;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
38 import org.onap.policy.apex.model.basicmodel.service.ModelService;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
40 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
41 import org.onap.policy.common.parameters.ParameterService;
42 import org.onap.policy.common.utils.resources.TextFileUtils;
45 * The Class TestAvroSchemaAai.
47 * @author Liam Fallon (liam.fallon@ericsson.com)
50 public class AvroSchemaAaiTest {
51 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
52 private AxContextSchemas schemas;
53 private String aaiInventoryResponseSchema;
58 * @throws IOException Signals that an I/O exception has occurred.
61 public void initTest() throws IOException {
62 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
63 ModelService.registerModel(AxContextSchemas.class, schemas);
65 aaiInventoryResponseSchema =
66 TextFileUtils.getTextFileAsString("src/test/resources/avsc/AAIInventoryResponseItemType.avsc");
73 public void initContext() {
74 SchemaParameters schemaParameters = new SchemaParameters();
75 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
76 schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
77 ParameterService.register(schemaParameters);
85 public void clearContext() {
86 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
90 * Test AAI response policy.
92 * @throws IOException Signals that an I/O exception has occurred.
95 public void testAaiResponsePolicy() throws IOException {
96 final AxContextSchema avroSchema =
97 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", aaiInventoryResponseSchema);
99 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
100 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
102 final Array<?> newArrayEmpty = (Array<?>) schemaHelper.createNewInstance();
103 assertEquals(0, newArrayEmpty.size());
105 final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/AAIResponse4Policy.json");
106 final Array<?> newArrayFull = (Array<?>) schemaHelper.createNewInstance(inString);
107 final String vnfName = ((GenericRecord) ((GenericRecord) newArrayFull.get(0)).get("generic_DasH_vnf"))
108 .get("vnf_DasH_name").toString();
109 assertEquals("ZRDM2MMEX39", vnfName);