diff options
Diffstat (limited to 'plugins/plugins-context/context-schema')
9 files changed, 81 insertions, 73 deletions
diff --git a/plugins/plugins-context/context-schema/context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java b/plugins/plugins-context/context-schema/context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java index b4cc38602..5fba274ce 100644 --- a/plugins/plugins-context/context-schema/context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java +++ b/plugins/plugins-context/context-schema/context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java @@ -113,11 +113,19 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { } @Override - public Object createNewInstance(final JsonElement jsonElement) { - final Gson gson = new GsonBuilder().serializeNulls().create(); - final String elementJsonString = gson.toJson(jsonElement); + public Object createNewInstance(final Object incomingObject) { + if (incomingObject instanceof JsonElement) { + final Gson gson = new GsonBuilder().serializeNulls().create(); + final String elementJsonString = gson.toJson((JsonElement) incomingObject); - return createNewInstance(elementJsonString); + return createNewInstance(elementJsonString); + } + else { + final String returnString = getUserKey().getID() + ": the object \"" + incomingObject + + "\" is not an instance of JsonObject"; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); + } } @Override @@ -191,7 +199,7 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { } @Override - public String marshal2Json(final Object object) { + public String marshal2String(final Object object) { // Condition the object for Avro encoding final Object conditionedObject = avroObjectMapper.mapToAvro(object); @@ -217,9 +225,9 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { } @Override - public JsonElement marshal2JsonElement(final Object schemaObject) { + public JsonElement marshal2Object(final Object schemaObject) { // Get the object as a Json string - final String schemaObjectAsString = marshal2Json(schemaObject); + final String schemaObjectAsString = marshal2String(schemaObject); // Get a Gson instance to convert the Json string to an object created by Json final Gson gson = new Gson(); diff --git a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaArray.java b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaArray.java index dc65c108d..21fab66d9 100644 --- a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaArray.java +++ b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaArray.java @@ -101,7 +101,7 @@ public class TestAvroSchemaArray { 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.marshal2Json(schemaObject); + final String outString = schemaHelper.marshal2String(schemaObject); assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", "")); } } diff --git a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaEnum.java b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaEnum.java index 0302345b3..ae19cd31a 100644 --- a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaEnum.java +++ b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaEnum.java @@ -114,7 +114,7 @@ public class TestAvroSchemaEnum { 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.marshal2Json(decodedObject); + final String outString = schemaHelper.marshal2String(decodedObject); assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " ")); } } diff --git a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaFixed.java b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaFixed.java index f4906e5df..41f622115 100644 --- a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaFixed.java +++ b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaFixed.java @@ -124,7 +124,7 @@ public class TestAvroSchemaFixed { 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.marshal2Json(decodedObject); + final String outString = schemaHelper.marshal2String(decodedObject); assertEquals(inString.replaceAll("[\\r?\\n]+", " "), outString.replaceAll("[\\r?\\n]+", " ")); } } diff --git a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperMarshal.java b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperMarshal.java index 1b35d8275..a710a2376 100644 --- a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperMarshal.java +++ b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaHelperMarshal.java @@ -59,9 +59,9 @@ public class TestAvroSchemaHelperMarshal { final SchemaHelper schemaHelper0 = new SchemaHelperFactory().createSchemaHelper(testKey, avroNullSchema.getKey()); - assertEquals("null", schemaHelper0.marshal2Json(null)); - assertEquals("null", schemaHelper0.marshal2Json(123)); - assertEquals("null", schemaHelper0.marshal2Json("Everything is marshalled to Null, no matter what it is")); + 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 @@ -73,10 +73,10 @@ public class TestAvroSchemaHelperMarshal { final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey, avroBooleanSchema.getKey()); - assertEquals("true", schemaHelper1.marshal2Json(true)); - assertEquals("false", schemaHelper1.marshal2Json(false)); + assertEquals("true", schemaHelper1.marshal2String(true)); + assertEquals("false", schemaHelper1.marshal2String(false)); try { - schemaHelper1.marshal2Json(0); + schemaHelper1.marshal2String(0); fail("Test should throw an exception here"); } catch (final Exception e) { e.printStackTrace(); @@ -85,7 +85,7 @@ public class TestAvroSchemaHelperMarshal { e.getMessage()); } try { - schemaHelper1.marshal2Json("0"); + schemaHelper1.marshal2String("0"); fail("Test should throw an exception here"); } catch (final Exception e) { e.printStackTrace(); @@ -104,22 +104,22 @@ public class TestAvroSchemaHelperMarshal { final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey, avroIntSchema.getKey()); - assertEquals("0", schemaHelper2.marshal2Json(0)); - assertEquals("1", schemaHelper2.marshal2Json(1)); - assertEquals("-1", schemaHelper2.marshal2Json(-1)); - assertEquals("1", schemaHelper2.marshal2Json(1.23)); - assertEquals("-1", schemaHelper2.marshal2Json(-1.23)); - assertEquals("2147483647", schemaHelper2.marshal2Json(2147483647)); - assertEquals("-2147483648", schemaHelper2.marshal2Json(-2147483648)); + 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.marshal2Json("Hello"); + 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.marshal2Json(null); + schemaHelper2.marshal2String(null); fail("Test should throw an exception here"); } catch (final Exception e) { assertTrue(e.getMessage() @@ -136,20 +136,20 @@ public class TestAvroSchemaHelperMarshal { final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey, avroLongSchema.getKey()); - assertEquals("0", schemaHelper3.marshal2Json(0L)); - assertEquals("1", schemaHelper3.marshal2Json(1L)); - assertEquals("-1", schemaHelper3.marshal2Json(-1L)); - assertEquals("9223372036854775807", schemaHelper3.marshal2Json(9223372036854775807L)); - assertEquals("-9223372036854775808", schemaHelper3.marshal2Json(-9223372036854775808L)); + 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.marshal2Json("Hello"); + 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.marshal2Json(null); + schemaHelper3.marshal2String(null); fail("Test should throw an exception here"); } catch (final Exception e) { assertTrue(e.getMessage() @@ -166,24 +166,24 @@ public class TestAvroSchemaHelperMarshal { final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey, avroFloatSchema.getKey()); - assertEquals("0.0", schemaHelper4.marshal2Json(0F)); - assertEquals("1.0", schemaHelper4.marshal2Json(1F)); - assertEquals("-1.0", schemaHelper4.marshal2Json(-1F)); - assertEquals("1.23", schemaHelper4.marshal2Json(1.23F)); - assertEquals("-1.23", schemaHelper4.marshal2Json(-1.23F)); - assertEquals("9.223372E18", schemaHelper4.marshal2Json(9.223372E18F)); - assertEquals("-9.223372E18", schemaHelper4.marshal2Json(-9.223372E18F)); - assertEquals("9.223372E18", schemaHelper4.marshal2Json(9.223372E18F)); - assertEquals("-9.223372E18", schemaHelper4.marshal2Json(-9.223372E18F)); + 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.marshal2Json("Hello"); + 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.marshal2Json(null); + schemaHelper4.marshal2String(null); fail("Test should throw an exception here"); } catch (final Exception e) { assertTrue(e.getMessage() @@ -201,24 +201,24 @@ public class TestAvroSchemaHelperMarshal { final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey, avroDoubleSchema.getKey()); - assertEquals("0.0", schemaHelper5.marshal2Json(0D)); - assertEquals("1.0", schemaHelper5.marshal2Json(1D)); - assertEquals("-1.0", schemaHelper5.marshal2Json(-1D)); - assertEquals("1.23", schemaHelper5.marshal2Json(1.23)); - assertEquals("-1.23", schemaHelper5.marshal2Json(-1.23)); - assertEquals("9.223372036854776E18", schemaHelper5.marshal2Json(9.223372036854776E18)); - assertEquals("-9.223372036854776E18", schemaHelper5.marshal2Json(-9.223372036854776E18)); - assertEquals("9.223372036854776E18", schemaHelper5.marshal2Json(9.223372036854776E18)); - assertEquals("-9.223372036854776E18", schemaHelper5.marshal2Json(-9.223372036854776E18)); + 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.marshal2Json("Hello"); + 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.marshal2Json(null); + schemaHelper5.marshal2String(null); fail("Test should throw an exception here"); } catch (final Exception e) { assertTrue(e.getMessage() @@ -235,18 +235,18 @@ public class TestAvroSchemaHelperMarshal { final SchemaHelper schemaHelper7 = new SchemaHelperFactory().createSchemaHelper(testKey, avroStringSchema.getKey()); - assertEquals("\"0\"", schemaHelper7.marshal2Json("0")); - assertEquals("\"1\"", schemaHelper7.marshal2Json("1")); - assertEquals("\"-1\"", schemaHelper7.marshal2Json("-1")); - assertEquals("\"1.23\"", schemaHelper7.marshal2Json("1.23")); - assertEquals("\"-1.23\"", schemaHelper7.marshal2Json("-1.23")); - assertEquals("\"9223372036854775807\"", schemaHelper7.marshal2Json("9223372036854775807")); - assertEquals("\"-9223372036854775808\"", schemaHelper7.marshal2Json("-9223372036854775808")); - assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2Json("9223372036854775808")); - assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2Json("-9223372036854775809")); - assertEquals("\"Hello\"", schemaHelper7.marshal2Json("Hello")); + 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.marshal2Json(null); + schemaHelper7.marshal2String(null); fail("Test should throw an exception here"); } catch (final Exception e) { assertTrue(e.getMessage() @@ -263,11 +263,11 @@ public class TestAvroSchemaHelperMarshal { final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); final byte[] helloBytes = {104, 101, 108, 108, 111}; - final String helloOut = schemaHelper.marshal2Json(helloBytes); + final String helloOut = schemaHelper.marshal2String(helloBytes); assertEquals("\"hello\"", helloOut); try { - schemaHelper.marshal2Json(null); + schemaHelper.marshal2String(null); fail("Test should throw an exception here"); } catch (final Exception e) { assertTrue(e.getMessage() diff --git a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaMap.java b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaMap.java index 74591bb99..33ca512b9 100644 --- a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaMap.java +++ b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaMap.java @@ -118,7 +118,7 @@ public class TestAvroSchemaMap { final String originalInString = TextFileUtils.getTextFileAsString(fileName); final HashMap<?, ?> firstDecodedMap = (HashMap<?, ?>) schemaHelper.unmarshal(originalInString); - final String outString = schemaHelper.marshal2Json(firstDecodedMap); + final String outString = schemaHelper.marshal2String(firstDecodedMap); final File tempOutFile = File.createTempFile("ApexAvro", ".json"); TextFileUtils.putStringAsFile(outString, tempOutFile); diff --git a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaRecord.java b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaRecord.java index b793ef0ae..e14236064 100644 --- a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaRecord.java +++ b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestAvroSchemaRecord.java @@ -124,7 +124,7 @@ public class TestAvroSchemaRecord { 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.marshal2Json(decodedObject); + final String outString = schemaHelper.marshal2String(decodedObject); assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", "")); } } diff --git a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestHealthCheckSchema.java b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestHealthCheckSchema.java index c32d9b3d4..026125af9 100644 --- a/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestHealthCheckSchema.java +++ b/plugins/plugins-context/context-schema/context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/TestHealthCheckSchema.java @@ -106,14 +106,14 @@ public class TestHealthCheckSchema { commonHeaderFlagsRecord.put("mode", "EXCLUSIVE"); final String eventString = TextFileUtils.getTextFileAsString("src/test/resources/data/HealthCheckEvent.json"); - final String outString = schemaHelper.marshal2Json(healthCheckRecord); + final String outString = schemaHelper.marshal2String(healthCheckRecord); assertEquals(eventString.toString().replaceAll("\\s+", ""), outString.replaceAll("\\s+", "")); } 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.marshal2Json(decodedObject); + final String outString = schemaHelper.marshal2String(decodedObject); assertEquals(inString.replaceAll("\\s+", ""), outString.replaceAll("\\s+", "")); } } diff --git a/plugins/plugins-context/context-schema/pom.xml b/plugins/plugins-context/context-schema/pom.xml index f72ec1583..d60cb5570 100644 --- a/plugins/plugins-context/context-schema/pom.xml +++ b/plugins/plugins-context/context-schema/pom.xml @@ -35,4 +35,4 @@ <modules> <module>context-schema-avro</module> </modules> -</project>
\ No newline at end of file +</project> |