From 6dd2ac76d046e98104f4314ea0c529961ef3e35e Mon Sep 17 00:00:00 2001 From: liamfallon Date: Thu, 21 Jun 2018 16:04:47 +0800 Subject: Add unit test for apex pdp context schema Unit tests added for context schema handling in the APEX PDP. Code coverage in the schema packages now above required limit. Issue-ID: POLICY-857 Change-Id: I66ad6eb0e758f7a80492caae514875229c28924f Signed-off-by: liamfallon --- .../impl/schema/SchemaHelperFactoryTest.java | 96 +++++++ .../java/JavaSchemaHelperInstanceCreationTest.java | 96 +++++++ .../impl/schema/java/JavaSchemaHelperTest.java | 310 +++++++++++++++++++++ .../apex/context/impl/schema/java/MyBaseClass.java | 32 +++ .../apex/context/impl/schema/java/MySubClass.java | 26 ++ .../impl/schema/java/TestInstanceCreation.java | 96 ------- 6 files changed, 560 insertions(+), 96 deletions(-) create mode 100644 context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactoryTest.java create mode 100644 context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperInstanceCreationTest.java create mode 100644 context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperTest.java create mode 100644 context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/MyBaseClass.java create mode 100644 context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/MySubClass.java delete mode 100644 context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/TestInstanceCreation.java (limited to 'context/context-management/src/test/java') diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactoryTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactoryTest.java new file mode 100644 index 000000000..21096e753 --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactoryTest.java @@ -0,0 +1,96 @@ +/*- + * ============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.context.impl.schema; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.context.ContextRuntimeException; +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.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; + +public class SchemaHelperFactoryTest { + private static AxContextSchema intSchema; + private static AxContextSchemas schemas; + private static AxContextSchema badSchema; + + @BeforeClass + public static void setupSchema() { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + + intSchema = new AxContextSchema(new AxArtifactKey("IntSchema", "0.0.1"), "JAVA", "java.lang.Integer"); + badSchema = new AxContextSchema(new AxArtifactKey("IntSchema", "0.0.1"), "JAVA", "java.lang.Bad"); + } + + @Test + public void testSchemaHelperFactory() { + try { + new SchemaHelperFactory().createSchemaHelper(null, null); + fail("this test should throw an exception"); + } catch (IllegalArgumentException e) { + assertEquals("Parameter \"owningEntityKey\" may not be null", e.getMessage()); + } + + AxArtifactKey ownerKey = new AxArtifactKey("Owner", "0.0.1"); + try { + new SchemaHelperFactory().createSchemaHelper(ownerKey, null); + fail("this test should throw an exception"); + } catch (IllegalArgumentException e) { + assertEquals("Parameter \"schemaKey\" may not be null", e.getMessage()); + } + + try { + new SchemaHelperFactory().createSchemaHelper(ownerKey, intSchema.getKey()); + fail("this test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("schema \"IntSchema:0.0.1\" for entity Owner:0.0.1 does not exist", e.getMessage()); + } + + schemas.getSchemasMap().put(intSchema.getKey(), intSchema); + new SchemaParameters(); + try { + new SchemaHelperFactory().createSchemaHelper(ownerKey, intSchema.getKey()); + fail("this test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("context schema helper parameters not found for context schema \"JAVA\"", e.getMessage()); + } + + new SchemaParameters().getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); + assertNotNull(new SchemaHelperFactory().createSchemaHelper(ownerKey, intSchema.getKey())); + + schemas.getSchemasMap().put(intSchema.getKey(), badSchema); + try { + new SchemaHelperFactory().createSchemaHelper(ownerKey, badSchema.getKey()); + fail("this test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("Owner:0.0.1: class/type java.lang.Bad for context schema \"IntSchema:0.0.1\" " + + "not found. Check the class path of the JVM", e.getMessage()); + } + } +} diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperInstanceCreationTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperInstanceCreationTest.java new file mode 100644 index 000000000..17e5deecf --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperInstanceCreationTest.java @@ -0,0 +1,96 @@ +/*- + * ============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.context.impl.schema.java; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +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.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; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class JavaSchemaHelperInstanceCreationTest { + private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); + private AxContextSchemas schemas; + + @Before + public void initTest() { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + new SchemaParameters(); + } + + @Test + public void testNullEncoding() { + final AxContextSchema javaBooleanSchema = + new AxContextSchema(new AxArtifactKey("Boolean", "0.0.1"), "Java", "java.lang.Boolean"); + final AxContextSchema javaLongSchema = + new AxContextSchema(new AxArtifactKey("Long", "0.0.1"), "Java", "java.lang.Long"); + final AxContextSchema javaStringSchema = + new AxContextSchema(new AxArtifactKey("String", "0.0.1"), "Java", "java.lang.String"); + + schemas.getSchemasMap().put(javaBooleanSchema.getKey(), javaBooleanSchema); + schemas.getSchemasMap().put(javaLongSchema.getKey(), javaLongSchema); + schemas.getSchemasMap().put(javaStringSchema.getKey(), javaStringSchema); + + final SchemaHelper schemaHelper0 = + new SchemaHelperFactory().createSchemaHelper(testKey, javaBooleanSchema.getKey()); + final SchemaHelper schemaHelper1 = + new SchemaHelperFactory().createSchemaHelper(testKey, javaLongSchema.getKey()); + final SchemaHelper schemaHelper2 = + new SchemaHelperFactory().createSchemaHelper(testKey, javaStringSchema.getKey()); + + try { + schemaHelper0.createNewInstance(); + fail("this 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, schemaHelper0.createNewInstance("true")); + + + try { + schemaHelper1.createNewInstance(); + fail("this 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(65536L, schemaHelper1.createNewInstance("65536")); + + assertEquals("", schemaHelper2.createNewInstance()); + assertEquals("true", schemaHelper2.createNewInstance("true")); + } +} diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperTest.java new file mode 100644 index 000000000..a7446303e --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperTest.java @@ -0,0 +1,310 @@ +/*- + * ============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.context.impl.schema.java; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.math.BigDecimal; + +import org.junit.Test; +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; + +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.google.gson.JsonPrimitive; + +public class JavaSchemaHelperTest { + + @Test + public void testJavaSchemaHelperInit() { + AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1"); + AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1"); + + AxContextSchema badJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Rubbish"); + + try { + new JavaSchemaHelper().init(userKey, badJavaTypeSchema); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("UserKey:0.0.1: class/type java.lang.Rubbish for context schema" + + " \"SchemaKey:0.0.1\" not found. Check the class path of the JVM", e.getMessage()); + } + + AxContextSchema builtInJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "short"); + + try { + new JavaSchemaHelper().init(userKey, builtInJavaTypeSchema); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("UserKey:0.0.1: class/type short for context schema " + + "\"SchemaKey:0.0.1\" not found. Primitive types are not supported." + + " Use the appropriate Java boxing type instead.", e.getMessage()); + } + } + + @Test + public void testJavaSchemaHelperMethods() { + AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1"); + AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1"); + + AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer"); + + SchemaHelper intSchemaHelper = new JavaSchemaHelper(); + + assertEquals(AxArtifactKey.getNullKey(), intSchemaHelper.getUserKey()); + assertEquals(null, intSchemaHelper.getSchema()); + assertEquals(null, intSchemaHelper.getSchemaClass()); + assertEquals(null, intSchemaHelper.getSchemaObject()); + + try { + intSchemaHelper.createNewInstance(); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null", + e.getMessage()); + } + + try { + intSchemaHelper.createNewInstance(Float.parseFloat("1.23")); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null", + e.getMessage()); + } + + try { + intSchemaHelper.createNewInstance("hello"); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null", + e.getMessage()); + } + + intSchemaHelper.init(userKey, intSchema); + assertEquals(userKey, intSchemaHelper.getUserKey()); + assertEquals(intSchema, intSchemaHelper.getSchema()); + assertEquals(Integer.class, intSchemaHelper.getSchemaClass()); + assertEquals(null, intSchemaHelper.getSchemaObject()); + + try { + intSchemaHelper.createNewInstance(); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("UserKey:0.0.1: could not create an instance of class " + + "\"java.lang.Integer\" using the default constructor \"Integer()\"", e.getMessage()); + } + + try { + intSchemaHelper.createNewInstance(Float.parseFloat("1.23")); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("UserKey:0.0.1: the object \"1.23\" of type " + + "\"java.lang.Float\" is not an instance of JsonObject and is not " + + "assignable to \"java.lang.Integer\"", e.getMessage()); + } + + try { + intSchemaHelper.createNewInstance("hello"); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("UserKey:0.0.1: could not create an instance of class \"java.lang.Integer\" " + + "using the string constructor \"Integer(String)\"", e.getMessage()); + } + + JsonElement jsonIntElement = null; + assertEquals(null, intSchemaHelper.createNewInstance(jsonIntElement)); + + jsonIntElement = new JsonParser().parse("123"); + + assertEquals(123, intSchemaHelper.createNewInstance(jsonIntElement)); + assertEquals(123, intSchemaHelper.createNewInstance(Integer.parseInt("123"))); + + assertEquals(null, intSchemaHelper.unmarshal(null)); + assertEquals(123, intSchemaHelper.unmarshal(Integer.parseInt("123"))); + assertEquals(123, intSchemaHelper.unmarshal(Byte.parseByte("123"))); + assertEquals(123, intSchemaHelper.unmarshal(Short.parseShort("123"))); + assertEquals(123, intSchemaHelper.unmarshal(Long.parseLong("123"))); + assertEquals(123, intSchemaHelper.unmarshal(Float.parseFloat("123"))); + assertEquals(123, intSchemaHelper.unmarshal(Double.parseDouble("123"))); + } + + @Test + public void testJavaSchemaHelperUnmarshal() { + AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1"); + AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1"); + + AxContextSchema byteSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Byte"); + AxContextSchema shortSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Short"); + AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer"); + AxContextSchema longSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Long"); + AxContextSchema floatSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Float"); + AxContextSchema doubleSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Double"); + AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String"); + AxContextSchema myBaseClassSchema = new AxContextSchema(schemaKey, "Java", + "org.onap.policy.apex.context.impl.schema.java.MyBaseClass"); + + SchemaHelper byteSchemaHelper = new JavaSchemaHelper(); + SchemaHelper shortSchemaHelper = new JavaSchemaHelper(); + SchemaHelper intSchemaHelper = new JavaSchemaHelper(); + SchemaHelper longSchemaHelper = new JavaSchemaHelper(); + SchemaHelper floatSchemaHelper = new JavaSchemaHelper(); + SchemaHelper doubleSchemaHelper = new JavaSchemaHelper(); + SchemaHelper stringSchemaHelper = new JavaSchemaHelper(); + SchemaHelper myBaseClassSchemaHelper = new JavaSchemaHelper(); + + byteSchemaHelper.init(userKey, byteSchema); + shortSchemaHelper.init(userKey, shortSchema); + intSchemaHelper.init(userKey, intSchema); + longSchemaHelper.init(userKey, longSchema); + floatSchemaHelper.init(userKey, floatSchema); + doubleSchemaHelper.init(userKey, doubleSchema); + stringSchemaHelper.init(userKey, stringSchema); + myBaseClassSchemaHelper.init(userKey, myBaseClassSchema); + + assertEquals(null, byteSchemaHelper.unmarshal(null)); + assertEquals(new Byte("123"), byteSchemaHelper.unmarshal("123")); + assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Integer.parseInt("123"))); + assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Byte.parseByte("123"))); + assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Short.parseShort("123"))); + assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Long.parseLong("123"))); + assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Float.parseFloat("123"))); + assertEquals(new Byte("123"), byteSchemaHelper.unmarshal(Double.parseDouble("123"))); + try { + byteSchemaHelper.unmarshal("one two three"); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not " + + "compatible with class \"java.lang.Byte\"", e.getMessage()); + } + + assertEquals(null, shortSchemaHelper.unmarshal(null)); + assertEquals(new Short("123"), shortSchemaHelper.unmarshal("123")); + assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Integer.parseInt("123"))); + assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Byte.parseByte("123"))); + assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Short.parseShort("123"))); + assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Long.parseLong("123"))); + assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Float.parseFloat("123"))); + assertEquals(new Short("123"), shortSchemaHelper.unmarshal(Double.parseDouble("123"))); + try { + shortSchemaHelper.unmarshal("one two three"); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not " + + "compatible with class \"java.lang.Short\"", e.getMessage()); + } + + assertEquals(null, intSchemaHelper.unmarshal(null)); + assertEquals(123, intSchemaHelper.unmarshal("123")); + assertEquals(123, intSchemaHelper.unmarshal(Integer.parseInt("123"))); + assertEquals(123, intSchemaHelper.unmarshal(Byte.parseByte("123"))); + assertEquals(123, intSchemaHelper.unmarshal(Short.parseShort("123"))); + assertEquals(123, intSchemaHelper.unmarshal(Long.parseLong("123"))); + assertEquals(123, intSchemaHelper.unmarshal(Float.parseFloat("123"))); + assertEquals(123, intSchemaHelper.unmarshal(Double.parseDouble("123"))); + try { + intSchemaHelper.unmarshal("one two three"); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not " + + "compatible with class \"java.lang.Integer\"", e.getMessage()); + } + + assertEquals(null, longSchemaHelper.unmarshal(null)); + assertEquals(123L, longSchemaHelper.unmarshal("123")); + assertEquals(123L, longSchemaHelper.unmarshal(Integer.parseInt("123"))); + assertEquals(123L, longSchemaHelper.unmarshal(Byte.parseByte("123"))); + assertEquals(123L, longSchemaHelper.unmarshal(Short.parseShort("123"))); + assertEquals(123L, longSchemaHelper.unmarshal(Long.parseLong("123"))); + assertEquals(123L, longSchemaHelper.unmarshal(Float.parseFloat("123"))); + assertEquals(123L, longSchemaHelper.unmarshal(Double.parseDouble("123"))); + try { + longSchemaHelper.unmarshal("one two three"); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not " + + "compatible with class \"java.lang.Long\"", e.getMessage()); + } + + assertEquals(null, floatSchemaHelper.unmarshal(null)); + assertEquals(new Float("123"), floatSchemaHelper.unmarshal("123")); + assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Integer.parseInt("123"))); + assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Byte.parseByte("123"))); + assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Short.parseShort("123"))); + assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Long.parseLong("123"))); + assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Float.parseFloat("123"))); + assertEquals(new Float("123"), floatSchemaHelper.unmarshal(Double.parseDouble("123"))); + try { + floatSchemaHelper.unmarshal("one two three"); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not " + + "compatible with class \"java.lang.Float\"", e.getMessage()); + } + + assertEquals(null, doubleSchemaHelper.unmarshal(null)); + assertEquals(new Double("123"), doubleSchemaHelper.unmarshal("123")); + assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Integer.parseInt("123"))); + assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Byte.parseByte("123"))); + assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Short.parseShort("123"))); + assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Long.parseLong("123"))); + assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Float.parseFloat("123"))); + assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(Double.parseDouble("123"))); + assertEquals(new Double("123"), doubleSchemaHelper.unmarshal(BigDecimal.valueOf(123))); + try { + doubleSchemaHelper.unmarshal("one two three"); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not " + + "compatible with class \"java.lang.Double\"", e.getMessage()); + } + + assertEquals("123", stringSchemaHelper.unmarshal(123)); + + MySubClass subClassInstance = new MySubClass("123"); + assertEquals(subClassInstance, myBaseClassSchemaHelper.unmarshal(subClassInstance)); + } + + @Test + public void testJavaSchemaHelperMarshal() { + AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1"); + AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1"); + + AxContextSchema intSchema = new AxContextSchema(schemaKey, "Java", "java.lang.Integer"); + SchemaHelper intSchemaHelper = new JavaSchemaHelper(); + intSchemaHelper.init(userKey, intSchema); + + assertEquals("null", intSchemaHelper.marshal2String(null)); + assertEquals("123", intSchemaHelper.marshal2String(123)); + try { + intSchemaHelper.marshal2String(123.45); + fail("test should throw an exception here"); + } catch (ContextRuntimeException e) { + assertEquals("UserKey:0.0.1: object \"123.45\" of class \"java.lang.Double\" not " + + "compatible with class \"java.lang.Integer\"", e.getMessage()); + } + + JsonPrimitive intJsonPrimitive = (JsonPrimitive) intSchemaHelper.marshal2Object(123); + assertEquals(123, intJsonPrimitive.getAsInt()); + } +} diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/MyBaseClass.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/MyBaseClass.java new file mode 100644 index 000000000..a27c64fff --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/MyBaseClass.java @@ -0,0 +1,32 @@ +/*- + * ============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.context.impl.schema.java; + +public class MyBaseClass { + final String stringField; + + public MyBaseClass(final String stringField) { + this.stringField = stringField; + } + + public String getStringField() { + return stringField; + } +} diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/MySubClass.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/MySubClass.java new file mode 100644 index 000000000..2f837a379 --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/MySubClass.java @@ -0,0 +1,26 @@ +/*- + * ============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.context.impl.schema.java; + +public class MySubClass extends MyBaseClass { + public MySubClass(String stringField) { + super(stringField); + } +} diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/TestInstanceCreation.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/TestInstanceCreation.java deleted file mode 100644 index 9c2c685c3..000000000 --- a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/TestInstanceCreation.java +++ /dev/null @@ -1,96 +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.context.impl.schema.java; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -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.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; - -/** - * @author Liam Fallon (liam.fallon@ericsson.com) - * @version - */ -public class TestInstanceCreation { - private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); - private AxContextSchemas schemas; - - @Before - public void initTest() { - schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); - ModelService.registerModel(AxContextSchemas.class, schemas); - new SchemaParameters(); - } - - @Test - public void testNullEncoding() { - final AxContextSchema javaBooleanSchema = - new AxContextSchema(new AxArtifactKey("Boolean", "0.0.1"), "Java", "java.lang.Boolean"); - final AxContextSchema javaLongSchema = - new AxContextSchema(new AxArtifactKey("Long", "0.0.1"), "Java", "java.lang.Long"); - final AxContextSchema javaStringSchema = - new AxContextSchema(new AxArtifactKey("String", "0.0.1"), "Java", "java.lang.String"); - - schemas.getSchemasMap().put(javaBooleanSchema.getKey(), javaBooleanSchema); - schemas.getSchemasMap().put(javaLongSchema.getKey(), javaLongSchema); - schemas.getSchemasMap().put(javaStringSchema.getKey(), javaStringSchema); - - final SchemaHelper schemaHelper0 = - new SchemaHelperFactory().createSchemaHelper(testKey, javaBooleanSchema.getKey()); - final SchemaHelper schemaHelper1 = - new SchemaHelperFactory().createSchemaHelper(testKey, javaLongSchema.getKey()); - final SchemaHelper schemaHelper2 = - new SchemaHelperFactory().createSchemaHelper(testKey, javaStringSchema.getKey()); - - try { - schemaHelper0.createNewInstance(); - fail("this 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, schemaHelper0.createNewInstance("true")); - - - try { - schemaHelper1.createNewInstance(); - fail("this 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(65536L, schemaHelper1.createNewInstance("65536")); - - assertEquals("", schemaHelper2.createNewInstance()); - assertEquals("true", schemaHelper2.createNewInstance("true")); - } -} -- cgit 1.2.3-korg