From e5b7871aaca39d60c3baa7ef25946c2e5d707441 Mon Sep 17 00:00:00 2001 From: MaxS-9779 Date: Sat, 8 Nov 2025 14:51:55 +0300 Subject: [PATCH] Added EYaml for reusable in EValProf to support profile settings. Added some tests to EYaml. --- .../io/github/artemget/entrys/yaml/EYaml.java | 57 +++++++++++++ .../artemget/entrys/yaml/package-info.java | 28 +++++++ .../artemget/entrys/yaml/EYamlTest.java | 80 +++++++++++++++++++ .../artemget/entrys/yaml/package-info.java | 28 +++++++ 4 files changed, 193 insertions(+) create mode 100644 src/main/java/io/github/artemget/entrys/yaml/EYaml.java create mode 100644 src/main/java/io/github/artemget/entrys/yaml/package-info.java create mode 100644 src/test/java/io/github/artemget/entrys/yaml/EYamlTest.java create mode 100644 src/test/java/io/github/artemget/entrys/yaml/package-info.java diff --git a/src/main/java/io/github/artemget/entrys/yaml/EYaml.java b/src/main/java/io/github/artemget/entrys/yaml/EYaml.java new file mode 100644 index 0000000..5dbb3a3 --- /dev/null +++ b/src/main/java/io/github/artemget/entrys/yaml/EYaml.java @@ -0,0 +1,57 @@ +/* + * MIT License + * + * Copyright (c) 2024-2025. Artem Getmanskii + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package io.github.artemget.entrys.yaml; + +import io.github.artemget.entrys.ESafe; +import io.github.artemget.entrys.file.EVal; + +/** + * By default gets entries from "src/main/resources/application.yaml". + * + * @since 0.4.0 + */ +public class EYaml extends ESafe { + + /** + * From yaml file at default dir. + * + * @param key Of entry + */ + public EYaml(final String key) { + this(key, "src/main/resources/application.yaml"); + } + + /** + * From yaml file at custom dir. + * + * @param key Of entry + * @param path To yaml file + */ + public EYaml(final String key, final String path) { + super( + () -> new EVal(key, path).value() + ); + } +} diff --git a/src/main/java/io/github/artemget/entrys/yaml/package-info.java b/src/main/java/io/github/artemget/entrys/yaml/package-info.java new file mode 100644 index 0000000..2a8a3ea --- /dev/null +++ b/src/main/java/io/github/artemget/entrys/yaml/package-info.java @@ -0,0 +1,28 @@ +/* + * MIT License + * + * Copyright (c) 2024-2025. Artem Getmanskii + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * System entries directory. + */ +package io.github.artemget.entrys.yaml; diff --git a/src/test/java/io/github/artemget/entrys/yaml/EYamlTest.java b/src/test/java/io/github/artemget/entrys/yaml/EYamlTest.java new file mode 100644 index 0000000..5d795ea --- /dev/null +++ b/src/test/java/io/github/artemget/entrys/yaml/EYamlTest.java @@ -0,0 +1,80 @@ +/* + * MIT License + * + * Copyright (c) 2024-2025. Artem Getmanskii + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package io.github.artemget.entrys.yaml; + +import io.github.artemget.entrys.EntryException; +import io.github.artemget.entrys.file.EVal; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; + +/** + * Test cases for {@link io.github.artemget.entrys.yaml.EYaml}. + * + * @since 0.3.0 + */ +@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"}) +final class EYamlTest { + + @Test + void parsesWithDefaultPath() throws EntryException { + final MockedConstruction mocked = Mockito.mockConstruction( + EVal.class, + (mock, context) -> Mockito.when(mock.value()).thenReturn("123") + ); + Assertions.assertEquals( + "123", + new EYaml("age").value() + ); + mocked.close(); + } + + @Test + void parsesWithCustomPath() throws EntryException { + final MockedConstruction mocked = Mockito.mockConstruction( + EVal.class, + (mock, context) -> Mockito.when(mock.value()).thenReturn("12345") + ); + Assertions.assertEquals( + "12345", + new EYaml("age", "configuration/application-test.yaml").value() + ); + mocked.close(); + } + + @Test + void shouldThrowException() { + final MockedConstruction mocked = Mockito.mockConstruction( + EVal.class, + (mock, context) -> Mockito.when(mock.value()).thenThrow(EntryException.class) + ); + Assertions.assertThrows( + EntryException.class, + () -> new EYaml("age", "src/main/resources/application.yaml").value() + ); + mocked.close(); + } +} diff --git a/src/test/java/io/github/artemget/entrys/yaml/package-info.java b/src/test/java/io/github/artemget/entrys/yaml/package-info.java new file mode 100644 index 0000000..222746f --- /dev/null +++ b/src/test/java/io/github/artemget/entrys/yaml/package-info.java @@ -0,0 +1,28 @@ +/* + * MIT License + * + * Copyright (c) 2024-2025. Artem Getmanskii + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * Test cases for system entries. + */ +package io.github.artemget.entrys.yaml;