From 3a078d7c9ccf958bc82f3551e84a0e3db67de94d Mon Sep 17 00:00:00 2001 From: "thibault.faure" Date: Tue, 6 Jun 2023 23:02:22 +0200 Subject: [PATCH] BAEL-5977 Code for the Throw Exception for Unexpected Input for Enum with Mapstruct article --- mapstruct/pom.xml | 2 +- .../java/com/baeldung/enums/InputLevel.java | 7 +++++ .../java/com/baeldung/enums/LevelMapper.java | 13 +++++++++ .../java/com/baeldung/enums/OutputLevel.java | 7 +++++ .../baeldung/enums/LevelMapperUnitTest.java | 28 +++++++++++++++++++ 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 mapstruct/src/main/java/com/baeldung/enums/InputLevel.java create mode 100644 mapstruct/src/main/java/com/baeldung/enums/LevelMapper.java create mode 100644 mapstruct/src/main/java/com/baeldung/enums/OutputLevel.java create mode 100644 mapstruct/src/test/java/com/baeldung/enums/LevelMapperUnitTest.java diff --git a/mapstruct/pom.xml b/mapstruct/pom.xml index 62c9ca5870..7c279acb7b 100644 --- a/mapstruct/pom.xml +++ b/mapstruct/pom.xml @@ -73,7 +73,7 @@ - 1.5.3.Final + 1.5.5.Final 4.3.4.RELEASE 0.2.0 diff --git a/mapstruct/src/main/java/com/baeldung/enums/InputLevel.java b/mapstruct/src/main/java/com/baeldung/enums/InputLevel.java new file mode 100644 index 0000000000..ba7e6735be --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/enums/InputLevel.java @@ -0,0 +1,7 @@ +package com.baeldung.enums; + +enum InputLevel { + + LOW, MEDIUM, HIGH + +} diff --git a/mapstruct/src/main/java/com/baeldung/enums/LevelMapper.java b/mapstruct/src/main/java/com/baeldung/enums/LevelMapper.java new file mode 100644 index 0000000000..d45d98b075 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/enums/LevelMapper.java @@ -0,0 +1,13 @@ +package com.baeldung.enums; + +import org.mapstruct.Mapper; +import org.mapstruct.MappingConstants; +import org.mapstruct.ValueMapping; + +@Mapper +interface LevelMapper { + + @ValueMapping(source = MappingConstants.ANY_REMAINING, target = MappingConstants.THROW_EXCEPTION) + OutputLevel inputLevelToOutputLevel(InputLevel inputLevel); + +} diff --git a/mapstruct/src/main/java/com/baeldung/enums/OutputLevel.java b/mapstruct/src/main/java/com/baeldung/enums/OutputLevel.java new file mode 100644 index 0000000000..c1554ec9b4 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/enums/OutputLevel.java @@ -0,0 +1,7 @@ +package com.baeldung.enums; + +enum OutputLevel { + + LOW, HIGH + +} diff --git a/mapstruct/src/test/java/com/baeldung/enums/LevelMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/enums/LevelMapperUnitTest.java new file mode 100644 index 0000000000..a933ad7e83 --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/enums/LevelMapperUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.enums; + +import org.junit.jupiter.api.Test; +import org.mapstruct.factory.Mappers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class LevelMapperUnitTest { + + LevelMapper levelMapper = Mappers.getMapper(LevelMapper.class); + + @Test + void givenHighInputLevel_WhenInputLevelToOutputLevel_ThenHighOutputLevel() { + assertEquals(OutputLevel.HIGH, levelMapper.inputLevelToOutputLevel(InputLevel.HIGH)); + } + + @Test + void givenMediumInputLevel_WhenInputLevelToOutputLevel_ThenThrows() { + assertThrows(IllegalArgumentException.class, () -> levelMapper.inputLevelToOutputLevel(InputLevel.MEDIUM)); + } + + @Test + void givenLowInputLevel_WhenInputLevelToOutputLevel_ThenLowOutputLevel() { + assertEquals(OutputLevel.LOW, levelMapper.inputLevelToOutputLevel(InputLevel.LOW)); + } + +}