c32d9b3d44df2cd36e4658cb74e0f6dd9695f183
[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.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.Before;
32 import org.junit.Test;
33 import org.onap.policy.apex.context.SchemaHelper;
34 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
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.apex.model.utilities.TextFileUtils;
42
43 /**
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  */
46 public class TestHealthCheckSchema {
47     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
48     private AxContextSchemas schemas;
49     private String healthCheckSchema;
50
51     @Before
52     public void initTest() throws IOException {
53         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
54         ModelService.registerModel(AxContextSchemas.class, schemas);
55
56         new SchemaParameters().getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
57         healthCheckSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/HealthCheckBodyType.avsc");
58     }
59
60
61     @Test
62     public void testHealthCheck() throws IOException {
63         final AxContextSchema avroSchema =
64                 new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "Avro", healthCheckSchema);
65
66         schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema);
67         final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey());
68
69         testUnmarshalMarshal(schemaHelper, "src/test/resources/data/HealthCheckEvent.json");
70
71         final GenericRecord healthCheckRecord = (Record) schemaHelper.createNewInstance();
72         final Schema healthCheckRecordSchema = healthCheckRecord.getSchema();
73
74         final GenericRecord inputRecord = new GenericData.Record(healthCheckRecordSchema.getField("input").schema());
75         final Schema inputRecordRecordSchema = inputRecord.getSchema();
76
77         final GenericRecord actionIndentifiersRecord =
78                 new GenericData.Record(inputRecordRecordSchema.getField("action_DasH_identifiers").schema());
79
80         final GenericRecord commonHeaderRecord =
81                 new GenericData.Record(inputRecordRecordSchema.getField("common_DasH_header").schema());
82         final Schema commonHeaderRecordSchema = commonHeaderRecord.getSchema();
83
84         final GenericRecord commonHeaderFlagsRecord =
85                 new GenericData.Record(commonHeaderRecordSchema.getField("flags").schema());
86
87         healthCheckRecord.put("input", inputRecord);
88         inputRecord.put("action_DasH_identifiers", actionIndentifiersRecord);
89         inputRecord.put("common_DasH_header", commonHeaderRecord);
90         commonHeaderRecord.put("flags", commonHeaderFlagsRecord);
91
92         inputRecord.put("action", "HealthCheck");
93         inputRecord.put("payload",
94                 "{\"host-ip-address\":\"131.160.203.125\",\"input.url\":\"131.160.203.125/afr\",\"request-action-type\":\"GET\",\"request-action\":\"AFR\"}");
95
96         actionIndentifiersRecord.put("vnf_DasH_id", "49414df5-3482-4fd8-9952-c463dff2770b");
97
98         commonHeaderRecord.put("request_DasH_id", "afr-request3");
99         commonHeaderRecord.put("originator_DasH_id", "AFR");
100         commonHeaderRecord.put("api_DasH_ver", "2.15");
101         commonHeaderRecord.put("sub_DasH_request_DasH_id", "AFR-subrequest");
102         commonHeaderRecord.put("timestamp", "2017-11-06T15:15:18.97Z");
103
104         commonHeaderFlagsRecord.put("ttl", "10000");
105         commonHeaderFlagsRecord.put("force", "TRUE");
106         commonHeaderFlagsRecord.put("mode", "EXCLUSIVE");
107
108         final String eventString = TextFileUtils.getTextFileAsString("src/test/resources/data/HealthCheckEvent.json");
109         final String outString = schemaHelper.marshal2Json(healthCheckRecord);
110         assertEquals(eventString.toString().replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));
111     }
112
113     private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException {
114         final String inString = TextFileUtils.getTextFileAsString(fileName);
115         final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString);
116         final String outString = schemaHelper.marshal2Json(decodedObject);
117         assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", ""));
118     }
119 }