diff options
author | Pamela Dragosh <pdragosh@research.att.com> | 2020-07-16 13:51:38 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-07-16 13:51:38 +0000 |
commit | 8180d5fc70e966973390c0981c694091c6e6df49 (patch) | |
tree | 925baae0addde22712cb0ec28ce5d9232b852bd6 /plugins/plugins-context/plugins-context-schema | |
parent | e9f108f0fcfff74f8960136a1723742d97f72c26 (diff) | |
parent | b8e3df6f43e46159c210ac7d1ec6983871d52ea5 (diff) |
Merge "Replaced try/catch blocks with assertj - apex-pdp"
Diffstat (limited to 'plugins/plugins-context/plugins-context-schema')
6 files changed, 150 insertions, 379 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\""); } /** |