From 1e80f98e25442b4badf570fd6ca652c21e499e28 Mon Sep 17 00:00:00 2001 From: waynedunican Date: Thu, 9 Jul 2020 13:03:28 +0100 Subject: Remove try/catch blocks with assertj - apex-pdp Removed try/catch blocks in apex-pdp and replaced with assertj assertions Part III Issue-ID: POLICY-2451 Change-Id: I1e6ede1c256c5bcf7899e1af130add71fdf1381c Signed-off-by: waynedunican --- .../apex/core/deployment/BatchDeployerTest.java | 4 +- .../core/deployment/EngineServiceFacadeTest.java | 3 + .../concepts/AxConceptGetterImplTest.java | 33 ++--- .../apex/model/basicmodel/concepts/AxKeyTest.java | 79 +++++------ .../basicmodel/concepts/AxReferenceKeyTest.java | 145 +++++++++------------ .../apex/model/basicmodel/dao/DaoMiscTest.java | 22 +--- .../apex/model/basicmodel/dao/EntityTest.java | 37 ++---- .../handling/ApexModelFileWriterTest.java | 69 +++++----- .../basicmodel/handling/ApexModelReaderTest.java | 143 +++++++------------- .../handling/ApexModelStringWriterTest.java | 58 +++------ .../basicmodel/handling/ApexModelWriterTest.java | 141 +++++++------------- .../handling/SupportApexBasicModelTester.java | 9 +- .../handling/SupportBasicModelTester.java | 104 ++++----------- .../model/basicmodel/service/ModelServiceTest.java | 33 ++--- .../contextmodel/concepts/ContextAlbumsTest.java | 12 +- .../contextmodel/concepts/ContextSchemasTest.java | 13 +- .../enginemodel/concepts/EngineModelTest.java | 29 +---- .../enginemodel/concepts/EngineStatsTest.java | 10 +- .../apex/model/modelapi/ApexModelApiTest.java | 31 ++--- .../apex/model/modelapi/ModelFacadeTest.java | 27 ++-- .../model/modelapi/ModelHandlerFacadeTest.java | 22 +--- .../apex/model/policymodel/concepts/LogicTest.java | 29 +---- 22 files changed, 361 insertions(+), 692 deletions(-) diff --git a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/BatchDeployerTest.java b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/BatchDeployerTest.java index ab44f900d..7e17cacdc 100644 --- a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/BatchDeployerTest.java +++ b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/BatchDeployerTest.java @@ -78,7 +78,7 @@ public class BatchDeployerTest { assertThatThrownBy(deployer::init).hasMessage("model deployment failed on parameters localhost 12345"); // Wait until the connection to the server closes following the bad connection // attempt - Awaitility.await().atLeast(Duration.ofMillis(100)); + Awaitility.await().atLeast(Duration.ofMillis(500)); // We are testing towards a dummy client, make it return a successful initiation dummyDeploymentClient.setInitSuccessful(true); @@ -105,7 +105,7 @@ public class BatchDeployerTest { .hasMessage("model deployment failed on parameters localhost 12345"); // Wait until the connection to the server closes following the bad connection // attempt - Awaitility.await().atLeast(Duration.ofMillis(100)); + Awaitility.await().atLeast(Duration.ofMillis(500)); dummyDeploymentClient.setInitSuccessful(true); diff --git a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/EngineServiceFacadeTest.java b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/EngineServiceFacadeTest.java index 6e0826c3f..1740c7e55 100644 --- a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/EngineServiceFacadeTest.java +++ b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/EngineServiceFacadeTest.java @@ -27,6 +27,8 @@ import static org.junit.Assert.assertNull; import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.time.Duration; +import org.awaitility.Awaitility; import org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; @@ -54,6 +56,7 @@ public class EngineServiceFacadeTest { .hasMessage("could not deploy apex model, deployer is not initialized"); // Second init should work + Awaitility.await().atLeast(Duration.ofMillis(1000)); dummyDeploymentClient.setInitSuccessful(true); facade.init(); diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxConceptGetterImplTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxConceptGetterImplTest.java index 37d3a9dd5..44a9de49e 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxConceptGetterImplTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxConceptGetterImplTest.java @@ -1,29 +1,30 @@ /*- * ============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.model.basicmodel.concepts; +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.fail; import java.util.NavigableMap; import java.util.TreeMap; @@ -41,17 +42,12 @@ public class AxConceptGetterImplTest { AxConceptGetterImpl getter = new AxConceptGetterImpl<>(keyMap); assertNotNull(getter); - + AxArtifactKey keyA = new AxArtifactKey("A", "0.0.1"); assertNull(getter.get(keyA)); - - try { - getter.get((String) null); - fail("test should throw an exception here"); - } catch (Exception getException) { - assertEquals("conceptKeyName may not be null", getException.getMessage()); - } + assertThatThrownBy(() -> getter.get((String) null)) + .hasMessage("conceptKeyName may not be null"); assertNull(getter.get("W")); AxArtifactKey keyZ = new AxArtifactKey("Z", "0.0.1"); @@ -69,19 +65,14 @@ public class AxConceptGetterImplTest { keyMap.remove(keyZ); assertEquals(keyW002, getter.get("W")); - try { - getter.get((String) null, "0.0.1"); - fail("test should throw an exception here"); - } catch (Exception getException) { - assertEquals("conceptKeyName may not be null", getException.getMessage()); - } - + assertThatThrownBy(() -> getter.get((String) null, "0.0.1")) + .hasMessage("conceptKeyName may not be null"); assertEquals(keyW002, getter.get("W", "0.0.2")); assertEquals(keyW002, getter.get("W", (String) null)); - + assertEquals(new TreeSet(keyMap.values()), getter.getAll(null)); assertEquals(new TreeSet(keyMap.values()), getter.getAll(null, null)); - + assertEquals(keyW001, getter.getAll("W", null).iterator().next()); assertEquals(keyW002, getter.getAll("W", "0.0.2").iterator().next()); assertEquals(0, getter.getAll("A", null).size()); diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyTest.java index ff12fc5e8..fbbc4a793 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyTest.java @@ -21,12 +21,12 @@ package org.onap.policy.apex.model.basicmodel.concepts; +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.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.lang.reflect.Field; import org.junit.Test; @@ -36,15 +36,9 @@ public class AxKeyTest { @Test public void testArtifactKey() { - try { - new AxArtifactKey("some bad key id"); - fail("This test should throw an exception"); - } catch (IllegalArgumentException e) { - assertEquals("parameter \"id\": value \"some bad key id\", " - + "does not match regular expression \"[A-Za-z0-9\\-_\\.]+:[0-9].[0-9].[0-9]\"", - e.getMessage()); - } - + assertThatThrownBy(() -> new AxArtifactKey("some bad key id")) + .hasMessage("parameter \"id\": value \"some bad key id\", " + + "does not match regular expression \"[A-Za-z0-9\\-_\\.]+:[0-9].[0-9].[0-9]\""); AxArtifactKey someKey0 = new AxArtifactKey(); assertEquals(AxArtifactKey.getNullKey(), someKey0); @@ -109,12 +103,8 @@ public class AxKeyTest { assertEquals(0, someKey7.compareTo(someKey1)); assertEquals(-12, someKey7.compareTo(someKey0)); - try { - someKey0.compareTo(null); - } catch (IllegalArgumentException e) { - assertEquals("comparison object may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> someKey0.compareTo(null)) + .hasMessage("comparison object may not be null"); assertEquals(0, someKey0.compareTo(someKey0)); assertEquals(353602977, someKey0.compareTo(new AxReferenceKey())); @@ -135,40 +125,33 @@ public class AxKeyTest { @Test - public void testValidation() { + public void testValidation() throws IllegalArgumentException, IllegalAccessException, + NoSuchFieldException, SecurityException { AxArtifactKey testKey = new AxArtifactKey("TheKey", "0.0.1"); assertEquals("TheKey:0.0.1", testKey.getId()); - try { - Field nameField = testKey.getClass().getDeclaredField("name"); - nameField.setAccessible(true); - nameField.set(testKey, "Key Name"); - AxValidationResult validationResult = new AxValidationResult(); - testKey.validate(validationResult); - nameField.set(testKey, "TheKey"); - nameField.setAccessible(false); - assertEquals( - "name invalid-parameter name with value Key Name " - + "does not match regular expression [A-Za-z0-9\\-_\\.]+", - validationResult.getMessageList().get(0).getMessage()); - } catch (Exception validationException) { - fail("test should not throw an exception"); - } - - try { - Field versionField = testKey.getClass().getDeclaredField("version"); - versionField.setAccessible(true); - versionField.set(testKey, "Key Version"); - AxValidationResult validationResult = new AxValidationResult(); - testKey.validate(validationResult); - versionField.set(testKey, "0.0.1"); - versionField.setAccessible(false); - assertEquals( - "version invalid-parameter version with value Key Version " - + "does not match regular expression [A-Za-z0-9.]+", - validationResult.getMessageList().get(0).getMessage()); - } catch (Exception validationException) { - fail("test should not throw an exception"); - } + Field nameField = testKey.getClass().getDeclaredField("name"); + nameField.setAccessible(true); + nameField.set(testKey, "Key Name"); + AxValidationResult validationResult = new AxValidationResult(); + testKey.validate(validationResult); + nameField.set(testKey, "TheKey"); + nameField.setAccessible(false); + assertEquals( + "name invalid-parameter name with value Key Name " + + "does not match regular expression [A-Za-z0-9\\-_\\.]+", + validationResult.getMessageList().get(0).getMessage()); + + Field versionField = testKey.getClass().getDeclaredField("version"); + versionField.setAccessible(true); + versionField.set(testKey, "Key Version"); + AxValidationResult validationResultV = new AxValidationResult(); + testKey.validate(validationResultV); + versionField.set(testKey, "0.0.1"); + versionField.setAccessible(false); + assertEquals( + "version invalid-parameter version with value Key Version " + + "does not match regular expression [A-Za-z0-9.]+", + validationResultV.getMessageList().get(0).getMessage()); } } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKeyTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKeyTest.java index cca0b61ae..49a14dd3c 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKeyTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKeyTest.java @@ -21,12 +21,12 @@ package org.onap.policy.apex.model.basicmodel.concepts; +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.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.lang.reflect.Field; import org.junit.Test; @@ -107,100 +107,71 @@ public class AxReferenceKeyTest { assertNotNull(testReferenceKey.getKeys()); - try { - testReferenceKey.equals(null); - fail("test should throw an exception here"); - } catch (Exception iae) { - assertEquals("comparison object may not be null", iae.getMessage()); - } - - try { - testReferenceKey.copyTo(null); - fail("test should throw an exception here"); - } catch (Exception iae) { - assertEquals("target may not be null", iae.getMessage()); - } - - try { - testReferenceKey.copyTo(new AxArtifactKey("Key", "0.0.1")); - fail("test should throw an exception here"); - } catch (Exception iae) { - assertEquals("org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey is not an instance of " - + "org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey", iae.getMessage()); - } - + assertThatThrownBy(() -> testReferenceKey.equals(null)) + .hasMessage("comparison object may not be null"); + assertThatThrownBy(() -> testReferenceKey.copyTo(null)) + .hasMessage("target may not be null"); + assertThatThrownBy(() -> testReferenceKey.copyTo(new AxArtifactKey("Key", "0.0.1"))) + .hasMessage("org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey is not an instance of " + + "org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey"); AxReferenceKey targetRefKey = new AxReferenceKey(); assertEquals(testReferenceKey, testReferenceKey.copyTo(targetRefKey)); } @Test - public void testValidation() { + public void testValidation() throws IllegalArgumentException, IllegalAccessException, + NoSuchFieldException, SecurityException { AxReferenceKey testReferenceKey = new AxReferenceKey(); testReferenceKey.setParentArtifactKey(new AxArtifactKey("PN", "0.0.1")); assertEquals("PN:0.0.1", testReferenceKey.getParentArtifactKey().getId()); - try { - Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName"); - parentNameField.setAccessible(true); - parentNameField.set(testReferenceKey, "Parent Name"); - AxValidationResult validationResult = new AxValidationResult(); - testReferenceKey.validate(validationResult); - parentNameField.set(testReferenceKey, "ParentName"); - parentNameField.setAccessible(false); - assertEquals( - "parentKeyName invalid-parameter parentKeyName with value Parent Name " - + "does not match regular expression [A-Za-z0-9\\-_\\.]+", - validationResult.getMessageList().get(0).getMessage()); - } catch (Exception validationException) { - fail("test should not throw an exception"); - } - - try { - Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion"); - parentVersionField.setAccessible(true); - parentVersionField.set(testReferenceKey, "Parent Version"); - AxValidationResult validationResult = new AxValidationResult(); - testReferenceKey.validate(validationResult); - parentVersionField.set(testReferenceKey, "0.0.1"); - parentVersionField.setAccessible(false); - assertEquals( - "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version " - + "does not match regular expression [A-Za-z0-9.]+", - validationResult.getMessageList().get(0).getMessage()); - } catch (Exception validationException) { - fail("test should not throw an exception"); - } - - try { - Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName"); - parentLocalNameField.setAccessible(true); - parentLocalNameField.set(testReferenceKey, "Parent Local Name"); - AxValidationResult validationResult = new AxValidationResult(); - testReferenceKey.validate(validationResult); - parentLocalNameField.set(testReferenceKey, "ParentLocalName"); - parentLocalNameField.setAccessible(false); - assertEquals( - "parentLocalName invalid-parameter parentLocalName with value " - + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$", - validationResult.getMessageList().get(0).getMessage()); - } catch (Exception validationException) { - fail("test should not throw an exception"); - } - - try { - Field localNameField = testReferenceKey.getClass().getDeclaredField("localName"); - localNameField.setAccessible(true); - localNameField.set(testReferenceKey, "Local Name"); - AxValidationResult validationResult = new AxValidationResult(); - testReferenceKey.validate(validationResult); - localNameField.set(testReferenceKey, "LocalName"); - localNameField.setAccessible(false); - assertEquals( - "localName invalid-parameter localName with value Local Name " - + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$", - validationResult.getMessageList().get(0).getMessage()); - } catch (Exception validationException) { - fail("test should not throw an exception"); - } + Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName"); + parentNameField.setAccessible(true); + parentNameField.set(testReferenceKey, "Parent Name"); + AxValidationResult validationResult = new AxValidationResult(); + testReferenceKey.validate(validationResult); + parentNameField.set(testReferenceKey, "ParentName"); + parentNameField.setAccessible(false); + assertEquals( + "parentKeyName invalid-parameter parentKeyName with value Parent Name " + + "does not match regular expression [A-Za-z0-9\\-_\\.]+", + validationResult.getMessageList().get(0).getMessage()); + + Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion"); + parentVersionField.setAccessible(true); + parentVersionField.set(testReferenceKey, "Parent Version"); + AxValidationResult validationResultPV = new AxValidationResult(); + testReferenceKey.validate(validationResultPV); + parentVersionField.set(testReferenceKey, "0.0.1"); + parentVersionField.setAccessible(false); + assertEquals( + "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version " + + "does not match regular expression [A-Za-z0-9.]+", + validationResultPV.getMessageList().get(0).getMessage()); + + Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName"); + parentLocalNameField.setAccessible(true); + parentLocalNameField.set(testReferenceKey, "Parent Local Name"); + AxValidationResult validationResultPL = new AxValidationResult(); + testReferenceKey.validate(validationResultPL); + parentLocalNameField.set(testReferenceKey, "ParentLocalName"); + parentLocalNameField.setAccessible(false); + assertEquals( + "parentLocalName invalid-parameter parentLocalName with value " + + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$", + validationResultPL.getMessageList().get(0).getMessage()); + + Field localNameField = testReferenceKey.getClass().getDeclaredField("localName"); + localNameField.setAccessible(true); + localNameField.set(testReferenceKey, "Local Name"); + AxValidationResult validationResultLN = new AxValidationResult(); + testReferenceKey.validate(validationResultLN); + localNameField.set(testReferenceKey, "LocalName"); + localNameField.setAccessible(false); + assertEquals( + "localName invalid-parameter localName with value Local Name " + + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$", + validationResultLN.getMessageList().get(0).getMessage()); + } } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/dao/DaoMiscTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/dao/DaoMiscTest.java index 2b4cd5c30..6f9bef83d 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/dao/DaoMiscTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/dao/DaoMiscTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-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. @@ -20,9 +21,9 @@ package org.onap.policy.apex.model.basicmodel.dao; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; import java.util.Properties; import org.junit.Test; @@ -47,21 +48,12 @@ public class DaoMiscTest { final DaoParameters daoParameters = new DaoParameters(); daoParameters.setPluginClass("somewhere.over.the.rainbow"); - try { - new ApexDaoFactory().createApexDao(daoParameters); - fail("test shold throw an exception here"); - } catch (final Exception e) { - assertEquals("Apex DAO class not found for DAO plugin \"somewhere.over.the.rainbow\"", e.getMessage()); - } - + assertThatThrownBy(() -> new ApexDaoFactory().createApexDao(daoParameters)) + .hasMessage("Apex DAO class not found for DAO plugin \"somewhere.over.the.rainbow\""); daoParameters.setPluginClass("java.lang.String"); - try { - new ApexDaoFactory().createApexDao(daoParameters); - fail("test shold throw an exception here"); - } catch (final Exception e) { - assertEquals("Specified Apex DAO plugin class \"java.lang.String\" " - + "does not implement the ApexDao interface", e.getMessage()); - } + assertThatThrownBy(() -> new ApexDaoFactory().createApexDao(daoParameters)) + .hasMessage("Specified Apex DAO plugin class \"java.lang.String\" " + + "does not implement the ApexDao interface"); } @Test diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/dao/EntityTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/dao/EntityTest.java index 370a4114f..f9b949701 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/dao/EntityTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/dao/EntityTest.java @@ -21,11 +21,10 @@ package org.onap.policy.apex.model.basicmodel.dao; +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 java.util.ArrayList; import java.util.List; @@ -51,34 +50,16 @@ public class EntityTest { apexDao = new ApexDaoFactory().createApexDao(daoParameters); - try { - apexDao.init(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertEquals("Apex persistence unit parameter not set", e.getMessage()); - } - - try { - apexDao.init(daoParameters); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertEquals("Apex persistence unit parameter not set", e.getMessage()); - } - + assertThatThrownBy(() -> apexDao.init(null)) + .hasMessage("Apex persistence unit parameter not set"); + assertThatThrownBy(() -> apexDao.init(daoParameters)) + .hasMessage("Apex persistence unit parameter not set"); daoParameters.setPluginClass("somewhere.over.the.rainbow"); daoParameters.setPersistenceUnit("Dorothy"); - try { - apexDao.init(daoParameters); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertEquals("Creation of Apex persistence unit \"Dorothy\" failed", e.getMessage()); - } - try { - apexDao.create(new AxArtifactKey()); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertEquals("Apex DAO has not been initialized", e.getMessage()); - } + assertThatThrownBy(() -> apexDao.init(daoParameters)) + .hasMessage("Creation of Apex persistence unit \"Dorothy\" failed"); + assertThatThrownBy(() -> apexDao.create(new AxArtifactKey())) + .hasMessage("Apex DAO has not been initialized"); apexDao.close(); } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelFileWriterTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelFileWriterTest.java index d6ff31e50..331e57f47 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelFileWriterTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelFileWriterTest.java @@ -1,27 +1,28 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-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.model.basicmodel.handling; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; @@ -40,27 +41,26 @@ public class ApexModelFileWriterTest { File tempFile = File.createTempFile("ApexFileWriterTest", "test"); File tempDir = tempFile.getParentFile(); - tempFile.delete(); - + File jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/ApexFileWriterTest.json"); File xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ApexFileWriterTest.xml"); - + AxModel model = new DummyApexBasicModelCreator().getModel(); modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath()); modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath()); - + jsonTempFile.delete(); xmlTempFile.delete(); new File(tempDir.getAbsolutePath() + "/aaa").delete(); new File(tempDir.getAbsolutePath() + "/ccc").delete(); - + jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json"); xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ddd/ApexFileWriterTest.xml"); - + modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath()); modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath()); - + jsonTempFile.delete(); xmlTempFile.delete(); @@ -68,28 +68,22 @@ public class ApexModelFileWriterTest { new File(tempDir.getAbsolutePath() + "/aaa").delete(); new File(tempDir.getAbsolutePath() + "/ccc/ddd").delete(); new File(tempDir.getAbsolutePath() + "/ccc").delete(); - + File dirA = new File(tempDir.getAbsolutePath() + "/aaa"); //File dirB = new File(tempDir.getAbsolutePath() + "/aaa/bbb"); dirA.createNewFile(); //dirB.createNewFile(); - + jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json"); jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml"); + final File jsonTempFile01 = jsonTempFile; + assertThatThrownBy(() -> modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, + jsonTempFile01.getAbsolutePath())) + .hasMessageContaining("could not create directory"); - try { - modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath()); - fail("this test should throw an exception here"); - } catch (Exception e) { - assertTrue(e.getMessage().contains("could not create directory ")); - } - - try { - modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, jsonTempFile.getAbsolutePath()); - fail("this test should throw an exception here"); - } catch (Exception e) { - assertTrue(e.getMessage().contains("could not create directory ")); - } + assertThatThrownBy(() -> modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, + jsonTempFile01.getAbsolutePath())) + .hasMessageContaining("could not create directory"); dirA.delete(); @@ -97,23 +91,18 @@ public class ApexModelFileWriterTest { File fileB = new File(tempDir.getAbsolutePath() + "/aaa/bbb"); dirA.mkdir(); fileB.createNewFile(); - + jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json"); jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml"); - try { - modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath()); - fail("this test should throw an exception here"); - } catch (Exception e) { - assertTrue(e.getMessage().contains("error processing file ")); - } - - try { - modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, jsonTempFile.getAbsolutePath()); - fail("this test should throw an exception here"); - } catch (Exception e) { - assertTrue(e.getMessage().contains("error processing file ")); - } + File jsonTempFile02 = jsonTempFile; + assertThatThrownBy(() -> modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, + jsonTempFile02.getAbsolutePath())) + .hasMessageContaining("error processing file"); + + assertThatThrownBy(() -> modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, + jsonTempFile02.getAbsolutePath())) + .hasMessageContaining("error processing file"); fileB.delete(); dirA.delete(); diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelReaderTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelReaderTest.java index 7f2f813d8..92f6206bb 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelReaderTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelReaderTest.java @@ -1,29 +1,30 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-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.model.basicmodel.handling; +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.assertTrue; -import static org.junit.Assert.fail; import java.io.BufferedReader; import java.io.ByteArrayInputStream; @@ -52,144 +53,100 @@ public class ApexModelReaderTest { public void testModelReader() throws IOException, ApexException { AxModel model = new DummyApexBasicModelCreator().getModel(); AxModel invalidModel = new DummyApexBasicModelCreator().getInvalidModel(); - + ApexModelWriter modelWriter = new ApexModelWriter(AxModel.class); modelWriter.setValidateFlag(true); modelWriter.setJsonOutput(true); - + ByteArrayOutputStream baos = new ByteArrayOutputStream(); modelWriter.write(model, baos); - + ByteArrayOutputStream baosInvalid = new ByteArrayOutputStream(); modelWriter.setValidateFlag(false); modelWriter.write(invalidModel, baosInvalid); - + ApexModelReader modelReader = new ApexModelReader(AxModel.class, true); - + modelReader.setValidateFlag(true); assertTrue(modelReader.getValidateFlag()); - + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); AxModel readModel = modelReader.read(bais); assertEquals(model, readModel); - + ByteArrayInputStream baisInvalid = new ByteArrayInputStream(baosInvalid.toByteArray()); - try { - modelReader.read(baisInvalid); - fail("test should throw an exceptino here"); - } catch (Exception e) { - assertTrue(e.getMessage().startsWith("Apex concept validation failed")); - } - + assertThatThrownBy(() -> modelReader.read(baisInvalid)) + .hasMessageStartingWith("Apex concept validation failed"); modelReader.setValidateFlag(false); assertFalse(modelReader.getValidateFlag()); - + ByteArrayInputStream bais2 = new ByteArrayInputStream(baos.toByteArray()); AxModel readModel2 = modelReader.read(bais2); assertEquals(model, readModel2); - + modelWriter.setJsonOutput(false); - + ByteArrayOutputStream baosXml = new ByteArrayOutputStream(); modelWriter.write(model, baosXml); - + ByteArrayInputStream baisXml = new ByteArrayInputStream(baosXml.toByteArray()); AxModel readModelXml = modelReader.read(baisXml); assertEquals(model, readModelXml); - + String dummyString = "SomeDummyText"; ByteArrayInputStream baisDummy = new ByteArrayInputStream(dummyString.getBytes()); - try { - modelReader.read(baisDummy); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("format of input for Apex concept is neither JSON nor XML", e.getMessage()); - } - - try { - ByteArrayInputStream nullBais = null; - modelReader.read(nullBais); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("concept stream may not be null", e.getMessage()); - } - - try { + assertThatThrownBy(() -> modelReader.read(baisDummy)) + .hasMessage("format of input for Apex concept is neither JSON nor XML"); + ByteArrayInputStream nullBais = null; + assertThatThrownBy(() -> modelReader.read(nullBais)) + .hasMessage("concept stream may not be null"); + + assertThatThrownBy(() -> { FileInputStream fis = new FileInputStream(new File("somewhere/over/the/rainbow")); modelReader.read(fis); - fail("test should throw an exception here"); - } catch (Exception e) { - assertTrue(e.getMessage().contains("rainbow")); - } - - File tempFile = File.createTempFile("Apex", "Dummy"); - try { - BufferedReader br = new BufferedReader(new FileReader(tempFile)); - br.close(); - modelReader.read(br); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("Unable to read Apex concept ", e.getMessage()); - } finally { - tempFile.delete(); - } - + }).hasMessageContaining("rainbow"); + final File tempFile = File.createTempFile("Apex", "Dummy"); + BufferedReader br = new BufferedReader(new FileReader(tempFile)); + br.close(); + assertThatThrownBy(() -> modelReader.read(br)) + .hasMessage("Unable to read Apex concept "); + tempFile.delete(); modelReader.setSchema(null); - - tempFile = File.createTempFile("Apex", "Dummy"); - try { - modelReader.setSchema(tempFile.getCanonicalPath()); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("Unable to load schema", e.getMessage()); - } finally { - tempFile.delete(); - } - + + final File tempFileA = File.createTempFile("Apex", "Dummy"); + assertThatThrownBy(() -> modelReader.setSchema(tempFileA.getCanonicalPath())) + .hasMessage("Unable to load schema"); + tempFile.delete(); modelReader.setSchema("xml/example.xsd"); } @Test - public void testSetInputTypeError() throws ApexModelException { + public void testSetInputTypeError() throws ApexModelException, + NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { MockitoAnnotations.initMocks(this); ApexModelReader modelReader = new ApexModelReader(AxModel.class, true); - try { - Field marshallerField = modelReader.getClass().getDeclaredField("unmarshaller"); - marshallerField.setAccessible(true); - marshallerField.set(modelReader, unmarshallerMock); - marshallerField.setAccessible(false); - } catch (Exception validationException) { - fail("test should not throw an exception"); - } + Field marshallerField = modelReader.getClass().getDeclaredField("unmarshaller"); + marshallerField.setAccessible(true); + marshallerField.set(modelReader, unmarshallerMock); + marshallerField.setAccessible(false); - try { + assertThatThrownBy(() -> { Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(unmarshallerMock) .unmarshal((StreamSource) Mockito.anyObject(), Mockito.anyObject()); modelReader.read("{Hello}"); - fail("Test should throw an exception here"); - } catch (Exception jaxbe) { - assertEquals("Unable to unmarshal Apex concept ", jaxbe.getMessage()); - } - - try { + }).hasMessage("Unable to unmarshal Apex concept "); + assertThatThrownBy(() -> { Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock) .setProperty(Mockito.anyString(), Mockito.anyString()); modelReader.read("{Hello}"); - fail("Test should throw an exception here"); - } catch (Exception jaxbe) { - assertEquals("JAXB error setting unmarshaller for JSON input", jaxbe.getMessage()); - } - - try { + }).hasMessage("JAXB error setting unmarshaller for JSON input"); + assertThatThrownBy(() -> { Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock) .setProperty(Mockito.anyString(), Mockito.anyString()); modelReader.read(""); - fail("Test should throw an exception here"); - } catch (Exception jaxbe) { - assertEquals("JAXB error setting unmarshaller for XML input", jaxbe.getMessage()); - } + }).hasMessage("JAXB error setting unmarshaller for XML input"); } } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelStringWriterTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelStringWriterTest.java index cad4e05f3..0b8d789ad 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelStringWriterTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelStringWriterTest.java @@ -1,29 +1,29 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-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.model.basicmodel.handling; -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.assertTrue; -import static org.junit.Assert.fail; import java.io.IOException; import org.junit.Test; @@ -37,53 +37,33 @@ public class ApexModelStringWriterTest { public void testModelStringWriter() throws IOException, ApexException { AxModel basicModel = new DummyApexBasicModelCreator().getModel(); assertNotNull(basicModel); - + AxKeyInfo intKeyInfo = basicModel.getKeyInformation().get("IntegerKIKey"); AxKeyInfo floatKeyInfo = basicModel.getKeyInformation().get("FloatKIKey"); // Ensure marshalling is OK ApexModelStringWriter stringWriter = new ApexModelStringWriter(true); - + assertNotNull(stringWriter.writeJsonString(intKeyInfo, AxKeyInfo.class)); assertNotNull(stringWriter.writeJsonString(floatKeyInfo, AxKeyInfo.class)); - + assertNotNull(stringWriter.writeString(intKeyInfo, AxKeyInfo.class, true)); assertNotNull(stringWriter.writeString(floatKeyInfo, AxKeyInfo.class, true)); - + assertNotNull(stringWriter.writeString(intKeyInfo, AxKeyInfo.class, false)); assertNotNull(stringWriter.writeString(floatKeyInfo, AxKeyInfo.class, false)); - + assertNotNull(stringWriter.writeXmlString(intKeyInfo, AxKeyInfo.class)); assertNotNull(stringWriter.writeXmlString(floatKeyInfo, AxKeyInfo.class)); - - try { - stringWriter.writeString(null, AxKeyInfo.class, true); - fail("test should thrown an exception here"); - } catch (Exception e) { - assertEquals("concept may not be null", e.getMessage()); - } - - try { - stringWriter.writeString(null, AxKeyInfo.class, false); - fail("test should thrown an exception here"); - } catch (Exception e) { - assertEquals("concept may not be null", e.getMessage()); - } - - try { - stringWriter.writeJsonString(null, AxKeyInfo.class); - fail("test should thrown an exception here"); - } catch (Exception e) { - assertEquals("error writing JSON string", e.getMessage()); - } - - try { - stringWriter.writeXmlString(null, AxKeyInfo.class); - fail("test should thrown an exception here"); - } catch (Exception e) { - assertEquals("error writing XML string", e.getMessage()); - } - + + assertThatThrownBy(() -> stringWriter.writeString(null, AxKeyInfo.class, true)) + .hasMessage("concept may not be null"); + assertThatThrownBy(() -> stringWriter.writeString(null, AxKeyInfo.class, false)) + .hasMessage("concept may not be null"); + assertThatThrownBy(() -> stringWriter.writeJsonString(null, AxKeyInfo.class)) + .hasMessage("error writing JSON string"); + assertThatThrownBy(() -> stringWriter.writeXmlString(null, AxKeyInfo.class)) + .hasMessage("error writing XML string"); stringWriter.setValidateFlag(true); assertTrue(stringWriter.isValidateFlag()); } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriterTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriterTest.java index 1b8461c4e..fbbe2467c 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriterTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriterTest.java @@ -1,29 +1,30 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-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.model.basicmodel.handling; +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.assertTrue; -import static org.junit.Assert.fail; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -75,126 +76,84 @@ public class ApexModelWriterTest { modelWriter.setValidateFlag(true); model.getKeyInformation().getKeyInfoMap().clear(); - try { - modelWriter.write(model, baos); - fail("Test should throw an exception here"); - } catch (Exception e) { - assertEquals("Apex concept xml (BasicModel:0.0.1) validation failed", e.getMessage().substring(0, 53)); - } + assertThatThrownBy(() -> modelWriter.write(model, baos)) + .hasMessageContaining("Apex concept xml (BasicModel:0.0.1) validation failed"); model.getKeyInformation().generateKeyInfo(model); - try { - modelWriter.write(null, baos); - fail("Test should throw an exception here"); - } catch (Exception e) { - assertEquals("concept may not be null", e.getMessage()); - } - - try { - ByteArrayOutputStream nullBaos = null; - modelWriter.write(model, nullBaos); - fail("Test should throw an exception here"); - } catch (Exception e) { - assertEquals("concept stream may not be null", e.getMessage()); - } + assertThatThrownBy(() -> modelWriter.write(null, baos)) + .hasMessage("concept may not be null"); + + ByteArrayOutputStream nullBaos = null; + assertThatThrownBy(() -> modelWriter.write(model, nullBaos)) + .hasMessage("concept stream may not be null"); } @Test - public void testSetOutputTypeError() throws ApexModelException { + public void testSetOutputTypeError() throws ApexModelException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException, PropertyException { MockitoAnnotations.initMocks(this); ApexModelWriter modelWriter = new ApexModelWriter(AxModel.class); - try { - Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller"); - marshallerField.setAccessible(true); - marshallerField.set(modelWriter, marshallerMock); - marshallerField.setAccessible(false); - } catch (Exception validationException) { - fail("test should not throw an exception"); - } - - try { - Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock) - .setProperty(Mockito.anyString(), Mockito.anyString()); - modelWriter.setJsonOutput(true); - fail("Test should throw an exception here"); - } catch (Exception jaxbe) { - assertEquals("JAXB error setting marshaller for JSON output", jaxbe.getMessage()); - } - - try { - Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock) - .setProperty(Mockito.anyString(), Mockito.anyString()); - modelWriter.setJsonOutput(false); - fail("Test should throw an exception here"); - } catch (Exception jaxbe) { - assertEquals("JAXB error setting marshaller for XML output", jaxbe.getMessage()); - } + Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller"); + marshallerField.setAccessible(true); + marshallerField.set(modelWriter, marshallerMock); + marshallerField.setAccessible(false); + Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock) + .setProperty(Mockito.anyString(), Mockito.anyString()); + assertThatThrownBy(() -> modelWriter.setJsonOutput(true)) + .hasMessage("JAXB error setting marshaller for JSON output"); + Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock) + .setProperty(Mockito.anyString(), Mockito.anyString()); + assertThatThrownBy(() -> modelWriter.setJsonOutput(false)) + .hasMessage("JAXB error setting marshaller for XML output"); } @Test - public void testOutputJsonError() throws ApexModelException { + public void testOutputJsonError() throws ApexModelException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException, JAXBException { MockitoAnnotations.initMocks(this); ApexModelWriter modelWriter = new ApexModelWriter(AxModel.class); - try { - Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller"); - marshallerField.setAccessible(true); - marshallerField.set(modelWriter, marshallerMock); - marshallerField.setAccessible(false); - } catch (Exception validationException) { - fail("test should not throw an exception"); - } + Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller"); + marshallerField.setAccessible(true); + marshallerField.set(modelWriter, marshallerMock); + marshallerField.setAccessible(false); modelWriter.setValidateFlag(false); modelWriter.setJsonOutput(true); - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - AxModel model = new DummyApexBasicModelCreator().getModel(); - - Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock) - .marshal((AxModel) Mockito.anyObject(), (Writer) Mockito.anyObject()); - - modelWriter.write(model, baos); - fail("Test should throw an exception here"); - } catch (Exception jaxbe) { - assertEquals("Unable to marshal Apex concept to JSON", jaxbe.getMessage()); - } + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + AxModel model = new DummyApexBasicModelCreator().getModel(); + Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock) + .marshal((AxModel) Mockito.anyObject(), (Writer) Mockito.anyObject()); + assertThatThrownBy(() -> modelWriter.write(model, baos)).hasMessage("Unable to marshal Apex concept to JSON"); } @Test - public void testOutputXmlError() throws ApexModelException { + public void testOutputXmlError() throws ApexModelException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException, JAXBException { MockitoAnnotations.initMocks(this); ApexModelWriter modelWriter = new ApexModelWriter(AxModel.class); modelWriter.setJsonOutput(false); - try { - Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller"); - marshallerField.setAccessible(true); - marshallerField.set(modelWriter, marshallerMock); - marshallerField.setAccessible(false); - } catch (Exception validationException) { - fail("test should not throw an exception"); - } + Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller"); + marshallerField.setAccessible(true); + marshallerField.set(modelWriter, marshallerMock); + marshallerField.setAccessible(false); modelWriter.setValidateFlag(false); modelWriter.setJsonOutput(false); - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - AxModel model = new DummyApexBasicModelCreator().getModel(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + AxModel model = new DummyApexBasicModelCreator().getModel(); - Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock) - .marshal((AxModel) Mockito.anyObject(), (Document) Mockito.anyObject()); + Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock) + .marshal((AxModel) Mockito.anyObject(), (Document) Mockito.anyObject()); - modelWriter.write(model, baos); - fail("Test should throw an exception here"); - } catch (Exception jaxbe) { - assertEquals("Unable to marshal Apex concept to XML", jaxbe.getMessage()); - } + assertThatThrownBy(() -> modelWriter.write(model, baos)) + .hasMessage("Unable to marshal Apex concept to XML"); } } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexBasicModelTester.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexBasicModelTester.java index 80e2e15b4..064a107e7 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexBasicModelTester.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexBasicModelTester.java @@ -21,11 +21,11 @@ package org.onap.policy.apex.model.basicmodel.handling; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.model.basicmodel.concepts.AxModel; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; @@ -52,11 +52,8 @@ public class SupportApexBasicModelTester { @Test public void testApexModelVaidateObservation() throws Exception { - try { - testApexModel.testApexModelVaidateObservation(); - } catch (final ApexException e) { - assertEquals("model should have observations", e.getMessage()); - } + assertThatThrownBy(testApexModel::testApexModelVaidateObservation) + .hasMessage("model should have observations"); } @Test diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportBasicModelTester.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportBasicModelTester.java index 57df96089..2761d86da 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportBasicModelTester.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportBasicModelTester.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-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. @@ -20,9 +21,7 @@ package org.onap.policy.apex.model.basicmodel.handling; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; @@ -37,12 +36,8 @@ public class SupportBasicModelTester { new DummyApexBasicModelCreator()); testApexModel.testApexModelValid(); - try { - testApexModel.testApexModelVaidateObservation(); - fail("Test should throw an exception"); - } catch (final Exception e) { - assertEquals("model should have observations", e.getMessage()); - } + assertThatThrownBy(testApexModel::testApexModelVaidateObservation) + .hasMessage("model should have observations"); testApexModel.testApexModelVaidateWarning(); testApexModel.testApexModelVaidateInvalidModel(); testApexModel.testApexModelVaidateMalstructured(); @@ -60,13 +55,8 @@ public class SupportBasicModelTester { testApexModel1.getModel().getKey().setVersion("0.0.2"); - try { - testApexModel0.checkModelEquality(testApexModel0.getModel(), testApexModel1.getModel(), - "Models are not equal"); - fail("test should throw an exception here"); - } catch (ApexException ae) { - assertEquals("Models are not equal", ae.getMessage()); - } + assertThatThrownBy(() -> testApexModel0.checkModelEquality(testApexModel0.getModel(), testApexModel1.getModel(), + "Models are not equal")).hasMessage("Models are not equal"); } @Test @@ -75,30 +65,14 @@ public class SupportBasicModelTester { new SupportApexModelCreator0()); testApexModel.testApexModelValid(); - try { - testApexModel.testApexModelVaidateObservation(); - fail("Test should throw an exception"); - } catch (final Exception e) { - assertEquals("model should have observations", e.getMessage()); - } - try { - testApexModel.testApexModelVaidateWarning(); - fail("Test should throw an exception"); - } catch (final Exception e) { - assertEquals("model should have warnings", e.getMessage()); - } - try { - testApexModel.testApexModelVaidateInvalidModel(); - fail("Test should throw an exception"); - } catch (final Exception e) { - assertEquals("model should not be valid ***validation of model successful***", e.getMessage()); - } - try { - testApexModel.testApexModelVaidateMalstructured(); - fail("Test should throw an exception"); - } catch (final Exception e) { - assertEquals("model should not be valid ***validation of model successful***", e.getMessage()); - } + assertThatThrownBy(() -> testApexModel.testApexModelVaidateObservation()) + .hasMessage("model should have observations"); + assertThatThrownBy(() -> testApexModel.testApexModelVaidateWarning()) + .hasMessage("model should have warnings"); + assertThatThrownBy(() -> testApexModel.testApexModelVaidateInvalidModel()) + .hasMessage("model should not be valid ***validation of model successful***"); + assertThatThrownBy(() -> testApexModel.testApexModelVaidateMalstructured()) + .hasMessage("model should not be valid ***validation of model successful***"); } @Test @@ -106,24 +80,12 @@ public class SupportBasicModelTester { final TestApexModel testApexModel = new TestApexModel(AxModel.class, new SupportApexModelCreator1()); - try { - testApexModel.testApexModelValid(); - fail("Test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("model is invalid")); - } - try { - testApexModel.testApexModelVaidateObservation(); - fail("Test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("model is invalid")); - } - try { - testApexModel.testApexModelVaidateWarning(); - fail("Test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("model is invalid")); - } + assertThatThrownBy(() -> testApexModel.testApexModelValid()) + .hasMessageStartingWith("model is invalid"); + assertThatThrownBy(() -> testApexModel.testApexModelVaidateObservation()) + .hasMessageStartingWith("model is invalid"); + assertThatThrownBy(() -> testApexModel.testApexModelVaidateWarning()) + .hasMessageStartingWith("model is invalid"); testApexModel.testApexModelVaidateInvalidModel(); testApexModel.testApexModelVaidateMalstructured(); } @@ -135,12 +97,8 @@ public class SupportBasicModelTester { testApexModel.testApexModelValid(); testApexModel.testApexModelVaidateObservation(); - try { - testApexModel.testApexModelVaidateWarning(); - fail("Test should throw an exception"); - } catch (final Exception e) { - assertEquals("model should have warnings", e.getMessage()); - } + assertThatThrownBy(() -> testApexModel.testApexModelVaidateWarning()) + .hasMessage("model should have warnings"); } @Test @@ -148,18 +106,10 @@ public class SupportBasicModelTester { final TestApexModel testApexModel = new TestApexModel(AxModel.class, new SupportApexModelCreator1()); - try { - testApexModel.testApexModelWriteReadJson(); - fail("Test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("error processing file")); - } - - try { - testApexModel.testApexModelWriteReadXml(); - fail("Test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("error processing file")); - } + assertThatThrownBy(() -> testApexModel.testApexModelWriteReadJson()) + .hasMessageStartingWith("error processing file"); + + assertThatThrownBy(() -> testApexModel.testApexModelWriteReadXml()) + .hasMessageStartingWith("error processing file"); } } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/service/ModelServiceTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/service/ModelServiceTest.java index 1ebfb6d71..746e10607 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/service/ModelServiceTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/service/ModelServiceTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-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. @@ -20,7 +21,7 @@ package org.onap.policy.apex.model.basicmodel.service; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -36,13 +37,9 @@ public class ModelServiceTest { ModelService.clear(); assertFalse(ModelService.existsModel(AxKeyInformation.class)); - try { - ModelService.getModel(AxKeyInformation.class); - } catch (final Exception e) { - assertEquals("Model for org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation " - + "not found in model service", e.getMessage()); - } - + assertThatThrownBy(() -> ModelService.getModel(AxKeyInformation.class)) + .hasMessage("Model for org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation " + + "not found in model service"); ModelService.registerModel(AxKeyInformation.class, new DummyApexBasicModelCreator().getModel().getKeyInformation()); assertTrue(ModelService.existsModel(AxKeyInformation.class)); @@ -51,13 +48,9 @@ public class ModelServiceTest { ModelService.deregisterModel(AxKeyInformation.class); assertFalse(ModelService.existsModel(AxKeyInformation.class)); - try { - ModelService.getModel(AxKeyInformation.class); - } catch (final Exception e) { - assertEquals("Model for org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation " - + "not found in model service", e.getMessage()); - } - + assertThatThrownBy(() -> ModelService.getModel(AxKeyInformation.class)) + .hasMessage("Model for org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation " + + "not found in model service"); ModelService.registerModel(AxKeyInformation.class, new DummyApexBasicModelCreator().getModel().getKeyInformation()); assertTrue(ModelService.existsModel(AxKeyInformation.class)); @@ -65,12 +58,8 @@ public class ModelServiceTest { ModelService.clear(); assertFalse(ModelService.existsModel(AxKeyInformation.class)); - try { - ModelService.getModel(AxKeyInformation.class); - } catch (final Exception e) { - assertEquals("Model for org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation " - + "not found in model service", e.getMessage()); - } - + assertThatThrownBy(() -> ModelService.getModel(AxKeyInformation.class)) + .hasMessage("Model for org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation " + + "not found in model service"); } } diff --git a/model/context-model/src/test/java/org/onap/policy/apex/model/contextmodel/concepts/ContextAlbumsTest.java b/model/context-model/src/test/java/org/onap/policy/apex/model/contextmodel/concepts/ContextAlbumsTest.java index 0406543fb..74b000ff3 100644 --- a/model/context-model/src/test/java/org/onap/policy/apex/model/contextmodel/concepts/ContextAlbumsTest.java +++ b/model/context-model/src/test/java/org/onap/policy/apex/model/contextmodel/concepts/ContextAlbumsTest.java @@ -21,11 +21,11 @@ package org.onap.policy.apex.model.contextmodel.concepts; +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.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; @@ -57,13 +57,9 @@ public class ContextAlbumsTest { assertEquals("NewAlbumName:0.0.1", album.getKeys().get(0).getId()); album.setKey(albumKey); - try { - album.setScope(""); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("parameter \"scope\": value \"\", does not match regular expression \"[A-Za-z0-9\\-_]+\"", - e.getMessage()); - } + assertThatThrownBy(() -> album.setScope("")) + .hasMessage("parameter \"scope\": value \"\", does not match regular expression " + + "\"[A-Za-z0-9\\-_]+\""); album.setScope("NewAlbumScope"); assertEquals("NewAlbumScope", album.getScope()); diff --git a/model/context-model/src/test/java/org/onap/policy/apex/model/contextmodel/concepts/ContextSchemasTest.java b/model/context-model/src/test/java/org/onap/policy/apex/model/contextmodel/concepts/ContextSchemasTest.java index e5fc19a20..abe7c008f 100644 --- a/model/context-model/src/test/java/org/onap/policy/apex/model/contextmodel/concepts/ContextSchemasTest.java +++ b/model/context-model/src/test/java/org/onap/policy/apex/model/contextmodel/concepts/ContextSchemasTest.java @@ -21,11 +21,11 @@ package org.onap.policy.apex.model.contextmodel.concepts; +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.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; @@ -51,14 +51,9 @@ public class ContextSchemasTest { assertEquals("NewSchemaName:0.0.1", schema.getKey().getId()); assertEquals("NewSchemaName:0.0.1", schema.getKeys().get(0).getId()); - try { - schema.setSchemaFlavour(""); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("parameter \"schemaFlavour\": value \"\", " - + "does not match regular expression \"[A-Za-z0-9\\-_]+\"", e.getMessage()); - } - + assertThatThrownBy(() -> schema.setSchemaFlavour("")) + .hasMessage("parameter \"schemaFlavour\": value \"\", " + + "does not match regular expression \"[A-Za-z0-9\\-_]+\""); schema.setSchemaFlavour("NewSchemaFlavour"); assertEquals("NewSchemaFlavour", schema.getSchemaFlavour()); diff --git a/model/engine-model/src/test/java/org/onap/policy/apex/model/enginemodel/concepts/EngineModelTest.java b/model/engine-model/src/test/java/org/onap/policy/apex/model/enginemodel/concepts/EngineModelTest.java index 6e4fbbf05..f3f2fb602 100644 --- a/model/engine-model/src/test/java/org/onap/policy/apex/model/enginemodel/concepts/EngineModelTest.java +++ b/model/engine-model/src/test/java/org/onap/policy/apex/model/enginemodel/concepts/EngineModelTest.java @@ -21,11 +21,11 @@ package org.onap.policy.apex.model.enginemodel.concepts; +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.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; @@ -63,13 +63,8 @@ public class EngineModelTest { new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats); model.register(); - try { - model.setKey(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("key may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> model.setKey(null)) + .hasMessage("key may not be null"); model.setKey(modelKey); assertEquals("ModelName:0.0.1", model.getKey().getId()); assertEquals("ModelName:0.0.1", model.getKeys().get(0).getId()); @@ -80,13 +75,8 @@ public class EngineModelTest { model.setTimestamp(-1); assertTrue(model.getTimeStampString().matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d{3}")); - try { - model.setState(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("state may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> model.setState(null)) + .hasMessage("state may not be null"); for (final AxEngineState state : AxEngineState.values()) { model.setState(state); assertEquals(state, model.getState()); @@ -95,13 +85,8 @@ public class EngineModelTest { model.setState(AxEngineState.READY); assertEquals(AxEngineState.READY, model.getState()); - try { - model.setStats(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("stats may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> model.setStats(null)) + .hasMessage("stats may not be null"); model.setStats(stats); assertEquals(stats, model.getStats()); diff --git a/model/engine-model/src/test/java/org/onap/policy/apex/model/enginemodel/concepts/EngineStatsTest.java b/model/engine-model/src/test/java/org/onap/policy/apex/model/enginemodel/concepts/EngineStatsTest.java index 9f48d1da5..5cb1bd40e 100644 --- a/model/engine-model/src/test/java/org/onap/policy/apex/model/enginemodel/concepts/EngineStatsTest.java +++ b/model/engine-model/src/test/java/org/onap/policy/apex/model/enginemodel/concepts/EngineStatsTest.java @@ -21,6 +21,7 @@ package org.onap.policy.apex.model.enginemodel.concepts; +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.assertNotNull; @@ -49,13 +50,8 @@ public class EngineStatsTest { final AxReferenceKey statsKey = new AxReferenceKey("EngineKey", "0.0.1", "EngineStats"); final AxEngineStats stats = new AxEngineStats(statsKey); - try { - stats.setKey(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("key may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> stats.setKey(null)) + .hasMessage("key may not be null"); stats.setKey(statsKey); assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKey().getId()); assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKeys().get(0).getId()); diff --git a/model/model-api/src/test/java/org/onap/policy/apex/model/modelapi/ApexModelApiTest.java b/model/model-api/src/test/java/org/onap/policy/apex/model/modelapi/ApexModelApiTest.java index e7330ee47..02195e4db 100644 --- a/model/model-api/src/test/java/org/onap/policy/apex/model/modelapi/ApexModelApiTest.java +++ b/model/model-api/src/test/java/org/onap/policy/apex/model/modelapi/ApexModelApiTest.java @@ -1,4 +1,4 @@ -/*- +/* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2020 Nordix Foundation. @@ -21,11 +21,10 @@ package org.onap.policy.apex.model.modelapi; +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.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; @@ -144,24 +143,13 @@ public class ApexModelApiTest { @Test public void testApexModelUrl() throws IOException { - ApexModel apexModel = new ApexModelFactory().createApexModel(null, false); - + final ApexModel apexModel = new ApexModelFactory().createApexModel(null, false); + //ApexApiResult result = null; + assertThatThrownBy(() -> apexModel.readFromUrl(null)) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> apexModel.writeToUrl(null, true)) + .isInstanceOf(IllegalArgumentException.class); ApexApiResult result = null; - - try { - result = apexModel.readFromUrl(null); - fail("expecting an IllegalArgumentException"); - } catch (final Exception e) { - assertTrue(e instanceof IllegalArgumentException); - } - - try { - result = apexModel.writeToUrl(null, true); - fail("expecting an IllegalArgumentException"); - } catch (final Exception e) { - assertTrue(e instanceof IllegalArgumentException); - } - result = apexModel.readFromUrl("zooby/looby"); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); @@ -174,9 +162,10 @@ public class ApexModelApiTest { result = apexModel.writeToUrl("zooby://zooby/looby", false); assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - apexModel = new ApexModelFactory().createApexModel(null, false); + final ApexModel apexModelJSon = new ApexModelFactory().createApexModel(null, false); final File tempJsonModelFile = File.createTempFile("ApexModelTest", ".json"); + result = apexModel.saveToFile(tempJsonModelFile.getCanonicalPath(), false); assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); diff --git a/model/model-api/src/test/java/org/onap/policy/apex/model/modelapi/ModelFacadeTest.java b/model/model-api/src/test/java/org/onap/policy/apex/model/modelapi/ModelFacadeTest.java index ac8f26095..a5f258e4c 100644 --- a/model/model-api/src/test/java/org/onap/policy/apex/model/modelapi/ModelFacadeTest.java +++ b/model/model-api/src/test/java/org/onap/policy/apex/model/modelapi/ModelFacadeTest.java @@ -1,28 +1,29 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-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.model.modelapi; +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.fail; import java.util.Properties; import java.util.UUID; @@ -33,22 +34,12 @@ public class ModelFacadeTest { @Test public void testModelFacade() { - try { - new ModelFacade(null, null, false); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("apexModel may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> new ModelFacade(null, null, false)) + .hasMessage("apexModel may not be null"); final ApexModel apexModel = new ApexModelFactory().createApexModel(null, false); - try { - new ModelFacade(apexModel, null, false); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("apexProperties may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> new ModelFacade(apexModel, null, false)) + .hasMessage("apexProperties may not be null"); final Properties modelProperties = new Properties(); final ModelFacade mf = new ModelFacade(apexModel, modelProperties, false); diff --git a/model/model-api/src/test/java/org/onap/policy/apex/model/modelapi/ModelHandlerFacadeTest.java b/model/model-api/src/test/java/org/onap/policy/apex/model/modelapi/ModelHandlerFacadeTest.java index a34b4f474..4a1c3276c 100644 --- a/model/model-api/src/test/java/org/onap/policy/apex/model/modelapi/ModelHandlerFacadeTest.java +++ b/model/model-api/src/test/java/org/onap/policy/apex/model/modelapi/ModelHandlerFacadeTest.java @@ -21,9 +21,9 @@ package org.onap.policy.apex.model.modelapi; +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.fail; import java.io.File; import java.io.IOException; @@ -35,29 +35,19 @@ import org.onap.policy.common.utils.resources.TextFileUtils; /** * Test the model handler facade. - * + * * @author Liam Fallon (liam.fallon@ericsson.com) */ public class ModelHandlerFacadeTest { @Test public void testModelHandlerFacade() throws IOException { - try { - new ModelHandlerFacade(null, null, false); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("apexModel may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> new ModelHandlerFacade(null, null, false)) + .hasMessage("apexModel may not be null"); final ApexModel apexModel = new ApexModelFactory().createApexModel(null, false); - try { - new ModelHandlerFacade(apexModel, null, false); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("apexProperties may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> new ModelHandlerFacade(apexModel, null, false)) + .hasMessage("apexProperties may not be null"); final Properties modelProperties = new Properties(); final ModelHandlerFacade mhf = new ModelHandlerFacade(apexModel, modelProperties, false); assertNotNull(mhf); diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/LogicTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/LogicTest.java index 655df059a..7961d8e8e 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/LogicTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/LogicTest.java @@ -21,6 +21,7 @@ package org.onap.policy.apex.model.policymodel.concepts; +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.assertNotNull; @@ -103,21 +104,11 @@ public class LogicTest { result = logic.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); - try { - logic.setLogicFlavour(null); - fail("test shold throw an exception here"); - } catch (final Exception e) { - assertEquals("parameter \"logicFlavour\" is null", e.getMessage()); - } - - try { - logic.setLogicFlavour(""); - fail("test shold throw an exception here"); - } catch (final Exception e) { - assertEquals("parameter \"logicFlavour\": value \"\", " - + "does not match regular expression \"[A-Za-z0-9\\-_]+\"", e.getMessage()); - } - + assertThatThrownBy(() -> logic.setLogicFlavour(null)) + .hasMessageContaining("parameter \"logicFlavour\" is null"); + assertThatThrownBy(() -> logic.setLogicFlavour("")) + .hasMessage("parameter \"logicFlavour\": value \"\", " + + "does not match regular expression \"[A-Za-z0-9\\-_]+\""); logic.setLogicFlavour(AxLogic.LOGIC_FLAVOUR_UNDEFINED); result = new AxValidationResult(); result = logic.validate(result); @@ -128,13 +119,7 @@ public class LogicTest { result = logic.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); - try { - logic.setLogic(null); - fail("test shold throw an exception here"); - } catch (final Exception e) { - assertEquals("logic may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> logic.setLogic(null)).hasMessage("logic may not be null"); logic.setLogic(""); result = new AxValidationResult(); result = logic.validate(result); -- cgit 1.2.3-korg