|
| 1 | +/*- |
| 2 | + * =LICENSE= |
| 3 | + * ORAS Java SDK |
| 4 | + * === |
| 5 | + * Copyright (C) 2024 - 2025 ORAS |
| 6 | + * === |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * =LICENSEEND= |
| 19 | + */ |
| 20 | + |
| 21 | +package land.oras.utils; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.util.LinkedHashMap; |
| 31 | +import java.util.Map; |
| 32 | +import land.oras.exception.OrasException; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.io.CleanupMode; |
| 35 | +import org.junit.jupiter.api.io.TempDir; |
| 36 | +import org.junit.jupiter.api.parallel.Execution; |
| 37 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 38 | + |
| 39 | +@Execution(ExecutionMode.CONCURRENT) |
| 40 | +class YamlUtilsTest { |
| 41 | + |
| 42 | + /** |
| 43 | + * Temporary dir |
| 44 | + */ |
| 45 | + @TempDir(cleanup = CleanupMode.ON_SUCCESS) |
| 46 | + private static Path dir; |
| 47 | + |
| 48 | + @Test |
| 49 | + void failToParseYamlString() { |
| 50 | + assertThrows(OrasException.class, () -> YamlUtils.fromYaml("not a yaml: too, bar: test", Object.class)); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @SuppressWarnings("unchecked") |
| 55 | + void shouldParseYaml() { |
| 56 | + String yamlMap = |
| 57 | + """ |
| 58 | + --- |
| 59 | + key1: "value1" |
| 60 | + key2: "value2" |
| 61 | + """; |
| 62 | + Map<String, String> map = YamlUtils.fromYaml(yamlMap, Map.class); |
| 63 | + assertNotNull(map); |
| 64 | + assertEquals(2, map.size()); |
| 65 | + assertEquals("value1", map.get("key1")); |
| 66 | + assertEquals("value2", map.get("key2")); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + @SuppressWarnings("unchecked") |
| 71 | + void shouldParseYamlFile() throws IOException { |
| 72 | + String yamlMap = |
| 73 | + """ |
| 74 | + --- |
| 75 | + key1: "value1" |
| 76 | + key2: "value2" |
| 77 | + """; |
| 78 | + Path yamlFile = dir.resolve("test.yaml"); |
| 79 | + Files.writeString(yamlFile, yamlMap); |
| 80 | + Map<String, String> map = YamlUtils.fromYaml(yamlFile, Map.class); |
| 81 | + assertNotNull(map); |
| 82 | + assertEquals(2, map.size()); |
| 83 | + assertEquals("value1", map.get("key1")); |
| 84 | + assertEquals("value2", map.get("key2")); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + @SuppressWarnings("unchecked") |
| 89 | + void shouldFailToParseYamlFile() { |
| 90 | + assertThrows(OrasException.class, () -> YamlUtils.fromYaml(Path.of("foo.yaml"), Map.class)); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void shouldConvertToYaml() { |
| 95 | + Map<String, String> map = new LinkedHashMap<>(); |
| 96 | + map.put("key1", "value1"); |
| 97 | + map.put("key2", "value2"); |
| 98 | + String yaml = YamlUtils.toYaml(map); |
| 99 | + assertNotNull(yaml); |
| 100 | + String expected = """ |
| 101 | + --- |
| 102 | + key1: "value1" |
| 103 | + key2: "value2" |
| 104 | + """; |
| 105 | + assertEquals(expected, yaml); |
| 106 | + } |
| 107 | +} |
0 commit comments