diff options
author | liamfallon <liam.fallon@ericsson.com> | 2018-06-21 16:04:47 +0800 |
---|---|---|
committer | liamfallon <liam.fallon@ericsson.com> | 2018-06-21 16:09:47 +0800 |
commit | 6dd2ac76d046e98104f4314ea0c529961ef3e35e (patch) | |
tree | af0614aa443e26d7bdd5003296229445a7da1f0d /context/context-management | |
parent | bb89ed3ad0765d323b6b026fc5fdd3b4a57fcc31 (diff) |
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 <liam.fallon@ericsson.com>
Diffstat (limited to 'context/context-management')
8 files changed, 482 insertions, 11 deletions
diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/AbstractSchemaHelper.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/AbstractSchemaHelper.java index 83d1b6ba5..8acc03501 100644 --- a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/AbstractSchemaHelper.java +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/AbstractSchemaHelper.java @@ -24,6 +24,7 @@ import java.lang.reflect.Constructor; 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.basicmodel.concepts.AxKey; import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; import org.onap.policy.apex.model.utilities.Assertions; @@ -41,7 +42,7 @@ public abstract class AbstractSchemaHelper implements SchemaHelper { private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractSchemaHelper.class); // The key of the user of this schema helper - private AxKey userKey = null; + private AxKey userKey = AxArtifactKey.getNullKey(); // The schema of this schema helper private AxContextSchema schema = null; diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactory.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactory.java index 7252d3772..54689e3fb 100644 --- a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactory.java +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactory.java @@ -51,8 +51,7 @@ public class SchemaHelperFactory { * @return a lock schema that can handle translation of objects in a particular schema format * @throws ContextRuntimeException the context runtime exception */ - public SchemaHelper createSchemaHelper(final AxKey owningEntityKey, final AxArtifactKey schemaKey) - throws ContextRuntimeException { + public SchemaHelper createSchemaHelper(final AxKey owningEntityKey, final AxArtifactKey schemaKey) { LOGGER.entry("schema helper factory, owningEntityKey=" + owningEntityKey); Assertions.argumentNotNull(owningEntityKey, ContextRuntimeException.class, "Parameter \"owningEntityKey\" may not be null"); @@ -69,12 +68,6 @@ public class SchemaHelperFactory { // Get the schema class using the parameter service final SchemaParameters schemaParameters = ParameterService.getParameters(SchemaParameters.class); - if (schemaParameters == null) { - final String resultString = "context schema parameters \"" + SchemaParameters.class.getCanonicalName() - + "\" not found in parameter service"; - LOGGER.warn(resultString); - throw new ContextRuntimeException(resultString); - } // Get the class for the schema helper from the schema parameters final SchemaHelperParameters schemaHelperParameters = diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java index e5d92395a..a4bfe6978 100644 --- a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java @@ -98,7 +98,18 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { */ @Override public Object createNewInstance(final Object incomingObject) { - if (incomingObject instanceof JsonElement) { + if (incomingObject == null) { + return null; + } + + if (getSchemaClass() == null) { + final String returnString = + getUserKey().getID() + ": could not create an instance, schema class for the schema is null"; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); + } + + if (incomingObject instanceof JsonElement) { final String elementJsonString = new Gson().toJson((JsonElement) incomingObject); return new Gson().fromJson(elementJsonString, this.getSchemaClass()); } @@ -190,6 +201,8 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { if (object instanceof Number) { if (getSchemaClass().isAssignableFrom(Byte.class)) { return ((Number) object).byteValue(); + } else if (getSchemaClass().isAssignableFrom(Short.class)) { + return ((Number) object).shortValue(); } else if (getSchemaClass().isAssignableFrom(Integer.class)) { return ((Number) object).intValue(); } else if (getSchemaClass().isAssignableFrom(Long.class)) { 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/TestInstanceCreation.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperInstanceCreationTest.java index 9c2c685c3..17e5deecf 100644 --- 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/JavaSchemaHelperInstanceCreationTest.java @@ -38,7 +38,7 @@ import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; * @author Liam Fallon (liam.fallon@ericsson.com) * @version */ -public class TestInstanceCreation { +public class JavaSchemaHelperInstanceCreationTest { private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); private AxContextSchemas schemas; 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); + } +} |