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 -------- .../restrequestor/RestRequestorEndpointTest.java | 207 ++++++++++ .../adapt/restrequestor/RestRequestorTest.java | 376 +++++++++++++++++ .../adapt/restrequestor/TestRestRequestor.java | 376 ----------------- .../restrequestor/TestRestRequestorEndpoint.java | 207 ---------- .../event/protocol/xml/TestXmlEventHandler.java | 147 ------- .../protocol/xml/TestXmlTaggedEventConsumer.java | 446 --------------------- .../event/protocol/xml/XmlEventHandlerTest.java | 147 +++++++ .../protocol/xml/XmlTaggedEventConsumerTest.java | 446 +++++++++++++++++++++ .../event/protocol/yaml/TestYamlEventProtocol.java | 287 ------------- .../protocol/yaml/TestYamlPluginStability.java | 278 ------------- .../event/protocol/yaml/YamlEventProtocolTest.java | 287 +++++++++++++ .../protocol/yaml/YamlPluginStabilityTest.java | 278 +++++++++++++ 34 files changed, 3992 insertions(+), 3992 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 create mode 100644 plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/RestRequestorEndpointTest.java create mode 100644 plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/RestRequestorTest.java delete mode 100644 plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/TestRestRequestor.java delete mode 100644 plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/TestRestRequestorEndpoint.java delete mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlEventHandler.java delete mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlTaggedEventConsumer.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventHandlerTest.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlTaggedEventConsumerTest.java delete mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlEventProtocol.java delete mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlPluginStability.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolTest.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java (limited to 'plugins') 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+", "")); - } -} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/RestRequestorEndpointTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/RestRequestorEndpointTest.java new file mode 100644 index 000000000..f84b6a703 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/RestRequestorEndpointTest.java @@ -0,0 +1,207 @@ +/*- + * ============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.apps.uservice.test.adapt.restrequestor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.gson.Gson; + +import java.util.Map; + +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; + +/** + * The Class TestRestRequestorEndpoint. + */ +@Path("/apex") +public class RestRequestorEndpointTest { + + private static Object counterLock = new Object(); + private static int postMessagesReceived = 0; + private static int putMessagesReceived = 0; + private static int statMessagesReceived = 0; + private static int getMessagesReceived = 0; + private static int deleteMessagesReceived = 0; + + private static String EVENT_STRING = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.events\",\n" + + "\"name\": \"ResponseEvent\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + + getMessagesReceived + "\",\n" + "\"target\": \"apex\",\n" + "\"intPar\": 9080\n" + "}"; + + /** + * Reset counters. + */ + public static void resetCounters() { + postMessagesReceived = 0; + putMessagesReceived = 0; + statMessagesReceived = 0; + getMessagesReceived = 0; + deleteMessagesReceived = 0; + } + + /** + * Service get stats. + * + * @return the response + */ + @Path("/event/Stats") + @GET + public Response serviceGetStats() { + synchronized (counterLock) { + statMessagesReceived++; + } + return Response.status(200) + .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + + ",\"DELETE\": " + deleteMessagesReceived + "}") + .build(); + } + + /** + * Service get event. + * + * @return the response + */ + @Path("/event/GetEvent") + @GET + public Response serviceGetEvent() { + synchronized (counterLock) { + getMessagesReceived++; + } + + return Response.status(200).entity(EVENT_STRING).build(); + } + + /** + * Service get empty event. + * + * @return the response + */ + @Path("/event/GetEmptyEvent") + @GET + public Response serviceGetEmptyEvent() { + return Response.status(200).build(); + } + + /** + * Service get event bad response. + * + * @return the response + */ + @Path("/event/GetEventBadResponse") + @GET + public Response serviceGetEventBadResponse() { + return Response.status(400).build(); + } + + /** + * Service post request. + * + * @param jsonString the json string + * @return the response + */ + @Path("/event/PostEvent") + @POST + public Response servicePostRequest(final String jsonString) { + synchronized (counterLock) { + postMessagesReceived++; + } + + @SuppressWarnings("unchecked") + final Map jsonMap = new Gson().fromJson(jsonString, Map.class); + assertTrue(jsonMap.containsKey("name")); + assertEquals("0.0.1", jsonMap.get("version")); + assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace")); + assertEquals("Act", jsonMap.get("source")); + assertEquals("Outside", jsonMap.get("target")); + + return Response.status(200).entity(EVENT_STRING).build(); + } + + /** + * Service post request bad response. + * + * @param jsonString the json string + * @return the response + */ + @Path("/event/PostEventBadResponse") + @POST + public Response servicePostRequestBadResponse(final String jsonString) { + return Response.status(400).build(); + } + + /** + * Service put request. + * + * @param jsonString the json string + * @return the response + */ + @Path("/event/PutEvent") + @PUT + public Response servicePutRequest(final String jsonString) { + synchronized (counterLock) { + putMessagesReceived++; + } + + @SuppressWarnings("unchecked") + final Map jsonMap = new Gson().fromJson(jsonString, Map.class); + assertTrue(jsonMap.containsKey("name")); + assertEquals("0.0.1", jsonMap.get("version")); + assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace")); + assertEquals("Act", jsonMap.get("source")); + assertEquals("Outside", jsonMap.get("target")); + + return Response.status(200).entity(EVENT_STRING).build(); + } + + /** + * Service delete request. + * + * @param jsonString the json string + * @return the response + */ + @Path("/event/DeleteEvent") + @DELETE + public Response serviceDeleteRequest(final String jsonString) { + synchronized (counterLock) { + deleteMessagesReceived++; + } + + return Response.status(200).entity(EVENT_STRING).build(); + } + + /** + * Service delete request bad response. + * + * @param jsonString the json string + * @return the response + */ + @Path("/event/DeleteEventBadResponse") + @DELETE + public Response serviceDeleteRequestBadResponse(final String jsonString) { + return Response.status(400).build(); + } +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/RestRequestorTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/RestRequestorTest.java new file mode 100644 index 000000000..7abed5a41 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/RestRequestorTest.java @@ -0,0 +1,376 @@ +/*- + * ============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.apps.uservice.test.adapt.restrequestor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.gson.Gson; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.net.URI; +import java.util.Map; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.core.Response; + +import org.glassfish.grizzly.http.server.HttpServer; +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; +import org.glassfish.jersey.server.ResourceConfig; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.core.infrastructure.messaging.MessagingException; +import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.service.engine.main.ApexMain; + +/** + * The Class TestRestRequestor. + */ +public class RestRequestorTest { + private static final String BASE_URI = "http://localhost:32801/TestRESTRequestor"; + private static HttpServer server; + + private ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + + private final PrintStream stdout = System.out; + private final PrintStream stderr = System.err; + + /** + * Sets the up. + * + * @throws Exception the exception + */ + @BeforeClass + public static void setUp() throws Exception { + final ResourceConfig rc = new ResourceConfig(RestRequestorEndpointTest.class); + server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc); + + while (!server.isStarted()) { + ThreadUtilities.sleep(50); + } + } + + /** + * Tear down. + * + * @throws Exception the exception + */ + @AfterClass + public static void tearDown() throws Exception { + server.shutdownNow(); + + new File("src/test/resources/events/EventsOut.json").delete(); + new File("src/test/resources/events/EventsOutMulti0.json").delete(); + new File("src/test/resources/events/EventsOutMulti1.json").delete(); + } + + /** + * Reset counters. + */ + @Before + public void resetCounters() { + RestRequestorEndpointTest.resetCounters(); + } + + /** + * Test rest requestor get. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorGet() throws MessagingException, ApexException, IOException { + final Client client = ClientBuilder.newClient(); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FileGet.json" }; + final ApexMain apexMain = new ApexMain(args); + + Response response = null; + + // Wait for the required amount of events to be received or for 10 seconds + Double getsSoFar = 0.0; + for (int i = 0; i < 40; i++) { + ThreadUtilities.sleep(100); + + response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") + .request("application/json").get(); + + if (Response.Status.OK.getStatusCode() != response.getStatus()) { + break; + } + + final String responseString = response.readEntity(String.class); + + @SuppressWarnings("unchecked") + final Map jsonMap = new Gson().fromJson(responseString, Map.class); + getsSoFar = Double.valueOf(jsonMap.get("GET").toString()); + + if (getsSoFar >= 50.0) { + break; + } + } + + apexMain.shutdown(); + client.close(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + + assertEquals(Double.valueOf(50.0), getsSoFar); + } + + /** + * Test REST requestor put. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorPut() throws MessagingException, ApexException, IOException { + final Client client = ClientBuilder.newClient(); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FilePut.json" }; + final ApexMain apexMain = new ApexMain(args); + + // Wait for the required amount of events to be received or for 10 seconds + Double putsSoFar = 0.0; + + Response response = null; + for (int i = 0; i < 40; i++) { + ThreadUtilities.sleep(100); + + response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") + .request("application/json").get(); + + if (Response.Status.OK.getStatusCode() != response.getStatus()) { + break; + } + + final String responseString = response.readEntity(String.class); + + @SuppressWarnings("unchecked") + final Map jsonMap = new Gson().fromJson(responseString, Map.class); + putsSoFar = Double.valueOf(jsonMap.get("PUT").toString()); + + if (putsSoFar >= 50.0) { + break; + } + } + + apexMain.shutdown(); + client.close(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + assertEquals(Double.valueOf(50.0), putsSoFar); + } + + /** + * Test REST requestor post. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorPost() throws MessagingException, ApexException, IOException { + final Client client = ClientBuilder.newClient(); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FilePost.json" }; + final ApexMain apexMain = new ApexMain(args); + + // Wait for the required amount of events to be received or for 10 seconds + Double postsSoFar = 0.0; + for (int i = 0; i < 40; i++) { + ThreadUtilities.sleep(100); + + final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") + .request("application/json").get(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + final String responseString = response.readEntity(String.class); + + @SuppressWarnings("unchecked") + final Map jsonMap = new Gson().fromJson(responseString, Map.class); + postsSoFar = Double.valueOf(jsonMap.get("POST").toString()); + + if (postsSoFar >= 50.0) { + break; + } + } + + apexMain.shutdown(); + client.close(); + + assertEquals(Double.valueOf(50.0), postsSoFar); + } + + /** + * Test REST requestor delete. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorDelete() throws MessagingException, ApexException, IOException { + final Client client = ClientBuilder.newClient(); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FileDelete.json" }; + final ApexMain apexMain = new ApexMain(args); + + // Wait for the required amount of events to be received or for 10 seconds + Double deletesSoFar = 0.0; + for (int i = 0; i < 40; i++) { + ThreadUtilities.sleep(100); + + final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") + .request("application/json").get(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + final String responseString = response.readEntity(String.class); + + @SuppressWarnings("unchecked") + final Map jsonMap = new Gson().fromJson(responseString, Map.class); + deletesSoFar = Double.valueOf(jsonMap.get("DELETE").toString()); + + if (deletesSoFar >= 50.0) { + break; + } + } + + apexMain.shutdown(); + client.close(); + + assertEquals(Double.valueOf(50.0), deletesSoFar); + } + + /** + * Test REST requestor multi inputs. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException { + final Client client = ClientBuilder.newClient(); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json" }; + final ApexMain apexMain = new ApexMain(args); + + // Wait for the required amount of events to be received or for 10 seconds + Double getsSoFar = 0.0; + for (int i = 0; i < 40; i++) { + ThreadUtilities.sleep(100); + + final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") + .request("application/json").get(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + final String responseString = response.readEntity(String.class); + + @SuppressWarnings("unchecked") + final Map jsonMap = new Gson().fromJson(responseString, Map.class); + getsSoFar = Double.valueOf(jsonMap.get("GET").toString()); + + if (getsSoFar >= 8.0) { + break; + } + } + + apexMain.shutdown(); + client.close(); + + assertEquals(Double.valueOf(8.0), getsSoFar); + + ThreadUtilities.sleep(1000); + } + + /** + * Test REST requestor producer alone. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException { + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json" }; + + final ApexMain apexMain = new ApexMain(args); + ThreadUtilities.sleep(200); + apexMain.shutdown(); + + final String outString = outContent.toString(); + + System.setOut(stdout); + System.setErr(stderr); + + assertTrue(outString.contains("REST Requestor producer (RestRequestorProducer) " + + "must run in peered requestor mode with a REST Requestor consumer")); + } + + /** + * Test REST requestor consumer alone. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException { + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json" }; + + final ApexMain apexMain = new ApexMain(args); + ThreadUtilities.sleep(200); + apexMain.shutdown(); + + final String outString = outContent.toString(); + + System.setOut(stdout); + System.setErr(stderr); + + assertTrue(outString.contains("peer \"RestRequestorProducer for peered mode REQUESTOR " + + "does not exist or is not defined with the same peered mode")); + } +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/TestRestRequestor.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/TestRestRequestor.java deleted file mode 100644 index 1ace80906..000000000 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/TestRestRequestor.java +++ /dev/null @@ -1,376 +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.apps.uservice.test.adapt.restrequestor; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import com.google.gson.Gson; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; -import java.net.URI; -import java.util.Map; - -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.core.Response; - -import org.glassfish.grizzly.http.server.HttpServer; -import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; -import org.glassfish.jersey.server.ResourceConfig; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.apex.core.infrastructure.messaging.MessagingException; -import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; -import org.onap.policy.apex.service.engine.main.ApexMain; - -/** - * The Class TestRestRequestor. - */ -public class TestRestRequestor { - private static final String BASE_URI = "http://localhost:32801/TestRESTRequestor"; - private static HttpServer server; - - private ByteArrayOutputStream outContent = new ByteArrayOutputStream(); - private ByteArrayOutputStream errContent = new ByteArrayOutputStream(); - - private final PrintStream stdout = System.out; - private final PrintStream stderr = System.err; - - /** - * Sets the up. - * - * @throws Exception the exception - */ - @BeforeClass - public static void setUp() throws Exception { - final ResourceConfig rc = new ResourceConfig(TestRestRequestorEndpoint.class); - server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc); - - while (!server.isStarted()) { - ThreadUtilities.sleep(50); - } - } - - /** - * Tear down. - * - * @throws Exception the exception - */ - @AfterClass - public static void tearDown() throws Exception { - server.shutdownNow(); - - new File("src/test/resources/events/EventsOut.json").delete(); - new File("src/test/resources/events/EventsOutMulti0.json").delete(); - new File("src/test/resources/events/EventsOutMulti1.json").delete(); - } - - /** - * Reset counters. - */ - @Before - public void resetCounters() { - TestRestRequestorEndpoint.resetCounters(); - } - - /** - * Test rest requestor get. - * - * @throws MessagingException the messaging exception - * @throws ApexException the apex exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testRestRequestorGet() throws MessagingException, ApexException, IOException { - final Client client = ClientBuilder.newClient(); - - final String[] args = - { "src/test/resources/prodcons/File2RESTRequest2FileGet.json" }; - final ApexMain apexMain = new ApexMain(args); - - Response response = null; - - // Wait for the required amount of events to be received or for 10 seconds - Double getsSoFar = 0.0; - for (int i = 0; i < 40; i++) { - ThreadUtilities.sleep(100); - - response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") - .request("application/json").get(); - - if (Response.Status.OK.getStatusCode() != response.getStatus()) { - break; - } - - final String responseString = response.readEntity(String.class); - - @SuppressWarnings("unchecked") - final Map jsonMap = new Gson().fromJson(responseString, Map.class); - getsSoFar = Double.valueOf(jsonMap.get("GET").toString()); - - if (getsSoFar >= 50.0) { - break; - } - } - - apexMain.shutdown(); - client.close(); - - assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); - - assertEquals(Double.valueOf(50.0), getsSoFar); - } - - /** - * Test REST requestor put. - * - * @throws MessagingException the messaging exception - * @throws ApexException the apex exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testRestRequestorPut() throws MessagingException, ApexException, IOException { - final Client client = ClientBuilder.newClient(); - - final String[] args = - { "src/test/resources/prodcons/File2RESTRequest2FilePut.json" }; - final ApexMain apexMain = new ApexMain(args); - - // Wait for the required amount of events to be received or for 10 seconds - Double putsSoFar = 0.0; - - Response response = null; - for (int i = 0; i < 40; i++) { - ThreadUtilities.sleep(100); - - response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") - .request("application/json").get(); - - if (Response.Status.OK.getStatusCode() != response.getStatus()) { - break; - } - - final String responseString = response.readEntity(String.class); - - @SuppressWarnings("unchecked") - final Map jsonMap = new Gson().fromJson(responseString, Map.class); - putsSoFar = Double.valueOf(jsonMap.get("PUT").toString()); - - if (putsSoFar >= 50.0) { - break; - } - } - - apexMain.shutdown(); - client.close(); - - assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); - assertEquals(Double.valueOf(50.0), putsSoFar); - } - - /** - * Test REST requestor post. - * - * @throws MessagingException the messaging exception - * @throws ApexException the apex exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testRestRequestorPost() throws MessagingException, ApexException, IOException { - final Client client = ClientBuilder.newClient(); - - final String[] args = - { "src/test/resources/prodcons/File2RESTRequest2FilePost.json" }; - final ApexMain apexMain = new ApexMain(args); - - // Wait for the required amount of events to be received or for 10 seconds - Double postsSoFar = 0.0; - for (int i = 0; i < 40; i++) { - ThreadUtilities.sleep(100); - - final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") - .request("application/json").get(); - - assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); - final String responseString = response.readEntity(String.class); - - @SuppressWarnings("unchecked") - final Map jsonMap = new Gson().fromJson(responseString, Map.class); - postsSoFar = Double.valueOf(jsonMap.get("POST").toString()); - - if (postsSoFar >= 50.0) { - break; - } - } - - apexMain.shutdown(); - client.close(); - - assertEquals(Double.valueOf(50.0), postsSoFar); - } - - /** - * Test REST requestor delete. - * - * @throws MessagingException the messaging exception - * @throws ApexException the apex exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testRestRequestorDelete() throws MessagingException, ApexException, IOException { - final Client client = ClientBuilder.newClient(); - - final String[] args = - { "src/test/resources/prodcons/File2RESTRequest2FileDelete.json" }; - final ApexMain apexMain = new ApexMain(args); - - // Wait for the required amount of events to be received or for 10 seconds - Double deletesSoFar = 0.0; - for (int i = 0; i < 40; i++) { - ThreadUtilities.sleep(100); - - final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") - .request("application/json").get(); - - assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); - final String responseString = response.readEntity(String.class); - - @SuppressWarnings("unchecked") - final Map jsonMap = new Gson().fromJson(responseString, Map.class); - deletesSoFar = Double.valueOf(jsonMap.get("DELETE").toString()); - - if (deletesSoFar >= 50.0) { - break; - } - } - - apexMain.shutdown(); - client.close(); - - assertEquals(Double.valueOf(50.0), deletesSoFar); - } - - /** - * Test REST requestor multi inputs. - * - * @throws MessagingException the messaging exception - * @throws ApexException the apex exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException { - final Client client = ClientBuilder.newClient(); - - final String[] args = - { "src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json" }; - final ApexMain apexMain = new ApexMain(args); - - // Wait for the required amount of events to be received or for 10 seconds - Double getsSoFar = 0.0; - for (int i = 0; i < 40; i++) { - ThreadUtilities.sleep(100); - - final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") - .request("application/json").get(); - - assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); - final String responseString = response.readEntity(String.class); - - @SuppressWarnings("unchecked") - final Map jsonMap = new Gson().fromJson(responseString, Map.class); - getsSoFar = Double.valueOf(jsonMap.get("GET").toString()); - - if (getsSoFar >= 8.0) { - break; - } - } - - apexMain.shutdown(); - client.close(); - - assertEquals(Double.valueOf(8.0), getsSoFar); - - ThreadUtilities.sleep(1000); - } - - /** - * Test REST requestor producer alone. - * - * @throws MessagingException the messaging exception - * @throws ApexException the apex exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException { - System.setOut(new PrintStream(outContent)); - System.setErr(new PrintStream(errContent)); - - final String[] args = - { "src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json" }; - - final ApexMain apexMain = new ApexMain(args); - ThreadUtilities.sleep(200); - apexMain.shutdown(); - - final String outString = outContent.toString(); - - System.setOut(stdout); - System.setErr(stderr); - - assertTrue(outString.contains("REST Requestor producer (RestRequestorProducer) " - + "must run in peered requestor mode with a REST Requestor consumer")); - } - - /** - * Test REST requestor consumer alone. - * - * @throws MessagingException the messaging exception - * @throws ApexException the apex exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException { - System.setOut(new PrintStream(outContent)); - System.setErr(new PrintStream(errContent)); - - final String[] args = - { "src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json" }; - - final ApexMain apexMain = new ApexMain(args); - ThreadUtilities.sleep(200); - apexMain.shutdown(); - - final String outString = outContent.toString(); - - System.setOut(stdout); - System.setErr(stderr); - - assertTrue(outString.contains("peer \"RestRequestorProducer for peered mode REQUESTOR " - + "does not exist or is not defined with the same peered mode")); - } -} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/TestRestRequestorEndpoint.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/TestRestRequestorEndpoint.java deleted file mode 100644 index 860127a7b..000000000 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/apps/uservice/test/adapt/restrequestor/TestRestRequestorEndpoint.java +++ /dev/null @@ -1,207 +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.apps.uservice.test.adapt.restrequestor; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import com.google.gson.Gson; - -import java.util.Map; - -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.core.Response; - -/** - * The Class TestRestRequestorEndpoint. - */ -@Path("/apex") -public class TestRestRequestorEndpoint { - - private static Object counterLock = new Object(); - private static int postMessagesReceived = 0; - private static int putMessagesReceived = 0; - private static int statMessagesReceived = 0; - private static int getMessagesReceived = 0; - private static int deleteMessagesReceived = 0; - - private static String EVENT_STRING = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.events\",\n" - + "\"name\": \"ResponseEvent\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" - + getMessagesReceived + "\",\n" + "\"target\": \"apex\",\n" + "\"intPar\": 9080\n" + "}"; - - /** - * Reset counters. - */ - public static void resetCounters() { - postMessagesReceived = 0; - putMessagesReceived = 0; - statMessagesReceived = 0; - getMessagesReceived = 0; - deleteMessagesReceived = 0; - } - - /** - * Service get stats. - * - * @return the response - */ - @Path("/event/Stats") - @GET - public Response serviceGetStats() { - synchronized (counterLock) { - statMessagesReceived++; - } - return Response.status(200) - .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived - + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived - + ",\"DELETE\": " + deleteMessagesReceived + "}") - .build(); - } - - /** - * Service get event. - * - * @return the response - */ - @Path("/event/GetEvent") - @GET - public Response serviceGetEvent() { - synchronized (counterLock) { - getMessagesReceived++; - } - - return Response.status(200).entity(EVENT_STRING).build(); - } - - /** - * Service get empty event. - * - * @return the response - */ - @Path("/event/GetEmptyEvent") - @GET - public Response serviceGetEmptyEvent() { - return Response.status(200).build(); - } - - /** - * Service get event bad response. - * - * @return the response - */ - @Path("/event/GetEventBadResponse") - @GET - public Response serviceGetEventBadResponse() { - return Response.status(400).build(); - } - - /** - * Service post request. - * - * @param jsonString the json string - * @return the response - */ - @Path("/event/PostEvent") - @POST - public Response servicePostRequest(final String jsonString) { - synchronized (counterLock) { - postMessagesReceived++; - } - - @SuppressWarnings("unchecked") - final Map jsonMap = new Gson().fromJson(jsonString, Map.class); - assertTrue(jsonMap.containsKey("name")); - assertEquals("0.0.1", jsonMap.get("version")); - assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace")); - assertEquals("Act", jsonMap.get("source")); - assertEquals("Outside", jsonMap.get("target")); - - return Response.status(200).entity(EVENT_STRING).build(); - } - - /** - * Service post request bad response. - * - * @param jsonString the json string - * @return the response - */ - @Path("/event/PostEventBadResponse") - @POST - public Response servicePostRequestBadResponse(final String jsonString) { - return Response.status(400).build(); - } - - /** - * Service put request. - * - * @param jsonString the json string - * @return the response - */ - @Path("/event/PutEvent") - @PUT - public Response servicePutRequest(final String jsonString) { - synchronized (counterLock) { - putMessagesReceived++; - } - - @SuppressWarnings("unchecked") - final Map jsonMap = new Gson().fromJson(jsonString, Map.class); - assertTrue(jsonMap.containsKey("name")); - assertEquals("0.0.1", jsonMap.get("version")); - assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace")); - assertEquals("Act", jsonMap.get("source")); - assertEquals("Outside", jsonMap.get("target")); - - return Response.status(200).entity(EVENT_STRING).build(); - } - - /** - * Service delete request. - * - * @param jsonString the json string - * @return the response - */ - @Path("/event/DeleteEvent") - @DELETE - public Response serviceDeleteRequest(final String jsonString) { - synchronized (counterLock) { - deleteMessagesReceived++; - } - - return Response.status(200).entity(EVENT_STRING).build(); - } - - /** - * Service delete request bad response. - * - * @param jsonString the json string - * @return the response - */ - @Path("/event/DeleteEventBadResponse") - @DELETE - public Response serviceDeleteRequestBadResponse(final String jsonString) { - return Response.status(400).build(); - } -} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlEventHandler.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlEventHandler.java deleted file mode 100644 index 1e42bef30..000000000 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlEventHandler.java +++ /dev/null @@ -1,147 +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.event.protocol.xml; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; -import org.onap.policy.apex.service.engine.event.ApexEvent; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * The Class TestApexXMLEventHandlerURL. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestXmlEventHandler { - private static final XLogger logger = XLoggerFactory.getXLogger(TestXmlEventHandler.class); - - /** - * Test XML to apex event. - * - * @throws ApexException on Apex event handling errors - */ - @Test - public void testXmltoApexEvent() throws ApexException { - try { - final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter(); - assertNotNull(xmlEventConverter); - - final String apexEventXmlStringIn = XmlEventGenerator.xmlEvent(); - - logger.debug("input event\n" + apexEventXmlStringIn); - - for (final ApexEvent apexEvent : xmlEventConverter.toApexEvent("XMLEventName", apexEventXmlStringIn)) { - assertNotNull(apexEvent); - - logger.debug(apexEvent.toString()); - - assertTrue(apexEvent.getName().equals("Event0000") || apexEvent.getName().equals("Event0100")); - assertTrue(apexEvent.getVersion().equals("0.0.1")); - assertTrue(apexEvent.getNameSpace().equals("org.onap.policy.apex.sample.events")); - assertTrue(apexEvent.getSource().equals("test")); - assertTrue(apexEvent.getTarget().equals("apex")); - assertTrue(apexEvent.get("TestSlogan").toString().startsWith("Test slogan for External Event")); - - final Object testMatchCaseSelected = apexEvent.get("TestMatchCaseSelected"); - assertTrue(testMatchCaseSelected == null); - } - } catch (final Exception e) { - e.printStackTrace(); - throw new ApexException("Exception reading Apex event xml file", e); - } - } - - /** - * Test apex event to xml. - * - * @throws ApexException on Apex event handling errors - */ - @Test - public void testApexEventToXml() throws ApexException { - try { - final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter(); - assertNotNull(xmlEventConverter); - - final Date event0000StartTime = new Date(); - final Map event0000DataMap = new HashMap(); - event0000DataMap.put("TestSlogan", "This is a test slogan"); - event0000DataMap.put("TestMatchCase", 12345); - event0000DataMap.put("TestTimestamp", event0000StartTime.getTime()); - event0000DataMap.put("TestTemperature", 34.5445667); - - final ApexEvent apexEvent0000 = - new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.sample.events", "test", "apex"); - apexEvent0000.putAll(event0000DataMap); - - final String apexEvent0000XmlString = xmlEventConverter.fromApexEvent(apexEvent0000); - - logger.debug(apexEvent0000XmlString); - - assertTrue(apexEvent0000XmlString.contains("Event0000")); - assertTrue(apexEvent0000XmlString.contains("0.0.1")); - assertTrue(apexEvent0000XmlString.contains("This is a test slogan")); - assertTrue(apexEvent0000XmlString.contains("12345")); - assertTrue(apexEvent0000XmlString.contains("" + event0000StartTime.getTime() + "")); - assertTrue(apexEvent0000XmlString.contains("34.5445667")); - - final Date event0004StartTime = new Date(1434363272000L); - final Map event0004DataMap = new HashMap(); - event0004DataMap.put("TestSlogan", "Test slogan for External Event"); - event0004DataMap.put("TestMatchCase", new Integer(2)); - event0004DataMap.put("TestTimestamp", new Long(event0004StartTime.getTime())); - event0004DataMap.put("TestTemperature", new Double(1064.43)); - event0004DataMap.put("TestMatchCaseSelected", new Integer(2)); - event0004DataMap.put("TestMatchStateTime", new Long(1434370506078L)); - event0004DataMap.put("TestEstablishCaseSelected", new Integer(0)); - event0004DataMap.put("TestEstablishStateTime", new Long(1434370506085L)); - event0004DataMap.put("TestDecideCaseSelected", new Integer(3)); - event0004DataMap.put("TestDecideStateTime", new Long(1434370506092L)); - event0004DataMap.put("TestActCaseSelected", new Integer(2)); - event0004DataMap.put("TestActStateTime", new Long(1434370506095L)); - - final ApexEvent apexEvent0004 = - new ApexEvent("Event0004", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex"); - apexEvent0004.putAll(event0004DataMap); - - final String apexEvent0004XmlString = xmlEventConverter.fromApexEvent(apexEvent0004); - - logger.debug(apexEvent0004XmlString); - - assertTrue(apexEvent0004XmlString.contains("Event0004")); - assertTrue(apexEvent0004XmlString.contains("0.0.1")); - assertTrue(apexEvent0004XmlString.contains("Test slogan for External Event")); - assertTrue(apexEvent0004XmlString.contains("1434370506078")); - assertTrue(apexEvent0004XmlString.contains("" + event0004StartTime.getTime() + "")); - assertTrue(apexEvent0004XmlString.contains("1064.43")); - } catch (final Exception e) { - e.printStackTrace(); - throw new ApexException("Exception reading Apex event xml file", e); - } - } -} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlTaggedEventConsumer.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlTaggedEventConsumer.java deleted file mode 100644 index 475373a7c..000000000 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/TestXmlTaggedEventConsumer.java +++ /dev/null @@ -1,446 +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.event.protocol.xml; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; - -import org.junit.Test; -import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.HeaderDelimitedTextBlockReader; -import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; - -/** - * The Class TestXmlTaggedEventConsumer. - */ -public class TestXmlTaggedEventConsumer { - - /** - * Test garbage text line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testGarbageTextLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream("hello there".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268", textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full event garbage before line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventGarbageBeforeLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "Garbage1469781869268".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish" - .getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test garbage text multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testGarbageTextMultiLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream("hello\nthere".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\n\n".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full event garbage before multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventGarbageBeforeMultiLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "Garbage\n\n\n1469781869268\n\n\n" - .getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full event garbage before after multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventGarbageBeforeAfterMultiLine() throws IOException { - String garbageString = "Garbage\n\n\n1469781869268" - + "\n\nRubbish\n\n"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full event garbage after multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventGarbageAfterMultiLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "\n\n1469781869268\n\nRubbish" - .getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test partial events line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testPartialEventsLine() throws IOException { - String garbageString = "1469781869268" - + "1469781869268"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" - + ""; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" - + "Rubbish\nRefuse"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" - + "RubbishRefuse"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n\n\n".getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n", textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full events multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventsMultiLine() throws IOException { - String garbageString = "\n\n1469781869268\n" - + "\n\n\n1469781869268\n\n"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", - textBlock.getText()); - assertFalse(textBlock.isEndOfText()); - - textBlock = xmlTaggedReader.readTextBlock(); - assertEquals("\n\n1469781869268\n", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full events garbage before multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventsGarbageBeforeMultiLine() throws IOException { - String garbageString = "Garbage\n\n\n1469781869268\n" - + "\n\n\n\n1469781869268\n\n"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", - textBlock.getText()); - assertFalse(textBlock.isEndOfText()); - - textBlock = xmlTaggedReader.readTextBlock(); - assertEquals("\n\n1469781869268\n", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full events garbage before after multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventsGarbageBeforeAfterMultiLine() throws IOException { - String garbageString = "Garbage\n\n\n1469781869268\n" - + "\nRubbish\n\n\n1469781869268\n" - + "\nRefuse\n"; - final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", - textBlock.getText()); - assertFalse(textBlock.isEndOfText()); - - textBlock = xmlTaggedReader.readTextBlock(); - assertEquals("\n\n1469781869268\n\nRefuse", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } - - /** - * Test full events garbage after multi line. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testFullEventsGarbageAfterMultiLine() throws IOException { - final InputStream xmlInputStream = new ByteArrayInputStream( - "\n\n1469781869268\n\nRubbish" - .getBytes()); - - final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", - textBlock.getText()); - assertTrue(textBlock.isEndOfText()); - } -} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventHandlerTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventHandlerTest.java new file mode 100644 index 000000000..685a1f236 --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventHandlerTest.java @@ -0,0 +1,147 @@ +/*- + * ============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.event.protocol.xml; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class TestApexXMLEventHandlerURL. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class XmlEventHandlerTest { + private static final XLogger logger = XLoggerFactory.getXLogger(XmlEventHandlerTest.class); + + /** + * Test XML to apex event. + * + * @throws ApexException on Apex event handling errors + */ + @Test + public void testXmltoApexEvent() throws ApexException { + try { + final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter(); + assertNotNull(xmlEventConverter); + + final String apexEventXmlStringIn = XmlEventGenerator.xmlEvent(); + + logger.debug("input event\n" + apexEventXmlStringIn); + + for (final ApexEvent apexEvent : xmlEventConverter.toApexEvent("XMLEventName", apexEventXmlStringIn)) { + assertNotNull(apexEvent); + + logger.debug(apexEvent.toString()); + + assertTrue(apexEvent.getName().equals("Event0000") || apexEvent.getName().equals("Event0100")); + assertTrue(apexEvent.getVersion().equals("0.0.1")); + assertTrue(apexEvent.getNameSpace().equals("org.onap.policy.apex.sample.events")); + assertTrue(apexEvent.getSource().equals("test")); + assertTrue(apexEvent.getTarget().equals("apex")); + assertTrue(apexEvent.get("TestSlogan").toString().startsWith("Test slogan for External Event")); + + final Object testMatchCaseSelected = apexEvent.get("TestMatchCaseSelected"); + assertTrue(testMatchCaseSelected == null); + } + } catch (final Exception e) { + e.printStackTrace(); + throw new ApexException("Exception reading Apex event xml file", e); + } + } + + /** + * Test apex event to xml. + * + * @throws ApexException on Apex event handling errors + */ + @Test + public void testApexEventToXml() throws ApexException { + try { + final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter(); + assertNotNull(xmlEventConverter); + + final Date event0000StartTime = new Date(); + final Map event0000DataMap = new HashMap(); + event0000DataMap.put("TestSlogan", "This is a test slogan"); + event0000DataMap.put("TestMatchCase", 12345); + event0000DataMap.put("TestTimestamp", event0000StartTime.getTime()); + event0000DataMap.put("TestTemperature", 34.5445667); + + final ApexEvent apexEvent0000 = + new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.sample.events", "test", "apex"); + apexEvent0000.putAll(event0000DataMap); + + final String apexEvent0000XmlString = xmlEventConverter.fromApexEvent(apexEvent0000); + + logger.debug(apexEvent0000XmlString); + + assertTrue(apexEvent0000XmlString.contains("Event0000")); + assertTrue(apexEvent0000XmlString.contains("0.0.1")); + assertTrue(apexEvent0000XmlString.contains("This is a test slogan")); + assertTrue(apexEvent0000XmlString.contains("12345")); + assertTrue(apexEvent0000XmlString.contains("" + event0000StartTime.getTime() + "")); + assertTrue(apexEvent0000XmlString.contains("34.5445667")); + + final Date event0004StartTime = new Date(1434363272000L); + final Map event0004DataMap = new HashMap(); + event0004DataMap.put("TestSlogan", "Test slogan for External Event"); + event0004DataMap.put("TestMatchCase", new Integer(2)); + event0004DataMap.put("TestTimestamp", new Long(event0004StartTime.getTime())); + event0004DataMap.put("TestTemperature", new Double(1064.43)); + event0004DataMap.put("TestMatchCaseSelected", new Integer(2)); + event0004DataMap.put("TestMatchStateTime", new Long(1434370506078L)); + event0004DataMap.put("TestEstablishCaseSelected", new Integer(0)); + event0004DataMap.put("TestEstablishStateTime", new Long(1434370506085L)); + event0004DataMap.put("TestDecideCaseSelected", new Integer(3)); + event0004DataMap.put("TestDecideStateTime", new Long(1434370506092L)); + event0004DataMap.put("TestActCaseSelected", new Integer(2)); + event0004DataMap.put("TestActStateTime", new Long(1434370506095L)); + + final ApexEvent apexEvent0004 = + new ApexEvent("Event0004", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex"); + apexEvent0004.putAll(event0004DataMap); + + final String apexEvent0004XmlString = xmlEventConverter.fromApexEvent(apexEvent0004); + + logger.debug(apexEvent0004XmlString); + + assertTrue(apexEvent0004XmlString.contains("Event0004")); + assertTrue(apexEvent0004XmlString.contains("0.0.1")); + assertTrue(apexEvent0004XmlString.contains("Test slogan for External Event")); + assertTrue(apexEvent0004XmlString.contains("1434370506078")); + assertTrue(apexEvent0004XmlString.contains("" + event0004StartTime.getTime() + "")); + assertTrue(apexEvent0004XmlString.contains("1064.43")); + } catch (final Exception e) { + e.printStackTrace(); + throw new ApexException("Exception reading Apex event xml file", e); + } + } +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlTaggedEventConsumerTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlTaggedEventConsumerTest.java new file mode 100644 index 000000000..aeb37331a --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlTaggedEventConsumerTest.java @@ -0,0 +1,446 @@ +/*- + * ============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.event.protocol.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.HeaderDelimitedTextBlockReader; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; + +/** + * The Class TestXmlTaggedEventConsumer. + */ +public class XmlTaggedEventConsumerTest { + + /** + * Test garbage text line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testGarbageTextLine() throws IOException { + final InputStream xmlInputStream = new ByteArrayInputStream("hello there".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268", textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full event garbage before line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventGarbageBeforeLine() throws IOException { + final InputStream xmlInputStream = new ByteArrayInputStream( + "Garbage1469781869268".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish" + .getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268Rubbish", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test garbage text multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testGarbageTextMultiLine() throws IOException { + final InputStream xmlInputStream = new ByteArrayInputStream("hello\nthere".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\n\n".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full event garbage before multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventGarbageBeforeMultiLine() throws IOException { + final InputStream xmlInputStream = new ByteArrayInputStream( + "Garbage\n\n\n1469781869268\n\n\n" + .getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full event garbage before after multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventGarbageBeforeAfterMultiLine() throws IOException { + String garbageString = "Garbage\n\n\n1469781869268" + + "\n\nRubbish\n\n"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full event garbage after multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventGarbageAfterMultiLine() throws IOException { + final InputStream xmlInputStream = new ByteArrayInputStream( + "\n\n1469781869268\n\nRubbish" + .getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test partial events line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testPartialEventsLine() throws IOException { + String garbageString = "1469781869268" + + "1469781869268"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" + + ""; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" + + "Rubbish\nRefuse"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("1469781869268" + + "RubbishRefuse"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n\n\n".getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n", textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full events multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventsMultiLine() throws IOException { + String garbageString = "\n\n1469781869268\n" + + "\n\n\n1469781869268\n\n"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", + textBlock.getText()); + assertFalse(textBlock.isEndOfText()); + + textBlock = xmlTaggedReader.readTextBlock(); + assertEquals("\n\n1469781869268\n", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full events garbage before multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventsGarbageBeforeMultiLine() throws IOException { + String garbageString = "Garbage\n\n\n1469781869268\n" + + "\n\n\n\n1469781869268\n\n"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n", + textBlock.getText()); + assertFalse(textBlock.isEndOfText()); + + textBlock = xmlTaggedReader.readTextBlock(); + assertEquals("\n\n1469781869268\n", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full events garbage before after multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventsGarbageBeforeAfterMultiLine() throws IOException { + String garbageString = "Garbage\n\n\n1469781869268\n" + + "\nRubbish\n\n\n1469781869268\n" + + "\nRefuse\n"; + final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", + textBlock.getText()); + assertFalse(textBlock.isEndOfText()); + + textBlock = xmlTaggedReader.readTextBlock(); + assertEquals("\n\n1469781869268\n\nRefuse", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + /** + * Test full events garbage after multi line. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testFullEventsGarbageAfterMultiLine() throws IOException { + final InputStream xmlInputStream = new ByteArrayInputStream( + "\n\n1469781869268\n\nRubbish" + .getBytes()); + + final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("\n\n1469781869268\n\nRubbish", + textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlEventProtocol.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlEventProtocol.java deleted file mode 100644 index 1d2a1d9f7..000000000 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlEventProtocol.java +++ /dev/null @@ -1,287 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 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.event.protocol.yaml; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; -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.AxReferenceKey; -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.eventmodel.concepts.AxEvent; -import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; -import org.onap.policy.apex.model.eventmodel.concepts.AxField; -import org.onap.policy.apex.model.utilities.TextFileUtils; -import org.onap.policy.apex.service.engine.event.ApexEvent; -import org.onap.policy.apex.service.engine.event.ApexEventException; -import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.HeaderDelimitedTextBlockReader; -import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; -import org.onap.policy.common.parameters.ParameterService; - -/** - * The Class TestYamlEventProtocol. - */ -public class TestYamlEventProtocol { - - /** - * Register test events and schemas. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @BeforeClass - public static void registerTestEventsAndSchemas() throws IOException { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); - ParameterService.register(schemaParameters, true); - - AxContextSchemas schemas = new AxContextSchemas(); - - AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", - "java.lang.Integer"); - schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema); - - AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), - "JAVA", "java.lang.Double"); - schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema); - - AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), - "JAVA", "java.lang.String"); - schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema); - - AxContextSchema arrayListSchema = new AxContextSchema(new AxArtifactKey("ArrayListSchema", "0.0.1"), "JAVA", - "java.util.ArrayList"); - schemas.getSchemasMap().put(arrayListSchema.getKey(), arrayListSchema); - - AxContextSchema linkedHashMapSchema = new AxContextSchema(new AxArtifactKey("LinkedHashMapSchema", "0.0.1"), - "JAVA", "java.util.LinkedHashMap"); - schemas.getSchemasMap().put(linkedHashMapSchema.getKey(), linkedHashMapSchema); - - ModelService.registerModel(AxContextSchemas.class, schemas); - - AxEvents events = new AxEvents(); - - AxEvent testEvent0 = new AxEvent(new AxArtifactKey("TestEvent0", "0.0.1")); - testEvent0.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - events.getEventMap().put(testEvent0.getKey(), testEvent0); - - AxEvent testEvent1 = new AxEvent(new AxArtifactKey("TestEvent1", "0.0.1")); - testEvent1.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te1Field0 = new AxField(new AxReferenceKey(testEvent1.getKey(), "yaml_field"), - arrayListSchema.getKey()); - testEvent1.getParameterMap().put("yaml_field", te1Field0); - events.getEventMap().put(testEvent1.getKey(), testEvent1); - - AxEvent testEvent2 = new AxEvent(new AxArtifactKey("TestEvent2", "0.0.1")); - testEvent2.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te2Field0 = new AxField(new AxReferenceKey(testEvent2.getKey(), "hr"), simpleIntSchema.getKey()); - testEvent2.getParameterMap().put("hr", te2Field0); - AxField te2Field1 = new AxField(new AxReferenceKey(testEvent2.getKey(), "avg"), simpleDoubleSchema.getKey()); - testEvent2.getParameterMap().put("avg", te2Field1); - AxField te2Field2 = new AxField(new AxReferenceKey(testEvent2.getKey(), "rbi"), simpleIntSchema.getKey()); - testEvent2.getParameterMap().put("rbi", te2Field2); - events.getEventMap().put(testEvent2.getKey(), testEvent2); - - AxEvent testEvent3 = new AxEvent(new AxArtifactKey("TestEvent3", "0.0.1")); - testEvent3.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te3Field0 = new AxField(new AxReferenceKey(testEvent3.getKey(), "american"), arrayListSchema.getKey()); - testEvent3.getParameterMap().put("american", te3Field0); - AxField te3Field1 = new AxField(new AxReferenceKey(testEvent3.getKey(), "national"), arrayListSchema.getKey()); - testEvent3.getParameterMap().put("national", te3Field1); - events.getEventMap().put(testEvent3.getKey(), testEvent3); - - AxEvent testEvent4 = new AxEvent(new AxArtifactKey("TestEvent4", "0.0.1")); - testEvent4.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te4Field0 = new AxField(new AxReferenceKey(testEvent4.getKey(), "yaml_field"), - arrayListSchema.getKey()); - testEvent4.getParameterMap().put("yaml_field", te4Field0); - events.getEventMap().put(testEvent4.getKey(), testEvent4); - - AxEvent testEvent5 = new AxEvent(new AxArtifactKey("TestEvent5", "0.0.1")); - testEvent5.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te5Field0 = new AxField(new AxReferenceKey(testEvent5.getKey(), "yaml_field"), - arrayListSchema.getKey()); - testEvent5.getParameterMap().put("yaml_field", te5Field0); - events.getEventMap().put(testEvent5.getKey(), testEvent5); - - AxEvent testEvent6 = new AxEvent(new AxArtifactKey("TestEvent6", "0.0.1")); - testEvent6.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te6Field0 = new AxField(new AxReferenceKey(testEvent6.getKey(), "MarkMcGwire"), - linkedHashMapSchema.getKey()); - testEvent6.getParameterMap().put("Mark McGwire", te6Field0); - AxField te6Field1 = new AxField(new AxReferenceKey(testEvent6.getKey(), "SammySosa"), - linkedHashMapSchema.getKey()); - testEvent6.getParameterMap().put("Sammy Sosa", te6Field1); - events.getEventMap().put(testEvent6.getKey(), testEvent6); - - AxEvent testEvent7 = new AxEvent(new AxArtifactKey("TestEvent7", "0.0.1")); - testEvent7.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te7Field0 = new AxField(new AxReferenceKey(testEvent7.getKey(), "time"), simpleIntSchema.getKey()); - testEvent7.getParameterMap().put("time", te7Field0); - AxField te7Field1 = new AxField(new AxReferenceKey(testEvent7.getKey(), "player"), simpleStringSchema.getKey()); - testEvent7.getParameterMap().put("player", te7Field1); - AxField te7Field2 = new AxField(new AxReferenceKey(testEvent7.getKey(), "action"), simpleStringSchema.getKey()); - testEvent7.getParameterMap().put("action", te7Field2); - events.getEventMap().put(testEvent7.getKey(), testEvent7); - - AxEvent testEvent8 = new AxEvent(new AxArtifactKey("TestEvent8", "0.0.1")); - testEvent8.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te8Field0 = new AxField(new AxReferenceKey(testEvent8.getKey(), "hr"), arrayListSchema.getKey()); - testEvent8.getParameterMap().put("hr", te8Field0); - AxField te8Field1 = new AxField(new AxReferenceKey(testEvent8.getKey(), "rbi"), arrayListSchema.getKey()); - testEvent8.getParameterMap().put("rbi", te8Field1); - events.getEventMap().put(testEvent8.getKey(), testEvent8); - - AxEvent testEvent9 = new AxEvent(new AxArtifactKey("TestEvent9", "0.0.1")); - testEvent9.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te9Field0 = new AxField(new AxReferenceKey(testEvent9.getKey(), "ChicagoCubs"), - arrayListSchema.getKey()); - testEvent9.getParameterMap().put("ChicagoCubs", te9Field0); - AxField te9Field1 = new AxField(new AxReferenceKey(testEvent9.getKey(), "AtlantaBraves"), - arrayListSchema.getKey()); - testEvent9.getParameterMap().put("AtlantaBraves", te9Field1); - events.getEventMap().put(testEvent9.getKey(), testEvent9); - - AxEvent testEvent10 = new AxEvent(new AxArtifactKey("TestEvent10", "0.0.1")); - testEvent10.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te10Field0 = new AxField(new AxReferenceKey(testEvent10.getKey(), "yaml_field"), - arrayListSchema.getKey()); - testEvent10.getParameterMap().put("yaml_field", te10Field0); - events.getEventMap().put(testEvent10.getKey(), testEvent10); - - AxEvent testEvent11 = new AxEvent(new AxArtifactKey("TestEvent11", "0.0.1")); - testEvent11.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te11Field0 = new AxField(new AxReferenceKey(testEvent11.getKey(), "tosca_definitions_version"), - simpleStringSchema.getKey()); - testEvent11.getParameterMap().put("tosca_definitions_version", te11Field0); - AxField te11Field1 = new AxField(new AxReferenceKey(testEvent11.getKey(), "description"), - simpleStringSchema.getKey(), true); - testEvent11.getParameterMap().put("description", te11Field1); - AxField te11Field2 = new AxField(new AxReferenceKey(testEvent11.getKey(), "node_types"), - linkedHashMapSchema.getKey(), true); - testEvent11.getParameterMap().put("node_types", te11Field2); - AxField te11Field3 = new AxField(new AxReferenceKey(testEvent11.getKey(), "topology_template"), - linkedHashMapSchema.getKey()); - testEvent11.getParameterMap().put("topology_template", te11Field3); - events.getEventMap().put(testEvent11.getKey(), testEvent11); - - ModelService.registerModel(AxEvents.class, events); - } - - /** - * Unregister test events and schemas. - */ - @AfterClass - public static void unregisterTestEventsAndSchemas() { - ModelService.clear(); - ParameterService.clear(); - } - - /** - * Test yaml processing. - * - * @throws ApexEventException the apex event exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testYamlProcessing() throws ApexEventException, IOException { - try { - testYamlDecodeEncode("TestEvent0", 1, 0, "Empty0"); - fail("test should fail here"); - } catch (ApexEventException e) { - assertEquals("event processing failed, event is null", e.getMessage()); - } - - testYamlDecodeEncode("TestEvent0", 1, 0, "Empty1"); - testYamlDecodeEncode("TestEvent1", 1, 1, "Collection0"); - testYamlDecodeEncode("TestEvent2", 1, 3, "Collection1"); - testYamlDecodeEncode("TestEvent3", 1, 2, "Collection2"); - testYamlDecodeEncode("TestEvent4", 1, 1, "Collection3"); - testYamlDecodeEncode("TestEvent5", 1, 1, "Collection4"); - testYamlDecodeEncode("TestEvent6", 1, 2, "Collection5"); - testYamlDecodeEncode("TestEvent1", 2, 1, "Structure0"); - testYamlDecodeEncode("TestEvent7", 2, 3, "Structure1"); - testYamlDecodeEncode("TestEvent8", 1, 2, "Structure2"); - testYamlDecodeEncode("TestEvent8", 1, 2, "Structure3"); - testYamlDecodeEncode("TestEvent9", 1, 2, "Structure4"); - testYamlDecodeEncode("TestEvent10", 1, 1, "Structure5"); - testYamlDecodeEncode("TestEvent11", 1, 4, "TOSCA0"); - } - - /** - * Test yaml decode encode. - * - * @param eventName the event name - * @param eventCount the event count - * @param parCount the par count - * @param fileName the file name - * @throws ApexEventException the apex event exception - * @throws IOException Signals that an I/O exception has occurred. - */ - private void testYamlDecodeEncode(final String eventName, final int eventCount, final int parCount, - final String fileName) throws ApexEventException, IOException { - YamlEventProtocolParameters parameters = new YamlEventProtocolParameters(); - parameters.setDelimiterAtStart(false); - - Apex2YamlEventConverter converter = new Apex2YamlEventConverter(); - converter.init(parameters); - - String filePath = "src/test/resources/yaml_in/" + fileName + ".yaml"; - FileInputStream fileInputStream = new FileInputStream(new File(filePath)); - HeaderDelimitedTextBlockReader reader = new HeaderDelimitedTextBlockReader(parameters); - reader.init(fileInputStream); - - List eventList = new ArrayList<>(); - - TextBlock textBlock; - do { - textBlock = reader.readTextBlock(); - - eventList.addAll(converter.toApexEvent(eventName, textBlock.getText())); - } - while (!textBlock.isEndOfText()); - - fileInputStream.close(); - - assertEquals(eventCount, eventList.size()); - - for (int eventNo = 0; eventNo < eventCount; eventNo++) { - assertEquals(parCount, eventList.get(0).size()); - - String eventYaml = (String) converter.fromApexEvent(eventList.get(eventNo)); - String expectedYaml = TextFileUtils - .getTextFileAsString("src/test/resources/yaml_out/" + fileName + '_' + eventNo + ".yaml"); - assertEquals(expectedYaml.replaceAll("\\s*", ""), eventYaml.replaceAll("\\s*", "")); - } - } -} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlPluginStability.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlPluginStability.java deleted file mode 100644 index d033de56d..000000000 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlPluginStability.java +++ /dev/null @@ -1,278 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 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.event.protocol.yaml; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.IOException; -import java.util.List; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; -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.AxReferenceKey; -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.eventmodel.concepts.AxEvent; -import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; -import org.onap.policy.apex.model.eventmodel.concepts.AxField; -import org.onap.policy.apex.service.engine.event.ApexEvent; -import org.onap.policy.apex.service.engine.event.ApexEventException; -import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; -import org.onap.policy.common.parameters.ParameterService; - -/** - * The Class TestYamlPluginStability. - */ -public class TestYamlPluginStability { - static AxEvent testEvent; - - /** - * Register test events and schemas. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @BeforeClass - public static void registerTestEventsAndSchemas() throws IOException { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - AxContextSchemas schemas = new AxContextSchemas(); - - AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", - "java.lang.Integer"); - schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema); - - AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), - "JAVA", "java.lang.Double"); - schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema); - - AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), - "JAVA", "java.lang.String"); - schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema); - - ModelService.registerModel(AxContextSchemas.class, schemas); - - testEvent = new AxEvent(new AxArtifactKey("TestEvent", "0.0.1")); - testEvent.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField teField0 = new AxField(new AxReferenceKey(testEvent.getKey(), "intValue"), simpleIntSchema.getKey()); - testEvent.getParameterMap().put("intValue", teField0); - AxField teField1 = new AxField(new AxReferenceKey(testEvent.getKey(), "doubleValue"), - simpleDoubleSchema.getKey()); - testEvent.getParameterMap().put("doubleValue", teField1); - AxField teField2 = new AxField(new AxReferenceKey(testEvent.getKey(), "stringValue"), - simpleStringSchema.getKey(), true); - testEvent.getParameterMap().put("stringValue", teField2); - - AxEvents events = new AxEvents(); - events.getEventMap().put(testEvent.getKey(), testEvent); - - ModelService.registerModel(AxEvents.class, events); - } - - /** - * Unregister test events and schemas. - */ - @AfterClass - public static void unregisterTestEventsAndSchemas() { - ModelService.clear(); - ParameterService.clear(); - } - - /** - * Test stability. - * - * @throws ApexEventException the apex event exception - */ - @Test - public void testStability() throws ApexEventException { - Apex2YamlEventConverter converter = new Apex2YamlEventConverter(); - - try { - converter.init(null); - fail("this test should throw an exception"); - } catch (ApexEventRuntimeException e) { - assertEquals("specified consumer properties are not applicable to the YAML event protocol", e.getMessage()); - } - - YamlEventProtocolParameters pars = new YamlEventProtocolParameters(); - converter.init(pars); - - try { - converter.toApexEvent("NonExistantEvent", ""); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: an event definition for an event named \"NonExistantEvent\"", - e.getMessage().substring(0, 89)); - } - - try { - converter.toApexEvent("TestEvent", null); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("event processing failed, event is null", e.getMessage()); - } - - try { - converter.toApexEvent("TestEvent", 1); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("error converting event \"1\" to a string", e.getMessage()); - } - - try { - converter.toApexEvent("TestEvent", ""); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertTrue(e.getMessage().contains("Field \"doubleValue\" is missing")); - } - - try { - converter.fromApexEvent(null); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("event processing failed, Apex event is null", e.getMessage()); - } - - ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(), - testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget()); - apexEvent.put("doubleValue", 123.45); - apexEvent.put("intValue", 123); - apexEvent.put("stringValue", "123.45"); - - apexEvent.setExceptionMessage("my wonderful exception message"); - String yamlString = (String) converter.fromApexEvent(apexEvent); - assertTrue(yamlString.contains("my wonderful exception message")); - - apexEvent.remove("intValue"); - try { - yamlString = (String) converter.fromApexEvent(apexEvent); - fail("this test should throw an exception"); - } catch (ApexEventRuntimeException e) { - assertEquals("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing", - e.getMessage().substring(0, 72)); - } - - try { - converter.toApexEvent(null, ""); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"", - e.getMessage().substring(0, 81)); - } - - pars.setNameAlias("TheNameField"); - try { - converter.toApexEvent(null, ""); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"", - e.getMessage().substring(0, 81)); - } - - apexEvent.put("intValue", 123); - - apexEvent.remove("stringValue"); - yamlString = (String) converter.fromApexEvent(apexEvent); - apexEvent.put("stringValue", "123.45"); - - String yamlInputString = "doubleValue: 123.45\n" + "intValue: 123"; - - List eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals(123.45, eventList.get(0).get("doubleValue")); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: null"; - - eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals(null, eventList.get(0).get("stringValue")); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: TestEvent"; - pars.setNameAlias("stringValue"); - eventList = converter.toApexEvent(null, yamlInputString); - assertEquals("TestEvent", eventList.get(0).get("stringValue")); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: SomeOtherEvent"; - eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals("SomeOtherEvent", eventList.get(0).get("stringValue")); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: 0.0.1"; - pars.setNameAlias(null); - pars.setVersionAlias("stringValue"); - eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals("0.0.1", eventList.get(0).get("stringValue")); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace"; - pars.setVersionAlias(null); - pars.setNameSpaceAlias("stringValue"); - try { - converter.toApexEvent("TestEvent", yamlInputString); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: namespace \"org.some.other.namespace\" on event", - e.getMessage().substring(0, 77)); - } - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" - + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml"; - eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals("org.onap.policy.apex.plugins.event.protocol.yaml", eventList.get(0).getNameSpace()); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MySource"; - pars.setNameSpaceAlias(null); - pars.setSourceAlias("stringValue"); - eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals("MySource", eventList.get(0).getSource()); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyTarget"; - pars.setSourceAlias(null); - pars.setTargetAlias("stringValue"); - eventList = converter.toApexEvent("TestEvent", yamlInputString); - assertEquals("MyTarget", eventList.get(0).getTarget()); - pars.setTargetAlias(null); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString"; - pars.setSourceAlias(null); - pars.setTargetAlias("intValue"); - try { - converter.toApexEvent("TestEvent", yamlInputString); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: field \"target\" with type \"java.lang.Integer\"", - e.getMessage().substring(0, 76)); - } - pars.setTargetAlias(null); - - yamlInputString = "doubleValue: 123.45\n" + "intValue: ~\n" + "stringValue: MyString"; - try { - converter.toApexEvent("TestEvent", yamlInputString); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertTrue(e.getMessage().contains("mandatory field \"intValue\" is missing")); - } - } -} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolTest.java new file mode 100644 index 000000000..2b6a13f89 --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolTest.java @@ -0,0 +1,287 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.event.protocol.yaml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; +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.AxReferenceKey; +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.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; +import org.onap.policy.apex.model.eventmodel.concepts.AxField; +import org.onap.policy.apex.model.utilities.TextFileUtils; +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.HeaderDelimitedTextBlockReader; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; +import org.onap.policy.common.parameters.ParameterService; + +/** + * The Class TestYamlEventProtocol. + */ +public class YamlEventProtocolTest { + + /** + * Register test events and schemas. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @BeforeClass + public static void registerTestEventsAndSchemas() throws IOException { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); + ParameterService.register(schemaParameters, true); + + AxContextSchemas schemas = new AxContextSchemas(); + + AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", + "java.lang.Integer"); + schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema); + + AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), + "JAVA", "java.lang.Double"); + schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema); + + AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), + "JAVA", "java.lang.String"); + schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema); + + AxContextSchema arrayListSchema = new AxContextSchema(new AxArtifactKey("ArrayListSchema", "0.0.1"), "JAVA", + "java.util.ArrayList"); + schemas.getSchemasMap().put(arrayListSchema.getKey(), arrayListSchema); + + AxContextSchema linkedHashMapSchema = new AxContextSchema(new AxArtifactKey("LinkedHashMapSchema", "0.0.1"), + "JAVA", "java.util.LinkedHashMap"); + schemas.getSchemasMap().put(linkedHashMapSchema.getKey(), linkedHashMapSchema); + + ModelService.registerModel(AxContextSchemas.class, schemas); + + AxEvents events = new AxEvents(); + + AxEvent testEvent0 = new AxEvent(new AxArtifactKey("TestEvent0", "0.0.1")); + testEvent0.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + events.getEventMap().put(testEvent0.getKey(), testEvent0); + + AxEvent testEvent1 = new AxEvent(new AxArtifactKey("TestEvent1", "0.0.1")); + testEvent1.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te1Field0 = new AxField(new AxReferenceKey(testEvent1.getKey(), "yaml_field"), + arrayListSchema.getKey()); + testEvent1.getParameterMap().put("yaml_field", te1Field0); + events.getEventMap().put(testEvent1.getKey(), testEvent1); + + AxEvent testEvent2 = new AxEvent(new AxArtifactKey("TestEvent2", "0.0.1")); + testEvent2.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te2Field0 = new AxField(new AxReferenceKey(testEvent2.getKey(), "hr"), simpleIntSchema.getKey()); + testEvent2.getParameterMap().put("hr", te2Field0); + AxField te2Field1 = new AxField(new AxReferenceKey(testEvent2.getKey(), "avg"), simpleDoubleSchema.getKey()); + testEvent2.getParameterMap().put("avg", te2Field1); + AxField te2Field2 = new AxField(new AxReferenceKey(testEvent2.getKey(), "rbi"), simpleIntSchema.getKey()); + testEvent2.getParameterMap().put("rbi", te2Field2); + events.getEventMap().put(testEvent2.getKey(), testEvent2); + + AxEvent testEvent3 = new AxEvent(new AxArtifactKey("TestEvent3", "0.0.1")); + testEvent3.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te3Field0 = new AxField(new AxReferenceKey(testEvent3.getKey(), "american"), arrayListSchema.getKey()); + testEvent3.getParameterMap().put("american", te3Field0); + AxField te3Field1 = new AxField(new AxReferenceKey(testEvent3.getKey(), "national"), arrayListSchema.getKey()); + testEvent3.getParameterMap().put("national", te3Field1); + events.getEventMap().put(testEvent3.getKey(), testEvent3); + + AxEvent testEvent4 = new AxEvent(new AxArtifactKey("TestEvent4", "0.0.1")); + testEvent4.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te4Field0 = new AxField(new AxReferenceKey(testEvent4.getKey(), "yaml_field"), + arrayListSchema.getKey()); + testEvent4.getParameterMap().put("yaml_field", te4Field0); + events.getEventMap().put(testEvent4.getKey(), testEvent4); + + AxEvent testEvent5 = new AxEvent(new AxArtifactKey("TestEvent5", "0.0.1")); + testEvent5.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te5Field0 = new AxField(new AxReferenceKey(testEvent5.getKey(), "yaml_field"), + arrayListSchema.getKey()); + testEvent5.getParameterMap().put("yaml_field", te5Field0); + events.getEventMap().put(testEvent5.getKey(), testEvent5); + + AxEvent testEvent6 = new AxEvent(new AxArtifactKey("TestEvent6", "0.0.1")); + testEvent6.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te6Field0 = new AxField(new AxReferenceKey(testEvent6.getKey(), "MarkMcGwire"), + linkedHashMapSchema.getKey()); + testEvent6.getParameterMap().put("Mark McGwire", te6Field0); + AxField te6Field1 = new AxField(new AxReferenceKey(testEvent6.getKey(), "SammySosa"), + linkedHashMapSchema.getKey()); + testEvent6.getParameterMap().put("Sammy Sosa", te6Field1); + events.getEventMap().put(testEvent6.getKey(), testEvent6); + + AxEvent testEvent7 = new AxEvent(new AxArtifactKey("TestEvent7", "0.0.1")); + testEvent7.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te7Field0 = new AxField(new AxReferenceKey(testEvent7.getKey(), "time"), simpleIntSchema.getKey()); + testEvent7.getParameterMap().put("time", te7Field0); + AxField te7Field1 = new AxField(new AxReferenceKey(testEvent7.getKey(), "player"), simpleStringSchema.getKey()); + testEvent7.getParameterMap().put("player", te7Field1); + AxField te7Field2 = new AxField(new AxReferenceKey(testEvent7.getKey(), "action"), simpleStringSchema.getKey()); + testEvent7.getParameterMap().put("action", te7Field2); + events.getEventMap().put(testEvent7.getKey(), testEvent7); + + AxEvent testEvent8 = new AxEvent(new AxArtifactKey("TestEvent8", "0.0.1")); + testEvent8.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te8Field0 = new AxField(new AxReferenceKey(testEvent8.getKey(), "hr"), arrayListSchema.getKey()); + testEvent8.getParameterMap().put("hr", te8Field0); + AxField te8Field1 = new AxField(new AxReferenceKey(testEvent8.getKey(), "rbi"), arrayListSchema.getKey()); + testEvent8.getParameterMap().put("rbi", te8Field1); + events.getEventMap().put(testEvent8.getKey(), testEvent8); + + AxEvent testEvent9 = new AxEvent(new AxArtifactKey("TestEvent9", "0.0.1")); + testEvent9.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te9Field0 = new AxField(new AxReferenceKey(testEvent9.getKey(), "ChicagoCubs"), + arrayListSchema.getKey()); + testEvent9.getParameterMap().put("ChicagoCubs", te9Field0); + AxField te9Field1 = new AxField(new AxReferenceKey(testEvent9.getKey(), "AtlantaBraves"), + arrayListSchema.getKey()); + testEvent9.getParameterMap().put("AtlantaBraves", te9Field1); + events.getEventMap().put(testEvent9.getKey(), testEvent9); + + AxEvent testEvent10 = new AxEvent(new AxArtifactKey("TestEvent10", "0.0.1")); + testEvent10.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te10Field0 = new AxField(new AxReferenceKey(testEvent10.getKey(), "yaml_field"), + arrayListSchema.getKey()); + testEvent10.getParameterMap().put("yaml_field", te10Field0); + events.getEventMap().put(testEvent10.getKey(), testEvent10); + + AxEvent testEvent11 = new AxEvent(new AxArtifactKey("TestEvent11", "0.0.1")); + testEvent11.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField te11Field0 = new AxField(new AxReferenceKey(testEvent11.getKey(), "tosca_definitions_version"), + simpleStringSchema.getKey()); + testEvent11.getParameterMap().put("tosca_definitions_version", te11Field0); + AxField te11Field1 = new AxField(new AxReferenceKey(testEvent11.getKey(), "description"), + simpleStringSchema.getKey(), true); + testEvent11.getParameterMap().put("description", te11Field1); + AxField te11Field2 = new AxField(new AxReferenceKey(testEvent11.getKey(), "node_types"), + linkedHashMapSchema.getKey(), true); + testEvent11.getParameterMap().put("node_types", te11Field2); + AxField te11Field3 = new AxField(new AxReferenceKey(testEvent11.getKey(), "topology_template"), + linkedHashMapSchema.getKey()); + testEvent11.getParameterMap().put("topology_template", te11Field3); + events.getEventMap().put(testEvent11.getKey(), testEvent11); + + ModelService.registerModel(AxEvents.class, events); + } + + /** + * Unregister test events and schemas. + */ + @AfterClass + public static void unregisterTestEventsAndSchemas() { + ModelService.clear(); + ParameterService.clear(); + } + + /** + * Test yaml processing. + * + * @throws ApexEventException the apex event exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testYamlProcessing() throws ApexEventException, IOException { + try { + testYamlDecodeEncode("TestEvent0", 1, 0, "Empty0"); + fail("test should fail here"); + } catch (ApexEventException e) { + assertEquals("event processing failed, event is null", e.getMessage()); + } + + testYamlDecodeEncode("TestEvent0", 1, 0, "Empty1"); + testYamlDecodeEncode("TestEvent1", 1, 1, "Collection0"); + testYamlDecodeEncode("TestEvent2", 1, 3, "Collection1"); + testYamlDecodeEncode("TestEvent3", 1, 2, "Collection2"); + testYamlDecodeEncode("TestEvent4", 1, 1, "Collection3"); + testYamlDecodeEncode("TestEvent5", 1, 1, "Collection4"); + testYamlDecodeEncode("TestEvent6", 1, 2, "Collection5"); + testYamlDecodeEncode("TestEvent1", 2, 1, "Structure0"); + testYamlDecodeEncode("TestEvent7", 2, 3, "Structure1"); + testYamlDecodeEncode("TestEvent8", 1, 2, "Structure2"); + testYamlDecodeEncode("TestEvent8", 1, 2, "Structure3"); + testYamlDecodeEncode("TestEvent9", 1, 2, "Structure4"); + testYamlDecodeEncode("TestEvent10", 1, 1, "Structure5"); + testYamlDecodeEncode("TestEvent11", 1, 4, "TOSCA0"); + } + + /** + * Test yaml decode encode. + * + * @param eventName the event name + * @param eventCount the event count + * @param parCount the par count + * @param fileName the file name + * @throws ApexEventException the apex event exception + * @throws IOException Signals that an I/O exception has occurred. + */ + private void testYamlDecodeEncode(final String eventName, final int eventCount, final int parCount, + final String fileName) throws ApexEventException, IOException { + YamlEventProtocolParameters parameters = new YamlEventProtocolParameters(); + parameters.setDelimiterAtStart(false); + + Apex2YamlEventConverter converter = new Apex2YamlEventConverter(); + converter.init(parameters); + + String filePath = "src/test/resources/yaml_in/" + fileName + ".yaml"; + FileInputStream fileInputStream = new FileInputStream(new File(filePath)); + HeaderDelimitedTextBlockReader reader = new HeaderDelimitedTextBlockReader(parameters); + reader.init(fileInputStream); + + List eventList = new ArrayList<>(); + + TextBlock textBlock; + do { + textBlock = reader.readTextBlock(); + + eventList.addAll(converter.toApexEvent(eventName, textBlock.getText())); + } + while (!textBlock.isEndOfText()); + + fileInputStream.close(); + + assertEquals(eventCount, eventList.size()); + + for (int eventNo = 0; eventNo < eventCount; eventNo++) { + assertEquals(parCount, eventList.get(0).size()); + + String eventYaml = (String) converter.fromApexEvent(eventList.get(eventNo)); + String expectedYaml = TextFileUtils + .getTextFileAsString("src/test/resources/yaml_out/" + fileName + '_' + eventNo + ".yaml"); + assertEquals(expectedYaml.replaceAll("\\s*", ""), eventYaml.replaceAll("\\s*", "")); + } + } +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java new file mode 100644 index 000000000..7f62961be --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java @@ -0,0 +1,278 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.event.protocol.yaml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.util.List; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; +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.AxReferenceKey; +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.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; +import org.onap.policy.apex.model.eventmodel.concepts.AxField; +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; +import org.onap.policy.common.parameters.ParameterService; + +/** + * The Class TestYamlPluginStability. + */ +public class YamlPluginStabilityTest { + static AxEvent testEvent; + + /** + * Register test events and schemas. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @BeforeClass + public static void registerTestEventsAndSchemas() throws IOException { + SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); + ParameterService.register(schemaParameters); + + AxContextSchemas schemas = new AxContextSchemas(); + + AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", + "java.lang.Integer"); + schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema); + + AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), + "JAVA", "java.lang.Double"); + schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema); + + AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), + "JAVA", "java.lang.String"); + schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema); + + ModelService.registerModel(AxContextSchemas.class, schemas); + + testEvent = new AxEvent(new AxArtifactKey("TestEvent", "0.0.1")); + testEvent.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); + AxField teField0 = new AxField(new AxReferenceKey(testEvent.getKey(), "intValue"), simpleIntSchema.getKey()); + testEvent.getParameterMap().put("intValue", teField0); + AxField teField1 = new AxField(new AxReferenceKey(testEvent.getKey(), "doubleValue"), + simpleDoubleSchema.getKey()); + testEvent.getParameterMap().put("doubleValue", teField1); + AxField teField2 = new AxField(new AxReferenceKey(testEvent.getKey(), "stringValue"), + simpleStringSchema.getKey(), true); + testEvent.getParameterMap().put("stringValue", teField2); + + AxEvents events = new AxEvents(); + events.getEventMap().put(testEvent.getKey(), testEvent); + + ModelService.registerModel(AxEvents.class, events); + } + + /** + * Unregister test events and schemas. + */ + @AfterClass + public static void unregisterTestEventsAndSchemas() { + ModelService.clear(); + ParameterService.clear(); + } + + /** + * Test stability. + * + * @throws ApexEventException the apex event exception + */ + @Test + public void testStability() throws ApexEventException { + Apex2YamlEventConverter converter = new Apex2YamlEventConverter(); + + try { + converter.init(null); + fail("this test should throw an exception"); + } catch (ApexEventRuntimeException e) { + assertEquals("specified consumer properties are not applicable to the YAML event protocol", e.getMessage()); + } + + YamlEventProtocolParameters pars = new YamlEventProtocolParameters(); + converter.init(pars); + + try { + converter.toApexEvent("NonExistantEvent", ""); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("Failed to unmarshal YAML event: an event definition for an event named \"NonExistantEvent\"", + e.getMessage().substring(0, 89)); + } + + try { + converter.toApexEvent("TestEvent", null); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("event processing failed, event is null", e.getMessage()); + } + + try { + converter.toApexEvent("TestEvent", 1); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("error converting event \"1\" to a string", e.getMessage()); + } + + try { + converter.toApexEvent("TestEvent", ""); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertTrue(e.getMessage().contains("Field \"doubleValue\" is missing")); + } + + try { + converter.fromApexEvent(null); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("event processing failed, Apex event is null", e.getMessage()); + } + + ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(), + testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget()); + apexEvent.put("doubleValue", 123.45); + apexEvent.put("intValue", 123); + apexEvent.put("stringValue", "123.45"); + + apexEvent.setExceptionMessage("my wonderful exception message"); + String yamlString = (String) converter.fromApexEvent(apexEvent); + assertTrue(yamlString.contains("my wonderful exception message")); + + apexEvent.remove("intValue"); + try { + yamlString = (String) converter.fromApexEvent(apexEvent); + fail("this test should throw an exception"); + } catch (ApexEventRuntimeException e) { + assertEquals("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing", + e.getMessage().substring(0, 72)); + } + + try { + converter.toApexEvent(null, ""); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"", + e.getMessage().substring(0, 81)); + } + + pars.setNameAlias("TheNameField"); + try { + converter.toApexEvent(null, ""); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"", + e.getMessage().substring(0, 81)); + } + + apexEvent.put("intValue", 123); + + apexEvent.remove("stringValue"); + yamlString = (String) converter.fromApexEvent(apexEvent); + apexEvent.put("stringValue", "123.45"); + + String yamlInputString = "doubleValue: 123.45\n" + "intValue: 123"; + + List eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals(123.45, eventList.get(0).get("doubleValue")); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: null"; + + eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals(null, eventList.get(0).get("stringValue")); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: TestEvent"; + pars.setNameAlias("stringValue"); + eventList = converter.toApexEvent(null, yamlInputString); + assertEquals("TestEvent", eventList.get(0).get("stringValue")); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: SomeOtherEvent"; + eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals("SomeOtherEvent", eventList.get(0).get("stringValue")); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: 0.0.1"; + pars.setNameAlias(null); + pars.setVersionAlias("stringValue"); + eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals("0.0.1", eventList.get(0).get("stringValue")); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace"; + pars.setVersionAlias(null); + pars.setNameSpaceAlias("stringValue"); + try { + converter.toApexEvent("TestEvent", yamlInputString); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("Failed to unmarshal YAML event: namespace \"org.some.other.namespace\" on event", + e.getMessage().substring(0, 77)); + } + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml"; + eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals("org.onap.policy.apex.plugins.event.protocol.yaml", eventList.get(0).getNameSpace()); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MySource"; + pars.setNameSpaceAlias(null); + pars.setSourceAlias("stringValue"); + eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals("MySource", eventList.get(0).getSource()); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyTarget"; + pars.setSourceAlias(null); + pars.setTargetAlias("stringValue"); + eventList = converter.toApexEvent("TestEvent", yamlInputString); + assertEquals("MyTarget", eventList.get(0).getTarget()); + pars.setTargetAlias(null); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString"; + pars.setSourceAlias(null); + pars.setTargetAlias("intValue"); + try { + converter.toApexEvent("TestEvent", yamlInputString); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertEquals("Failed to unmarshal YAML event: field \"target\" with type \"java.lang.Integer\"", + e.getMessage().substring(0, 76)); + } + pars.setTargetAlias(null); + + yamlInputString = "doubleValue: 123.45\n" + "intValue: ~\n" + "stringValue: MyString"; + try { + converter.toApexEvent("TestEvent", yamlInputString); + fail("this test should throw an exception"); + } catch (ApexEventException e) { + assertTrue(e.getMessage().contains("mandatory field \"intValue\" is missing")); + } + } +} -- cgit 1.2.3-korg