diff options
author | waynedunican <wayne.dunican@est.tech> | 2020-07-10 08:25:22 +0100 |
---|---|---|
committer | waynedunican <wayne.dunican@est.tech> | 2020-07-15 13:42:21 +0100 |
commit | b8e3df6f43e46159c210ac7d1ec6983871d52ea5 (patch) | |
tree | 4992a599861db084882ad6d881ed011dd9569aaa /plugins | |
parent | 592e04f6301dd38ae48d38501cc251fc3d5ad2fb (diff) |
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 <wayne.dunican@est.tech>
Diffstat (limited to 'plugins')
12 files changed, 250 insertions, 636 deletions
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(); + } } |