From 4752a9ede61f2df4e67ec18f6c53cdcd0c35885f Mon Sep 17 00:00:00 2001 From: waynedunican Date: Tue, 7 Jul 2020 17:51:42 +0100 Subject: Replace try/catch with assertj - apex-pdp Replaced try/catch blocks in apex-pdp with assertj assertions. Part II of changes Issue-ID: POLICY-2451 Change-Id: I29823054fbeb6d1a287d1234859aaf2d4609ef6d Signed-off-by: waynedunican --- .../context/distribution/ContextUpdate.java | 54 +++-------- .../SequentialContextInstantiation.java | 108 ++++++--------------- 2 files changed, 42 insertions(+), 120 deletions(-) (limited to 'testsuites/integration/integration-context-test/src/test/java') diff --git a/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/ContextUpdate.java b/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/ContextUpdate.java index 71968fffe..53c8d1fb8 100644 --- a/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/ContextUpdate.java +++ b/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/ContextUpdate.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 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,14 +21,12 @@ package org.onap.policy.apex.testsuites.integration.context.distribution; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.APEX_DISTRIBUTOR; import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.DATE_CONTEXT_ALBUM; -import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.EXCEPTION_MESSAGE; import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.LONG_CONTEXT_ALBUM; import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.MAP_CONTEXT_ALBUM; import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.TIME_ZONE; @@ -41,7 +39,6 @@ import java.util.Locale; import java.util.Map; import org.onap.policy.apex.context.ContextAlbum; import org.onap.policy.apex.context.ContextException; -import org.onap.policy.apex.context.ContextRuntimeException; import org.onap.policy.apex.context.Distributor; import org.onap.policy.apex.context.impl.distribution.DistributorFactory; import org.onap.policy.apex.context.test.concepts.TestContextDateItem; @@ -89,43 +86,18 @@ public class ContextUpdate { longContextAlbum.put(NUMBER_ZERO, 0); longContextAlbum.put(NUMBER_ZERO, NUMBER_ZERO); - try { - longContextAlbum.put(NUMBER_ZERO, ZERO); - fail(EXCEPTION_MESSAGE); - } catch (final ContextRuntimeException e) { - assertEquals("Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\":" + assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, ZERO)) + .hasMessage("Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\":" + " LongContextAlbum:0.0.1: object \"zero\" of class \"java.lang.String\" not compatible with" - + " class \"java.lang.Long\"", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - - try { - longContextAlbum.put(NUMBER_ZERO, ""); - fail(EXCEPTION_MESSAGE); - } catch (final ContextRuntimeException e) { - assertEquals( - "Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\": LongContextAlbum" - + ":0.0.1: object \"\" of class \"java.lang.String\" not compatible with class \"java.lang.Long\"", - e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - - try { - longContextAlbum.put(NUMBER_ZERO, null); - fail(EXCEPTION_MESSAGE); - } catch (final ContextRuntimeException e) { - assertEquals("album \"LongContextAlbum:0.0.1\" null values are illegal on key \"0\" for put()", - e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - - try { - longContextAlbum.put(null, null); - fail(EXCEPTION_MESSAGE); - } catch (final ContextRuntimeException e) { - assertEquals("album \"LongContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + + " class \"java.lang.Long\""); + assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, "")) + .hasMessage("Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\": " + + "LongContextAlbum:0.0.1: object \"\" of class \"java.lang.String\" not " + + "compatible with class \"java.lang.Long\""); + assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, null)) + .hasMessage("album \"LongContextAlbum:0.0.1\" null values are illegal on key \"0\" for put()"); + assertThatThrownBy(() -> longContextAlbum.put(null, null)) + .hasMessage("album \"LongContextAlbum:0.0.1\" null keys are illegal on keys for put()"); assertNull(dateContextAlbum.put("date0", tciA)); assertTrue(dateContextAlbum.put("date0", tciA).equals(tciA)); diff --git a/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/SequentialContextInstantiation.java b/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/SequentialContextInstantiation.java index 3f690f0b5..287dd6147 100644 --- a/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/SequentialContextInstantiation.java +++ b/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/SequentialContextInstantiation.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 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,16 +21,15 @@ package org.onap.policy.apex.testsuites.integration.context.distribution; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import static org.onap.policy.apex.testsuites.integration.context.factory.TestContextAlbumFactory.createMultiAlbumsContextModel; import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.APEX_DISTRIBUTOR; import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.BYTE_VAL; import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.DATE_CONTEXT_ALBUM; -import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.EXCEPTION_MESSAGE; import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.FLOAT_VAL; import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.INT_VAL; import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.LONG_VAL; @@ -115,15 +114,10 @@ public class SequentialContextInstantiation { final TestContextDateTzItem tci9 = getTestContextDateTzItem(testDate); - try { - dateAlbum.put("tci9", tci9); - } catch (final Exception e) { - final String message = "class \"" + TestContextDateTzItem.class.getName() - + "\" not compatible with class \"" + TestContextDateLocaleItem.class.getName() - + "\""; - assertTrue(e.getMessage().contains(message)); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> dateAlbum.put("tci9", tci9)) + .hasMessageContaining("class \"" + TestContextDateTzItem.class.getName() + + "\" not compatible with class \"" + TestContextDateLocaleItem.class.getName() + + "\""); final TestContextDateLocaleItem tciA01 = new TestContextDateLocaleItem(tciA00); final TestContextDateLocaleItem tciA02 = new TestContextDateLocaleItem(tciA00); @@ -145,14 +139,9 @@ public class SequentialContextInstantiation { // Get another reference to the album final ContextAlbum dateAlbum1 = getContextAlbum(DATE_CONTEXT_ALBUM, contextDistributor); - try { - dateAlbum1.putAll(valueMap1); - fail(EXCEPTION_MESSAGE); - } catch (final Exception e) { - assertTrue(e.getMessage().endsWith("not compatible with class \"" - + TestContextDateLocaleItem.class.getName() + "\"")); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> dateAlbum1.putAll(valueMap1)) + .hasMessageContaining("not compatible with class \"" + + TestContextDateLocaleItem.class.getName() + "\""); assertEquals(5, dateAlbum1.size()); valueMap1.clear(); @@ -217,23 +206,14 @@ public class SequentialContextInstantiation { } private void assertContains(final ContextAlbum dateAlbum, final TestContextDateLocaleItem tciA01) { - try { - dateAlbum.containsKey(null); - fail(EXCEPTION_MESSAGE); - } catch (final Exception e) { - assertEquals("null values are illegal on method parameter \"key\"", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> dateAlbum.containsKey(null)) + .hasMessageContaining("null values are illegal on method parameter \"key\""); assertTrue(dateAlbum.containsKey(ITEM05)); assertTrue(!dateAlbum.containsKey("Item07")); - try { - dateAlbum.containsValue(null); - } catch (final Exception e) { - assertEquals("null values are illegal on method parameter \"value\"", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> dateAlbum.containsValue(null)) + .hasMessageContaining("null values are illegal on method parameter \"value\""); assertTrue(dateAlbum.containsValue(tciA01)); assertTrue(!dateAlbum.containsValue("Hello")); @@ -241,32 +221,18 @@ public class SequentialContextInstantiation { private void assertAlbumGetAndPutMethods(final ContextAlbum dateAlbum, final TestContextDateLocaleItem tciA03, final TestContextDateLocaleItem tciA00) { - try { - dateAlbum.get(null); - fail(EXCEPTION_MESSAGE); - } catch (final Exception e) { - assertEquals("album \"DateContextAlbum:0.0.1\" null keys are illegal on keys for get()", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> dateAlbum.get(null)) + .hasMessageContaining("album \"DateContextAlbum:0.0.1\" null keys are illegal on keys for get()"); final Object aObject = dateAlbum.get(ITEM03); assertEquals(tciA03, aObject); - try { - dateAlbum.put(null, null); - fail(EXCEPTION_MESSAGE); - } catch (final Exception e) { - assertEquals("album \"DateContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> dateAlbum.put(null, null)) + .hasMessageContaining("album \"DateContextAlbum:0.0.1\" null keys are illegal on keys for put()"); // Put null ContextItem should work (return null) - try { - dateAlbum.put(ITEM_NULL, null); - } catch (final Exception e) { - assertEquals("album \"DateContextAlbum:0.0.1\" null values are illegal on key \"ItemNull\" for put()", - e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> dateAlbum.put(ITEM_NULL, null)) + .hasMessageContaining("album \"DateContextAlbum:0.0.1\" null values are illegal on " + + "key \"ItemNull\" for put()"); // Should return null assertNull(dateAlbum.get(ITEM_NULL)); @@ -279,37 +245,21 @@ public class SequentialContextInstantiation { retItem = (TestContextDateLocaleItem) dateAlbum.put(ITEM03, tciA03); assertEquals(tciA03Clone, retItem); - try { - dateAlbum.put(ITEM_NULL, null); - fail(EXCEPTION_MESSAGE); - - } catch (final Exception e) { - assert ("album \"DateContextAlbum:0.0.1\" null values are illegal on key \"ItemNull\" for put()" - .equals(e.getMessage())); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> dateAlbum.put(ITEM_NULL, null)) + .hasMessageContaining("album \"DateContextAlbum:0.0.1\" null values are illegal on " + + "key \"ItemNull\" for put()"); dateAlbum.put(TEST_AA, tciA00); assertEquals(tciA00, dateAlbum.get(TEST_AA)); // Should print warning - try { - dateAlbum.put(TEST_AA, null); - fail(EXCEPTION_MESSAGE); - } catch (final Exception e) { - assertEquals("album \"DateContextAlbum:0.0.1\" null values are illegal on key \"TestAA\" for put()", - e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> dateAlbum.put(TEST_AA, null)) + .hasMessageContaining("album \"DateContextAlbum:0.0.1\" null values are illegal on key \"TestAA\" " + + "for put()"); assertEquals(8, dateAlbum.size()); - try { - dateAlbum.put(TEST_AB, null); - fail(EXCEPTION_MESSAGE); - } catch (final Exception e) { - assertEquals("album \"DateContextAlbum:0.0.1\" null values are illegal on key \"TestAB\" for put()", - e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> dateAlbum.put(TEST_AB, null)) + .hasMessageContaining("album \"DateContextAlbum:0.0.1\" null values are illegal on key \"TestAB\" " + + "for put()"); assertEquals(8, dateAlbum.size()); } -- cgit 1.2.3-korg