From 70e614910a05b3588902a779e996a9c695c4b3d9 Mon Sep 17 00:00:00 2001 From: "adheli.tavares" Date: Tue, 11 Jun 2024 11:00:03 +0100 Subject: Convert junit4 to junit5 Issue-ID: POLICY-5041 Change-Id: I6bad59c375dffdc87c31730057ce7b0543754545 Signed-off-by: adheli.tavares --- .../policy/apex/context/ContextExceptionTest.java | 17 +- .../apex/context/impl/ContextAlbumImplTest.java | 168 ++++++++-------- .../apex/context/impl/DummyContextAlbumImpl.java | 71 ++++--- .../impl/schema/SchemaHelperFactoryTest.java | 24 +-- .../java/JavaSchemaHelperInstanceCreationTest.java | 38 ++-- .../java/JavaSchemaHelperParametersTest.java | 19 +- .../impl/schema/java/JavaSchemaHelperTest.java | 211 ++++++++++++--------- 7 files changed, 288 insertions(+), 260 deletions(-) (limited to 'context/context-management/src/test/java/org/onap') diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/ContextExceptionTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/ContextExceptionTest.java index ff07b1e50..19f695cda 100644 --- a/context/context-management/src/test/java/org/onap/policy/apex/context/ContextExceptionTest.java +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/ContextExceptionTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,25 +21,25 @@ package org.onap.policy.apex.context; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; -import org.junit.Test; +import org.junit.jupiter.api.Test; -public class ContextExceptionTest { +class ContextExceptionTest { @Test - public void testContextException() { + void testContextException() { assertEquals("context exception message", new ContextException("context exception message").getMessage()); assertEquals("context exception message", - new ContextException("context exception message", new IOException()).getMessage()); + new ContextException("context exception message", new IOException()).getMessage()); } @Test - public void testContextRuntimeException() { + void testContextRuntimeException() { assertEquals("context exception message", - new ContextRuntimeException("context exception message").getMessage()); + new ContextRuntimeException("context exception message").getMessage()); assertEquals("context exception message", - new ContextRuntimeException("context exception message", new IOException()).getMessage()); + new ContextRuntimeException("context exception message", new IOException()).getMessage()); } } diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java index 6ebb79f43..216e3686a 100644 --- a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2020, 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,15 +22,17 @@ package org.onap.policy.apex.context.impl; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.LinkedHashMap; import java.util.Map; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.context.ContextAlbum; import org.onap.policy.apex.context.ContextException; import org.onap.policy.apex.context.Distributor; @@ -49,11 +51,11 @@ 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; -public class ContextAlbumImplTest { +class ContextAlbumImplTest { /** - * Set ups everything for the test. + * Set-ups everything for the test. */ - @BeforeClass + @BeforeAll public static void prepareForTest() { final ContextParameters contextParameters = new ContextParameters(); contextParameters.getLockManagerParameters() @@ -79,7 +81,7 @@ public class ContextAlbumImplTest { /** * Clear down the test data. */ - @AfterClass + @AfterAll public static void cleanUpAfterTest() { ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); @@ -90,7 +92,7 @@ public class ContextAlbumImplTest { } @Test - public void testNullsOnConstructor() { + void testNullsOnConstructor() { assertThatThrownBy(() -> new ContextAlbumImpl(null, null, null)) .hasMessage("Context album definition may not be null"); @@ -101,13 +103,13 @@ public class ContextAlbumImplTest { .hasMessage("Album map may not be null"); assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), - new LinkedHashMap())) - .hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas " - + "not found in model service"); + new LinkedHashMap<>())) + .hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas " + + "not found in model service"); } @Test - public void testAlbumInterface() throws ContextException { + void testAlbumInterface() throws ContextException { AxContextSchemas schemas = new AxContextSchemas(); AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), "JAVA", "java.lang.String"); @@ -118,39 +120,25 @@ public class ContextAlbumImplTest { true, AxArtifactKey.getNullKey()); assertThatThrownBy( - () -> new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(), new LinkedHashMap())) - .hasMessageContaining("could not initiate schema management for context album AxContextAlbum"); + () -> new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(), new LinkedHashMap<>())) + .hasMessageContaining("could not initiate schema management for context album AxContextAlbum"); axContextAlbum.setItemSchema(simpleStringSchema.getKey()); Distributor distributor = new JvmLocalDistributor(); distributor.init(axContextAlbum.getKey()); - ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap()); + ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<>()); - assertEquals("TestContextAlbum", album.getName()); - assertEquals("TestContextAlbum:0.0.1", album.getKey().getId()); - assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getId()); - assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getId()); + assertGetValuesFromContextAlbum(album); assertThatThrownBy(() -> album.containsKey(null)) .hasMessage("null values are illegal on method parameter \"key\""); - assertEquals(false, album.containsKey("Key0")); - - assertThatThrownBy(() -> album.containsValue(null)) - .hasMessage("null values are illegal on method parameter \"value\""); - assertEquals(false, album.containsValue("some value")); - - assertThatThrownBy(() -> album.get(null)) - .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()"); + assertFalse(album.containsKey("Key0")); - assertThatThrownBy(() -> album.put(null, null)) - .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()"); - - assertThatThrownBy(() -> album.put("KeyNull", null)) - .hasMessage("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\"" + " for put()"); + assertUsingNullValues_RaiseExceptions(album); AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy", false, simpleStringSchema.getKey()); - ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap()); + ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<>()); assertThatThrownBy(() -> albumRo.put("KeyReadOnly", "A value for a Read Only Album")) .hasMessage("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums " @@ -173,52 +161,25 @@ public class ContextAlbumImplTest { assertThatThrownBy(albumRo::clear) .hasMessage("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums"); - // The following locking tests pass because the locking protects access to Key0 - // across all - // copies of the distributed album whether the key exists or not - album.lockForReading("Key0"); - assertEquals(null, album.get("Key0")); - album.unlockForReading("Key0"); - assertEquals(null, album.get("Key0")); - - album.lockForWriting("Key0"); - assertEquals(null, album.get("Key0")); - album.unlockForWriting("Key0"); - assertEquals(null, album.get("Key0")); - - // Test write access, trivial test because Integration Test does - // a full test of access locking over albums in different JVMs - album.lockForWriting("Key0"); - assertEquals(null, album.get("Key0")); - album.put("Key0", "value of Key0"); - assertEquals("value of Key0", album.get("Key0")); - album.unlockForWriting("Key0"); - assertEquals("value of Key0", album.get("Key0")); - - // Test read access, trivial test because Integration Test does - // a full test of access locking over albums in different JVMs - album.lockForReading("Key0"); - assertEquals("value of Key0", album.get("Key0")); - album.unlockForReading("Key0"); + assertLockingUnlocking(album); AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1"); AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState"); - AxConcept[] userArtifactStack = { somePolicyKey, somePolicyState }; + AxConcept[] userArtifactStack = {somePolicyKey, somePolicyState}; album.setUserArtifactStack(userArtifactStack); assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getId()); assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getId()); - assertEquals(true, album.keySet().contains("Key0")); - assertEquals(true, album.values().contains("value of Key0")); + assertTrue(album.containsKey("Key0")); + assertTrue(album.containsValue("value of Key0")); assertEquals(1, album.entrySet().size()); // The flush() operation fails because the distributor is not initialized with - // the album which - // is fine for unit test + // the album which is fine for unit test assertThatThrownBy(album::flush).hasMessage("map flush failed, supplied map is null"); assertEquals(1, album.size()); - assertEquals(false, album.isEmpty()); + assertFalse(album.isEmpty()); album.put("Key0", "New value of Key0"); assertEquals("New value of Key0", album.get("Key0")); @@ -231,13 +192,64 @@ public class ContextAlbumImplTest { assertEquals("New value of Key0", album.remove("Key0")); album.clear(); - assertTrue(album.isEmpty()); ModelService.clear(); } + private static void assertUsingNullValues_RaiseExceptions(ContextAlbum album) { + assertThatThrownBy(() -> album.containsValue(null)) + .hasMessage("null values are illegal on method parameter \"value\""); + assertFalse(album.containsValue("some value")); + + assertThatThrownBy(() -> album.get(null)) + .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()"); + + assertThatThrownBy(() -> album.put(null, null)) + .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()"); + + assertThatThrownBy(() -> album.put("KeyNull", null)) + .hasMessage("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\"" + " for put()"); + } + + private static void assertGetValuesFromContextAlbum(ContextAlbum album) { + assertEquals("TestContextAlbum", album.getName()); + assertEquals("TestContextAlbum:0.0.1", album.getKey().getId()); + assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getId()); + assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getId()); + } + + private static void assertLockingUnlocking(ContextAlbum album) throws ContextException { + // The following locking tests pass because the locking protects access to Key0 + // across all + // copies of the distributed album whether the key exists or not + album.lockForReading("Key0"); + assertNull(album.get("Key0")); + album.unlockForReading("Key0"); + assertNull(album.get("Key0")); + + album.lockForWriting("Key0"); + assertNull(album.get("Key0")); + album.unlockForWriting("Key0"); + assertNull(album.get("Key0")); + + // Test write access, trivial test because Integration Test does + // a full test of access locking over albums in different JVMs + album.lockForWriting("Key0"); + assertNull(album.get("Key0")); + album.put("Key0", "value of Key0"); + assertEquals("value of Key0", album.get("Key0")); + album.unlockForWriting("Key0"); + assertEquals("value of Key0", album.get("Key0")); + + // Test read access, trivial test because Integration Test does + // a full test of access locking over albums in different JVMs + album.lockForReading("Key0"); + assertEquals("value of Key0", album.get("Key0")); + album.unlockForReading("Key0"); + } + @Test - public void testCompareToEqualsHash() throws ContextException { + void testCompareToEqualsHash() throws ContextException { AxContextSchemas schemas = new AxContextSchemas(); AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", "java.lang.Integer"); @@ -253,26 +265,22 @@ public class ContextAlbumImplTest { axContextAlbum.setItemSchema(simpleIntSchema.getKey()); Distributor distributor = new JvmLocalDistributor(); distributor.init(axContextAlbum.getKey()); - ContextAlbumImpl album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap()); + ContextAlbumImpl album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<>()); assertNotEquals(0, album.hashCode()); - assertEquals(0, album.compareTo(album)); assertEquals(1, album.compareTo(null)); - assertEquals(album, album); assertNotEquals(album, new DummyContextAlbumImpl()); - ContextAlbumImpl otherAlbum = new ContextAlbumImpl(axContextAlbum, distributor, - new LinkedHashMap()); + ContextAlbumImpl otherAlbum = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<>()); assertEquals(album, otherAlbum); otherAlbum.put("Key", 123); assertNotEquals(album, otherAlbum); assertThatThrownBy(() -> { - ContextAlbumImpl otherAlbumBad = new ContextAlbumImpl(axContextAlbum, distributor, - new LinkedHashMap()); + ContextAlbumImpl otherAlbumBad = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<>()); otherAlbumBad.put("Key", "BadValue"); }).hasMessage("Failed to set context value for key \"Key\" in album \"TestContextAlbum:0.0.1\": " + "TestContextAlbum:0.0.1: object \"BadValue\" of class \"java.lang.String\" " @@ -281,7 +289,7 @@ public class ContextAlbumImplTest { "Policy", true, AxArtifactKey.getNullKey()); otherAxContextAlbum.setItemSchema(simpleStringSchema.getKey()); - otherAlbum = new ContextAlbumImpl(otherAxContextAlbum, distributor, new LinkedHashMap()); + otherAlbum = new ContextAlbumImpl(otherAxContextAlbum, distributor, new LinkedHashMap<>()); assertNotEquals(album, otherAlbum); assertThatThrownBy(album::flush).hasMessage("map flush failed, supplied map is null"); diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/DummyContextAlbumImpl.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/DummyContextAlbumImpl.java index 20398eb1a..6f7cd9fcb 100644 --- a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/DummyContextAlbumImpl.java +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/DummyContextAlbumImpl.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * 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========================================================= */ @@ -33,7 +34,6 @@ import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; /** * Dummy album implementation class. - * */ public class DummyContextAlbumImpl implements ContextAlbum { @@ -42,7 +42,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public void clear() { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -50,7 +50,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public boolean containsKey(Object key) { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -58,7 +58,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public boolean containsValue(Object value) { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -66,7 +66,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public Set> entrySet() { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -74,7 +74,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public Object get(Object key) { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -82,7 +82,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public boolean isEmpty() { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -90,7 +90,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public Set keySet() { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -98,7 +98,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public Object put(String key, Object value) { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -106,8 +106,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public void putAll(Map map) { - throw new NotImplementedException("Not Implemeted on dummy class"); - + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -115,7 +114,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public Object remove(Object key) { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -123,7 +122,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public int size() { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -131,7 +130,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public Collection values() { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -139,7 +138,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public AxArtifactKey getKey() { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -147,7 +146,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public String getName() { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -155,7 +154,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public AxContextAlbum getAlbumDefinition() { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -163,43 +162,39 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public SchemaHelper getSchemaHelper() { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** * {@inheritDoc}. */ @Override - public void lockForReading(String key) throws ContextException { - throw new NotImplementedException("Not Implemeted on dummy class"); - + public void lockForReading(String key) { + throw new NotImplementedException("Not Implemented on dummy class"); } /** * {@inheritDoc}. */ @Override - public void lockForWriting(String key) throws ContextException { - throw new NotImplementedException("Not Implemeted on dummy class"); - + public void lockForWriting(String key) { + throw new NotImplementedException("Not Implemented on dummy class"); } /** * {@inheritDoc}. */ @Override - public void unlockForReading(String key) throws ContextException { - throw new NotImplementedException("Not Implemeted on dummy class"); - + public void unlockForReading(String key) { + throw new NotImplementedException("Not Implemented on dummy class"); } /** * {@inheritDoc}. */ @Override - public void unlockForWriting(String key) throws ContextException { - throw new NotImplementedException("Not Implemeted on dummy class"); - + public void unlockForWriting(String key) { + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -207,7 +202,7 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public AxConcept[] getUserArtifactStack() { - throw new NotImplementedException("Not Implemeted on dummy class"); + throw new NotImplementedException("Not Implemented on dummy class"); } /** @@ -215,17 +210,15 @@ public class DummyContextAlbumImpl implements ContextAlbum { */ @Override public void setUserArtifactStack(AxConcept[] userArtifactStack) { - throw new NotImplementedException("Not Implemeted on dummy class"); - + throw new NotImplementedException("Not Implemented on dummy class"); } /** * {@inheritDoc}. */ @Override - public void flush() throws ContextException { - throw new NotImplementedException("Not Implemeted on dummy class"); - + public void flush() { + throw new NotImplementedException("Not Implemented on dummy 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 index 663424411..501b76c0a 100644 --- 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 @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation + * Modifications Copyright (C) 2020, 2024 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,11 +22,11 @@ package org.onap.policy.apex.context.impl.schema; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; import org.onap.policy.apex.context.parameters.ContextParameterConstants; import org.onap.policy.apex.context.parameters.SchemaParameters; @@ -36,16 +36,16 @@ 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; -public class SchemaHelperFactoryTest { +class SchemaHelperFactoryTest { private static AxContextSchema intSchema; private static AxContextSchemas schemas; private static AxContextSchema badSchema; /** - * Set ups schema for the test. + * Set-ups schema for the test. */ - @BeforeClass - public static void setupSchema() { + @BeforeAll + static void setupSchema() { schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); ModelService.registerModel(AxContextSchemas.class, schemas); @@ -53,13 +53,13 @@ public class SchemaHelperFactoryTest { badSchema = new AxContextSchema(new AxArtifactKey("IntSchema", "0.0.1"), "JAVA", "java.lang.Bad"); } - @AfterClass - public static void clearParameters() { + @AfterAll + static void clearParameters() { ParameterService.clear(); } @Test - public void testSchemaHelperFactory() { + void testSchemaHelperFactory() { assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(null, null)) .hasMessage("Parameter \"owningEntityKey\" may not be null"); AxArtifactKey ownerKey = new AxArtifactKey("Owner", "0.0.1"); 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 index 620a78152..f4c8c3e2d 100644 --- 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 @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation + * Modifications Copyright (C) 2020, 2024 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,11 +22,11 @@ package org.onap.policy.apex.context.impl.schema.java; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.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; @@ -40,17 +40,17 @@ import org.onap.policy.common.parameters.ParameterService; /** * JavaSchemaHelperInstanceCreationTest. + * * @author Liam Fallon (liam.fallon@ericsson.com) - * @version */ -public class JavaSchemaHelperInstanceCreationTest { +class JavaSchemaHelperInstanceCreationTest { private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); private AxContextSchemas schemas; /** - * Set ups everything for the test. + * Set-ups everything for the test. */ - @Before + @BeforeEach public void initTest() { schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); ModelService.registerModel(AxContextSchemas.class, schemas); @@ -62,40 +62,40 @@ public class JavaSchemaHelperInstanceCreationTest { ParameterService.register(schemaParameters); } - @AfterClass + @AfterAll public static void cleanUpAfterTest() { ParameterService.clear(); } @Test - public void testNullEncoding() { + void testNullEncoding() { final AxContextSchema javaBooleanSchema = - new AxContextSchema(new AxArtifactKey("Boolean", "0.0.1"), "Java", "java.lang.Boolean"); + 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"); + 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"); + 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()); + new SchemaHelperFactory().createSchemaHelper(testKey, javaBooleanSchema.getKey()); final SchemaHelper schemaHelper1 = - new SchemaHelperFactory().createSchemaHelper(testKey, javaLongSchema.getKey()); + new SchemaHelperFactory().createSchemaHelper(testKey, javaLongSchema.getKey()); final SchemaHelper schemaHelper2 = - new SchemaHelperFactory().createSchemaHelper(testKey, javaStringSchema.getKey()); + new SchemaHelperFactory().createSchemaHelper(testKey, javaStringSchema.getKey()); assertThatThrownBy(schemaHelper0::createNewInstance) .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\"" - + " using the default constructor \"Boolean()\""); + + " using the default constructor \"Boolean()\""); assertEquals(true, schemaHelper0.createNewInstance("true")); assertThatThrownBy(schemaHelper1::createNewInstance) .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\"" - + " using the default constructor \"Long()\""); + + " using the default constructor \"Long()\""); assertEquals(65536L, schemaHelper1.createNewInstance("65536")); assertEquals("", schemaHelper2.createNewInstance()); diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperParametersTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperParametersTest.java index 65b2ed6ff..418149e50 100644 --- a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperParametersTest.java +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperParametersTest.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,26 +22,26 @@ package org.onap.policy.apex.context.impl.schema.java; -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 static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.LinkedHashMap; import java.util.Map; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Test Java schema helper parameters. */ -public class JavaSchemaHelperParametersTest { +class JavaSchemaHelperParametersTest { @Test - public void testJavaSchemaHelperParameters() { + void testJavaSchemaHelperParameters() { JavaSchemaHelperParameters pars = new JavaSchemaHelperParameters(); assertEquals("org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelper", - pars.getSchemaHelperPluginClass()); + pars.getSchemaHelperPluginClass()); assertEquals(0, pars.getJsonAdapters().size()); @@ -84,7 +85,7 @@ public class JavaSchemaHelperParametersTest { } @Test - public void testJavaSchemaHelperJsonAdapterParameters() { + void testJavaSchemaHelperJsonAdapterParameters() { JavaSchemaHelperJsonAdapterParameters pars = new JavaSchemaHelperJsonAdapterParameters(); assertNull(pars.getName()); 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 index d06ac7469..773b6d84c 100644 --- 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 @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2020, 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,16 +22,17 @@ package org.onap.policy.apex.context.impl.schema.java; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; import java.math.BigDecimal; import java.time.Instant; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.context.SchemaHelper; import org.onap.policy.apex.context.parameters.ContextParameterConstants; import org.onap.policy.apex.context.parameters.SchemaParameters; @@ -39,11 +40,11 @@ import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; import org.onap.policy.common.parameters.ParameterService; -public class JavaSchemaHelperTest { +class JavaSchemaHelperTest { /** * Initialize JSON adapters. */ - @BeforeClass + @BeforeAll public static void registerParameters() { JavaSchemaHelperParameters javaSchemaHelperPars = new JavaSchemaHelperParameters(); @@ -59,13 +60,13 @@ public class JavaSchemaHelperTest { ParameterService.register(schemaPars); } - @AfterClass + @AfterAll public static void deregisterParameters() { ParameterService.clear(); } @Test - public void testJavaSchemaHelperInit() { + void testJavaSchemaHelperInit() { AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1"); AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1"); @@ -73,23 +74,23 @@ public class JavaSchemaHelperTest { assertThatThrownBy(() -> new JavaSchemaHelper().init(userKey, badJavaTypeSchema)) .hasMessage("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"); + + " \"SchemaKey:0.0.1\" not found. Check the class path of the JVM"); AxContextSchema builtInJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "short"); assertThatThrownBy(() -> new JavaSchemaHelper().init(userKey, builtInJavaTypeSchema)) .hasMessage("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."); + + "\"SchemaKey:0.0.1\" not found. Primitive types are not supported." + + " Use the appropriate Java boxing type instead."); } @Test - public void testJavaSchemaHelperMethods() { + void testJavaSchemaHelperMethods() { SchemaHelper intSchemaHelper = new JavaSchemaHelper(); assertEquals(AxArtifactKey.getNullKey(), intSchemaHelper.getUserKey()); - assertEquals(null, intSchemaHelper.getSchema()); - assertEquals(null, intSchemaHelper.getSchemaClass()); - assertEquals(null, intSchemaHelper.getSchemaObject()); + assertNull(intSchemaHelper.getSchema()); + assertNull(intSchemaHelper.getSchemaClass()); + assertNull(intSchemaHelper.getSchemaObject()); assertThatThrownBy(intSchemaHelper::createNewInstance) .hasMessage("NULL:0.0.0: could not create an instance, schema class for the schema is null"); @@ -105,27 +106,27 @@ public class JavaSchemaHelperTest { assertEquals(userKey, intSchemaHelper.getUserKey()); assertEquals(intSchema, intSchemaHelper.getSchema()); assertEquals(Integer.class, intSchemaHelper.getSchemaClass()); - assertEquals(null, intSchemaHelper.getSchemaObject()); + assertNull(intSchemaHelper.getSchemaObject()); assertThatThrownBy(intSchemaHelper::createNewInstance) .hasMessage("UserKey:0.0.1: could not create an instance of class " - + "\"java.lang.Integer\" using the default constructor \"Integer()\""); + + "\"java.lang.Integer\" using the default constructor \"Integer()\""); assertThatThrownBy(() -> intSchemaHelper.createNewInstance(Float.parseFloat("1.23"))) .hasMessage("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\""); + + "\"java.lang.Float\" is not an instance of JsonObject and is not " + + "assignable to \"java.lang.Integer\""); assertThatThrownBy(() -> intSchemaHelper.createNewInstance("hello")) .hasMessage("UserKey:0.0.1: could not create an instance of class \"java.lang.Integer\" " - + "using the string constructor \"Integer(String)\""); + + "using the string constructor \"Integer(String)\""); JsonElement jsonIntElement = null; - assertEquals(null, intSchemaHelper.createNewInstance(jsonIntElement)); + assertNull(intSchemaHelper.createNewInstance(jsonIntElement)); jsonIntElement = JsonParser.parseString("123"); assertEquals(123, intSchemaHelper.createNewInstance(jsonIntElement)); assertEquals(123, intSchemaHelper.createNewInstance(Integer.parseInt("123"))); - assertEquals(null, intSchemaHelper.unmarshal(null)); + assertNull(intSchemaHelper.unmarshal(null)); assertEquals(123, intSchemaHelper.unmarshal(Integer.parseInt("123"))); assertEquals(123, intSchemaHelper.unmarshal(Byte.parseByte("123"))); assertEquals(123, intSchemaHelper.unmarshal(Short.parseShort("123"))); @@ -135,7 +136,7 @@ public class JavaSchemaHelperTest { } @Test - public void testJavaSchemaHelperUnmarshal() { + void testJavaSchemaHelperUnmarshal() { AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1"); AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1"); @@ -167,44 +168,53 @@ public class JavaSchemaHelperTest { stringSchemaHelper.init(userKey, stringSchema); AxContextSchema myBaseClassSchema = new AxContextSchema(schemaKey, "Java", - "org.onap.policy.apex.context.impl.schema.java.SupportBaseClass"); + "org.onap.policy.apex.context.impl.schema.java.SupportBaseClass"); SchemaHelper myBaseClassSchemaHelper = new JavaSchemaHelper(); myBaseClassSchemaHelper.init(userKey, myBaseClassSchema); - assertEquals(null, byteSchemaHelper.unmarshal(null)); - assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal("123")); - assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Integer.parseInt("123"))); - assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Byte.parseByte("123"))); - assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Short.parseShort("123"))); - assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Long.parseLong("123"))); - assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Float.parseFloat("123"))); - assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Double.parseDouble("123"))); - assertThatThrownBy(() -> byteSchemaHelper.unmarshal("one two three")) - .hasMessage("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not " - + "compatible with class \"java.lang.Byte\""); - assertEquals(null, shortSchemaHelper.unmarshal(null)); - assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal("123")); - assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Integer.parseInt("123"))); - assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Byte.parseByte("123"))); - assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Short.parseShort("123"))); - assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Long.parseLong("123"))); - assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Float.parseFloat("123"))); - assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Double.parseDouble("123"))); - assertThatThrownBy(() -> shortSchemaHelper.unmarshal("one two three")) + assertByteUnmarshal(byteSchemaHelper); + assertShortUnmarshal(shortSchemaHelper); + assertIntUnmarshal(intSchemaHelper); + assertLongUnmarshal(longSchemaHelper); + assertFloatUnmarshal(floatSchemaHelper); + assertDoubleUnmarshal(doubleSchemaHelper); + assertEquals("123", stringSchemaHelper.unmarshal(123)); + + SupportSubClass subClassInstance = new SupportSubClass("123"); + assertEquals(subClassInstance, myBaseClassSchemaHelper.unmarshal(subClassInstance)); + } + + private static void assertDoubleUnmarshal(SchemaHelper doubleSchemaHelper) { + assertNull(doubleSchemaHelper.unmarshal(null)); + assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal("123")); + assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Integer.parseInt("123"))); + assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Byte.parseByte("123"))); + assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Short.parseShort("123"))); + assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Long.parseLong("123"))); + assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Float.parseFloat("123"))); + assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Double.parseDouble("123"))); + assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(BigDecimal.valueOf(123))); + assertThatThrownBy(() -> doubleSchemaHelper.unmarshal("one two three")) .hasMessage("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not " - + "compatible with class \"java.lang.Short\""); - 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"))); - assertThatThrownBy(() -> intSchemaHelper.unmarshal("one two three")) + + "compatible with class \"java.lang.Double\""); + } + + private static void assertFloatUnmarshal(SchemaHelper floatSchemaHelper) { + assertNull(floatSchemaHelper.unmarshal(null)); + assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal("123")); + assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Integer.parseInt("123"))); + assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Byte.parseByte("123"))); + assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Short.parseShort("123"))); + assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Long.parseLong("123"))); + assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Float.parseFloat("123"))); + assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Double.parseDouble("123"))); + assertThatThrownBy(() -> floatSchemaHelper.unmarshal("one two three")) .hasMessage("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not " - + "compatible with class \"java.lang.Integer\""); - assertEquals(null, longSchemaHelper.unmarshal(null)); + + "compatible with class \"java.lang.Float\""); + } + + private static void assertLongUnmarshal(SchemaHelper longSchemaHelper) { + assertNull(longSchemaHelper.unmarshal(null)); assertEquals(123L, longSchemaHelper.unmarshal("123")); assertEquals(123L, longSchemaHelper.unmarshal(Integer.parseInt("123"))); assertEquals(123L, longSchemaHelper.unmarshal(Byte.parseByte("123"))); @@ -214,38 +224,53 @@ public class JavaSchemaHelperTest { assertEquals(123L, longSchemaHelper.unmarshal(Double.parseDouble("123"))); assertThatThrownBy(() -> longSchemaHelper.unmarshal("one two three")) .hasMessage("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not " - + "compatible with class \"java.lang.Long\""); - assertEquals(null, floatSchemaHelper.unmarshal(null)); - assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal("123")); - assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Integer.parseInt("123"))); - assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Byte.parseByte("123"))); - assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Short.parseShort("123"))); - assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Long.parseLong("123"))); - assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Float.parseFloat("123"))); - assertEquals(Float.valueOf("123"), floatSchemaHelper.unmarshal(Double.parseDouble("123"))); - assertThatThrownBy(() -> floatSchemaHelper.unmarshal("one two three")) + + "compatible with class \"java.lang.Long\""); + } + + private static void assertIntUnmarshal(SchemaHelper intSchemaHelper) { + assertNull(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"))); + assertThatThrownBy(() -> intSchemaHelper.unmarshal("one two three")) .hasMessage("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not " - + "compatible with class \"java.lang.Float\""); - assertEquals(null, doubleSchemaHelper.unmarshal(null)); - assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal("123")); - assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Integer.parseInt("123"))); - assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Byte.parseByte("123"))); - assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Short.parseShort("123"))); - assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Long.parseLong("123"))); - assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Float.parseFloat("123"))); - assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(Double.parseDouble("123"))); - assertEquals(Double.valueOf("123"), doubleSchemaHelper.unmarshal(BigDecimal.valueOf(123))); - assertThatThrownBy(() -> doubleSchemaHelper.unmarshal("one two three")) + + "compatible with class \"java.lang.Integer\""); + } + + private static void assertShortUnmarshal(SchemaHelper shortSchemaHelper) { + assertNull(shortSchemaHelper.unmarshal(null)); + assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal("123")); + assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Integer.parseInt("123"))); + assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Byte.parseByte("123"))); + assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Short.parseShort("123"))); + assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Long.parseLong("123"))); + assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Float.parseFloat("123"))); + assertEquals(Short.valueOf("123"), shortSchemaHelper.unmarshal(Double.parseDouble("123"))); + assertThatThrownBy(() -> shortSchemaHelper.unmarshal("one two three")) .hasMessage("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not " - + "compatible with class \"java.lang.Double\""); - assertEquals("123", stringSchemaHelper.unmarshal(123)); + + "compatible with class \"java.lang.Short\""); + } - SupportSubClass subClassInstance = new SupportSubClass("123"); - assertEquals(subClassInstance, myBaseClassSchemaHelper.unmarshal(subClassInstance)); + private static void assertByteUnmarshal(SchemaHelper byteSchemaHelper) { + assertNull(byteSchemaHelper.unmarshal(null)); + assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal("123")); + assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Integer.parseInt("123"))); + assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Byte.parseByte("123"))); + assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Short.parseShort("123"))); + assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Long.parseLong("123"))); + assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Float.parseFloat("123"))); + assertEquals(Byte.valueOf("123"), byteSchemaHelper.unmarshal(Double.parseDouble("123"))); + assertThatThrownBy(() -> byteSchemaHelper.unmarshal("one two three")) + .hasMessage("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not " + + "compatible with class \"java.lang.Byte\""); } @Test - public void testJavaSchemaHelperMarshal() { + void testJavaSchemaHelperMarshal() { AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1"); AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1"); @@ -257,13 +282,13 @@ public class JavaSchemaHelperTest { assertEquals("123", intSchemaHelper.marshal2String(123)); assertThatThrownBy(() -> intSchemaHelper.marshal2String(123.45)) .hasMessage("UserKey:0.0.1: object \"123.45\" of class \"java.lang.Double\" not " - + "compatible with class \"java.lang.Integer\""); + + "compatible with class \"java.lang.Integer\""); JsonPrimitive intJsonPrimitive = (JsonPrimitive) intSchemaHelper.marshal2Object(123); assertEquals(123, intJsonPrimitive.getAsInt()); } @Test - public void testJavaSchemaHelperAdapters() { + void testJavaSchemaHelperAdapters() { AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1"); AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1"); @@ -275,22 +300,22 @@ public class JavaSchemaHelperTest { assertEquals("\"Hello\"", stringSchemaHelper.marshal2String("Hello")); assertThatThrownBy(() -> stringSchemaHelper.marshal2String(Instant.ofEpochMilli(1000))) .hasMessage("UserKey:0.0.1: object \"1970-01-01T00:00:01Z\" of class \"java.time.Instant\" " - + "not compatible with class \"java.lang.String\""); + + "not compatible with class \"java.lang.String\""); JsonPrimitive stringJsonPrimitive = (JsonPrimitive) stringSchemaHelper.marshal2Object("Another String"); assertEquals("Another String", stringJsonPrimitive.getAsString()); } @Test - public void testJavaSchemaHelperBadAdapter() { + void testJavaSchemaHelperBadAdapter() { AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1"); AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1"); SchemaParameters pars = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME); JavaSchemaHelperParameters javaShPars = - (JavaSchemaHelperParameters) pars.getSchemaHelperParameterMap().get("Java"); + (JavaSchemaHelperParameters) pars.getSchemaHelperParameterMap().get("Java"); javaShPars.getJsonAdapters().get("String") - .setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.SupportBadJsonAdapter"); + .setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.SupportBadJsonAdapter"); AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String"); SchemaHelper stringSchemaHelper = new JavaSchemaHelper(); @@ -298,16 +323,16 @@ public class JavaSchemaHelperTest { assertThatThrownBy(() -> stringSchemaHelper.marshal2String("Hello")) .hasMessage("UserKey:0.0.1: instantiation of adapter class " - + "\"org.onap.policy.apex.context.impl.schema.java.SupportBadJsonAdapter\" to decode and encode " - + "class \"java.lang.String\" failed: null"); + + "\"org.onap.policy.apex.context.impl.schema.java.SupportBadJsonAdapter\" to decode and encode " + + "class \"java.lang.String\" failed: null"); } @Test - public void testJavaSchemaHelperDefaultAdapter() { + void testJavaSchemaHelperDefaultAdapter() { SchemaParameters pars = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME); JavaSchemaHelperParameters javaShPars = - (JavaSchemaHelperParameters) pars.getSchemaHelperParameterMap().get("Java"); + (JavaSchemaHelperParameters) pars.getSchemaHelperParameterMap().get("Java"); pars.getSchemaHelperParameterMap().clear(); -- cgit 1.2.3-korg