From b8e3df6f43e46159c210ac7d1ec6983871d52ea5 Mon Sep 17 00:00:00 2001 From: waynedunican Date: Fri, 10 Jul 2020 08:25:22 +0100 Subject: Replaced try/catch blocks with assertj - apex-pdp Removed try/catch blocks in apex-pdp with assertj assertions Part IV Issue-ID: POLICY-2451 Change-Id: I4b6accb9f944195db9ccf8a5de165b169301ac7f Signed-off-by: waynedunican --- .../model/policymodel/concepts/PoliciesTest.java | 251 +++++++++------------ .../policymodel/concepts/StateOutputTest.java | 39 +--- .../concepts/StateTaskReferenceTest.java | 39 +--- .../apex/model/policymodel/concepts/StateTest.java | 75 ++---- .../policymodel/concepts/TaskParameterTest.java | 18 +- .../handling/PolicyLogicReaderTest.java | 83 +++---- .../handling/PolicyModelSplitterTest.java | 41 +--- .../context/schema/avro/AvroSchemaEnumTest.java | 43 ++-- .../context/schema/avro/AvroSchemaFixedTest.java | 55 ++--- .../avro/AvroSchemaHelperBadSchemasTest.java | 69 ++---- .../schema/avro/AvroSchemaHelperMarshalTest.java | 121 +++------- .../schema/avro/AvroSchemaHelperUnmarshalTest.java | 229 ++++++------------- .../context/schema/avro/AvroSchemaRecordTest.java | 12 +- .../restclient/ApexRestClientConusmerTest.java | 52 ++--- .../restclient/ApexRestClientProducerTest.java | 50 ++-- .../RestClientCarrierTechnologyParametersTest.java | 33 +-- .../event/protocol/yaml/YamlEventProtocolTest.java | 11 +- .../protocol/yaml/YamlPluginStabilityTest.java | 122 +++------- .../java/JavaStateFinalizerExecutorTest.java | 89 ++------ 19 files changed, 447 insertions(+), 985 deletions(-) diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PoliciesTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PoliciesTest.java index 28121fce6..084623f6f 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PoliciesTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PoliciesTest.java @@ -1,32 +1,32 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * 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.policymodel.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.util.Map; import java.util.TreeMap; @@ -54,7 +54,7 @@ public class PoliciesTest { assertNotNull(new AxPolicy(new AxArtifactKey())); assertNotNull(new AxPolicy(new AxArtifactKey(), "PolicyTemplate", stateMapEmpty, "FirstState")); - AxPolicy policy = new AxPolicy(); + final AxPolicy policy = new AxPolicy(); final AxArtifactKey policyKey = new AxArtifactKey("PolicyName", "0.0.1"); @@ -65,199 +65,180 @@ public class PoliciesTest { badState.getStateOutputs().put(badStateOutput.getKey().getLocalName(), badStateOutput); stateMap.put(firstState.getKey().getLocalName(), firstState); - try { - policy.setKey(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("key may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> policy.setKey(null)) + .hasMessage("key may not be null"); policy.setKey(policyKey); assertEquals("PolicyName:0.0.1", policy.getKey().getId()); assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId()); - try { - policy.setTemplate(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("template may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> policy.setTemplate(null)) + .hasMessage("template may not be null"); policy.setTemplate("PolicyTemplate"); assertEquals("PolicyTemplate", policy.getTemplate()); - try { - policy.setStateMap(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("stateMap may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> policy.setStateMap(null)) + .hasMessage("stateMap may not be null"); policy.setStateMap(stateMap); assertEquals(stateMap, policy.getStateMap()); - try { - policy.setFirstState(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("firstState may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> policy.setFirstState(null)) + .hasMessage("firstState may not be null"); policy.setFirstState("FirstState"); assertEquals("FirstState", policy.getFirstState()); assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId()); - policy = new SupportApexPolicyModelCreator().getModel().getPolicies().get("policy"); + final AxPolicy policyPN = new SupportApexPolicyModelCreator().getModel().getPolicies().get("policy"); AxValidationResult result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); - final AxArtifactKey savedPolicyKey = policy.getKey(); - policy.setKey(AxArtifactKey.getNullKey()); + final AxArtifactKey savedPolicyKey = policyPN.getKey(); + policyPN.setKey(AxArtifactKey.getNullKey()); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.INVALID, result.getValidationResult()); - policy.setKey(savedPolicyKey); + policyPN.setKey(savedPolicyKey); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); - final String savedTemplate = policy.getTemplate(); - policy.setTemplate(""); + final String savedTemplate = policyPN.getTemplate(); + policyPN.setTemplate(""); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.OBSERVATION, result.getValidationResult()); - policy.setTemplate(savedTemplate); + policyPN.setTemplate(savedTemplate); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); - final Map savedStateMap = policy.getStateMap(); + final Map savedStateMap = policyPN.getStateMap(); - policy.setStateMap(stateMapEmpty); + policyPN.setStateMap(stateMapEmpty); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.INVALID, result.getValidationResult()); - policy.setStateMap(savedStateMap); + policyPN.setStateMap(savedStateMap); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); savedStateMap.put(AxKey.NULL_KEY_NAME, firstState); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.INVALID, result.getValidationResult()); savedStateMap.remove(AxKey.NULL_KEY_NAME); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); savedStateMap.put("NullState", null); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.INVALID, result.getValidationResult()); savedStateMap.remove("NullState"); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); savedStateMap.put("BadStateKey", firstState); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.INVALID, result.getValidationResult()); savedStateMap.remove("BadStateKey"); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); savedStateMap.put(badState.getKey().getLocalName(), badState); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.INVALID, result.getValidationResult()); savedStateMap.remove(badState.getKey().getLocalName()); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); - final String savedFirstState = policy.getFirstState(); + final String savedFirstState = policyPN.getFirstState(); - policy.setFirstState(""); + policyPN.setFirstState(""); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.INVALID, result.getValidationResult()); - policy.setFirstState(savedFirstState); + policyPN.setFirstState(savedFirstState); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); - policy.setFirstState("NonExistantFirstState"); + policyPN.setFirstState("NonExistantFirstState"); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.INVALID, result.getValidationResult()); - policy.setFirstState(savedFirstState); + policyPN.setFirstState(savedFirstState); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); - final AxState clonedState = new AxState(policy.getStateMap().get("state")); + final AxState clonedState = new AxState(policyPN.getStateMap().get("state")); clonedState.getKey().setLocalName("ClonedState"); clonedState.afterUnmarshal(null, null); savedStateMap.put(clonedState.getKey().getLocalName(), clonedState); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.WARNING, result.getValidationResult()); savedStateMap.remove(clonedState.getKey().getLocalName()); result = new AxValidationResult(); - result = policy.validate(result); + result = policyPN.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); - policy.clean(); + policyPN.clean(); - final AxPolicy clonedPolicy = new AxPolicy(policy); + final AxPolicy clonedPolicy = new AxPolicy(policyPN); assertEquals("AxPolicy:(key=AxArtifactKey:(name=policy,version=0.0.1),template=FREEFORM,sta", clonedPolicy.toString().substring(0, 77)); - assertFalse(policy.hashCode() == 0); + assertFalse(policyPN.hashCode() == 0); - assertTrue(policy.equals(policy)); - assertTrue(policy.equals(clonedPolicy)); - assertFalse(policy.equals(null)); - assertFalse(policy.equals((Object) "Hello")); - assertFalse(policy.equals( + assertTrue(policyPN.equals(policyPN)); + assertTrue(policyPN.equals(clonedPolicy)); + assertFalse(policyPN.equals(null)); + assertFalse(policyPN.equals((Object) "Hello")); + assertFalse(policyPN.equals( new AxPolicy(AxArtifactKey.getNullKey(), savedTemplate, savedStateMap, savedFirstState))); - assertFalse(policy.equals(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState))); - assertFalse(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState))); - assertFalse(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState"))); - assertTrue(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState))); - - assertEquals(0, policy.compareTo(policy)); - assertEquals(0, policy.compareTo(clonedPolicy)); - assertNotEquals(0, policy.compareTo(new AxArtifactKey())); - assertNotEquals(0, policy.compareTo(null)); - assertNotEquals(0, policy.compareTo( + assertFalse(policyPN.equals(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState))); + assertFalse(policyPN.equals(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState))); + assertFalse(policyPN.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState"))); + assertTrue(policyPN.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState))); + + assertEquals(0, policyPN.compareTo(policyPN)); + assertEquals(0, policyPN.compareTo(clonedPolicy)); + assertNotEquals(0, policyPN.compareTo(new AxArtifactKey())); + assertNotEquals(0, policyPN.compareTo(null)); + assertNotEquals(0, policyPN.compareTo( new AxPolicy(AxArtifactKey.getNullKey(), savedTemplate, savedStateMap, savedFirstState))); - assertNotEquals(0, - policy.compareTo(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState))); - assertNotEquals(0, - policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState))); - assertNotEquals(0, - policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState"))); - assertEquals(0, policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState))); + assertNotEquals(0, policyPN.compareTo(new AxPolicy(savedPolicyKey, + "SomeTemplate", savedStateMap, savedFirstState))); + assertNotEquals(0, policyPN.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, + stateMapEmpty, savedFirstState))); + assertNotEquals(0, policyPN.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, + savedStateMap, "SomeFirstState"))); + assertEquals(0, policyPN.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, + savedStateMap, savedFirstState))); - assertNotNull(policy.getKeys()); + assertNotNull(policyPN.getKeys()); final AxPolicies policies = new AxPolicies(); result = new AxValidationResult(); @@ -272,7 +253,7 @@ public class PoliciesTest { result = policies.validate(result); assertEquals(ValidationResult.INVALID, result.getValidationResult()); - policies.getPolicyMap().put(savedPolicyKey, policy); + policies.getPolicyMap().put(savedPolicyKey, policyPN); result = new AxValidationResult(); result = policies.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); @@ -297,7 +278,7 @@ public class PoliciesTest { result = policies.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); - policies.getPolicyMap().put(new AxArtifactKey("BadEventKey", "0.0.1"), policy); + policies.getPolicyMap().put(new AxArtifactKey("BadEventKey", "0.0.1"), policyPN); result = new AxValidationResult(); result = policies.validate(result); assertEquals(ValidationResult.INVALID, result.getValidationResult()); @@ -343,78 +324,64 @@ public class PoliciesTest { assertNotNull(stateTree.getReferencedStateList()); assertNotNull(stateTree.getReferencedStateSet()); - final AxState secondState = new AxState(policy.getStateMap().get("state")); + final AxState secondState = new AxState(policyPN.getStateMap().get("state")); secondState.getKey().setLocalName("SecondState"); secondState.afterUnmarshal(null, null); - policy.getStateMap().put("SecondState", secondState); - policy.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey()); + policyPN.getStateMap().put("SecondState", secondState); + policyPN.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey()); - stateTree = policy.getStateTree(); + stateTree = policyPN.getStateTree(); assertNotNull(stateTree); assertNotNull(stateTree.getReferencedStateList()); assertNotNull(stateTree.getReferencedStateSet()); assertNotNull(stateTree.getNextStates()); - policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0") - .setNextState(policy.getStateMap().get("state").getKey()); - try { - policy.getStateTree(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("loop detected in state tree for policy policy:0.0.1 state SecondState, " - + "next state state referenced more than once", e.getMessage()); - } - - policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0") + policyPN.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0") + .setNextState(policyPN.getStateMap().get("state").getKey()); + assertThatThrownBy(() -> policyPN.getStateTree()) + .hasMessageContaining("loop detected in state tree for policy policy:0.0.1 state SecondState, " + + "next state state referenced more than once"); + policyPN.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0") .setNextState(AxReferenceKey.getNullKey()); - final AxState thirdState = new AxState(policy.getStateMap().get("state")); + final AxState thirdState = new AxState(policyPN.getStateMap().get("state")); thirdState.getKey().setLocalName("ThirdState"); thirdState.afterUnmarshal(null, null); - policy.getStateMap().put("ThirdState", thirdState); - policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0").setNextState(thirdState.getKey()); - policy.getStateMap().get("ThirdState").getStateOutputs().get("stateOutput0") + policyPN.getStateMap().put("ThirdState", thirdState); + policyPN.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0") + .setNextState(thirdState.getKey()); + policyPN.getStateMap().get("ThirdState").getStateOutputs().get("stateOutput0") .setNextState(AxReferenceKey.getNullKey()); - stateTree = policy.getStateTree(); + stateTree = policyPN.getStateTree(); final AxStateOutput ssS0Clone = new AxStateOutput( - policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")); + policyPN.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")); ssS0Clone.getKey().setLocalName("ssS0Clone"); - policy.getStateMap().get("SecondState").getStateOutputs().put("ssS0Clone", ssS0Clone); - - try { - policy.getStateTree(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("loop detected in state tree for policy policy:0.0.1 state SecondState, " - + "next state ThirdState referenced more than once", e.getMessage()); - } + policyPN.getStateMap().get("SecondState").getStateOutputs().put("ssS0Clone", ssS0Clone); - policy.getStateMap().get("SecondState").getStateOutputs().remove("ssS0Clone"); + assertThatThrownBy(() -> policyPN.getStateTree()) + .hasMessageContaining("loop detected in state tree for policy policy:0.0.1 state SecondState, " + + "next state ThirdState referenced more than once"); + policyPN.getStateMap().get("SecondState").getStateOutputs().remove("ssS0Clone"); - policy.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey()); + policyPN.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey()); secondState.getStateOutputs().get("stateOutput0").setNextState(thirdState.getKey()); thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey()); - stateTree = policy.getStateTree(); + stateTree = policyPN.getStateTree(); assertNotNull(stateTree.getState()); thirdState.getStateOutputs().get("stateOutput0").setNextState(secondState.getKey()); - try { - policy.getStateTree(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("loop detected in state tree for policy policy:0.0.1 state ThirdState, " - + "next state SecondState referenced more than once", e.getMessage()); - } - + assertThatThrownBy(() -> policyPN.getStateTree()) + .hasMessageContaining("loop detected in state tree for policy policy:0.0.1 state ThirdState, " + + "next state SecondState referenced more than once"); thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey()); - stateTree = policy.getStateTree(); + stateTree = policyPN.getStateTree(); - final AxStateTree otherStateTree = policy.getStateTree(); + final AxStateTree otherStateTree = policyPN.getStateTree(); assertEquals(0, stateTree.compareTo(otherStateTree)); for (final AxStateTree childStateTree : stateTree.getNextStates()) { diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateOutputTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateOutputTest.java index 307a746e5..77ce93d4c 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateOutputTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateOutputTest.java @@ -1,32 +1,32 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * 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.policymodel.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 org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; @@ -36,7 +36,7 @@ import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.Validat /** * Test state outputs. - * + * * @author Liam Fallon (liam.fallon@ericsson.com) */ public class StateOutputTest { @@ -54,34 +54,19 @@ public class StateOutputTest { final AxReferenceKey nsKey = new AxReferenceKey("SOStateParent", "0.0.1", "NotUsed", "NextStateName"); final AxArtifactKey eKey = new AxArtifactKey("EventName", "0.0.1"); - try { - so.setKey(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("key may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> so.setKey(null)) + .hasMessage("key may not be null"); so.setKey(soKey); assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKey().getId()); assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKeys().get(0).getId()); - try { - so.setNextState(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("nextState may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> so.setNextState(null)) + .hasMessage("nextState may not be null"); so.setNextState(nsKey); assertEquals(nsKey, so.getNextState()); - try { - so.setOutgoingEvent(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("outgoingEvent may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> so.setOutgoingEvent(null)) + .hasMessage("outgoingEvent may not be null"); so.setOutgoingEvent(eKey); assertEquals(eKey, so.getOutgingEvent()); diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTaskReferenceTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTaskReferenceTest.java index 87e896155..aa5dd6109 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTaskReferenceTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTaskReferenceTest.java @@ -1,32 +1,32 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * 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.policymodel.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 org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; @@ -36,7 +36,7 @@ import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.Validat /** * Test state task references. - * + * * @author Liam Fallon (liam.fallon@ericsson.com) */ public class StateTaskReferenceTest { @@ -54,24 +54,14 @@ public class StateTaskReferenceTest { AxReferenceKey stRefKey = new AxReferenceKey("StateParent", "0.0.1", "SOState", "SOName"); - try { - stRef.setKey(null); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("key may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> stRef.setKey(null)) + .hasMessage("key may not be null"); stRef.setKey(stRefKey); assertEquals("StateParent:0.0.1:SOState:SOName", stRef.getKey().getId()); assertEquals("StateParent:0.0.1:SOState:SOName", stRef.getKeys().get(0).getId()); - try { - stRef.setStateTaskOutputType(null); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("outputType may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> stRef.setStateTaskOutputType(null)) + .hasMessage("outputType may not be null"); stRef.setStateTaskOutputType(AxStateTaskOutputType.UNDEFINED); assertEquals(AxStateTaskOutputType.UNDEFINED, stRef.getStateTaskOutputType()); stRef.setStateTaskOutputType(AxStateTaskOutputType.DIRECT); @@ -79,13 +69,8 @@ public class StateTaskReferenceTest { stRef.setStateTaskOutputType(AxStateTaskOutputType.LOGIC); assertEquals(AxStateTaskOutputType.LOGIC, stRef.getStateTaskOutputType()); - try { - stRef.setOutput(null); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("output may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> stRef.setOutput(null)) + .hasMessage("output may not be null"); AxReferenceKey soKey = new AxReferenceKey("StateParent", "0.0.1", "SOState", "STRef0"); stRef.setOutput(soKey); assertEquals(soKey, stRef.getOutput()); diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTest.java index adcc040b7..ef015e798 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTest.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,12 +21,12 @@ 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.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.util.Set; import java.util.TreeMap; @@ -75,13 +76,8 @@ public class StateTest { final AxArtifactKey taskKey2 = new AxArtifactKey("Task2", "0.0.1"); final AxArtifactKey taskKeyBad = new AxArtifactKey("TaskBad", "0.0.1"); - try { - state.setKey(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("key may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> state.setKey(null)) + .hasMessage("key may not be null"); state.setKey(stateKey); assertEquals("PolicyName:0.0.1:NULL:StateName", state.getKey().getId()); assertEquals("PolicyName:0.0.1:NULL:StateName", state.getKeys().get(0).getId()); @@ -116,75 +112,40 @@ public class StateTest { trMap.put(taskKey1.getKey(), str1); trMap.put(taskKey2.getKey(), str2); - try { - state.setTrigger(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("trigger may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> state.setTrigger(null)) + .hasMessage("trigger may not be null"); state.setTrigger(triggerKey); assertEquals(triggerKey, state.getTrigger()); - try { - state.setStateOutputs(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("stateOutputs may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> state.setStateOutputs(null)) + .hasMessage("stateOutputs may not be null"); state.setStateOutputs(soMap); assertEquals(soMap, state.getStateOutputs()); - try { - state.setContextAlbumReferences(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("contextAlbumReferenceSet may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> state.setContextAlbumReferences(null)) + .hasMessage("contextAlbumReferenceSet may not be null"); state.setContextAlbumReferences(ctxtSet); assertEquals(ctxtSet, state.getContextAlbumReferences()); - try { - state.setTaskSelectionLogic(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("taskSelectionLogic may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> state.setTaskSelectionLogic(null)) + .hasMessage("taskSelectionLogic may not be null"); assertEquals(false, state.checkSetTaskSelectionLogic()); state.setTaskSelectionLogic(tsl); assertEquals(tsl, state.getTaskSelectionLogic()); assertEquals(true, state.checkSetTaskSelectionLogic()); - try { - state.setStateFinalizerLogicMap(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("stateFinalizerLogic may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> state.setStateFinalizerLogicMap(null)) + .hasMessage("stateFinalizerLogic may not be null"); state.setStateFinalizerLogicMap(sflMap); assertEquals(sflMap, state.getStateFinalizerLogicMap()); - try { - state.setDefaultTask(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("defaultTask may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> state.setDefaultTask(null)) + .hasMessage("defaultTask may not be null"); state.setDefaultTask(defTaskKey); assertEquals(defTaskKey, state.getDefaultTask()); - try { - state.setTaskReferences(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("taskReferenceMap may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> state.setTaskReferences(null)) + .hasMessage("taskReferenceMap may not be null"); state.setTaskReferences(trMap); assertEquals(trMap, state.getTaskReferences()); diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TaskParameterTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TaskParameterTest.java index 0fc08d436..3dcc5e8d7 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TaskParameterTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TaskParameterTest.java @@ -1,26 +1,27 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * 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.policymodel.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; @@ -71,13 +72,8 @@ public class TaskParameterTest { result = par.validate(result); assertEquals(ValidationResult.VALID, result.getValidationResult()); - try { - par.setDefaultValue(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("defaultValue may not be null", e.getMessage()); - } - + assertThatThrownBy(() -> par.setDefaultValue(null)) + .hasMessage("defaultValue may not be null"); par.setDefaultValue(""); result = new AxValidationResult(); result = par.validate(result); diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyLogicReaderTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyLogicReaderTest.java index ebb6d9765..a2cbe7415 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyLogicReaderTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyLogicReaderTest.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.policymodel.handling; +import static org.assertj.core.api.Assertions.assertThat; +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 org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.AxKey; @@ -31,7 +32,7 @@ import org.onap.policy.apex.model.policymodel.concepts.AxLogic; /** * Logic reader for policy tests. - * + * * @author Liam Fallon (liam.fallon@ericsson.com) */ public class PolicyLogicReaderTest { @@ -48,66 +49,36 @@ public class PolicyLogicReaderTest { plReader.setDefaultLogic("FunkyDefaultLogic"); assertEquals("FunkyDefaultLogic", plReader.getDefaultLogic()); - try { - new AxLogic(logicKey, "FunkyLogic", plReader); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("logic not found for logic " - + "\"somewhere/over/the/rainbow/funkylogic/FunkyDefaultLogic.funkylogic\"", e.getMessage()); - } - + assertThatThrownBy(() -> new AxLogic(logicKey, "FunkyLogic", plReader)) + .hasMessage("logic not found for logic " + + "\"somewhere/over/the/rainbow/funkylogic/FunkyDefaultLogic.funkylogic\""); plReader.setDefaultLogic(null); - try { - new AxLogic(logicKey, "FunkyLogic", plReader); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("logic not found for logic " - + "\"somewhere/over/the/rainbow/funkylogic/LogicParentLogicInstanceName.funkylogic\"", - e.getMessage()); - } - + assertThatThrownBy(() -> new AxLogic(logicKey, "FunkyLogic", plReader)) + .hasMessage("logic not found for logic " + + "\"somewhere/over/the/rainbow/funkylogic/LogicParentLogicInstanceName.funkylogic\""); logicKey.setParentLocalName("LogicParentLocalName"); - try { - new AxLogic(logicKey, "FunkyLogic", plReader); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("logic not found for logic " + "\"somewhere/over/the/rainbow/funkylogic/" - + "LogicParentLogicParentLocalNameLogicInstanceName.funkylogic\"", e.getMessage()); - } - + assertThatThrownBy(() -> new AxLogic(logicKey, "FunkyLogic", plReader)) + .hasMessage("logic not found for logic " + "\"somewhere/over/the/rainbow/funkylogic/" + + "LogicParentLogicParentLocalNameLogicInstanceName.funkylogic\""); plReader.setLogicPackage("path.to.apex.logic"); - try { - final AxLogic logic = new AxLogic(logicKey, "FunkyLogic", plReader); - assertTrue(logic.getLogic().endsWith("Way out man, this is funky logic!")); - } catch (final Exception e) { - fail("test should not throw an exception"); - } + + AxLogic logic = new AxLogic(logicKey, "FunkyLogic", plReader); + assertThat(logic.getLogic()).endsWith("Way out man, this is funky logic!"); plReader.setLogicPackage("somewhere.over.the.rainbow"); plReader.setDefaultLogic("JavaLogic"); - try { - final AxLogic logic = new AxLogic(logicKey, "JAVA", plReader); - assertEquals("somewhere.over.the.rainbow.java.JavaLogic", logic.getLogic()); - } catch (final Exception e) { - fail("test should not throw an exception"); - } + logic = new AxLogic(logicKey, "JAVA", plReader); + assertEquals("somewhere.over.the.rainbow.java.JavaLogic", logic.getLogic()); plReader.setDefaultLogic(null); - try { - final AxLogic logic = new AxLogic(logicKey, "JAVA", plReader); - assertEquals("somewhere.over.the.rainbow.java.LogicParentLogicParentLocalNameLogicInstanceName", - logic.getLogic()); - } catch (final Exception e) { - fail("test should not throw an exception"); - } + + logic = new AxLogic(logicKey, "JAVA", plReader); + assertEquals("somewhere.over.the.rainbow.java.LogicParentLogicParentLocalNameLogicInstanceName", + logic.getLogic()); logicKey.setParentLocalName(AxKey.NULL_KEY_NAME); - try { - final AxLogic logic = new AxLogic(logicKey, "JAVA", plReader); - assertEquals("somewhere.over.the.rainbow.java.LogicParentLogicInstanceName", logic.getLogic()); - } catch (final Exception e) { - fail("test should not throw an exception"); - } + logic = new AxLogic(logicKey, "JAVA", plReader); + assertEquals("somewhere.over.the.rainbow.java.LogicParentLogicInstanceName", logic.getLogic()); } } diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelSplitterTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelSplitterTest.java index 5dbdc0fab..f8fc99dd8 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelSplitterTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelSplitterTest.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.policymodel.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.util.Set; import java.util.TreeSet; @@ -34,7 +34,7 @@ import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; public class PolicyModelSplitterTest { @Test - public void test() { + public void test() throws ApexModelException { final AxPolicyModel apexModel = new SupportApexPolicyModelCreator().getModel(); final Set requiredPolicySet = new TreeSet(); @@ -43,11 +43,7 @@ public class PolicyModelSplitterTest { // There's only one policy so a split of this model on that policy should return the same // model AxPolicyModel splitApexModel = null; - try { - splitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet); - } catch (final ApexModelException e) { - fail(e.getMessage()); - } + splitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet); // The only difference between the models should be that the unused event outEvent1 should // not be in the split model @@ -59,19 +55,11 @@ public class PolicyModelSplitterTest { requiredPolicySet.add(new AxArtifactKey("MissingPolicy", "0.0.1")); AxPolicyModel missingSplitApexModel = null; - try { - missingSplitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredMissingPolicySet); - } catch (final ApexModelException e) { - fail(e.getMessage()); - } + missingSplitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredMissingPolicySet); assertNotNull(missingSplitApexModel); splitApexModel = null; - try { - splitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet, true); - } catch (final ApexModelException e) { - fail(e.getMessage()); - } + splitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet, true); // The only difference between the models should be that the unused event outEvent1 should // not be in the split model @@ -81,13 +69,8 @@ public class PolicyModelSplitterTest { // There's only one policy so a split of this model on that policy should return the same // model - try { - apexModel.getKey().setName("InvalidPolicyModelName"); - PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("source model is invalid: \n***validation of model f", e.getMessage().substring(0, 50)); - } - + apexModel.getKey().setName("InvalidPolicyModelName"); + assertThatThrownBy(() -> PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet)) + .hasMessageContaining("source model is invalid: \n***validation of model f"); } } diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaEnumTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaEnumTest.java index 51ac21209..e4a336676 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaEnumTest.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaEnumTest.java @@ -21,8 +21,8 @@ package org.onap.policy.apex.plugins.context.schema.avro; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import java.io.IOException; import org.apache.avro.generic.GenericData.EnumSymbol; @@ -120,35 +120,18 @@ public class AvroSchemaEnumTest { testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleHearts.json"); - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL", - e.getMessage()); - } - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL", - e.getMessage()); - } - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad0.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"\"TWEED\"\" Avro unmarshalling failed: Unknown symbol in enum TWEED", - e.getMessage()); - } - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad1.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals( - "AvroTest:0.0.1: object \"\"Hearts\"\" Avro unmarshalling failed: Unknown symbol in enum Hearts", - e.getMessage()); - } + assertThatThrownBy(() -> testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json")) + .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. " + + "Got VALUE_NULL"); + assertThatThrownBy(() -> testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleNull.json")) + .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. " + + "Got VALUE_NULL"); + assertThatThrownBy(() -> testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad0.json")) + .hasMessage("AvroTest:0.0.1: object \"\"TWEED\"\" Avro unmarshalling failed: Unknown symbol " + + "in enum TWEED"); + assertThatThrownBy(() -> testUnmarshalMarshal(schemaHelper, "src/test/resources/data/EnumExampleBad1.json")) + .hasMessage("AvroTest:0.0.1: object \"\"Hearts\"\" Avro unmarshalling failed: Unknown symbol " + + "in enum Hearts"); } /** diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaFixedTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaFixedTest.java index 19f0761a1..368619ee8 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaFixedTest.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaFixedTest.java @@ -21,9 +21,9 @@ package org.onap.policy.apex.plugins.context.schema.avro; +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.IOException; import org.apache.avro.generic.GenericData.Fixed; @@ -98,15 +98,10 @@ public class AvroSchemaFixedTest { schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - try { - schemaHelper.createNewInstance(); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance " + assertThatThrownBy(() -> schemaHelper.createNewInstance()) + .hasMessage("AvroTest:0.0.1: could not create an instance " + "of class \"org.apache.avro.generic.GenericData$Fixed\" " - + "using the default constructor \"Fixed()\"", e.getMessage()); - } - + + "using the default constructor \"Fixed()\""); final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/FixedExampleGood.json"); final Fixed newFixedFull = (Fixed) schemaHelper.createNewInstance(inString); assertTrue(newFixedFull.toString().startsWith("[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65")); @@ -128,35 +123,19 @@ public class AvroSchemaFixedTest { testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleGood.json"); - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL", - e.getMessage()); - } - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. Got VALUE_NULL", - e.getMessage()); - } - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad0.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"\"BADBAD\"\" " - + "Avro unmarshalling failed: Expected fixed length 64, but got6", e.getMessage()); - } - try { - testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad1.json"); - fail("This test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object " - + "\"\"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0\"\" " - + "Avro unmarshalling failed: Expected fixed length 64, but got65", e.getMessage()); - } + assertThatThrownBy(() -> testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json")) + .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. " + + "Got VALUE_NULL"); + assertThatThrownBy(() -> testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleNull.json")) + .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: Expected fixed. " + + "Got VALUE_NULL"); + assertThatThrownBy(() -> testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad0.json")) + .hasMessage("AvroTest:0.0.1: object \"\"BADBAD\"\" " + + "Avro unmarshalling failed: Expected fixed length 64, but got6"); + assertThatThrownBy(() -> testUnmarshalMarshal(schemaHelper, "src/test/resources/data/FixedExampleBad1.json")) + .hasMessage("AvroTest:0.0.1: object " + + "\"\"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0\"\" " + + "Avro unmarshalling failed: Expected fixed length 64, but got65"); } /** diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperBadSchemasTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperBadSchemasTest.java index 0eae99bfc..5bbaf97fa 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperBadSchemasTest.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperBadSchemasTest.java @@ -1,27 +1,27 @@ /*- * ============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.plugins.context.schema.avro; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import org.junit.After; import org.junit.Before; @@ -83,71 +83,36 @@ public class AvroSchemaHelperBadSchemasTest { final AxContextSchema avroBadSchema0 = new AxContextSchema(new AxArtifactKey("AvroBad0", "0.0.1"), "AVRO", "}"); schemas.getSchemasMap().put(avroBadSchema0.getKey(), avroBadSchema0); - try { - new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema0.getKey()); - fail("This test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad0:0.0.1\" schema is invalid")); - } - + assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema0.getKey())) + .hasMessageStartingWith("AvroTest:0.0.1: avro context schema \"AvroBad0:0.0.1\" schema is invalid"); final AxContextSchema avroBadSchema1 = new AxContextSchema(new AxArtifactKey("AvroBad1", "0.0.1"), "AVRO", ""); schemas.getSchemasMap().put(avroBadSchema1.getKey(), avroBadSchema1); - try { - new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema1.getKey()); - fail("This test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad1:0.0.1\" schema is invalid")); - } - + assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema1.getKey())) + .hasMessageStartingWith("AvroTest:0.0.1: avro context schema \"AvroBad1:0.0.1\" schema is invalid"); final AxContextSchema avroBadSchema2 = new AxContextSchema(new AxArtifactKey("AvroBad2", "0.0.1"), "AVRO", "{}"); schemas.getSchemasMap().put(avroBadSchema2.getKey(), avroBadSchema2); - try { - new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema2.getKey()); - fail("This test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad2:0.0.1\" schema is invalid")); - } - + assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema2.getKey())) + .hasMessageStartingWith("AvroTest:0.0.1: avro context schema \"AvroBad2:0.0.1\" schema is invalid"); final AxContextSchema avroBadSchema3 = new AxContextSchema(new AxArtifactKey("AvroBad3", "0.0.1"), "AVRO", "{zooby}"); schemas.getSchemasMap().put(avroBadSchema3.getKey(), avroBadSchema3); - try { - new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema3.getKey()); - fail("This test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad3:0.0.1\" schema is invalid")); - } - + assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema3.getKey())) + .hasMessageStartingWith("AvroTest:0.0.1: avro context schema \"AvroBad3:0.0.1\" schema is invalid"); final AxContextSchema avroBadSchema4 = new AxContextSchema(new AxArtifactKey("AvroBad4", "0.0.1"), "AVRO", "{\"zooby\"}"); schemas.getSchemasMap().put(avroBadSchema4.getKey(), avroBadSchema4); - try { - new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema4.getKey()); - fail("This test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad4:0.0.1\" schema is invalid")); - } - + assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema4.getKey())) + .hasMessageStartingWith("AvroTest:0.0.1: avro context schema \"AvroBad4:0.0.1\" schema is invalid"); final AxContextSchema avroBadSchema5 = new AxContextSchema(new AxArtifactKey("AvroBad5", "0.0.1"), "AVRO", "{\"type\": \"zooby\"}"); schemas.getSchemasMap().put(avroBadSchema5.getKey(), avroBadSchema5); - try { - new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema5.getKey()); - fail("This test should throw an exception"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: avro context schema \"AvroBad5:0.0.1\" schema is invalid")); - } + assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema5.getKey())) + .hasMessageStartingWith("AvroTest:0.0.1: avro context schema \"AvroBad5:0.0.1\" schema is invalid"); } } diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperMarshalTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperMarshalTest.java index 004521c6c..d84481da8 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperMarshalTest.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperMarshalTest.java @@ -21,9 +21,8 @@ package org.onap.policy.apex.plugins.context.schema.avro; +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 org.junit.After; import org.junit.Before; @@ -109,24 +108,14 @@ public class AvroSchemaHelperMarshalTest { assertEquals("true", schemaHelper1.marshal2String(true)); assertEquals("false", schemaHelper1.marshal2String(false)); - try { - schemaHelper1.marshal2String(0); - fail("Test should throw an exception here"); - } catch (final Exception e) { - e.printStackTrace(); - assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: " + assertThatThrownBy(() -> schemaHelper1.marshal2String(0)) + .hasMessage("AvroTest:0.0.1: object \"0\" Avro marshalling failed: " + "class java.lang.Integer cannot be cast to class java.lang.Boolean (java.lang.Integer and " - + "java.lang.Boolean are in module java.base of loader 'bootstrap')", e.getMessage()); - } - try { - schemaHelper1.marshal2String("0"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - e.printStackTrace(); - assertEquals("AvroTest:0.0.1: object \"0\" Avro marshalling failed: class java.lang.String cannot be cast " - + "to class java.lang.Boolean (java.lang.String and java.lang.Boolean are in module java.base of " - + "loader 'bootstrap')", e.getMessage()); - } + + "java.lang.Boolean are in module java.base of loader 'bootstrap')"); + assertThatThrownBy(() -> schemaHelper1.marshal2String("0")) + .hasMessage("AvroTest:0.0.1: object \"0\" Avro marshalling failed: class java.lang.String " + + "cannot be cast to class java.lang.Boolean (java.lang.String and java.lang.Boolean are in module" + + " java.base of loader 'bootstrap')"); } /** @@ -148,20 +137,11 @@ public class AvroSchemaHelperMarshalTest { assertEquals("-1", schemaHelper2.marshal2String(-1.23)); assertEquals("2147483647", schemaHelper2.marshal2String(2147483647)); assertEquals("-2147483648", schemaHelper2.marshal2String(-2147483648)); - try { - schemaHelper2.marshal2String("Hello"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " - + "class java.lang.String cannot be cast to class java.lang.Number")); - } - try { - schemaHelper2.marshal2String(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Integer\"")); - } + assertThatThrownBy(() -> schemaHelper2.marshal2String("Hello")) + .hasMessageStartingWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " + + "class java.lang.String cannot be cast to class java.lang.Number"); + assertThatThrownBy(() -> schemaHelper2.marshal2String(null)) + .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Integer\""); } /** @@ -181,20 +161,11 @@ public class AvroSchemaHelperMarshalTest { assertEquals("-1", schemaHelper3.marshal2String(-1L)); assertEquals("9223372036854775807", schemaHelper3.marshal2String(9223372036854775807L)); assertEquals("-9223372036854775808", schemaHelper3.marshal2String(-9223372036854775808L)); - try { - schemaHelper3.marshal2String("Hello"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " - + "class java.lang.String cannot be cast to class java.lang.Long")); - } - try { - schemaHelper3.marshal2String(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Long\"")); - } + assertThatThrownBy(() -> schemaHelper3.marshal2String("Hello")) + .hasMessageStartingWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " + + "class java.lang.String cannot be cast to class java.lang.Long"); + assertThatThrownBy(() -> schemaHelper3.marshal2String(null)) + .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Long\""); } /** @@ -218,20 +189,11 @@ public class AvroSchemaHelperMarshalTest { assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F)); assertEquals("9.223372E18", schemaHelper4.marshal2String(9.223372E18F)); assertEquals("-9.223372E18", schemaHelper4.marshal2String(-9.223372E18F)); - try { - schemaHelper4.marshal2String("Hello"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " - + "class java.lang.String cannot be cast to class java.lang.Float")); - } - try { - schemaHelper4.marshal2String(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Float\"")); - } + assertThatThrownBy(() -> schemaHelper4.marshal2String("Hello")) + .hasMessageStartingWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " + + "class java.lang.String cannot be cast to class java.lang.Float"); + assertThatThrownBy(() -> schemaHelper4.marshal2String(null)) + .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Float\""); } /** @@ -255,20 +217,11 @@ public class AvroSchemaHelperMarshalTest { assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18)); assertEquals("9.223372036854776E18", schemaHelper5.marshal2String(9.223372036854776E18)); assertEquals("-9.223372036854776E18", schemaHelper5.marshal2String(-9.223372036854776E18)); - try { - schemaHelper5.marshal2String("Hello"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " - + "class java.lang.String cannot be cast to class java.lang.Double")); - } - try { - schemaHelper5.marshal2String(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Double\"")); - } + assertThatThrownBy(() -> schemaHelper5.marshal2String("Hello")) + .hasMessageStartingWith("AvroTest:0.0.1: object \"Hello\" Avro marshalling failed: " + + "class java.lang.String cannot be cast to class java.lang.Double"); + assertThatThrownBy(() -> schemaHelper5.marshal2String(null)) + .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.Double\""); } /** @@ -293,13 +246,8 @@ public class AvroSchemaHelperMarshalTest { assertEquals("\"9223372036854775808\"", schemaHelper7.marshal2String("9223372036854775808")); assertEquals("\"-9223372036854775809\"", schemaHelper7.marshal2String("-9223372036854775809")); assertEquals("\"Hello\"", schemaHelper7.marshal2String("Hello")); - try { - schemaHelper7.marshal2String(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.String\"")); - } + assertThatThrownBy(() -> schemaHelper7.marshal2String(null)) + .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"java.lang.String\""); } /** @@ -317,12 +265,7 @@ public class AvroSchemaHelperMarshalTest { final String helloOut = schemaHelper.marshal2String(helloBytes); assertEquals("\"hello\"", helloOut); - try { - schemaHelper.marshal2String(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: cannot encode a null object of class \"[Ljava.lang.Byte;\"")); - } + assertThatThrownBy(() -> schemaHelper.marshal2String(null)) + .hasMessageStartingWith("AvroTest:0.0.1: cannot encode a null object of class \"[Ljava.lang.Byte;\""); } } diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperUnmarshalTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperUnmarshalTest.java index fee21ae2a..2b6050acc 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperUnmarshalTest.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelperUnmarshalTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,9 +21,8 @@ package org.onap.policy.apex.plugins.context.schema.avro; +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 org.apache.avro.util.Utf8; import org.junit.After; @@ -91,23 +90,13 @@ public class AvroSchemaHelperUnmarshalTest { final SchemaHelper schemaHelper0 = new SchemaHelperFactory().createSchemaHelper(testKey, avroNullSchema.getKey()); - try { - schemaHelper0.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance, schema class for the schema is null", - e.getMessage()); - } - + assertThatThrownBy(schemaHelper0::createNewInstance) + .hasMessage("AvroTest:0.0.1: could not create an instance, schema class for the schema is null"); assertEquals(null, schemaHelper0.unmarshal("null")); - try { - schemaHelper0.unmarshal("123"); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"123\" Avro unmarshalling failed: " - + "Expected null. Got VALUE_NUMBER_INT", e.getMessage()); - } + assertThatThrownBy(() -> schemaHelper0.unmarshal("123")) + .hasMessage("AvroTest:0.0.1: object \"123\" Avro unmarshalling failed: " + + "Expected null. Got VALUE_NUMBER_INT"); } /** @@ -122,25 +111,17 @@ public class AvroSchemaHelperUnmarshalTest { final SchemaHelper schemaHelper1 = new SchemaHelperFactory().createSchemaHelper(testKey, avroBooleanSchema.getKey()); - try { - schemaHelper1.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\" " - + "using the default constructor \"Boolean()\"", e.getMessage()); - } + assertThatThrownBy(schemaHelper1::createNewInstance) + .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\" " + + "using the default constructor \"Boolean()\""); assertEquals(true, schemaHelper1.createNewInstance("true")); assertEquals(true, schemaHelper1.unmarshal("true")); assertEquals(false, schemaHelper1.unmarshal("false")); - try { - schemaHelper1.unmarshal(0); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: object \"0\" of type \"java.lang.Integer\" must be assignable to " - + "\"java.lang.Boolean\" or be a Json string representation of it for " - + "Avro unmarshalling", e.getMessage()); - } + assertThatThrownBy(() -> schemaHelper1.unmarshal(0)) + .hasMessage("AvroTest:0.0.1: object \"0\" of type \"java.lang.Integer\" must be assignable to " + + "\"java.lang.Boolean\" or be a Json string representation of it for " + + "Avro unmarshalling"); } /** @@ -155,13 +136,9 @@ public class AvroSchemaHelperUnmarshalTest { final SchemaHelper schemaHelper2 = new SchemaHelperFactory().createSchemaHelper(testKey, avroIntSchema.getKey()); - try { - schemaHelper2.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Integer\" " - + "using the default constructor \"Integer()\"", e.getMessage()); - } + assertThatThrownBy(schemaHelper2::createNewInstance) + .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Integer\" " + + "using the default constructor \"Integer()\""); assertEquals(123, schemaHelper2.createNewInstance("123")); assertEquals(0, schemaHelper2.unmarshal("0")); @@ -171,27 +148,15 @@ public class AvroSchemaHelperUnmarshalTest { assertEquals(-1, schemaHelper2.unmarshal("-1.23")); assertEquals(2147483647, schemaHelper2.unmarshal("2147483647")); assertEquals(-2147483648, schemaHelper2.unmarshal("-2147483648")); - try { - schemaHelper2.unmarshal("2147483648"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"2147483648\" Avro unmarshalling failed: " - + "Numeric value (2147483648) out of range of int")); - } - try { - schemaHelper2.unmarshal("-2147483649"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().startsWith("AvroTest:0.0.1: object \"-2147483649\" Avro unmarshalling failed: " - + "Numeric value (-2147483649) out of range of int")); - } - try { - schemaHelper2.unmarshal(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " - + "String to read from cannot be null!")); - } + assertThatThrownBy(() -> schemaHelper2.unmarshal("2147483648")) + .hasMessageStartingWith("AvroTest:0.0.1: object \"2147483648\" Avro unmarshalling failed: " + + "Numeric value (2147483648) out of range of int"); + assertThatThrownBy(() -> schemaHelper2.unmarshal("-2147483649")) + .hasMessageStartingWith("AvroTest:0.0.1: object \"-2147483649\" Avro unmarshalling failed: " + + "Numeric value (-2147483649) out of range of int"); + assertThatThrownBy(() -> schemaHelper2.unmarshal(null)) + .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " + + "String to read from cannot be null!"); } /** @@ -206,13 +171,9 @@ public class AvroSchemaHelperUnmarshalTest { final SchemaHelper schemaHelper3 = new SchemaHelperFactory().createSchemaHelper(testKey, avroLongSchema.getKey()); - try { - schemaHelper3.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" " - + "using the default constructor \"Long()\"", e.getMessage()); - } + assertThatThrownBy(schemaHelper3::createNewInstance) + .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" " + + "using the default constructor \"Long()\""); assertEquals(123456789L, schemaHelper3.createNewInstance("123456789")); assertEquals(0L, schemaHelper3.unmarshal("0")); @@ -222,36 +183,18 @@ public class AvroSchemaHelperUnmarshalTest { assertEquals(-1L, schemaHelper3.unmarshal("-1.23")); assertEquals(9223372036854775807L, schemaHelper3.unmarshal("9223372036854775807")); assertEquals(-9223372036854775808L, schemaHelper3.unmarshal("-9223372036854775808")); - try { - schemaHelper3.unmarshal("9223372036854775808"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: object \"9223372036854775808\" Avro unmarshalling failed: " - + "Numeric value (9223372036854775808) out of range of long")); - } - try { - schemaHelper3.unmarshal("-9223372036854775809"); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage() - .startsWith("AvroTest:0.0.1: object \"-9223372036854775809\" Avro unmarshalling failed: " - + "Numeric value (-9223372036854775809) out of range of long")); - } - try { - schemaHelper3.unmarshal("\"Hello\""); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: " - + "Expected long. Got VALUE_STRING")); - } - try { - schemaHelper3.unmarshal(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " - + "String to read from cannot be null!")); - } + assertThatThrownBy(() -> schemaHelper3.unmarshal("9223372036854775808")) + .hasMessageStartingWith("AvroTest:0.0.1: object \"9223372036854775808\" Avro unmarshalling failed: " + + "Numeric value (9223372036854775808) out of range of long"); + assertThatThrownBy(() -> schemaHelper3.unmarshal("-9223372036854775809")) + .hasMessageStartingWith("AvroTest:0.0.1: object \"-9223372036854775809\" Avro unmarshalling failed: " + + "Numeric value (-9223372036854775809) out of range of long"); + assertThatThrownBy(() -> schemaHelper3.unmarshal("\"Hello\"")) + .hasMessage("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: " + + "Expected long. Got VALUE_STRING"); + assertThatThrownBy(() -> schemaHelper3.unmarshal(null)) + .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " + + "String to read from cannot be null!"); } /** @@ -266,13 +209,9 @@ public class AvroSchemaHelperUnmarshalTest { final SchemaHelper schemaHelper4 = new SchemaHelperFactory().createSchemaHelper(testKey, avroFloatSchema.getKey()); - try { - schemaHelper4.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Float\" " - + "using the default constructor \"Float()\"", e.getMessage()); - } + assertThatThrownBy(schemaHelper4::createNewInstance) + .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Float\" " + + "using the default constructor \"Float()\""); assertEquals(1.2345F, schemaHelper4.createNewInstance("1.2345")); assertEquals(0.0F, schemaHelper4.unmarshal("0")); @@ -284,20 +223,12 @@ public class AvroSchemaHelperUnmarshalTest { assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775808")); assertEquals(9.223372E18F, schemaHelper4.unmarshal("9223372036854775808")); assertEquals(-9.223372E18F, schemaHelper4.unmarshal("-9223372036854775809")); - try { - schemaHelper4.unmarshal("\"Hello\""); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: " - + "Expected float. Got VALUE_STRING")); - } - try { - schemaHelper4.unmarshal(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " - + "String to read from cannot be null!")); - } + assertThatThrownBy(() -> schemaHelper4.unmarshal("\"Hello\"")) + .hasMessage("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: " + + "Expected float. Got VALUE_STRING"); + assertThatThrownBy(() -> schemaHelper4.unmarshal(null)) + .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " + + "String to read from cannot be null!"); } /** @@ -312,13 +243,9 @@ public class AvroSchemaHelperUnmarshalTest { final SchemaHelper schemaHelper5 = new SchemaHelperFactory().createSchemaHelper(testKey, avroDoubleSchema.getKey()); - try { - schemaHelper5.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance of class \"java.lang.Double\" " - + "using the default constructor \"Double()\"", e.getMessage()); - } + assertThatThrownBy(schemaHelper5::createNewInstance) + .hasMessage("AvroTest:0.0.1: could not create an instance of class \"java.lang.Double\" " + + "using the default constructor \"Double()\""); assertEquals(1.2345E06, schemaHelper5.createNewInstance("1.2345E06")); assertEquals(0.0, schemaHelper5.unmarshal("0")); @@ -330,20 +257,12 @@ public class AvroSchemaHelperUnmarshalTest { assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775808")); assertEquals(9.223372036854776E18, schemaHelper5.unmarshal("9223372036854775808")); assertEquals(-9.223372036854776E18, schemaHelper5.unmarshal("-9223372036854775809")); - try { - schemaHelper5.unmarshal("\"Hello\""); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: " - + "Expected double. Got VALUE_STRING")); - } - try { - schemaHelper5.unmarshal(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " - + "String to read from cannot be null!")); - } + assertThatThrownBy(() -> schemaHelper5.unmarshal("\"Hello\"")) + .hasMessage("AvroTest:0.0.1: object \"\"Hello\"\" Avro unmarshalling failed: " + + "Expected double. Got VALUE_STRING"); + assertThatThrownBy(() -> schemaHelper5.unmarshal(null)) + .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " + + "String to read from cannot be null!"); } /** @@ -372,13 +291,9 @@ public class AvroSchemaHelperUnmarshalTest { assertEquals("-9223372036854775809", schemaHelper7.unmarshal("-9223372036854775809")); assertEquals("Hello", schemaHelper7.unmarshal("Hello")); assertEquals("Hello", schemaHelper7.unmarshal(new Utf8("Hello"))); - try { - schemaHelper7.unmarshal(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " - + "String to read from cannot be null!")); - } + assertThatThrownBy(() -> schemaHelper7.unmarshal(null)) + .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " + + "String to read from cannot be null!"); } /** @@ -392,13 +307,9 @@ public class AvroSchemaHelperUnmarshalTest { schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - try { - schemaHelper.createNewInstance(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("AvroTest:0.0.1: could not create an instance of class \"[Ljava.lang.Byte;\" " - + "using the default constructor \"Byte[]()\"", e.getMessage()); - } + assertThatThrownBy(schemaHelper::createNewInstance) + .hasMessage("AvroTest:0.0.1: could not create an instance of class \"[Ljava.lang.Byte;\" " + + "using the default constructor \"Byte[]()\""); final byte[] newBytes = (byte[]) schemaHelper.createNewInstance("\"hello\""); assertEquals(5, newBytes.length); assertEquals(104, newBytes[0]); @@ -407,12 +318,8 @@ public class AvroSchemaHelperUnmarshalTest { assertEquals(108, newBytes[3]); assertEquals(111, newBytes[4]); - try { - schemaHelper.unmarshal(null); - fail("Test should throw an exception here"); - } catch (final Exception e) { - assertTrue(e.getMessage().equals("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " - + "String to read from cannot be null!")); - } + assertThatThrownBy(() -> schemaHelper.unmarshal(null)) + .hasMessage("AvroTest:0.0.1: object \"null\" Avro unmarshalling failed: " + + "String to read from cannot be null!"); } } diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaRecordTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaRecordTest.java index ca1ac6561..8d85f4ecb 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaRecordTest.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaRecordTest.java @@ -21,15 +21,14 @@ package org.onap.policy.apex.plugins.context.schema.avro; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import java.io.IOException; import org.apache.avro.generic.GenericRecord; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.onap.policy.apex.context.ContextRuntimeException; import org.onap.policy.apex.context.SchemaHelper; import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; import org.onap.policy.apex.context.parameters.ContextParameterConstants; @@ -149,13 +148,8 @@ public class AvroSchemaRecordTest { subRecord = (GenericRecord) schemaHelper.createNewSubInstance("EmailAddress"); assertEquals(null, subRecord.get("address")); - try { - subRecord = (GenericRecord) schemaHelper.createNewSubInstance("IDontExist"); - fail("test should throw an exception here"); - } catch (ContextRuntimeException cre) { - assertEquals("AvroTest:0.0.1: the schema \"User\" does not have a subtype of type \"IDontExist\"", - cre.getMessage()); - } + assertThatThrownBy(() -> schemaHelper.createNewSubInstance("IDontExist")) + .hasMessage("AvroTest:0.0.1: the schema \"User\" does not have a subtype of type \"IDontExist\""); } /** diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java index ea5e80161..e506513a8 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java @@ -21,6 +21,7 @@ package org.onap.policy.apex.plugins.event.carrier.restclient; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -76,27 +77,19 @@ public class ApexRestClientConusmerTest { EventHandlerParameters consumerParameters = new EventHandlerParameters(); SupportApexEventReceiver incomingEventReceiver = new SupportApexEventReceiver(); - try { - arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); - fail("test should throw an exception here"); - } catch (ApexEventException e) { - assertEquals( - "specified consumer properties are not applicable to REST client consumer (RestClientConsumer)", - e.getMessage()); - } + assertThatThrownBy(() -> arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver)) + .hasMessageContaining("specified consumer properties are not applicable to REST client" + + " consumer (RestClientConsumer)"); RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(rcctp); rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE); - try { + assertThatThrownBy(() -> { arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); - fail("test should throw an exception here"); - } catch (ApexEventException e) { - assertEquals("specified HTTP method of \"DELETE\" is invalid, only HTTP method \"GET\" is supported " - + "for event reception on REST client consumer (RestClientConsumer)", e.getMessage()); - } + }).hasMessageContaining("specified HTTP method of \"DELETE\" is invalid, only HTTP method \"GET\" is " + + "supported for event reception on REST client consumer (RestClientConsumer)"); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rcctp.getHttpMethod()); rcctp.setHttpMethod(null); rcctp.setHttpCodeFilter("zzz"); @@ -174,7 +167,7 @@ public class ApexRestClientConusmerTest { } @Test - public void testApexRestClientConsumerJsonError() { + public void testApexRestClientConsumerJsonError() throws ApexEventException { MockitoAnnotations.initMocks(this); ApexRestClientConsumer arcc = new ApexRestClientConsumer(); @@ -186,18 +179,13 @@ public class ApexRestClientConusmerTest { consumerParameters.setCarrierTechnologyParameters(rcctp); rcctp.setHttpCodeFilter("[1-5][0][0-5]"); - try { - arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); - - assertEquals("RestClientConsumer", arcc.getName()); + arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); + assertEquals("RestClientConsumer", arcc.getName()); - arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); @@ -341,14 +329,8 @@ public class ApexRestClientConusmerTest { ByteArrayOutputStream outContent = new ByteArrayOutputStream(); System.setOut(new PrintStream(outContent)); - try { - // We have not set the URL, this test should not receive any events - arcc.start(); - await().atMost(200, TimeUnit.MILLISECONDS).until(() -> incomingEventReceiver.getEventCount() == 0); - arcc.stop(); - } catch (Exception e) { - // test invalid status code - assertEquals("received an invalid status code \"200\"", e.getMessage()); - } + arcc.start(); + await().atMost(200, TimeUnit.MILLISECONDS).until(() -> incomingEventReceiver.getEventCount() == 0); + arcc.stop(); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java index 0ee8fe6ad..a4ef6e38e 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java @@ -21,9 +21,9 @@ package org.onap.policy.apex.plugins.event.carrier.restclient; +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 javax.ws.rs.client.Client; @@ -64,29 +64,18 @@ public class ApexRestClientProducerTest { assertNotNull(arcp); EventHandlerParameters producerParameters = new EventHandlerParameters(); - try { - arcp.init("RestClientProducer", producerParameters); - fail("test should throw an exception here"); - } catch (ApexEventException e) { - assertEquals( - "specified producer properties are not applicable to REST client producer (RestClientProducer)", - e.getMessage()); - } + assertThatThrownBy(() -> arcp.init("RestClientProducer", producerParameters)) + .hasMessage("specified producer properties are not applicable to REST client producer" + + " (RestClientProducer)"); RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); producerParameters.setCarrierTechnologyParameters(rcctp); rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE); - try { - arcp.init("RestClientConsumer", producerParameters); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); - fail("test should throw an exception here"); - } catch (ApexEventException e) { - assertEquals( - "specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" " - + "are supported for event sending on REST client producer (RestClientConsumer)", - e.getMessage()); - } + assertThatThrownBy(() -> arcp.init("RestClientConsumer", producerParameters)) + .hasMessage("specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\"" + + " and \"PUT\" are supported for event sending on REST client producer (RestClientConsumer)"); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rcctp.getHttpMethod()); rcctp.setHttpMethod(null); arcp.init("RestClientConsumer", producerParameters); assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); @@ -168,15 +157,12 @@ public class ApexRestClientProducerTest { rcctp.setUrl("http://some.place2.that.{key}.not/{tag}and.again.{tag}"); Properties properties = new Properties(); properties.put("tag", "exist"); - try { + assertThatThrownBy(() -> { arcp.sendEvent(123, properties, "EventName", "This is an Event"); arcp.stop(); - fail("test should throw an exception"); - } catch (Exception e) { - assertEquals("key \"key\" specified on url " - + "\"http://some.place2.that.{key}.not/{tag}and.again.{tag}\" not found " - + "in execution properties passed by the current policy", e.getMessage()); - } + }).hasMessageContaining("key \"key\" specified on url " + + "\"http://some.place2.that.{key}.not/{tag}and.again.{tag}\" not found " + + "in execution properties passed by the current policy"); } @Test @@ -303,14 +289,8 @@ public class ApexRestClientProducerTest { Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); arcp.setClient(httpClientMock); - try { - arcp.sendEvent(123, null, "EventName", "This is an Event"); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals( - "send of event to URL \"http://some.place.that.does.not/exist\" using HTTP \"POST\" " - + "failed with status code 400 and message \"null\", event:\n" + "This is an Event", - e.getMessage()); - } + assertThatThrownBy(() -> arcp.sendEvent(123, null, "EventName", "This is an Event")) + .hasMessageContaining("send of event to URL \"http://some.place.that.does.not/exist\" using HTTP \"POST\" " + + "failed with status code 400 and message \"null\", event:\n" + "This is an Event"); } } \ No newline at end of file diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java index 42a9c0f73..f704ad3ec 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java @@ -22,10 +22,10 @@ package org.onap.policy.apex.plugins.event.carrier.restclient; import static org.assertj.core.api.Assertions.assertThatCode; +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.util.Set; import org.junit.Test; @@ -45,13 +45,8 @@ public class RestClientCarrierTechnologyParametersTest { arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json"); arguments.setRelativeFileRoot("."); - try { - new ApexParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (ParameterException pe) { - assertTrue(pe.getMessage().contains("HTTP header array entry is null\n parameter")); - assertTrue(pe.getMessage().trim().endsWith("HTTP header array entry is null")); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessageContaining("HTTP header array entry is null\n parameter"); } @Test @@ -60,15 +55,9 @@ public class RestClientCarrierTechnologyParametersTest { arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json"); arguments.setRelativeFileRoot("."); - try { - new ApexParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (ParameterException pe) { - assertTrue(pe.getMessage() - .contains("HTTP header array entries must have one key and one value: [aaa, bbb, ccc]")); - assertTrue(pe.getMessage().trim() - .endsWith("HTTP header array entries must have one key and one value: [aaa]")); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessageContaining("HTTP header array entries must have one key and one value: [aaa, bbb, ccc]") + .hasMessageEndingWith("HTTP header array entries must have one key and one value: [aaa]\n"); } @Test @@ -77,13 +66,9 @@ public class RestClientCarrierTechnologyParametersTest { arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json"); arguments.setRelativeFileRoot("."); - try { - new ApexParameterHandler().getParameters(arguments); - fail("test should throw an exception here"); - } catch (ParameterException pe) { - assertTrue(pe.getMessage().contains("HTTP header key is null or blank: [null, bbb]")); - assertTrue(pe.getMessage().trim().endsWith("HTTP header value is null or blank: [ccc, null]")); - } + assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)) + .hasMessageContaining("HTTP header key is null or blank: [null, bbb]") + .hasMessageEndingWith("HTTP header value is null or blank: [ccc, null]\n"); } @Test diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolTest.java index 3d620ebf4..2fbae65e4 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolTest.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolTest.java @@ -21,8 +21,8 @@ package org.onap.policy.apex.plugins.event.protocol.yaml; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import java.io.File; import java.io.FileInputStream; @@ -215,13 +215,8 @@ public class YamlEventProtocolTest { */ @Test public void testYamlProcessing() throws ApexEventException, IOException { - try { - testYamlDecodeEncode("TestEvent0", 1, 0, "Empty0"); - fail("test should fail here"); - } catch (ApexEventException e) { - assertEquals("event processing failed, event is null", e.getMessage()); - } - + assertThatThrownBy(() -> testYamlDecodeEncode("TestEvent0", 1, 0, "Empty0")) + .hasMessage("event processing failed, event is null"); testYamlDecodeEncode("TestEvent0", 1, 0, "Empty1"); testYamlDecodeEncode("TestEvent1", 1, 1, "Collection0"); testYamlDecodeEncode("TestEvent2", 1, 3, "Collection1"); diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java index 6f82c7dfd..398bddb16 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlPluginStabilityTest.java @@ -1,6 +1,7 @@ /*- * ============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. @@ -20,9 +21,9 @@ package org.onap.policy.apex.plugins.event.protocol.yaml; +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.IOException; import java.util.List; @@ -41,7 +42,6 @@ import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; import org.onap.policy.apex.model.eventmodel.concepts.AxField; import org.onap.policy.apex.service.engine.event.ApexEvent; import org.onap.policy.apex.service.engine.event.ApexEventException; -import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; import org.onap.policy.common.parameters.ParameterService; /** @@ -112,52 +112,22 @@ public class YamlPluginStabilityTest { public void testStability() throws ApexEventException { Apex2YamlEventConverter converter = new Apex2YamlEventConverter(); - try { - converter.init(null); - fail("this test should throw an exception"); - } catch (ApexEventRuntimeException e) { - assertEquals("specified consumer properties are not applicable to the YAML event protocol", e.getMessage()); - } - + assertThatThrownBy(() -> converter.init(null)) + .hasMessage("specified consumer properties are not applicable to the YAML event protocol"); YamlEventProtocolParameters pars = new YamlEventProtocolParameters(); converter.init(pars); - try { - converter.toApexEvent("NonExistantEvent", ""); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: an event definition for an event named \"NonExistantEvent\"", - e.getMessage().substring(0, 89)); - } - - try { - converter.toApexEvent("TestEvent", null); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("event processing failed, event is null", e.getMessage()); - } - - try { - converter.toApexEvent("TestEvent", 1); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("error converting event \"1\" to a string", e.getMessage()); - } - - try { - converter.toApexEvent("TestEvent", ""); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertTrue(e.getMessage().contains("Field \"doubleValue\" is missing")); - } - - try { - converter.fromApexEvent(null); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("event processing failed, Apex event is null", e.getMessage()); - } - + assertThatThrownBy(() -> converter.toApexEvent("NonExistantEvent", "")) + .hasMessageContaining("Failed to unmarshal YAML event: an event definition for an event named " + + "\"NonExistantEvent\""); + assertThatThrownBy(() -> converter.toApexEvent("TestEvent", null)) + .hasMessage("event processing failed, event is null"); + assertThatThrownBy(() -> converter.toApexEvent("TestEvent", 1)) + .hasMessage("error converting event \"1\" to a string"); + assertThatThrownBy(() -> converter.toApexEvent("TestEvent", "")) + .hasMessageContaining("Field \"doubleValue\" is missing"); + assertThatThrownBy(() -> converter.fromApexEvent(null)) + .hasMessage("event processing failed, Apex event is null"); ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(), testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget()); apexEvent.put("doubleValue", 123.45); @@ -169,31 +139,15 @@ public class YamlPluginStabilityTest { assertTrue(yamlString.contains("my wonderful exception message")); apexEvent.remove("intValue"); - try { - yamlString = (String) converter.fromApexEvent(apexEvent); - fail("this test should throw an exception"); - } catch (ApexEventRuntimeException e) { - assertEquals("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing", - e.getMessage().substring(0, 72)); - } - - try { - converter.toApexEvent(null, ""); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"", - e.getMessage().substring(0, 81)); - } - + assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)) + .hasMessageContaining("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing"); + assertThatThrownBy(() -> converter.toApexEvent(null, "")) + .hasMessageContaining("Failed to unmarshal YAML event: event received without mandatory parameter" + + " \"name\""); pars.setNameAlias("TheNameField"); - try { - converter.toApexEvent(null, ""); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"", - e.getMessage().substring(0, 81)); - } - + assertThatThrownBy(() -> converter.toApexEvent(null, "")) + .hasMessageContaining("Failed to unmarshal YAML event: event received without mandatory parameter" + + " \"name\""); apexEvent.put("intValue", 123); apexEvent.remove("stringValue"); @@ -228,14 +182,9 @@ public class YamlPluginStabilityTest { yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace"; pars.setVersionAlias(null); pars.setNameSpaceAlias("stringValue"); - try { - converter.toApexEvent("TestEvent", yamlInputString); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: namespace \"org.some.other.namespace\" on event", - e.getMessage().substring(0, 77)); - } - + final String yamlInputStringCopy = yamlInputString; + assertThatThrownBy(() -> converter.toApexEvent("TestEvent", yamlInputStringCopy)) + .hasMessageContaining("Failed to unmarshal YAML event: namespace \"org.some.other.namespace\" on event"); yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml"; eventList = converter.toApexEvent("TestEvent", yamlInputString); @@ -257,21 +206,12 @@ public class YamlPluginStabilityTest { yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString"; pars.setSourceAlias(null); pars.setTargetAlias("intValue"); - try { - converter.toApexEvent("TestEvent", yamlInputString); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertEquals("Failed to unmarshal YAML event: field \"target\" with type \"java.lang.Integer\"", - e.getMessage().substring(0, 76)); - } + assertThatThrownBy(() -> converter.toApexEvent("TestEvent", yamlInputStringCopy)) + .hasMessageContaining("Failed to unmarshal YAML event: field \"target\" with type \"java.lang.Integer\""); pars.setTargetAlias(null); - yamlInputString = "doubleValue: 123.45\n" + "intValue: ~\n" + "stringValue: MyString"; - try { - converter.toApexEvent("TestEvent", yamlInputString); - fail("this test should throw an exception"); - } catch (ApexEventException e) { - assertTrue(e.getMessage().contains("mandatory field \"intValue\" is missing")); - } + assertThatThrownBy(() -> converter.toApexEvent("TestEvent", "doubleValue: 123.45\n" + "intValue: ~\n" + + "stringValue: MyString")) + .hasMessageContaining("mandatory field \"intValue\" is missing"); } } diff --git a/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutorTest.java b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutorTest.java index 106467ac4..a1c296436 100644 --- a/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-java/src/test/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutorTest.java @@ -1,6 +1,7 @@ /*- * ============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. @@ -20,9 +21,9 @@ package org.onap.policy.apex.plugins.executor.java; +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 org.junit.After; @@ -75,88 +76,48 @@ public class JavaStateFinalizerExecutorTest { } @Test - public void testJavaStateFinalizerExecutor() { + public void testJavaStateFinalizerExecutor() throws StateMachineException, ContextException { JavaStateFinalizerExecutor jsfe = new JavaStateFinalizerExecutor(); assertNotNull(jsfe); - try { - jsfe.prepare(); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals(java.lang.NullPointerException.class, jtseException.getClass()); - } - + assertThatThrownBy(jsfe::prepare).isInstanceOf(java.lang.NullPointerException.class); ApexInternalContext internalContext = null; - try { - internalContext = new ApexInternalContext(new AxPolicyModel()); - } catch (ContextException e) { - fail("test should not throw an exception here"); - } + + internalContext = new ApexInternalContext(new AxPolicyModel()); StateExecutor parentStateExcutor = null; - try { - parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl()); - } catch (StateMachineException e) { - fail("test should not throw an exception here"); - } + + parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl()); AxState state = new AxState(); parentStateExcutor.setContext(null, state, internalContext); AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic(); jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext); - try { - jsfe.prepare(); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals("instantiation error on Java class \"\"", jtseException.getMessage()); - } - + assertThatThrownBy(jsfe::prepare) + .hasMessage("instantiation error on Java class \"\""); stateFinalizerLogic.setLogic("java.lang.String"); - try { - jsfe.prepare(); - } catch (Exception jtseException) { - fail("test should not throw an exception here"); - } - - try { - jsfe.execute(-1, new Properties(), null); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals("state finalizer logic failed to run for state finalizer \"NULL:0.0.0:NULL:NULL\"", - jtseException.getMessage()); - } + jsfe.prepare(); + assertThatThrownBy(() -> jsfe.execute(-1, new Properties(), null)) + .hasMessage("state finalizer logic failed to run for state finalizer \"NULL:0.0.0:NULL:NULL\""); AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1")); EnEvent event = new EnEvent(axEvent); - try { - jsfe.execute(-1, new Properties(), event); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals("state finalizer logic failed to run for state finalizer \"NULL:0.0.0:NULL:NULL\"", - jtseException.getMessage()); - } - + assertThatThrownBy(() -> jsfe.execute(-1, new Properties(), event)) + .hasMessage("state finalizer logic failed to run for state finalizer \"NULL:0.0.0:NULL:NULL\""); stateFinalizerLogic.setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaStateFinalizerLogic"); - try { - jsfe.prepare(); + jsfe.prepare(); + assertThatThrownBy(() -> { jsfe.execute(-1, new Properties(), event); - fail("test should throw an exception here"); - } catch (Exception jtseException) { - assertEquals("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" " - + "on finalizer logic NULL:0.0.0:NULL:NULL", jtseException.getMessage()); - } - + }).hasMessage("execute-post: state finalizer logic execution failure on state " + + "\"NULL:0.0.0:NULL:NULL\" on finalizer logic NULL:0.0.0:NULL:NULL"); state.getStateOutputs().put("SelectedOutputIsMe", null); - try { - jsfe.prepare(); - String stateOutput = jsfe.execute(0, new Properties(), event); - assertEquals("SelectedOutputIsMe", stateOutput); - jsfe.cleanUp(); - } catch (Exception jtseException) { - jtseException.printStackTrace(); - fail("test should not throw an exception here"); - } + + jsfe.prepare(); + String stateOutput = jsfe.execute(0, new Properties(), event); + assertEquals("SelectedOutputIsMe", stateOutput); + jsfe.cleanUp(); + } } -- cgit 1.2.3-korg