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;
28 import org.apache.avro.Schema;
29 import org.apache.avro.generic.GenericData;
30 import org.apache.avro.generic.GenericData.Record;
31 import org.apache.avro.generic.GenericRecord;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.apex.context.SchemaHelper;
36 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
37 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
38 import org.onap.policy.apex.context.parameters.SchemaParameters;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
41 import org.onap.policy.apex.model.basicmodel.service.ModelService;
42 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
43 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
44 import org.onap.policy.common.parameters.ParameterService;
45 import org.onap.policy.common.utils.resources.TextFileUtils;
48 * The Class TestHealthCheckSchema.
50 * @author Liam Fallon (liam.fallon@ericsson.com)
52 public class HealthCheckSchemaTest {
53 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
54 private AxContextSchemas schemas;
55 private String healthCheckSchema;
60 * @throws IOException Signals that an I/O exception has occurred.
63 public void initTest() throws IOException {
64 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
65 ModelService.registerModel(AxContextSchemas.class, schemas);
67 healthCheckSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/HealthCheckBodyType.avsc");
74 public void initContext() {
75 SchemaParameters schemaParameters = new SchemaParameters();
76 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
77 schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
78 ParameterService.register(schemaParameters);
86 public void clearContext() {
87 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
93 * @throws IOException Signals that an I/O exception has occurred.
96 public void testHealthCheck() throws IOException {
97 final AxContextSchema avroSchema =
98 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", healthCheckSchema);
100 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
101 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
103 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/HealthCheckEvent.json");
105 final GenericRecord healthCheckRecord = (Record) schemaHelper.createNewInstance();
106 final Schema healthCheckRecordSchema = healthCheckRecord.getSchema();
108 final GenericRecord inputRecord = new GenericData.Record(healthCheckRecordSchema.getField("input").schema());
109 final Schema inputRecordRecordSchema = inputRecord.getSchema();
111 final GenericRecord actionIndentifiersRecord =
112 new GenericData.Record(inputRecordRecordSchema.getField("action_DasH_identifiers").schema());
114 final GenericRecord commonHeaderRecord =
115 new GenericData.Record(inputRecordRecordSchema.getField("common_DasH_header").schema());
116 final Schema commonHeaderRecordSchema = commonHeaderRecord.getSchema();
118 final GenericRecord commonHeaderFlagsRecord =
119 new GenericData.Record(commonHeaderRecordSchema.getField("flags").schema());
121 healthCheckRecord.put("input", inputRecord);
122 inputRecord.put("action_DasH_identifiers", actionIndentifiersRecord);
123 inputRecord.put("common_DasH_header", commonHeaderRecord);
124 commonHeaderRecord.put("flags", commonHeaderFlagsRecord);
126 inputRecord.put("action", "HealthCheck");
127 inputRecord.put("payload", "{\"host-ip-address\":\"131.160.203.125\",\"input.url\":\"131.160.203.125/afr\","
128 + "\"request-action-type\":\"GET\",\"request-action\":\"AFR\"}");
130 actionIndentifiersRecord.put("vnf_DasH_id", "49414df5-3482-4fd8-9952-c463dff2770b");
132 commonHeaderRecord.put("request_DasH_id", "afr-request3");
133 commonHeaderRecord.put("originator_DasH_id", "AFR");
134 commonHeaderRecord.put("api_DasH_ver", "2.15");
135 commonHeaderRecord.put("sub_DasH_request_DasH_id", "AFR-subrequest");
136 commonHeaderRecord.put("timestamp", "2017-11-06T15:15:18.97Z");
138 commonHeaderFlagsRecord.put("ttl", "10000");
139 commonHeaderFlagsRecord.put("force", "TRUE");
140 commonHeaderFlagsRecord.put("mode", "EXCLUSIVE");
142 final String eventString = TextFileUtils.getTextFileAsString("src/test/resources/data/HealthCheckEvent.json");
143 final String outString = schemaHelper.marshal2String(healthCheckRecord);
144 assertEquals(eventString.toString().replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));
148 * Test unmarshal marshal.
150 * @param schemaHelper the schema helper
151 * @param fileName the file name
152 * @throws IOException Signals that an I/O exception has occurred.
154 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
155 final String inString = TextFileUtils.getTextFileAsString(fileName);
156 final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString);
157 final String outString = schemaHelper.marshal2String(decodedObject);
158 assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));