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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.context.schema.avro;
23 import static org.junit.Assert.assertEquals;
25 import java.io.IOException;
27 import org.apache.avro.Schema;
28 import org.apache.avro.generic.GenericData;
29 import org.apache.avro.generic.GenericData.Record;
30 import org.apache.avro.generic.GenericRecord;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.context.SchemaHelper;
35 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
36 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
37 import org.onap.policy.apex.context.parameters.SchemaParameters;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
40 import org.onap.policy.apex.model.basicmodel.service.ModelService;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
42 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
43 import org.onap.policy.apex.model.utilities.TextFileUtils;
44 import org.onap.policy.common.parameters.ParameterService;
47 * The Class TestHealthCheckSchema.
49 * @author Liam Fallon (liam.fallon@ericsson.com)
51 public class TestHealthCheckSchema {
52 private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
53 private AxContextSchemas schemas;
54 private String healthCheckSchema;
59 * @throws IOException Signals that an I/O exception has occurred.
62 public void initTest() throws IOException {
63 schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
64 ModelService.registerModel(AxContextSchemas.class, schemas);
66 healthCheckSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/HealthCheckBodyType.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);
92 * @throws IOException Signals that an I/O exception has occurred.
95 public void testHealthCheck() throws IOException {
96 final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO",
99 schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
100 final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
102 testUnmarshalMarshal(schemaHelper, "src/test/resources/data/HealthCheckEvent.json");
104 final GenericRecord healthCheckRecord = (Record) schemaHelper.createNewInstance();
105 final Schema healthCheckRecordSchema = healthCheckRecord.getSchema();
107 final GenericRecord inputRecord = new GenericData.Record(healthCheckRecordSchema.getField("input").schema());
108 final Schema inputRecordRecordSchema = inputRecord.getSchema();
110 final GenericRecord actionIndentifiersRecord = new GenericData.Record(
111 inputRecordRecordSchema.getField("action_DasH_identifiers").schema());
113 final GenericRecord commonHeaderRecord = new GenericData.Record(
114 inputRecordRecordSchema.getField("common_DasH_header").schema());
115 final Schema commonHeaderRecordSchema = commonHeaderRecord.getSchema();
117 final GenericRecord commonHeaderFlagsRecord = new GenericData.Record(
118 commonHeaderRecordSchema.getField("flags").schema());
120 healthCheckRecord.put("input", inputRecord);
121 inputRecord.put("action_DasH_identifiers", actionIndentifiersRecord);
122 inputRecord.put("common_DasH_header", commonHeaderRecord);
123 commonHeaderRecord.put("flags", commonHeaderFlagsRecord);
125 inputRecord.put("action", "HealthCheck");
126 inputRecord.put("payload", "{\"host-ip-address\":\"131.160.203.125\",\"input.url\":\"131.160.203.125/afr\","
127 + "\"request-action-type\":\"GET\",\"request-action\":\"AFR\"}");
129 actionIndentifiersRecord.put("vnf_DasH_id", "49414df5-3482-4fd8-9952-c463dff2770b");
131 commonHeaderRecord.put("request_DasH_id", "afr-request3");
132 commonHeaderRecord.put("originator_DasH_id", "AFR");
133 commonHeaderRecord.put("api_DasH_ver", "2.15");
134 commonHeaderRecord.put("sub_DasH_request_DasH_id", "AFR-subrequest");
135 commonHeaderRecord.put("timestamp", "2017-11-06T15:15:18.97Z");
137 commonHeaderFlagsRecord.put("ttl", "10000");
138 commonHeaderFlagsRecord.put("force", "TRUE");
139 commonHeaderFlagsRecord.put("mode", "EXCLUSIVE");
141 final String eventString = TextFileUtils.getTextFileAsString("src/test/resources/data/HealthCheckEvent.json");
142 final String outString = schemaHelper.marshal2String(healthCheckRecord);
143 assertEquals(eventString.toString().replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));
147 * Test unmarshal marshal.
149 * @param schemaHelper the schema helper
150 * @param fileName the file name
151 * @throws IOException Signals that an I/O exception has occurred.
153 private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
154 final String inString = TextFileUtils.getTextFileAsString(fileName);
155 final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString);
156 final String outString = schemaHelper.marshal2String(decodedObject);
157 assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));