From 6ae054c03543bc4d323ff0ae710c0b57e4f4e610 Mon Sep 17 00:00:00 2001 From: waynedunican Date: Mon, 6 Jul 2020 09:31:10 +0100 Subject: Replace try/catch blocks with assertj - apex-pdp Replaced try/catch blocks in apex-pdp with assertj assertions Issue-ID: POLICY-2451 Change-Id: I83375b9e0f38d399b84bb68ce5d441e1cec73ef5 Signed-off-by: waynedunican --- .../common/model/SampleDomainModelSaverTest.java | 31 ++---- .../common/testclasses/TestPingClassTest.java | 83 +++++----------- .../context/distribution/ContextInstantiation.java | 105 +++++---------------- 3 files changed, 56 insertions(+), 163 deletions(-) (limited to 'testsuites') diff --git a/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/model/SampleDomainModelSaverTest.java b/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/model/SampleDomainModelSaverTest.java index 241f69dc0..c7ab1d9a0 100644 --- a/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/model/SampleDomainModelSaverTest.java +++ b/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/model/SampleDomainModelSaverTest.java @@ -1,28 +1,29 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 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. * 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.testsuites.integration.common.model; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; @@ -30,6 +31,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; /** * Test the sample domain model saver. @@ -37,32 +39,19 @@ import org.junit.Test; public class SampleDomainModelSaverTest { @Test - public void testSampleDomainModelSaver() throws IOException { - try { - SampleDomainModelSaver.main(null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("java.lang.NullPointerException", exc.getClass().getName()); - } + public void testSampleDomainModelSaver() throws IOException, ApexException { + assertThatThrownBy(() -> SampleDomainModelSaver.main(null)).isInstanceOf(NullPointerException.class); String[] args0 = { "two", "arguments" }; - try { - SampleDomainModelSaver.main(args0); - } catch (Exception exc) { - fail("test should not throw an exception"); - } + SampleDomainModelSaver.main(args0); Path tempDirectory = Files.createTempDirectory("ApexModelTempDir"); String[] args1 = { tempDirectory.toString() }; - try { - SampleDomainModelSaver.main(args1); - } catch (Exception exc) { - fail("test should not throw an exception"); - } + SampleDomainModelSaver.main(args1); File tempDir = new File(tempDirectory.toString()); assertTrue(tempDir.isDirectory()); diff --git a/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/testclasses/TestPingClassTest.java b/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/testclasses/TestPingClassTest.java index 82f46c04a..92f86f434 100644 --- a/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/testclasses/TestPingClassTest.java +++ b/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/testclasses/TestPingClassTest.java @@ -21,8 +21,8 @@ package org.onap.policy.apex.testsuites.integration.common.testclasses; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; @@ -32,7 +32,7 @@ import org.onap.policy.apex.model.basicmodel.concepts.ApexException; */ public class TestPingClassTest { @Test - public void testPingClass() { + public void testPingClass() throws ApexException { PingTestClass ptc = new PingTestClass(); ptc.setName("Hello"); @@ -46,84 +46,43 @@ public class TestPingClassTest { ptc.setPongTime(-1); assertEquals(-1, ptc.getPongTime()); - - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, name does not start with \"Rose\"", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, name does not start with \"Rose\""); ptc.setName(null); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, name length null or less than 4", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, name length null or less than 4"); ptc.setName("Ros"); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, name length null or less than 4", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, name length null or less than 4"); ptc.setName("Rose"); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, description length null or less than 44", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, description length null or less than 44"); ptc.setDescription(null); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, description length null or less than 44", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, description length null or less than 44"); ptc.setDescription("A rose by any other name would smell as swee"); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, description length null or less than 44", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, description length null or less than 44"); ptc.setDescription("A rose by any other name would smell as swell"); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, description is incorrect", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, description is incorrect"); ptc.setDescription("A rose by any other name would smell as sweet"); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, pong time -1 is less than ping time 0", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, pong time -1 is less than ping time 0"); ptc.setPongTime(-2); - try { - ptc.verify(); - fail("test should throw an exception"); - } catch (ApexException ae) { - assertEquals("TestPing is not valid, pong time -2 is less than ping time 0", ae.getMessage()); - } + assertThatThrownBy(() -> ptc.verify()).isInstanceOf(ApexException.class) + .hasMessageContaining("TestPing is not valid, pong time -2 is less than ping time 0"); ptc.setPongTime(1); - try { - ptc.verify(); - } catch (ApexException ae) { - fail("test should not throw an exception"); - } + ptc.verify(); assertEquals( "PingTestClass(id=0, name=Rose, " diff --git a/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/ContextInstantiation.java b/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/ContextInstantiation.java index 475316a4d..9e9e4626e 100644 --- a/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/ContextInstantiation.java +++ b/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/ContextInstantiation.java @@ -21,6 +21,7 @@ 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.assertFalse; import static org.junit.Assert.assertNotNull; @@ -242,15 +243,8 @@ public class ContextInstantiation { assertTrue(externalContextAlbum.values().containsAll(mapValues)); // Check that clearing does not work - try { - externalContextAlbum.clear(); - fail(EXCEPTION_MESSAGE); - } catch (final ContextRuntimeException e) { - assertEquals("album \"ExternalContextAlbum:0.0.1\" clear() not allowed on read only albums", - e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - + assertThatThrownBy(() -> externalContextAlbum.clear()).isInstanceOf(ContextRuntimeException.class) + .hasMessageContaining("album \"ExternalContextAlbum:0.0.1\" clear() not allowed on read only albums"); assertEquals(1, externalContextAlbum.size()); assertContextAlbumContains(externalContext, externalContextAlbum); @@ -258,32 +252,16 @@ public class ContextInstantiation { final Set> entrySet = externalContextAlbum.entrySet(); assertEquals(1, entrySet.size()); - try { - externalContextAlbum.get(null); - } catch (final ContextRuntimeException e) { - assertEquals("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for get()", - e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - + assertThatThrownBy(() -> externalContextAlbum.get(null)).isInstanceOf(ContextRuntimeException.class) + .hasMessageContaining("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for get()"); final Object aObject = externalContextAlbum.get(EXTERNAL_CONTEXT); assertEquals(aObject, externalContext); // put null keys should fail, throws a runtime exception - try { - externalContextAlbum.put(null, null); - } catch (final ContextRuntimeException e) { - assertEquals("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for put()", - e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - - try { - externalContextAlbum.put("TestExternalContextItem00A", null); - } catch (final ContextRuntimeException e) { - assertEquals(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItem00A\" for put()", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> externalContextAlbum.put(null, null)) + .hasMessageContaining("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for put()"); + assertThatThrownBy(() -> externalContextAlbum.put("TestExternalContextItem00A", null)) + .hasMessageContaining(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItem00A\" for put()"); assertEquals(tciAa, externalContextItem.getTestExternalContextItem00A()); // Should return the hash set @@ -305,29 +283,17 @@ public class ContextInstantiation { assertTrue(externalContextAlbum.put(EXTERNAL_CONTEXT, externalContext).equals(externalContextOther)); externalContextItem = (TestExternalContextItem) externalContextAlbum.get(EXTERNAL_CONTEXT); assertEquals(INT_VAL_3, externalContextItem.getTestExternalContextItem002().getIntValue()); + assertThatThrownBy(() -> externalContextAlbum.put("TestExternalContextItem00A", null)) + .hasMessageContaining(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItem00A\" for put()"); - try { - externalContextAlbum.put("TestExternalContextItem00A", null); - } catch (final ContextRuntimeException e) { - assert (e.getMessage().equals(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItem00A\" for put()")); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } assertTrue(externalContextAlbum.get(EXTERNAL_CONTEXT).equals(externalContext)); - try { - externalContextAlbum.put("TestExternalContextItemFFF", null); - } catch (final ContextRuntimeException e) { - assert (e.getMessage().equals(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItemFFF\" for put()")); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> externalContextAlbum.put("TestExternalContextItemFFF", null)) + .hasMessageContaining(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItemFFF\" for put()"); assertEquals(1, externalContextAlbum.size()); - try { - externalContextAlbum.put("TestExternalContextItemFFF", null); - } catch (final ContextRuntimeException e) { - assertEquals(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItemFFF\" for put()", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> externalContextAlbum.put("TestExternalContextItemFFF", null)) + .hasMessageContaining(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItemFFF\" for put()"); assertEquals(1, externalContextAlbum.size()); // Should ignore remove @@ -341,23 +307,13 @@ public class ContextInstantiation { private void assertContextAlbumContains(final TestExternalContextItem externalContext, final ContextAlbum externalContextAlbum) { - try { - externalContextAlbum.containsKey(null); - } catch (final ContextRuntimeException e) { - assertEquals("null values are illegal on method parameter \"key\"", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - + assertThatThrownBy(() -> externalContextAlbum.containsKey(null)) + .hasMessageContaining("null values are illegal on method parameter \"key\""); assertTrue(externalContextAlbum.containsKey(EXTERNAL_CONTEXT)); assertTrue(!externalContextAlbum.containsKey(GLOBAL_CONTEXT_KEY)); - try { - externalContextAlbum.containsValue(null); - } catch (final ContextRuntimeException e) { - assertEquals("null values are illegal on method parameter \"value\"", e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - + assertThatThrownBy(() -> externalContextAlbum.containsValue(null)) + .hasMessageContaining("null values are illegal on method parameter \"value\""); assertTrue(externalContextAlbum.containsValue(externalContext)); assertFalse(externalContextAlbum.containsValue("Hello")); } @@ -444,23 +400,12 @@ public class ContextInstantiation { private void assertPutMethods(final ContextAlbum policyContextAlbum, final TestContextStringItem contextStringItem, final Map valueMapA) { - try { - policyContextAlbum.put(TEST_POLICY_CONTEXT_ITEM000, contextStringItem); - fail(EXCEPTION_MESSAGE); - } catch (final ContextRuntimeException e) { - assertEquals(getMessage(TEST_POLICY_CONTEXT_ITEM000, "TestContextItem006", - TestContextStringItem.class.getName(), "stringValue=" + STRING_VAL), e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } - - try { - policyContextAlbum.putAll(valueMapA); - fail(EXCEPTION_MESSAGE); - } catch (final ContextRuntimeException e) { - assertEquals(getMessage(TEST_POLICY_CONTEXT_ITEM001, "TestContextItem003", - TestContextLongItem.class.getName(), "longValue=" + INT_VAL_3), e.getMessage()); - LOGGER.trace(NORMAL_TEST_EXCEPTION, e); - } + assertThatThrownBy(() -> policyContextAlbum.put(TEST_POLICY_CONTEXT_ITEM000, contextStringItem)) + .hasMessageContaining(getMessage(TEST_POLICY_CONTEXT_ITEM000, "TestContextItem006", + TestContextStringItem.class.getName(), "stringValue=" + STRING_VAL)); + assertThatThrownBy(() -> policyContextAlbum.putAll(valueMapA)) + .hasMessageContaining(getMessage(TEST_POLICY_CONTEXT_ITEM001, "TestContextItem003", + TestContextLongItem.class.getName(), "longValue=" + INT_VAL_3)); } private AxContextModel getAxContextModel() { -- cgit 1.2.3-korg