From e8b197315437cac84872752e2ea090d8fb233941 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Tue, 20 Nov 2018 14:46:45 +0900 Subject: Rename test classes in apex-pdp Make test classes name consistence under auth, model, plugins, testsuits, tools projects Issue-ID: POLICY-1263 Change-Id: I49ec9a9f5b457d6381e693de2c04ec0268ad1b02 Signed-off-by: Parshad Patel --- .../context/schema/avro/AvroSchemaAaiTest.java | 111 ++++++ .../context/schema/avro/AvroSchemaArrayTest.java | 159 ++++++++ .../context/schema/avro/AvroSchemaEnumTest.java | 167 +++++++++ .../context/schema/avro/AvroSchemaFixedTest.java | 175 +++++++++ .../avro/AvroSchemaHelperBadSchemasTest.java | 153 ++++++++ .../schema/avro/AvroSchemaHelperMarshalTest.java | 326 ++++++++++++++++ .../schema/avro/AvroSchemaHelperUnmarshalTest.java | 417 +++++++++++++++++++++ .../context/schema/avro/AvroSchemaMapTest.java | 209 +++++++++++ .../context/schema/avro/AvroSchemaRecordTest.java | 222 +++++++++++ .../context/schema/avro/AvroSchemaUnionTest.java | 153 ++++++++ .../context/schema/avro/HealthCheckSchemaTest.java | 159 ++++++++ .../context/schema/avro/TestAvroSchemaAai.java | 111 ------ .../context/schema/avro/TestAvroSchemaArray.java | 159 -------- .../context/schema/avro/TestAvroSchemaEnum.java | 167 --------- .../context/schema/avro/TestAvroSchemaFixed.java | 175 --------- .../avro/TestAvroSchemaHelperBadSchemas.java | 153 -------- .../schema/avro/TestAvroSchemaHelperMarshal.java | 326 ---------------- .../schema/avro/TestAvroSchemaHelperUnmarshal.java | 417 --------------------- .../context/schema/avro/TestAvroSchemaMap.java | 209 ----------- .../context/schema/avro/TestAvroSchemaRecord.java | 222 ----------- .../context/schema/avro/TestAvroSchemaUnion.java | 153 -------- .../context/schema/avro/TestHealthCheckSchema.java | 159 -------- 22 files changed, 2251 insertions(+), 2251 deletions(-) create mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaAaiTest.java create mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaArrayTest.java create mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaEnumTest.java create mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaFixedTest.java create mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperBadSchemasTest.java create mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperMarshalTest.java create mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperUnmarshalTest.java create mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaMapTest.java create mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaRecordTest.java create mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaUnionTest.java create mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/HealthCheckSchemaTest.java delete mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaAai.java delete mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaArray.java delete mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaEnum.java delete mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaFixed.java delete mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperBadSchemas.java delete mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperMarshal.java delete mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperUnmarshal.java delete mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaMap.java delete mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaRecord.java delete mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaUnion.java delete mode 100644 plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestHealthCheckSchema.java (limited to 'plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test') diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaAaiTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaAaiTest.java new file mode 100644 index 000000000..b92615932 --- /dev/null +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaAaiTest.java @@ -0,0 +1,111 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.context.schema.avro; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.apache.avro.generic.GenericData.Array; +import org.apache.avro.generic.GenericRecord; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.utilities.TextFileUtils; +import org.onap.policy.common.parameters.ParameterService; + +/** + * The Class TestAvroSchemaAai. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class AvroSchemaAaiTest { + private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); + private AxContextSchemas schemas; + private String aaiInventoryResponseSchema; + + /** + * Inits the test. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void initTest() throws IOException { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + + aaiInventoryResponseSchema = + TextFileUtils.getTextFileAsString("src/test/resources/avsc/AAIInventoryResponseItemType.avsc"); + } + + /** + * Inits the context. + */ + @Before + public void initContext() { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); + ParameterService.register(schemaParameters); + + } + + /** + * Clear context. + */ + @After + public void clearContext() { + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + /** + * Test AAI response policy. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testAaiResponsePolicy() throws IOException { + final AxContextSchema avroSchema = + new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", aaiInventoryResponseSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + final Array newArrayEmpty = (Array) schemaHelper.createNewInstance(); + assertEquals(0, newArrayEmpty.size()); + + final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/AAIResponse4Policy.json"); + final Array newArrayFull = (Array) schemaHelper.createNewInstance(inString); + final String vnfName = ((GenericRecord) ((GenericRecord) newArrayFull.get(0)).get("generic_DasH_vnf")) + .get("vnf_DasH_name").toString(); + assertEquals("ZRDM2MMEX39", vnfName); + } +} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaArrayTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaArrayTest.java new file mode 100644 index 000000000..e72f7c7b3 --- /dev/null +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaArrayTest.java @@ -0,0 +1,159 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.context.schema.avro; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.apache.avro.generic.GenericData.Array; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.utilities.TextFileUtils; +import org.onap.policy.common.parameters.ParameterService; + +// TODO: Auto-generated Javadoc +/** + * The Class TestAvroSchemaArray. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class AvroSchemaArrayTest { + private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); + private AxContextSchemas schemas; + private String longArraySchema; + private String addressArraySchema; + + /** + * Inits the test. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void initTest() throws IOException { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + longArraySchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/ArrayExampleLong.avsc"); + addressArraySchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/ArrayExampleAddress.avsc"); + } + + /** + * Inits the context. + */ + @Before + public void initContext() { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); + ParameterService.register(schemaParameters); + + } + + /** + * Clear context. + */ + @After + public void clearContext() { + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + /** + * Test array init. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testArrayInit() throws IOException { + final AxContextSchema avroSchema = + new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", addressArraySchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + final Array newArrayEmpty = (Array) schemaHelper.createNewInstance(); + assertEquals(0, newArrayEmpty.size()); + + final String inString = + TextFileUtils.getTextFileAsString("src/test/resources/data/ArrayExampleAddressFull.json"); + final Array newArrayFull = (Array) schemaHelper.createNewInstance(inString); + assertEquals("{\"streetaddress\": \"1600 Pennsylvania Avenue\", \"city\": \"Washington DC\"}", + newArrayFull.get(0).toString()); + } + + /** + * Test long array unmarshal marshal. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testLongArrayUnmarshalMarshal() throws IOException { + final AxContextSchema avroSchema = + new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", longArraySchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleLongNull.json"); + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleLongFull.json"); + } + + /** + * Test address array unmarshal marshal. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testAddressArrayUnmarshalMarshal() throws IOException { + final AxContextSchema avroSchema = + new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", addressArraySchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleAddressNull.json"); + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleAddressFull.json"); + } + + /** + * Test unmarshal marshal. + * + * @param schemaHelper the schema helper + * @param fileName the file name + * @throws IOException Signals that an I/O exception has occurred. + */ + private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException { + final String inString = TextFileUtils.getTextFileAsString(fileName); + final Array schemaObject = (Array) schemaHelper.unmarshal(inString); + final String outString = schemaHelper.marshal2String(schemaObject); + assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", "")); + } +} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaEnumTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaEnumTest.java new file mode 100644 index 000000000..54a46958b --- /dev/null +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaEnumTest.java @@ -0,0 +1,167 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.context.schema.avro; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.IOException; + +import org.apache.avro.generic.GenericData.EnumSymbol; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.utilities.TextFileUtils; +import org.onap.policy.common.parameters.ParameterService; + +// TODO: Auto-generated Javadoc +/** + * The Class TestAvroSchemaEnum. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class AvroSchemaEnumTest { + private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); + private AxContextSchemas schemas; + private String enumSchema; + + /** + * Inits the test. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void initTest() throws IOException { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + enumSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/EnumSchema.avsc"); + } + + /** + * Inits the context. + */ + @Before + public void initContext() { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); + ParameterService.register(schemaParameters); + + } + + /** + * Clear context. + */ + @After + public void clearContext() { + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + /** + * Test enum init. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testEnumInit() throws IOException { + final AxContextSchema avroSchema = + new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", enumSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + final EnumSymbol newEnumEmpty = (EnumSymbol) schemaHelper.createNewInstance(); + assertEquals("SPADES", newEnumEmpty.toString()); + + final EnumSymbol newEnumFull = (EnumSymbol) schemaHelper.createNewInstance("\"HEARTS\""); + assertEquals("HEARTS", newEnumFull.toString()); + } + + /** + * Test enum unmarshal marshal. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testEnumUnmarshalMarshal() throws IOException { + final AxContextSchema avroSchema = + new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", enumSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleHearts.json"); + + try { + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json"); + fail("This test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL", + e.getMessage()); + } + try { + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json"); + fail("This test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL", + e.getMessage()); + } + try { + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad0.json"); + fail("This test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: object \"\"TWEED\"\" Avro unmarshalling failed: Unknown symbol in enum TWEED", + e.getMessage()); + } + try { + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad1.json"); + fail("This test should throw an exception here"); + } catch (final Exception e) { + assertEquals( + "AvroTest:0.0.1: object \"\"Hearts\"\" Avro unmarshalling failed: Unknown symbol in enum Hearts", + e.getMessage()); + } + } + + /** + * Test unmarshal marshal. + * + * @param schemaHelper the schema helper + * @param fileName the file name + * @throws IOException Signals that an I/O exception has occurred. + */ + private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException { + final String inString = TextFileUtils.getTextFileAsString(fileName); + final EnumSymbol decodedObject = (EnumSymbol) schemaHelper.unmarshal(inString); + final String outString = schemaHelper.marshal2String(decodedObject); + assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " ")); + } +} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaFixedTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaFixedTest.java new file mode 100644 index 000000000..7fb417c78 --- /dev/null +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaFixedTest.java @@ -0,0 +1,175 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.context.schema.avro; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.IOException; + +import org.apache.avro.generic.GenericData.Fixed; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.utilities.TextFileUtils; +import org.onap.policy.common.parameters.ParameterService; + +/** + * The Class TestAvroSchemaFixed. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class AvroSchemaFixedTest { + private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); + private AxContextSchemas schemas; + private String fixedSchema; + + /** + * Inits the test. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void initTest() throws IOException { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + fixedSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/FixedSchema.avsc"); + } + + /** + * Inits the context. + */ + @Before + public void initContext() { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); + ParameterService.register(schemaParameters); + + } + + /** + * Clear context. + */ + @After + public void clearContext() { + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + /** + * Test fixed init. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFixedInit() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", + fixedSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + try { + schemaHelper.createNewInstance(); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: could not create an instance " + + "of class \"org.apache.avro.generic.GenericData.Fixed\" " + + "using the default constructor \"Fixed()\"", e.getMessage()); + } + + final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/FixedExampleGood.json"); + final Fixed newFixedFull = (Fixed) schemaHelper.createNewInstance(inString); + assertTrue(newFixedFull.toString().startsWith("[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65")); + assertTrue(newFixedFull.toString().endsWith("53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70]")); + } + + /** + * Test fixed unmarshal marshal. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFixedUnmarshalMarshal() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", + fixedSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleGood.json"); + + try { + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json"); + fail("This test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL", + e.getMessage()); + } + try { + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json"); + fail("This test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL", + e.getMessage()); + } + try { + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad0.json"); + fail("This test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: object \"\"BADBAD\"\" " + + "Avro unmarshalling failed: Expected fixed length 64, but got6", e.getMessage()); + } + try { + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad1.json"); + fail("This test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: object " + + "\"\"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0\"\" " + + "Avro unmarshalling failed: Expected fixed length 64, but got65", e.getMessage()); + } + } + + /** + * Test unmarshal marshal. + * + * @param schemaHelper the schema helper + * @param fileName the file name + * @throws IOException Signals that an I/O exception has occurred. + */ + private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException { + final String inString = TextFileUtils.getTextFileAsString(fileName); + final Fixed decodedObject = (Fixed) schemaHelper.unmarshal(inString); + final String outString = schemaHelper.marshal2String(decodedObject); + assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " ")); + } +} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperBadSchemasTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperBadSchemasTest.java new file mode 100644 index 000000000..0eae99bfc --- /dev/null +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperBadSchemasTest.java @@ -0,0 +1,153 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.context.schema.avro; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.common.parameters.ParameterService; + +/** + * The Class TestAvroSchemaHelperBadSchemas. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class AvroSchemaHelperBadSchemasTest { + private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); + private AxContextSchemas schemas; + + /** + * Inits the test. + */ + @Before + public void initTest() { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + } + + /** + * Inits the context. + */ + @Before + public void initContext() { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); + ParameterService.register(schemaParameters); + + } + + /** + * Clear context. + */ + @After + public void clearContext() { + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + /** + * Bad schema test. + */ + @Test + public void badSchemaTest() { + final AxContextSchema avroBadSchema0 = new AxContextSchema(new AxArtifactKey("AvroBad0", "0.0.1"), "AVRO", "}"); + schemas.getSchemasMap().put(avroBadSchema0.getKey(), avroBadSchema0); + + try { + new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema0.getKey()); + fail("This test should throw an exception"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad0:0.0.1\" schema is invalid")); + } + + final AxContextSchema avroBadSchema1 = new AxContextSchema(new AxArtifactKey("AvroBad1", "0.0.1"), "AVRO", ""); + schemas.getSchemasMap().put(avroBadSchema1.getKey(), avroBadSchema1); + + try { + new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema1.getKey()); + fail("This test should throw an exception"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad1:0.0.1\" schema is invalid")); + } + + final AxContextSchema avroBadSchema2 = + new AxContextSchema(new AxArtifactKey("AvroBad2", "0.0.1"), "AVRO", "{}"); + schemas.getSchemasMap().put(avroBadSchema2.getKey(), avroBadSchema2); + + try { + new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema2.getKey()); + fail("This test should throw an exception"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad2:0.0.1\" schema is invalid")); + } + + final AxContextSchema avroBadSchema3 = + new AxContextSchema(new AxArtifactKey("AvroBad3", "0.0.1"), "AVRO", "{zooby}"); + schemas.getSchemasMap().put(avroBadSchema3.getKey(), avroBadSchema3); + + try { + new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema3.getKey()); + fail("This test should throw an exception"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad3:0.0.1\" schema is invalid")); + } + + final AxContextSchema avroBadSchema4 = + new AxContextSchema(new AxArtifactKey("AvroBad4", "0.0.1"), "AVRO", "{\"zooby\"}"); + schemas.getSchemasMap().put(avroBadSchema4.getKey(), avroBadSchema4); + + try { + new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema4.getKey()); + fail("This test should throw an exception"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad4:0.0.1\" schema is invalid")); + } + + final AxContextSchema avroBadSchema5 = + new AxContextSchema(new AxArtifactKey("AvroBad5", "0.0.1"), "AVRO", "{\"type\": \"zooby\"}"); + schemas.getSchemasMap().put(avroBadSchema5.getKey(), avroBadSchema5); + + try { + new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema5.getKey()); + fail("This test should throw an exception"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad5:0.0.1\" schema is invalid")); + } + } +} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperMarshalTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperMarshalTest.java new file mode 100644 index 000000000..6d3ebb30a --- /dev/null +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperMarshalTest.java @@ -0,0 +1,326 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.context.schema.avro; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.common.parameters.ParameterService; + +/** + * The Class TestAvroSchemaHelperMarshal. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class AvroSchemaHelperMarshalTest { + private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); + private AxContextSchemas schemas; + + /** + * Inits the test. + */ + @Before + public void initTest() { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + } + + /** + * Inits the context. + */ + @Before + public void initContext() { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); + ParameterService.register(schemaParameters); + + } + + /** + * Clear context. + */ + @After + public void clearContext() { + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + /** + * Test null marshal. + */ + @Test + public void testNullMarshal() { + final AxContextSchema avroNullSchema = new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO", + "{\"type\": \"null\"}"); + + schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema); + final SchemaHelper schemaHelper0 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroNullSchema.getKey()); + + assertEquals("null", schemaHelper0.marshal2String(null)); + assertEquals("null", schemaHelper0.marshal2String(123)); + assertEquals("null", schemaHelper0.marshal2String("Everything is marshalled to Null, no matter what it is")); + } + + /** + * Test boolean marshal. + */ + @Test + public void testBooleanMarshal() { + final AxContextSchema avroBooleanSchema = new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO", + "{\"type\": \"boolean\"}"); + + schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema); + final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroBooleanSchema.getKey()); + + assertEquals("true", schemaHelper1.marshal2String(true)); + assertEquals("false", schemaHelper1.marshal2String(false)); + try { + schemaHelper1.marshal2String(0); + fail("Test should throw an exception here"); + } catch (final Exception e) { + e.printStackTrace(); + assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: " + + "java.lang.Integer cannot be cast to java.lang.Boolean", e.getMessage()); + } + try { + schemaHelper1.marshal2String("0"); + fail("Test should throw an exception here"); + } catch (final Exception e) { + e.printStackTrace(); + assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: " + + "java.lang.String cannot be cast to java.lang.Boolean", e.getMessage()); + } + } + + /** + * Test int marshal. + */ + @Test + public void testIntMarshal() { + final AxContextSchema avroIntSchema = new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO", + "{\"type\": \"int\"}"); + + schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema); + final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroIntSchema.getKey()); + + assertEquals("0", schemaHelper2.marshal2String(0)); + assertEquals("1", schemaHelper2.marshal2String(1)); + assertEquals("-1", schemaHelper2.marshal2String(-1)); + assertEquals("1", schemaHelper2.marshal2String(1.23)); + assertEquals("-1", schemaHelper2.marshal2String(-1.23)); + assertEquals("2147483647", schemaHelper2.marshal2String(2147483647)); + assertEquals("-2147483648", schemaHelper2.marshal2String(-2147483648)); + try { + schemaHelper2.marshal2String("Hello"); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " + + "java.lang.String cannot be cast to java.lang.Number")); + } + try { + schemaHelper2.marshal2String(null); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Integer\"")); + } + } + + /** + * Test long marshal. + */ + @Test + public void testLongMarshal() { + final AxContextSchema avroLongSchema = new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO", + "{\"type\": \"long\"}"); + + schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema); + final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroLongSchema.getKey()); + + assertEquals("0", schemaHelper3.marshal2String(0L)); + assertEquals("1", schemaHelper3.marshal2String(1L)); + assertEquals("-1", schemaHelper3.marshal2String(-1L)); + assertEquals("9223372036854775807", schemaHelper3.marshal2String(9223372036854775807L)); + assertEquals("-9223372036854775808", schemaHelper3.marshal2String(-9223372036854775808L)); + try { + schemaHelper3.marshal2String("Hello"); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " + + "java.lang.String cannot be cast to java.lang.Long")); + } + try { + schemaHelper3.marshal2String(null); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Long\"")); + } + } + + /** + * Test float marshal. + */ + @Test + public void testFloatMarshal() { + final AxContextSchema avroFloatSchema = new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO", + "{\"type\": \"float\"}"); + + schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema); + final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroFloatSchema.getKey()); + + assertEquals("0.0", schemaHelper4.marshal2String(0F)); + assertEquals("1.0", schemaHelper4.marshal2String(1F)); + assertEquals("-1.0", schemaHelper4.marshal2String(-1F)); + assertEquals("1.23", schemaHelper4.marshal2String(1.23F)); + assertEquals("-1.23", schemaHelper4.marshal2String(-1.23F)); + assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F)); + assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F)); + assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F)); + assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F)); + try { + schemaHelper4.marshal2String("Hello"); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " + + "java.lang.String cannot be cast to java.lang.Float")); + } + try { + schemaHelper4.marshal2String(null); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Float\"")); + } + } + + /** + * Test double marshal. + */ + @Test + public void testDoubleMarshal() { + final AxContextSchema avroDoubleSchema = new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO", + "{\"type\": \"double\"}"); + + schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema); + final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroDoubleSchema.getKey()); + + assertEquals("0.0", schemaHelper5.marshal2String(0D)); + assertEquals("1.0", schemaHelper5.marshal2String(1D)); + assertEquals("-1.0", schemaHelper5.marshal2String(-1D)); + assertEquals("1.23", schemaHelper5.marshal2String(1.23)); + assertEquals("-1.23", schemaHelper5.marshal2String(-1.23)); + assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18)); + assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18)); + assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18)); + assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18)); + try { + schemaHelper5.marshal2String("Hello"); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " + + "java.lang.String cannot be cast to java.lang.Double")); + } + try { + schemaHelper5.marshal2String(null); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Double\"")); + } + } + + /** + * Test string marshal. + */ + @Test + public void testStringMarshal() { + final AxContextSchema avroStringSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", + "{\"type\": \"string\"}"); + + schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema); + final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroStringSchema.getKey()); + + assertEquals("\"0\"", schemaHelper7.marshal2String("0")); + assertEquals("\"1\"", schemaHelper7.marshal2String("1")); + assertEquals("\"-1\"", schemaHelper7.marshal2String("-1")); + assertEquals("\"1.23\"", schemaHelper7.marshal2String("1.23")); + assertEquals("\"-1.23\"", schemaHelper7.marshal2String("-1.23")); + assertEquals("\"9223372036854775807\"", schemaHelper7.marshal2String("9223372036854775807")); + assertEquals("\"-9223372036854775808\"", schemaHelper7.marshal2String("-9223372036854775808")); + assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2String("9223372036854775808")); + assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2String("-9223372036854775809")); + assertEquals("\"Hello\"", schemaHelper7.marshal2String("Hello")); + try { + schemaHelper7.marshal2String(null); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.String\"")); + } + } + + /** + * Test bytes marshal. + */ + @Test + public void testBytesMarshal() { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", + "{\"type\": \"bytes\"}"); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + final byte[] helloBytes = + { 104, 101, 108, 108, 111 }; + final String helloOut = schemaHelper.marshal2String(helloBytes); + assertEquals("\"hello\"", helloOut); + + try { + schemaHelper.marshal2String(null); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Byte[]\"")); + } + } +} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperUnmarshalTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperUnmarshalTest.java new file mode 100644 index 000000000..a8711e06d --- /dev/null +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperUnmarshalTest.java @@ -0,0 +1,417 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.context.schema.avro; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.apache.avro.util.Utf8; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.common.parameters.ParameterService; + +/** + * The Class TestAvroSchemaHelperUnmarshal. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class AvroSchemaHelperUnmarshalTest { + private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); + private AxContextSchemas schemas; + + /** + * Inits the test. + */ + @Before + public void initTest() { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + } + + /** + * Inits the context. + */ + @Before + public void initContext() { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); + ParameterService.register(schemaParameters); + + } + + /** + * Clear context. + */ + @After + public void clearContext() { + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + /** + * Test null unmarshal. + */ + @Test + public void testNullUnmarshal() { + final AxContextSchema avroNullSchema = new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO", + "{\"type\": \"null\"}"); + + schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema); + final SchemaHelper schemaHelper0 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroNullSchema.getKey()); + + try { + schemaHelper0.createNewInstance(); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: could not create an instance, schema class for the schema is null", + e.getMessage()); + } + + assertEquals(null, schemaHelper0.unmarshal("null")); + + try { + schemaHelper0.unmarshal("123"); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: object \"123\" Avro unmarshalling failed: " + + "Expected null. Got VALUE_NUMBER_INT", e.getMessage()); + } + } + + /** + * Test boolean unmarshal. + */ + @Test + public void testBooleanUnmarshal() { + final AxContextSchema avroBooleanSchema = new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO", + "{\"type\": \"boolean\"}"); + + schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema); + final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroBooleanSchema.getKey()); + + try { + schemaHelper1.createNewInstance(); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\" " + + "using the default constructor \"Boolean()\"", e.getMessage()); + } + assertEquals(true, schemaHelper1.createNewInstance("true")); + + assertEquals(true, schemaHelper1.unmarshal("true")); + assertEquals(false, schemaHelper1.unmarshal("false")); + try { + schemaHelper1.unmarshal(0); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: object \"0\" of type \"java.lang.Integer\" must be assignable to " + + "\"java.lang.Boolean\" or be a Json string representation of it for " + + "Avro unmarshalling", e.getMessage()); + } + } + + /** + * Test int unmarshal. + */ + @Test + public void testIntUnmarshal() { + final AxContextSchema avroIntSchema = new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO", + "{\"type\": \"int\"}"); + + schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema); + final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroIntSchema.getKey()); + + try { + schemaHelper2.createNewInstance(); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Integer\" " + + "using the default constructor \"Integer()\"", e.getMessage()); + } + assertEquals(123, schemaHelper2.createNewInstance("123")); + + assertEquals(0, schemaHelper2.unmarshal("0")); + assertEquals(1, schemaHelper2.unmarshal("1")); + assertEquals(-1, schemaHelper2.unmarshal("-1")); + assertEquals(1, schemaHelper2.unmarshal("1.23")); + assertEquals(-1, schemaHelper2.unmarshal("-1.23")); + assertEquals(2147483647, schemaHelper2.unmarshal("2147483647")); + assertEquals(-2147483648, schemaHelper2.unmarshal("-2147483648")); + try { + schemaHelper2.unmarshal("2147483648"); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"2147483648\" Avro unmarshalling failed: " + + "Numeric value (2147483648) out of range of int")); + } + try { + schemaHelper2.unmarshal("-2147483649"); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"-2147483649\" Avro unmarshalling failed: " + + "Numeric value (-2147483649) out of range of int")); + } + try { + schemaHelper2.unmarshal(null); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " + + "String to read from cannot be null!")); + } + } + + /** + * Test long unmarshal. + */ + @Test + public void testLongUnmarshal() { + final AxContextSchema avroLongSchema = new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO", + "{\"type\": \"long\"}"); + + schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema); + final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroLongSchema.getKey()); + + try { + schemaHelper3.createNewInstance(); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" " + + "using the default constructor \"Long()\"", e.getMessage()); + } + assertEquals(123456789L, schemaHelper3.createNewInstance("123456789")); + + assertEquals(0L, schemaHelper3.unmarshal("0")); + assertEquals(1L, schemaHelper3.unmarshal("1")); + assertEquals(-1L, schemaHelper3.unmarshal("-1")); + assertEquals(1L, schemaHelper3.unmarshal("1.23")); + assertEquals(-1L, schemaHelper3.unmarshal("-1.23")); + assertEquals(9223372036854775807L, schemaHelper3.unmarshal("9223372036854775807")); + assertEquals(-9223372036854775808L, schemaHelper3.unmarshal("-9223372036854775808")); + try { + schemaHelper3.unmarshal("9223372036854775808"); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: object \"9223372036854775808\" Avro unmarshalling failed: " + + "Numeric value (9223372036854775808) out of range of long")); + } + try { + schemaHelper3.unmarshal("-9223372036854775809"); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .startsWith("AvroTest:0.0.1: object \"-9223372036854775809\" Avro unmarshalling failed: " + + "Numeric value (-9223372036854775809) out of range of long")); + } + try { + schemaHelper3.unmarshal("\"Hello\""); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: " + + "Expected long. Got VALUE_STRING")); + } + try { + schemaHelper3.unmarshal(null); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " + + "String to read from cannot be null!")); + } + } + + /** + * Test float unmarshal. + */ + @Test + public void testFloatUnmarshal() { + final AxContextSchema avroFloatSchema = new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO", + "{\"type\": \"float\"}"); + + schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema); + final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroFloatSchema.getKey()); + + try { + schemaHelper4.createNewInstance(); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Float\" " + + "using the default constructor \"Float()\"", e.getMessage()); + } + assertEquals(1.2345F, schemaHelper4.createNewInstance("1.2345")); + + assertEquals(0.0F, schemaHelper4.unmarshal("0")); + assertEquals(1.0F, schemaHelper4.unmarshal("1")); + assertEquals(-1.0F, schemaHelper4.unmarshal("-1")); + assertEquals(1.23F, schemaHelper4.unmarshal("1.23")); + assertEquals(-1.23F, schemaHelper4.unmarshal("-1.23")); + assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775807")); + assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775808")); + assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775808")); + assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775809")); + try { + schemaHelper4.unmarshal("\"Hello\""); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: " + + "Expected float. Got VALUE_STRING")); + } + try { + schemaHelper4.unmarshal(null); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " + + "String to read from cannot be null!")); + } + } + + /** + * Test double unmarshal. + */ + @Test + public void testDoubleUnmarshal() { + final AxContextSchema avroDoubleSchema = new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO", + "{\"type\": \"double\"}"); + + schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema); + final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroDoubleSchema.getKey()); + + try { + schemaHelper5.createNewInstance(); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Double\" " + + "using the default constructor \"Double()\"", e.getMessage()); + } + assertEquals(1.2345E06, schemaHelper5.createNewInstance("1.2345E06")); + + assertEquals(0.0, schemaHelper5.unmarshal("0")); + assertEquals(1.0, schemaHelper5.unmarshal("1")); + assertEquals(-1.0, schemaHelper5.unmarshal("-1")); + assertEquals(1.23, schemaHelper5.unmarshal("1.23")); + assertEquals(-1.23, schemaHelper5.unmarshal("-1.23")); + assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775807")); + assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775808")); + assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775808")); + assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775809")); + try { + schemaHelper5.unmarshal("\"Hello\""); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: " + + "Expected double. Got VALUE_STRING")); + } + try { + schemaHelper5.unmarshal(null); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " + + "String to read from cannot be null!")); + } + } + + /** + * Test string unmarshal. + */ + @Test + public void testStringUnmarshal() { + final AxContextSchema avroStringSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", + "{\"type\": \"string\"}"); + + schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema); + final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey, + avroStringSchema.getKey()); + + assertEquals("", schemaHelper7.createNewInstance("")); + assertEquals("1.2345E06", schemaHelper7.createNewInstance("1.2345E06")); + + assertEquals("0", schemaHelper7.unmarshal("0")); + assertEquals("1", schemaHelper7.unmarshal("1")); + assertEquals("-1", schemaHelper7.unmarshal("-1")); + assertEquals("1.23", schemaHelper7.unmarshal("1.23")); + assertEquals("-1.23", schemaHelper7.unmarshal("-1.23")); + assertEquals("9223372036854775807", schemaHelper7.unmarshal("9223372036854775807")); + assertEquals("-9223372036854775808", schemaHelper7.unmarshal("-9223372036854775808")); + assertEquals("9223372036854775808", schemaHelper7.unmarshal("9223372036854775808")); + assertEquals("-9223372036854775809", schemaHelper7.unmarshal("-9223372036854775809")); + assertEquals("Hello", schemaHelper7.unmarshal("Hello")); + assertEquals("Hello", schemaHelper7.unmarshal(new Utf8("Hello"))); + try { + schemaHelper7.unmarshal(null); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " + + "String to read from cannot be null!")); + } + } + + /** + * Test bytes unmarshal. + */ + @Test + public void testBytesUnmarshal() { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", + "{\"type\": \"bytes\"}"); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + try { + schemaHelper.createNewInstance(); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Byte[]\" " + + "using the default constructor \"Byte[]()\"", e.getMessage()); + } + final byte[] newBytes = (byte[]) schemaHelper.createNewInstance("\"hello\""); + assertEquals(5, newBytes.length); + assertEquals(104, newBytes[0]); + assertEquals(101, newBytes[1]); + assertEquals(108, newBytes[2]); + assertEquals(108, newBytes[3]); + assertEquals(111, newBytes[4]); + + try { + schemaHelper.unmarshal(null); + fail("Test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " + + "String to read from cannot be null!")); + } + } +} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaMapTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaMapTest.java new file mode 100644 index 000000000..666dd064b --- /dev/null +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaMapTest.java @@ -0,0 +1,209 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.context.schema.avro; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; + +import org.apache.avro.generic.GenericRecord; +import org.apache.avro.util.Utf8; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.utilities.TextFileUtils; +import org.onap.policy.common.parameters.ParameterService; + +/** + * The Class TestAvroSchemaMap. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class AvroSchemaMapTest { + private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); + private AxContextSchemas schemas; + private String longMapSchema; + private String addressMapSchema; + private String addressMapSchemaInvalidFields; + + /** + * Inits the test. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void initTest() throws IOException { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + longMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleLong.avsc"); + addressMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleAddress.avsc"); + addressMapSchemaInvalidFields = TextFileUtils + .getTextFileAsString("src/test/resources/avsc/MapExampleAddressInvalidFields.avsc"); + } + + /** + * Inits the context. + */ + @Before + public void initContext() { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); + ParameterService.register(schemaParameters); + + } + + /** + * Clear context. + */ + @After + public void clearContext() { + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + /** + * Test map init. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testMapInit() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", + addressMapSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + final HashMap newMapEmpty = (HashMap) schemaHelper.createNewInstance(); + assertEquals(0, newMapEmpty.size()); + + final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/MapExampleAddressFull.json"); + final HashMap newMapFull = (HashMap) schemaHelper.createNewInstance(inString); + + assertEquals("{\"streetaddress\": \"221 B Baker St.\", \"city\": \"London\"}", + newMapFull.get(new Utf8("address2")).toString()); + } + + /** + * Test long map unmarshal marshal. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testLongMapUnmarshalMarshal() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", + longMapSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongNull.json"); + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongFull.json"); + } + + /** + * Test address map unmarshal marshal. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testAddressMapUnmarshalMarshal() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", + addressMapSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressNull.json"); + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressFull.json"); + } + + /** + * Test sub record create. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testSubRecordCreateRecord() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", + addressMapSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + GenericRecord subRecord = (GenericRecord) schemaHelper.createNewSubInstance("AddressUSRecord"); + assertEquals(null, subRecord.get("streetAddress")); + } + + /** + * Test address map unmarshal marshal invalid fields. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testAddressMapUnmarshalMarshalInvalidFields() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", + addressMapSchemaInvalidFields); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressInvalidFields.json"); + } + + /** + * Test unmarshal marshal. + * + * @param schemaHelper the schema helper + * @param fileName the file name + * @throws IOException Signals that an I/O exception has occurred. + */ + private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException { + final String originalInString = TextFileUtils.getTextFileAsString(fileName); + final HashMap firstDecodedMap = (HashMap) schemaHelper.unmarshal(originalInString); + + final String outString = schemaHelper.marshal2String(firstDecodedMap); + + final File tempOutFile = File.createTempFile("ApexAvro", ".json"); + TextFileUtils.putStringAsFile(outString, tempOutFile); + + final String decodeEncodeInString = TextFileUtils.getTextFileAsString(fileName); + tempOutFile.delete(); + + final HashMap secondDecodedMap = (HashMap) schemaHelper.unmarshal(decodeEncodeInString); + + // Now check that our doubly encoded map equals the first decoded map, Java map equals + // checks values and keys + assertEquals(firstDecodedMap, secondDecodedMap); + } +} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaRecordTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaRecordTest.java new file mode 100644 index 000000000..35e305734 --- /dev/null +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaRecordTest.java @@ -0,0 +1,222 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.context.schema.avro; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.IOException; + +import org.apache.avro.generic.GenericRecord; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.utilities.TextFileUtils; +import org.onap.policy.common.parameters.ParameterService; + +// TODO: Auto-generated Javadoc +/** + * The Class TestAvroSchemaRecord. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class AvroSchemaRecordTest { + private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); + private AxContextSchemas schemas; + private String recordSchema; + private String recordSchemaVpn; + private String recordSchemaVpnReuse; + private String recordSchemaInvalidFields; + + /** + * Inits the test. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void initTest() throws IOException { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + recordSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExample.avsc"); + recordSchemaVpn = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPN.avsc"); + recordSchemaVpnReuse = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPNReuse.avsc"); + recordSchemaInvalidFields = TextFileUtils + .getTextFileAsString("src/test/resources/avsc/RecordExampleInvalidFields.avsc"); + } + + /** + * Inits the context. + */ + @Before + public void initContext() { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); + ParameterService.register(schemaParameters); + + } + + /** + * Clear context. + */ + @After + public void clearContext() { + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + /** + * Test record init. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRecordInit() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", + recordSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + final GenericRecord newRecordEmpty = (GenericRecord) schemaHelper.createNewInstance(); + assertEquals(null, newRecordEmpty.get("passwordHash")); + + final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/RecordExampleFull.json"); + final GenericRecord newRecordFull = (GenericRecord) schemaHelper.createNewInstance(inString); + assertEquals("gobbledygook", newRecordFull.get("passwordHash").toString()); + } + + /** + * Test record unmarshal marshal. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRecordUnmarshalMarshal() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", + recordSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleNull.json"); + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleFull.json"); + } + + /** + * Test record create. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRecordCreateRecord() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", + recordSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + GenericRecord subRecord = (GenericRecord) schemaHelper.createNewSubInstance("AddressUSRecord"); + assertEquals(null, subRecord.get("streetAddress")); + + subRecord = (GenericRecord) schemaHelper.createNewSubInstance("EmailAddress"); + assertEquals(null, subRecord.get("address")); + + try { + subRecord = (GenericRecord) schemaHelper.createNewSubInstance("IDontExist"); + fail("test should throw an exception here"); + } catch (ContextRuntimeException cre) { + assertEquals("AvroTest:0.0.1: the schema \"User\" does not have a subtype of type \"IDontExist\"", + cre.getMessage()); + } + } + + /** + * Test record unmarshal marshal invalid. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRecordUnmarshalMarshalInvalid() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", + recordSchemaInvalidFields); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleInvalidFields.json"); + } + + /** + * Test VPN record unmarshal marshal. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testVpnRecordUnmarshalMarshal() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", + recordSchemaVpn); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleVPNFull.json"); + } + + /** + * Test VPN record reuse. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testVpnRecordReuse() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", + recordSchemaVpnReuse); + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + } + + /** + * Test unmarshal marshal. + * + * @param schemaHelper the schema helper + * @param fileName the file name + * @throws IOException Signals that an I/O exception has occurred. + */ + private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException { + final String inString = TextFileUtils.getTextFileAsString(fileName); + final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString); + final String outString = schemaHelper.marshal2String(decodedObject); + assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", "")); + } +} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaUnionTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaUnionTest.java new file mode 100644 index 000000000..720b5046d --- /dev/null +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaUnionTest.java @@ -0,0 +1,153 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.context.schema.avro; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.apache.avro.generic.GenericRecord; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.utilities.TextFileUtils; +import org.onap.policy.common.parameters.ParameterService; + +/** + * The Class TestAvroSchemaUnion. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class AvroSchemaUnionTest { + private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); + private AxContextSchemas schemas; + private String uinionSchema; + + /** + * Inits the test. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void initTest() throws IOException { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + uinionSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/UnionExample.avsc"); + } + + /** + * Inits the context. + */ + @Before + public void initContext() { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); + ParameterService.register(schemaParameters); + + } + + /** + * Clear context. + */ + @After + public void clearContext() { + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + /** + * Test union all fields. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Ignore + @Test + public void testUnionAllFields() throws IOException { + final AxContextSchema avroSchema = + new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleAllFields.json"); + final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString); + + assertEquals("Ben", user.get("name").toString()); + assertEquals(7, user.get("favourite_number")); + assertEquals("red", user.get("favourite_colour").toString()); + } + + /** + * Test union optional field. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Ignore + @Test + public void testUnionOptionalField() throws IOException { + final AxContextSchema avroSchema = + new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + final String inString = + TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleOptionalField.json"); + final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString); + + assertEquals("Ben", user.get("name").toString()); + assertEquals(7, user.get("favourite_number")); + assertEquals("red", user.get("favourite_colour").toString()); + } + + /** + * Test union null field. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Ignore + @Test + public void testUnionNullField() throws IOException { + final AxContextSchema avroSchema = + new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleNullField.json"); + final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString); + + assertEquals("Ben", user.get("name").toString()); + assertEquals(7, user.get("favourite_number")); + assertEquals("red", user.get("favourite_colour").toString()); + } +} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/HealthCheckSchemaTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/HealthCheckSchemaTest.java new file mode 100644 index 000000000..9d99c2b6e --- /dev/null +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/HealthCheckSchemaTest.java @@ -0,0 +1,159 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.context.schema.avro; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.apache.avro.Schema; +import org.apache.avro.generic.GenericData; +import org.apache.avro.generic.GenericData.Record; +import org.apache.avro.generic.GenericRecord; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.utilities.TextFileUtils; +import org.onap.policy.common.parameters.ParameterService; + +/** + * The Class TestHealthCheckSchema. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class HealthCheckSchemaTest { + private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); + private AxContextSchemas schemas; + private String healthCheckSchema; + + /** + * Inits the test. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void initTest() throws IOException { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + + healthCheckSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/HealthCheckBodyType.avsc"); + } + + /** + * Inits the context. + */ + @Before + public void initContext() { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); + ParameterService.register(schemaParameters); + + } + + /** + * Clear context. + */ + @After + public void clearContext() { + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + /** + * Test health check. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testHealthCheck() throws IOException { + final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", + healthCheckSchema); + + schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); + final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); + + testUnmarshalMarshal(schemaHelper, "src/test/resources/data/HealthCheckEvent.json"); + + final GenericRecord healthCheckRecord = (Record) schemaHelper.createNewInstance(); + final Schema healthCheckRecordSchema = healthCheckRecord.getSchema(); + + final GenericRecord inputRecord = new GenericData.Record(healthCheckRecordSchema.getField("input").schema()); + final Schema inputRecordRecordSchema = inputRecord.getSchema(); + + final GenericRecord actionIndentifiersRecord = new GenericData.Record( + inputRecordRecordSchema.getField("action_DasH_identifiers").schema()); + + final GenericRecord commonHeaderRecord = new GenericData.Record( + inputRecordRecordSchema.getField("common_DasH_header").schema()); + final Schema commonHeaderRecordSchema = commonHeaderRecord.getSchema(); + + final GenericRecord commonHeaderFlagsRecord = new GenericData.Record( + commonHeaderRecordSchema.getField("flags").schema()); + + healthCheckRecord.put("input", inputRecord); + inputRecord.put("action_DasH_identifiers", actionIndentifiersRecord); + inputRecord.put("common_DasH_header", commonHeaderRecord); + commonHeaderRecord.put("flags", commonHeaderFlagsRecord); + + inputRecord.put("action", "HealthCheck"); + inputRecord.put("payload", "{\"host-ip-address\":\"131.160.203.125\",\"input.url\":\"131.160.203.125/afr\"," + + "\"request-action-type\":\"GET\",\"request-action\":\"AFR\"}"); + + actionIndentifiersRecord.put("vnf_DasH_id", "49414df5-3482-4fd8-9952-c463dff2770b"); + + commonHeaderRecord.put("request_DasH_id", "afr-request3"); + commonHeaderRecord.put("originator_DasH_id", "AFR"); + commonHeaderRecord.put("api_DasH_ver", "2.15"); + commonHeaderRecord.put("sub_DasH_request_DasH_id", "AFR-subrequest"); + commonHeaderRecord.put("timestamp", "2017-11-06T15:15:18.97Z"); + + commonHeaderFlagsRecord.put("ttl", "10000"); + commonHeaderFlagsRecord.put("force", "TRUE"); + commonHeaderFlagsRecord.put("mode", "EXCLUSIVE"); + + final String eventString = TextFileUtils.getTextFileAsString("src/test/resources/data/HealthCheckEvent.json"); + final String outString = schemaHelper.marshal2String(healthCheckRecord); + assertEquals(eventString.toString().replaceAll("\\s+", ""), outString.replaceAll("\\s+", "")); + } + + /** + * Test unmarshal marshal. + * + * @param schemaHelper the schema helper + * @param fileName the file name + * @throws IOException Signals that an I/O exception has occurred. + */ + private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException { + final String inString = TextFileUtils.getTextFileAsString(fileName); + final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString); + final String outString = schemaHelper.marshal2String(decodedObject); + assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", "")); + } +} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaAai.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaAai.java deleted file mode 100644 index bca896d2e..000000000 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaAai.java +++ /dev/null @@ -1,111 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.plugins.context.schema.avro; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; - -import org.apache.avro.generic.GenericData.Array; -import org.apache.avro.generic.GenericRecord; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.context.SchemaHelper; -import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.service.ModelService; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.apex.model.utilities.TextFileUtils; -import org.onap.policy.common.parameters.ParameterService; - -/** - * The Class TestAvroSchemaAai. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @version - */ -public class TestAvroSchemaAai { - private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); - private AxContextSchemas schemas; - private String aaiInventoryResponseSchema; - - /** - * Inits the test. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void initTest() throws IOException { - schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); - ModelService.registerModel(AxContextSchemas.class, schemas); - - aaiInventoryResponseSchema = - TextFileUtils.getTextFileAsString("src/test/resources/avsc/AAIInventoryResponseItemType.avsc"); - } - - /** - * Inits the context. - */ - @Before - public void initContext() { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - } - - /** - * Clear context. - */ - @After - public void clearContext() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - } - - /** - * Test AAI response policy. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testAaiResponsePolicy() throws IOException { - final AxContextSchema avroSchema = - new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", aaiInventoryResponseSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - final Array newArrayEmpty = (Array) schemaHelper.createNewInstance(); - assertEquals(0, newArrayEmpty.size()); - - final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/AAIResponse4Policy.json"); - final Array newArrayFull = (Array) schemaHelper.createNewInstance(inString); - final String vnfName = ((GenericRecord) ((GenericRecord) newArrayFull.get(0)).get("generic_DasH_vnf")) - .get("vnf_DasH_name").toString(); - assertEquals("ZRDM2MMEX39", vnfName); - } -} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaArray.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaArray.java deleted file mode 100644 index 972e0bd32..000000000 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaArray.java +++ /dev/null @@ -1,159 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.plugins.context.schema.avro; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; - -import org.apache.avro.generic.GenericData.Array; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.context.SchemaHelper; -import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.service.ModelService; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.apex.model.utilities.TextFileUtils; -import org.onap.policy.common.parameters.ParameterService; - -// TODO: Auto-generated Javadoc -/** - * The Class TestAvroSchemaArray. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @version - */ -public class TestAvroSchemaArray { - private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); - private AxContextSchemas schemas; - private String longArraySchema; - private String addressArraySchema; - - /** - * Inits the test. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void initTest() throws IOException { - schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); - ModelService.registerModel(AxContextSchemas.class, schemas); - longArraySchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/ArrayExampleLong.avsc"); - addressArraySchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/ArrayExampleAddress.avsc"); - } - - /** - * Inits the context. - */ - @Before - public void initContext() { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - } - - /** - * Clear context. - */ - @After - public void clearContext() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - } - - /** - * Test array init. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testArrayInit() throws IOException { - final AxContextSchema avroSchema = - new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", addressArraySchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - final Array newArrayEmpty = (Array) schemaHelper.createNewInstance(); - assertEquals(0, newArrayEmpty.size()); - - final String inString = - TextFileUtils.getTextFileAsString("src/test/resources/data/ArrayExampleAddressFull.json"); - final Array newArrayFull = (Array) schemaHelper.createNewInstance(inString); - assertEquals("{\"streetaddress\": \"1600 Pennsylvania Avenue\", \"city\": \"Washington DC\"}", - newArrayFull.get(0).toString()); - } - - /** - * Test long array unmarshal marshal. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testLongArrayUnmarshalMarshal() throws IOException { - final AxContextSchema avroSchema = - new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", longArraySchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleLongNull.json"); - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleLongFull.json"); - } - - /** - * Test address array unmarshal marshal. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testAddressArrayUnmarshalMarshal() throws IOException { - final AxContextSchema avroSchema = - new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", addressArraySchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleAddressNull.json"); - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/ArrayExampleAddressFull.json"); - } - - /** - * Test unmarshal marshal. - * - * @param schemaHelper the schema helper - * @param fileName the file name - * @throws IOException Signals that an I/O exception has occurred. - */ - private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException { - final String inString = TextFileUtils.getTextFileAsString(fileName); - final Array schemaObject = (Array) schemaHelper.unmarshal(inString); - final String outString = schemaHelper.marshal2String(schemaObject); - assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", "")); - } -} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaEnum.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaEnum.java deleted file mode 100644 index 883f14bcb..000000000 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaEnum.java +++ /dev/null @@ -1,167 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.plugins.context.schema.avro; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import java.io.IOException; - -import org.apache.avro.generic.GenericData.EnumSymbol; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.context.SchemaHelper; -import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.service.ModelService; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.apex.model.utilities.TextFileUtils; -import org.onap.policy.common.parameters.ParameterService; - -// TODO: Auto-generated Javadoc -/** - * The Class TestAvroSchemaEnum. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @version - */ -public class TestAvroSchemaEnum { - private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); - private AxContextSchemas schemas; - private String enumSchema; - - /** - * Inits the test. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void initTest() throws IOException { - schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); - ModelService.registerModel(AxContextSchemas.class, schemas); - enumSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/EnumSchema.avsc"); - } - - /** - * Inits the context. - */ - @Before - public void initContext() { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - } - - /** - * Clear context. - */ - @After - public void clearContext() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - } - - /** - * Test enum init. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testEnumInit() throws IOException { - final AxContextSchema avroSchema = - new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", enumSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - final EnumSymbol newEnumEmpty = (EnumSymbol) schemaHelper.createNewInstance(); - assertEquals("SPADES", newEnumEmpty.toString()); - - final EnumSymbol newEnumFull = (EnumSymbol) schemaHelper.createNewInstance("\"HEARTS\""); - assertEquals("HEARTS", newEnumFull.toString()); - } - - /** - * Test enum unmarshal marshal. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testEnumUnmarshalMarshal() throws IOException { - final AxContextSchema avroSchema = - new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", enumSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleHearts.json"); - - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL", - e.getMessage()); - } - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL", - e.getMessage()); - } - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad0.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"\"TWEED\"\" Avro unmarshalling failed: Unknown symbol in enum TWEED", - e.getMessage()); - } - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad1.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals( - "AvroTest:0.0.1: object \"\"Hearts\"\" Avro unmarshalling failed: Unknown symbol in enum Hearts", - e.getMessage()); - } - } - - /** - * Test unmarshal marshal. - * - * @param schemaHelper the schema helper - * @param fileName the file name - * @throws IOException Signals that an I/O exception has occurred. - */ - private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException { - final String inString = TextFileUtils.getTextFileAsString(fileName); - final EnumSymbol decodedObject = (EnumSymbol) schemaHelper.unmarshal(inString); - final String outString = schemaHelper.marshal2String(decodedObject); - assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " ")); - } -} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaFixed.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaFixed.java deleted file mode 100644 index 26f4ba8af..000000000 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaFixed.java +++ /dev/null @@ -1,175 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.plugins.context.schema.avro; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.IOException; - -import org.apache.avro.generic.GenericData.Fixed; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.context.SchemaHelper; -import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.service.ModelService; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.apex.model.utilities.TextFileUtils; -import org.onap.policy.common.parameters.ParameterService; - -/** - * The Class TestAvroSchemaFixed. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @version - */ -public class TestAvroSchemaFixed { - private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); - private AxContextSchemas schemas; - private String fixedSchema; - - /** - * Inits the test. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void initTest() throws IOException { - schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); - ModelService.registerModel(AxContextSchemas.class, schemas); - fixedSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/FixedSchema.avsc"); - } - - /** - * Inits the context. - */ - @Before - public void initContext() { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - } - - /** - * Clear context. - */ - @After - public void clearContext() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - } - - /** - * Test fixed init. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFixedInit() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", - fixedSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - try { - schemaHelper.createNewInstance(); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance " - + "of class \"org.apache.avro.generic.GenericData.Fixed\" " - + "using the default constructor \"Fixed()\"", e.getMessage()); - } - - final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/FixedExampleGood.json"); - final Fixed newFixedFull = (Fixed) schemaHelper.createNewInstance(inString); - assertTrue(newFixedFull.toString().startsWith("[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65")); - assertTrue(newFixedFull.toString().endsWith("53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70]")); - } - - /** - * Test fixed unmarshal marshal. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFixedUnmarshalMarshal() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroArray", "0.0.1"), "AVRO", - fixedSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleGood.json"); - - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL", - e.getMessage()); - } - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL", - e.getMessage()); - } - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad0.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"\"BADBAD\"\" " - + "Avro unmarshalling failed: Expected fixed length 64, but got6", e.getMessage()); - } - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad1.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object " - + "\"\"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0\"\" " - + "Avro unmarshalling failed: Expected fixed length 64, but got65", e.getMessage()); - } - } - - /** - * Test unmarshal marshal. - * - * @param schemaHelper the schema helper - * @param fileName the file name - * @throws IOException Signals that an I/O exception has occurred. - */ - private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException { - final String inString = TextFileUtils.getTextFileAsString(fileName); - final Fixed decodedObject = (Fixed) schemaHelper.unmarshal(inString); - final String outString = schemaHelper.marshal2String(decodedObject); - assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " ")); - } -} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperBadSchemas.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperBadSchemas.java deleted file mode 100644 index cc79f9eb6..000000000 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperBadSchemas.java +++ /dev/null @@ -1,153 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.plugins.context.schema.avro; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.service.ModelService; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.common.parameters.ParameterService; - -/** - * The Class TestAvroSchemaHelperBadSchemas. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @version - */ -public class TestAvroSchemaHelperBadSchemas { - private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); - private AxContextSchemas schemas; - - /** - * Inits the test. - */ - @Before - public void initTest() { - schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); - ModelService.registerModel(AxContextSchemas.class, schemas); - } - - /** - * Inits the context. - */ - @Before - public void initContext() { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - } - - /** - * Clear context. - */ - @After - public void clearContext() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - } - - /** - * Bad schema test. - */ - @Test - public void badSchemaTest() { - final AxContextSchema avroBadSchema0 = new AxContextSchema(new AxArtifactKey("AvroBad0", "0.0.1"), "AVRO", "}"); - schemas.getSchemasMap().put(avroBadSchema0.getKey(), avroBadSchema0); - - try { - new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema0.getKey()); - fail("This test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad0:0.0.1\" schema is invalid")); - } - - final AxContextSchema avroBadSchema1 = new AxContextSchema(new AxArtifactKey("AvroBad1", "0.0.1"), "AVRO", ""); - schemas.getSchemasMap().put(avroBadSchema1.getKey(), avroBadSchema1); - - try { - new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema1.getKey()); - fail("This test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad1:0.0.1\" schema is invalid")); - } - - final AxContextSchema avroBadSchema2 = - new AxContextSchema(new AxArtifactKey("AvroBad2", "0.0.1"), "AVRO", "{}"); - schemas.getSchemasMap().put(avroBadSchema2.getKey(), avroBadSchema2); - - try { - new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema2.getKey()); - fail("This test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad2:0.0.1\" schema is invalid")); - } - - final AxContextSchema avroBadSchema3 = - new AxContextSchema(new AxArtifactKey("AvroBad3", "0.0.1"), "AVRO", "{zooby}"); - schemas.getSchemasMap().put(avroBadSchema3.getKey(), avroBadSchema3); - - try { - new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema3.getKey()); - fail("This test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad3:0.0.1\" schema is invalid")); - } - - final AxContextSchema avroBadSchema4 = - new AxContextSchema(new AxArtifactKey("AvroBad4", "0.0.1"), "AVRO", "{\"zooby\"}"); - schemas.getSchemasMap().put(avroBadSchema4.getKey(), avroBadSchema4); - - try { - new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema4.getKey()); - fail("This test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad4:0.0.1\" schema is invalid")); - } - - final AxContextSchema avroBadSchema5 = - new AxContextSchema(new AxArtifactKey("AvroBad5", "0.0.1"), "AVRO", "{\"type\": \"zooby\"}"); - schemas.getSchemasMap().put(avroBadSchema5.getKey(), avroBadSchema5); - - try { - new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema5.getKey()); - fail("This test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad5:0.0.1\" schema is invalid")); - } - } -} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperMarshal.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperMarshal.java deleted file mode 100644 index 86d8426de..000000000 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperMarshal.java +++ /dev/null @@ -1,326 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.plugins.context.schema.avro; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.context.SchemaHelper; -import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.service.ModelService; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.common.parameters.ParameterService; - -/** - * The Class TestAvroSchemaHelperMarshal. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @version - */ -public class TestAvroSchemaHelperMarshal { - private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); - private AxContextSchemas schemas; - - /** - * Inits the test. - */ - @Before - public void initTest() { - schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); - ModelService.registerModel(AxContextSchemas.class, schemas); - } - - /** - * Inits the context. - */ - @Before - public void initContext() { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - } - - /** - * Clear context. - */ - @After - public void clearContext() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - } - - /** - * Test null marshal. - */ - @Test - public void testNullMarshal() { - final AxContextSchema avroNullSchema = new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO", - "{\"type\": \"null\"}"); - - schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema); - final SchemaHelper schemaHelper0 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroNullSchema.getKey()); - - assertEquals("null", schemaHelper0.marshal2String(null)); - assertEquals("null", schemaHelper0.marshal2String(123)); - assertEquals("null", schemaHelper0.marshal2String("Everything is marshalled to Null, no matter what it is")); - } - - /** - * Test boolean marshal. - */ - @Test - public void testBooleanMarshal() { - final AxContextSchema avroBooleanSchema = new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO", - "{\"type\": \"boolean\"}"); - - schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema); - final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroBooleanSchema.getKey()); - - assertEquals("true", schemaHelper1.marshal2String(true)); - assertEquals("false", schemaHelper1.marshal2String(false)); - try { - schemaHelper1.marshal2String(0); - fail("Test should throw an exception here"); - } catch (final Exception e) { - e.printStackTrace(); - assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: " - + "java.lang.Integer cannot be cast to java.lang.Boolean", e.getMessage()); - } - try { - schemaHelper1.marshal2String("0"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - e.printStackTrace(); - assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: " - + "java.lang.String cannot be cast to java.lang.Boolean", e.getMessage()); - } - } - - /** - * Test int marshal. - */ - @Test - public void testIntMarshal() { - final AxContextSchema avroIntSchema = new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO", - "{\"type\": \"int\"}"); - - schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema); - final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroIntSchema.getKey()); - - assertEquals("0", schemaHelper2.marshal2String(0)); - assertEquals("1", schemaHelper2.marshal2String(1)); - assertEquals("-1", schemaHelper2.marshal2String(-1)); - assertEquals("1", schemaHelper2.marshal2String(1.23)); - assertEquals("-1", schemaHelper2.marshal2String(-1.23)); - assertEquals("2147483647", schemaHelper2.marshal2String(2147483647)); - assertEquals("-2147483648", schemaHelper2.marshal2String(-2147483648)); - try { - schemaHelper2.marshal2String("Hello"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " - + "java.lang.String cannot be cast to java.lang.Number")); - } - try { - schemaHelper2.marshal2String(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Integer\"")); - } - } - - /** - * Test long marshal. - */ - @Test - public void testLongMarshal() { - final AxContextSchema avroLongSchema = new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO", - "{\"type\": \"long\"}"); - - schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema); - final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroLongSchema.getKey()); - - assertEquals("0", schemaHelper3.marshal2String(0L)); - assertEquals("1", schemaHelper3.marshal2String(1L)); - assertEquals("-1", schemaHelper3.marshal2String(-1L)); - assertEquals("9223372036854775807", schemaHelper3.marshal2String(9223372036854775807L)); - assertEquals("-9223372036854775808", schemaHelper3.marshal2String(-9223372036854775808L)); - try { - schemaHelper3.marshal2String("Hello"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " - + "java.lang.String cannot be cast to java.lang.Long")); - } - try { - schemaHelper3.marshal2String(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Long\"")); - } - } - - /** - * Test float marshal. - */ - @Test - public void testFloatMarshal() { - final AxContextSchema avroFloatSchema = new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO", - "{\"type\": \"float\"}"); - - schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema); - final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroFloatSchema.getKey()); - - assertEquals("0.0", schemaHelper4.marshal2String(0F)); - assertEquals("1.0", schemaHelper4.marshal2String(1F)); - assertEquals("-1.0", schemaHelper4.marshal2String(-1F)); - assertEquals("1.23", schemaHelper4.marshal2String(1.23F)); - assertEquals("-1.23", schemaHelper4.marshal2String(-1.23F)); - assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F)); - assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F)); - assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F)); - assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F)); - try { - schemaHelper4.marshal2String("Hello"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " - + "java.lang.String cannot be cast to java.lang.Float")); - } - try { - schemaHelper4.marshal2String(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Float\"")); - } - } - - /** - * Test double marshal. - */ - @Test - public void testDoubleMarshal() { - final AxContextSchema avroDoubleSchema = new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO", - "{\"type\": \"double\"}"); - - schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema); - final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroDoubleSchema.getKey()); - - assertEquals("0.0", schemaHelper5.marshal2String(0D)); - assertEquals("1.0", schemaHelper5.marshal2String(1D)); - assertEquals("-1.0", schemaHelper5.marshal2String(-1D)); - assertEquals("1.23", schemaHelper5.marshal2String(1.23)); - assertEquals("-1.23", schemaHelper5.marshal2String(-1.23)); - assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18)); - assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18)); - assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18)); - assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18)); - try { - schemaHelper5.marshal2String("Hello"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " - + "java.lang.String cannot be cast to java.lang.Double")); - } - try { - schemaHelper5.marshal2String(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Double\"")); - } - } - - /** - * Test string marshal. - */ - @Test - public void testStringMarshal() { - final AxContextSchema avroStringSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", - "{\"type\": \"string\"}"); - - schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema); - final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroStringSchema.getKey()); - - assertEquals("\"0\"", schemaHelper7.marshal2String("0")); - assertEquals("\"1\"", schemaHelper7.marshal2String("1")); - assertEquals("\"-1\"", schemaHelper7.marshal2String("-1")); - assertEquals("\"1.23\"", schemaHelper7.marshal2String("1.23")); - assertEquals("\"-1.23\"", schemaHelper7.marshal2String("-1.23")); - assertEquals("\"9223372036854775807\"", schemaHelper7.marshal2String("9223372036854775807")); - assertEquals("\"-9223372036854775808\"", schemaHelper7.marshal2String("-9223372036854775808")); - assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2String("9223372036854775808")); - assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2String("-9223372036854775809")); - assertEquals("\"Hello\"", schemaHelper7.marshal2String("Hello")); - try { - schemaHelper7.marshal2String(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.String\"")); - } - } - - /** - * Test bytes marshal. - */ - @Test - public void testBytesMarshal() { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", - "{\"type\": \"bytes\"}"); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - final byte[] helloBytes = - { 104, 101, 108, 108, 111 }; - final String helloOut = schemaHelper.marshal2String(helloBytes); - assertEquals("\"hello\"", helloOut); - - try { - schemaHelper.marshal2String(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Byte[]\"")); - } - } -} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperUnmarshal.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperUnmarshal.java deleted file mode 100644 index 6dede515e..000000000 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperUnmarshal.java +++ /dev/null @@ -1,417 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.plugins.context.schema.avro; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.apache.avro.util.Utf8; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.context.SchemaHelper; -import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.service.ModelService; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.common.parameters.ParameterService; - -/** - * The Class TestAvroSchemaHelperUnmarshal. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @version - */ -public class TestAvroSchemaHelperUnmarshal { - private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); - private AxContextSchemas schemas; - - /** - * Inits the test. - */ - @Before - public void initTest() { - schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); - ModelService.registerModel(AxContextSchemas.class, schemas); - } - - /** - * Inits the context. - */ - @Before - public void initContext() { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - } - - /** - * Clear context. - */ - @After - public void clearContext() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - } - - /** - * Test null unmarshal. - */ - @Test - public void testNullUnmarshal() { - final AxContextSchema avroNullSchema = new AxContextSchema(new AxArtifactKey("AvroNull", "0.0.1"), "AVRO", - "{\"type\": \"null\"}"); - - schemas.getSchemasMap().put(avroNullSchema.getKey(), avroNullSchema); - final SchemaHelper schemaHelper0 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroNullSchema.getKey()); - - try { - schemaHelper0.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance, schema class for the schema is null", - e.getMessage()); - } - - assertEquals(null, schemaHelper0.unmarshal("null")); - - try { - schemaHelper0.unmarshal("123"); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"123\" Avro unmarshalling failed: " - + "Expected null. Got VALUE_NUMBER_INT", e.getMessage()); - } - } - - /** - * Test boolean unmarshal. - */ - @Test - public void testBooleanUnmarshal() { - final AxContextSchema avroBooleanSchema = new AxContextSchema(new AxArtifactKey("AvroBoolean", "0.0.1"), "AVRO", - "{\"type\": \"boolean\"}"); - - schemas.getSchemasMap().put(avroBooleanSchema.getKey(), avroBooleanSchema); - final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroBooleanSchema.getKey()); - - try { - schemaHelper1.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\" " - + "using the default constructor \"Boolean()\"", e.getMessage()); - } - assertEquals(true, schemaHelper1.createNewInstance("true")); - - assertEquals(true, schemaHelper1.unmarshal("true")); - assertEquals(false, schemaHelper1.unmarshal("false")); - try { - schemaHelper1.unmarshal(0); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"0\" of type \"java.lang.Integer\" must be assignable to " - + "\"java.lang.Boolean\" or be a Json string representation of it for " - + "Avro unmarshalling", e.getMessage()); - } - } - - /** - * Test int unmarshal. - */ - @Test - public void testIntUnmarshal() { - final AxContextSchema avroIntSchema = new AxContextSchema(new AxArtifactKey("AvroInt", "0.0.1"), "AVRO", - "{\"type\": \"int\"}"); - - schemas.getSchemasMap().put(avroIntSchema.getKey(), avroIntSchema); - final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroIntSchema.getKey()); - - try { - schemaHelper2.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Integer\" " - + "using the default constructor \"Integer()\"", e.getMessage()); - } - assertEquals(123, schemaHelper2.createNewInstance("123")); - - assertEquals(0, schemaHelper2.unmarshal("0")); - assertEquals(1, schemaHelper2.unmarshal("1")); - assertEquals(-1, schemaHelper2.unmarshal("-1")); - assertEquals(1, schemaHelper2.unmarshal("1.23")); - assertEquals(-1, schemaHelper2.unmarshal("-1.23")); - assertEquals(2147483647, schemaHelper2.unmarshal("2147483647")); - assertEquals(-2147483648, schemaHelper2.unmarshal("-2147483648")); - try { - schemaHelper2.unmarshal("2147483648"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"2147483648\" Avro unmarshalling failed: " - + "Numeric value (2147483648) out of range of int")); - } - try { - schemaHelper2.unmarshal("-2147483649"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"-2147483649\" Avro unmarshalling failed: " - + "Numeric value (-2147483649) out of range of int")); - } - try { - schemaHelper2.unmarshal(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " - + "String to read from cannot be null!")); - } - } - - /** - * Test long unmarshal. - */ - @Test - public void testLongUnmarshal() { - final AxContextSchema avroLongSchema = new AxContextSchema(new AxArtifactKey("AvroLong", "0.0.1"), "AVRO", - "{\"type\": \"long\"}"); - - schemas.getSchemasMap().put(avroLongSchema.getKey(), avroLongSchema); - final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroLongSchema.getKey()); - - try { - schemaHelper3.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" " - + "using the default constructor \"Long()\"", e.getMessage()); - } - assertEquals(123456789L, schemaHelper3.createNewInstance("123456789")); - - assertEquals(0L, schemaHelper3.unmarshal("0")); - assertEquals(1L, schemaHelper3.unmarshal("1")); - assertEquals(-1L, schemaHelper3.unmarshal("-1")); - assertEquals(1L, schemaHelper3.unmarshal("1.23")); - assertEquals(-1L, schemaHelper3.unmarshal("-1.23")); - assertEquals(9223372036854775807L, schemaHelper3.unmarshal("9223372036854775807")); - assertEquals(-9223372036854775808L, schemaHelper3.unmarshal("-9223372036854775808")); - try { - schemaHelper3.unmarshal("9223372036854775808"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: object \"9223372036854775808\" Avro unmarshalling failed: " - + "Numeric value (9223372036854775808) out of range of long")); - } - try { - schemaHelper3.unmarshal("-9223372036854775809"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: object \"-9223372036854775809\" Avro unmarshalling failed: " - + "Numeric value (-9223372036854775809) out of range of long")); - } - try { - schemaHelper3.unmarshal("\"Hello\""); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: " - + "Expected long. Got VALUE_STRING")); - } - try { - schemaHelper3.unmarshal(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " - + "String to read from cannot be null!")); - } - } - - /** - * Test float unmarshal. - */ - @Test - public void testFloatUnmarshal() { - final AxContextSchema avroFloatSchema = new AxContextSchema(new AxArtifactKey("AvroFloat", "0.0.1"), "AVRO", - "{\"type\": \"float\"}"); - - schemas.getSchemasMap().put(avroFloatSchema.getKey(), avroFloatSchema); - final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroFloatSchema.getKey()); - - try { - schemaHelper4.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Float\" " - + "using the default constructor \"Float()\"", e.getMessage()); - } - assertEquals(1.2345F, schemaHelper4.createNewInstance("1.2345")); - - assertEquals(0.0F, schemaHelper4.unmarshal("0")); - assertEquals(1.0F, schemaHelper4.unmarshal("1")); - assertEquals(-1.0F, schemaHelper4.unmarshal("-1")); - assertEquals(1.23F, schemaHelper4.unmarshal("1.23")); - assertEquals(-1.23F, schemaHelper4.unmarshal("-1.23")); - assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775807")); - assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775808")); - assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775808")); - assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775809")); - try { - schemaHelper4.unmarshal("\"Hello\""); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: " - + "Expected float. Got VALUE_STRING")); - } - try { - schemaHelper4.unmarshal(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " - + "String to read from cannot be null!")); - } - } - - /** - * Test double unmarshal. - */ - @Test - public void testDoubleUnmarshal() { - final AxContextSchema avroDoubleSchema = new AxContextSchema(new AxArtifactKey("AvroDouble", "0.0.1"), "AVRO", - "{\"type\": \"double\"}"); - - schemas.getSchemasMap().put(avroDoubleSchema.getKey(), avroDoubleSchema); - final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroDoubleSchema.getKey()); - - try { - schemaHelper5.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Double\" " - + "using the default constructor \"Double()\"", e.getMessage()); - } - assertEquals(1.2345E06, schemaHelper5.createNewInstance("1.2345E06")); - - assertEquals(0.0, schemaHelper5.unmarshal("0")); - assertEquals(1.0, schemaHelper5.unmarshal("1")); - assertEquals(-1.0, schemaHelper5.unmarshal("-1")); - assertEquals(1.23, schemaHelper5.unmarshal("1.23")); - assertEquals(-1.23, schemaHelper5.unmarshal("-1.23")); - assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775807")); - assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775808")); - assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775808")); - assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775809")); - try { - schemaHelper5.unmarshal("\"Hello\""); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: " - + "Expected double. Got VALUE_STRING")); - } - try { - schemaHelper5.unmarshal(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " - + "String to read from cannot be null!")); - } - } - - /** - * Test string unmarshal. - */ - @Test - public void testStringUnmarshal() { - final AxContextSchema avroStringSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", - "{\"type\": \"string\"}"); - - schemas.getSchemasMap().put(avroStringSchema.getKey(), avroStringSchema); - final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey, - avroStringSchema.getKey()); - - assertEquals("", schemaHelper7.createNewInstance("")); - assertEquals("1.2345E06", schemaHelper7.createNewInstance("1.2345E06")); - - assertEquals("0", schemaHelper7.unmarshal("0")); - assertEquals("1", schemaHelper7.unmarshal("1")); - assertEquals("-1", schemaHelper7.unmarshal("-1")); - assertEquals("1.23", schemaHelper7.unmarshal("1.23")); - assertEquals("-1.23", schemaHelper7.unmarshal("-1.23")); - assertEquals("9223372036854775807", schemaHelper7.unmarshal("9223372036854775807")); - assertEquals("-9223372036854775808", schemaHelper7.unmarshal("-9223372036854775808")); - assertEquals("9223372036854775808", schemaHelper7.unmarshal("9223372036854775808")); - assertEquals("-9223372036854775809", schemaHelper7.unmarshal("-9223372036854775809")); - assertEquals("Hello", schemaHelper7.unmarshal("Hello")); - assertEquals("Hello", schemaHelper7.unmarshal(new Utf8("Hello"))); - try { - schemaHelper7.unmarshal(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " - + "String to read from cannot be null!")); - } - } - - /** - * Test bytes unmarshal. - */ - @Test - public void testBytesUnmarshal() { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroString", "0.0.1"), "AVRO", - "{\"type\": \"bytes\"}"); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - try { - schemaHelper.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Byte[]\" " - + "using the default constructor \"Byte[]()\"", e.getMessage()); - } - final byte[] newBytes = (byte[]) schemaHelper.createNewInstance("\"hello\""); - assertEquals(5, newBytes.length); - assertEquals(104, newBytes[0]); - assertEquals(101, newBytes[1]); - assertEquals(108, newBytes[2]); - assertEquals(108, newBytes[3]); - assertEquals(111, newBytes[4]); - - try { - schemaHelper.unmarshal(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " - + "String to read from cannot be null!")); - } - } -} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaMap.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaMap.java deleted file mode 100644 index 37069be47..000000000 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaMap.java +++ /dev/null @@ -1,209 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.plugins.context.schema.avro; - -import static org.junit.Assert.assertEquals; - -import java.io.File; -import java.io.IOException; -import java.util.HashMap; - -import org.apache.avro.generic.GenericRecord; -import org.apache.avro.util.Utf8; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.context.SchemaHelper; -import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.service.ModelService; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.apex.model.utilities.TextFileUtils; -import org.onap.policy.common.parameters.ParameterService; - -/** - * The Class TestAvroSchemaMap. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @version - */ -public class TestAvroSchemaMap { - private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); - private AxContextSchemas schemas; - private String longMapSchema; - private String addressMapSchema; - private String addressMapSchemaInvalidFields; - - /** - * Inits the test. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void initTest() throws IOException { - schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); - ModelService.registerModel(AxContextSchemas.class, schemas); - longMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleLong.avsc"); - addressMapSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/MapExampleAddress.avsc"); - addressMapSchemaInvalidFields = TextFileUtils - .getTextFileAsString("src/test/resources/avsc/MapExampleAddressInvalidFields.avsc"); - } - - /** - * Inits the context. - */ - @Before - public void initContext() { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - } - - /** - * Clear context. - */ - @After - public void clearContext() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - } - - /** - * Test map init. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testMapInit() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", - addressMapSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - final HashMap newMapEmpty = (HashMap) schemaHelper.createNewInstance(); - assertEquals(0, newMapEmpty.size()); - - final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/MapExampleAddressFull.json"); - final HashMap newMapFull = (HashMap) schemaHelper.createNewInstance(inString); - - assertEquals("{\"streetaddress\": \"221 B Baker St.\", \"city\": \"London\"}", - newMapFull.get(new Utf8("address2")).toString()); - } - - /** - * Test long map unmarshal marshal. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testLongMapUnmarshalMarshal() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", - longMapSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongNull.json"); - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleLongFull.json"); - } - - /** - * Test address map unmarshal marshal. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testAddressMapUnmarshalMarshal() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", - addressMapSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressNull.json"); - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressFull.json"); - } - - /** - * Test sub record create. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testSubRecordCreateRecord() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", - addressMapSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - GenericRecord subRecord = (GenericRecord) schemaHelper.createNewSubInstance("AddressUSRecord"); - assertEquals(null, subRecord.get("streetAddress")); - } - - /** - * Test address map unmarshal marshal invalid fields. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testAddressMapUnmarshalMarshalInvalidFields() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroMap", "0.0.1"), "AVRO", - addressMapSchemaInvalidFields); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/MapExampleAddressInvalidFields.json"); - } - - /** - * Test unmarshal marshal. - * - * @param schemaHelper the schema helper - * @param fileName the file name - * @throws IOException Signals that an I/O exception has occurred. - */ - private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException { - final String originalInString = TextFileUtils.getTextFileAsString(fileName); - final HashMap firstDecodedMap = (HashMap) schemaHelper.unmarshal(originalInString); - - final String outString = schemaHelper.marshal2String(firstDecodedMap); - - final File tempOutFile = File.createTempFile("ApexAvro", ".json"); - TextFileUtils.putStringAsFile(outString, tempOutFile); - - final String decodeEncodeInString = TextFileUtils.getTextFileAsString(fileName); - tempOutFile.delete(); - - final HashMap secondDecodedMap = (HashMap) schemaHelper.unmarshal(decodeEncodeInString); - - // Now check that our doubly encoded map equals the first decoded map, Java map equals - // checks values and keys - assertEquals(firstDecodedMap, secondDecodedMap); - } -} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaRecord.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaRecord.java deleted file mode 100644 index b79a5cd91..000000000 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaRecord.java +++ /dev/null @@ -1,222 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.plugins.context.schema.avro; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import java.io.IOException; - -import org.apache.avro.generic.GenericRecord; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.context.ContextRuntimeException; -import org.onap.policy.apex.context.SchemaHelper; -import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.service.ModelService; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.apex.model.utilities.TextFileUtils; -import org.onap.policy.common.parameters.ParameterService; - -// TODO: Auto-generated Javadoc -/** - * The Class TestAvroSchemaRecord. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @version - */ -public class TestAvroSchemaRecord { - private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); - private AxContextSchemas schemas; - private String recordSchema; - private String recordSchemaVpn; - private String recordSchemaVpnReuse; - private String recordSchemaInvalidFields; - - /** - * Inits the test. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void initTest() throws IOException { - schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); - ModelService.registerModel(AxContextSchemas.class, schemas); - recordSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExample.avsc"); - recordSchemaVpn = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPN.avsc"); - recordSchemaVpnReuse = TextFileUtils.getTextFileAsString("src/test/resources/avsc/RecordExampleVPNReuse.avsc"); - recordSchemaInvalidFields = TextFileUtils - .getTextFileAsString("src/test/resources/avsc/RecordExampleInvalidFields.avsc"); - } - - /** - * Inits the context. - */ - @Before - public void initContext() { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - } - - /** - * Clear context. - */ - @After - public void clearContext() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - } - - /** - * Test record init. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testRecordInit() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", - recordSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - final GenericRecord newRecordEmpty = (GenericRecord) schemaHelper.createNewInstance(); - assertEquals(null, newRecordEmpty.get("passwordHash")); - - final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/RecordExampleFull.json"); - final GenericRecord newRecordFull = (GenericRecord) schemaHelper.createNewInstance(inString); - assertEquals("gobbledygook", newRecordFull.get("passwordHash").toString()); - } - - /** - * Test record unmarshal marshal. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testRecordUnmarshalMarshal() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", - recordSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleNull.json"); - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleFull.json"); - } - - /** - * Test record create. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testRecordCreateRecord() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", - recordSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - GenericRecord subRecord = (GenericRecord) schemaHelper.createNewSubInstance("AddressUSRecord"); - assertEquals(null, subRecord.get("streetAddress")); - - subRecord = (GenericRecord) schemaHelper.createNewSubInstance("EmailAddress"); - assertEquals(null, subRecord.get("address")); - - try { - subRecord = (GenericRecord) schemaHelper.createNewSubInstance("IDontExist"); - fail("test should throw an exception here"); - } catch (ContextRuntimeException cre) { - assertEquals("AvroTest:0.0.1: the schema \"User\" does not have a subtype of type \"IDontExist\"", - cre.getMessage()); - } - } - - /** - * Test record unmarshal marshal invalid. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testRecordUnmarshalMarshalInvalid() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", - recordSchemaInvalidFields); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleInvalidFields.json"); - } - - /** - * Test VPN record unmarshal marshal. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testVpnRecordUnmarshalMarshal() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", - recordSchemaVpn); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/RecordExampleVPNFull.json"); - } - - /** - * Test VPN record reuse. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testVpnRecordReuse() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", - recordSchemaVpnReuse); - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - } - - /** - * Test unmarshal marshal. - * - * @param schemaHelper the schema helper - * @param fileName the file name - * @throws IOException Signals that an I/O exception has occurred. - */ - private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException { - final String inString = TextFileUtils.getTextFileAsString(fileName); - final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString); - final String outString = schemaHelper.marshal2String(decodedObject); - assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", "")); - } -} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaUnion.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaUnion.java deleted file mode 100644 index c957d8d4c..000000000 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaUnion.java +++ /dev/null @@ -1,153 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.plugins.context.schema.avro; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; - -import org.apache.avro.generic.GenericRecord; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.onap.policy.apex.context.SchemaHelper; -import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.service.ModelService; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.apex.model.utilities.TextFileUtils; -import org.onap.policy.common.parameters.ParameterService; - -/** - * The Class TestAvroSchemaUnion. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @version - */ -public class TestAvroSchemaUnion { - private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); - private AxContextSchemas schemas; - private String uinionSchema; - - /** - * Inits the test. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void initTest() throws IOException { - schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); - ModelService.registerModel(AxContextSchemas.class, schemas); - uinionSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/UnionExample.avsc"); - } - - /** - * Inits the context. - */ - @Before - public void initContext() { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - } - - /** - * Clear context. - */ - @After - public void clearContext() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - } - - /** - * Test union all fields. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Ignore - @Test - public void testUnionAllFields() throws IOException { - final AxContextSchema avroSchema = - new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleAllFields.json"); - final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString); - - assertEquals("Ben", user.get("name").toString()); - assertEquals(7, user.get("favourite_number")); - assertEquals("red", user.get("favourite_colour").toString()); - } - - /** - * Test union optional field. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Ignore - @Test - public void testUnionOptionalField() throws IOException { - final AxContextSchema avroSchema = - new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - final String inString = - TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleOptionalField.json"); - final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString); - - assertEquals("Ben", user.get("name").toString()); - assertEquals(7, user.get("favourite_number")); - assertEquals("red", user.get("favourite_colour").toString()); - } - - /** - * Test union null field. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Ignore - @Test - public void testUnionNullField() throws IOException { - final AxContextSchema avroSchema = - new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleNullField.json"); - final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString); - - assertEquals("Ben", user.get("name").toString()); - assertEquals(7, user.get("favourite_number")); - assertEquals("red", user.get("favourite_colour").toString()); - } -} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestHealthCheckSchema.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestHealthCheckSchema.java deleted file mode 100644 index 646b8aa04..000000000 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestHealthCheckSchema.java +++ /dev/null @@ -1,159 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.plugins.context.schema.avro; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; - -import org.apache.avro.Schema; -import org.apache.avro.generic.GenericData; -import org.apache.avro.generic.GenericData.Record; -import org.apache.avro.generic.GenericRecord; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.context.SchemaHelper; -import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.service.ModelService; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.apex.model.utilities.TextFileUtils; -import org.onap.policy.common.parameters.ParameterService; - -/** - * The Class TestHealthCheckSchema. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestHealthCheckSchema { - private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); - private AxContextSchemas schemas; - private String healthCheckSchema; - - /** - * Inits the test. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void initTest() throws IOException { - schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); - ModelService.registerModel(AxContextSchemas.class, schemas); - - healthCheckSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/HealthCheckBodyType.avsc"); - } - - /** - * Inits the context. - */ - @Before - public void initContext() { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - } - - /** - * Clear context. - */ - @After - public void clearContext() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - } - - /** - * Test health check. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testHealthCheck() throws IOException { - final AxContextSchema avroSchema = new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", - healthCheckSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/HealthCheckEvent.json"); - - final GenericRecord healthCheckRecord = (Record) schemaHelper.createNewInstance(); - final Schema healthCheckRecordSchema = healthCheckRecord.getSchema(); - - final GenericRecord inputRecord = new GenericData.Record(healthCheckRecordSchema.getField("input").schema()); - final Schema inputRecordRecordSchema = inputRecord.getSchema(); - - final GenericRecord actionIndentifiersRecord = new GenericData.Record( - inputRecordRecordSchema.getField("action_DasH_identifiers").schema()); - - final GenericRecord commonHeaderRecord = new GenericData.Record( - inputRecordRecordSchema.getField("common_DasH_header").schema()); - final Schema commonHeaderRecordSchema = commonHeaderRecord.getSchema(); - - final GenericRecord commonHeaderFlagsRecord = new GenericData.Record( - commonHeaderRecordSchema.getField("flags").schema()); - - healthCheckRecord.put("input", inputRecord); - inputRecord.put("action_DasH_identifiers", actionIndentifiersRecord); - inputRecord.put("common_DasH_header", commonHeaderRecord); - commonHeaderRecord.put("flags", commonHeaderFlagsRecord); - - inputRecord.put("action", "HealthCheck"); - inputRecord.put("payload", "{\"host-ip-address\":\"131.160.203.125\",\"input.url\":\"131.160.203.125/afr\"," - + "\"request-action-type\":\"GET\",\"request-action\":\"AFR\"}"); - - actionIndentifiersRecord.put("vnf_DasH_id", "49414df5-3482-4fd8-9952-c463dff2770b"); - - commonHeaderRecord.put("request_DasH_id", "afr-request3"); - commonHeaderRecord.put("originator_DasH_id", "AFR"); - commonHeaderRecord.put("api_DasH_ver", "2.15"); - commonHeaderRecord.put("sub_DasH_request_DasH_id", "AFR-subrequest"); - commonHeaderRecord.put("timestamp", "2017-11-06T15:15:18.97Z"); - - commonHeaderFlagsRecord.put("ttl", "10000"); - commonHeaderFlagsRecord.put("force", "TRUE"); - commonHeaderFlagsRecord.put("mode", "EXCLUSIVE"); - - final String eventString = TextFileUtils.getTextFileAsString("src/test/resources/data/HealthCheckEvent.json"); - final String outString = schemaHelper.marshal2String(healthCheckRecord); - assertEquals(eventString.toString().replaceAll("\\s+", ""), outString.replaceAll("\\s+", "")); - } - - /** - * Test unmarshal marshal. - * - * @param schemaHelper the schema helper - * @param fileName the file name - * @throws IOException Signals that an I/O exception has occurred. - */ - private void testUnmarshalMarshal(final SchemaHelper schemaHelper, final String fileName) throws IOException { - final String inString = TextFileUtils.getTextFileAsString(fileName); - final GenericRecord decodedObject = (GenericRecord) schemaHelper.unmarshal(inString); - final String outString = schemaHelper.marshal2String(decodedObject); - assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", "")); - } -} -- cgit 1.2.3-korg