| 1 | package nl.timfalken.birdcourier; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.io.InputStream; |
| 5 | import java.nio.charset.StandardCharsets; |
| 6 | import java.nio.file.Files; |
| 7 | import java.nio.file.Path; |
| 8 | import java.util.Map; |
| 9 | import java.util.Set; |
| 10 | |
| 11 | import org.junit.jupiter.api.Test; |
| 12 | import org.yaml.snakeyaml.Yaml; |
| 13 | |
| 14 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 16 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 17 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 18 | |
| 19 | class PluginConfigTest { |
| 20 | |
| 21 | @Test |
| 22 | @SuppressWarnings("unchecked") |
| 23 | void pluginYmlDeclaresCommandsAndPermissions() throws IOException { |
| 24 | Map<String, Object> yaml = loadYaml("plugin.yml"); |
| 25 | assertEquals("BirdCourier", yaml.get("name")); |
| 26 | assertEquals("nl.timfalken.birdcourier.BirdCourierPlugin", yaml.get("main")); |
| 27 | assertEquals("26.2", String.valueOf(yaml.get("api-version")).replace("'", "")); |
| 28 | assertEquals(gradleVersion(), String.valueOf(yaml.get("version"))); |
| 29 | |
| 30 | Map<String, Object> commands = (Map<String, Object>) yaml.get("commands"); |
| 31 | assertNotNull(commands); |
| 32 | assertEquals(Set.of("send", "courier", "birdcourier"), commands.keySet()); |
| 33 | assertEquals("birdcourier.send", ((Map<String, Object>) commands.get("send")).get("permission")); |
| 34 | assertEquals("birdcourier.courier", ((Map<String, Object>) commands.get("courier")).get("permission")); |
| 35 | assertEquals("birdcourier.admin", ((Map<String, Object>) commands.get("birdcourier")).get("permission")); |
| 36 | |
| 37 | Map<String, Object> permissions = (Map<String, Object>) yaml.get("permissions"); |
| 38 | assertNotNull(permissions); |
| 39 | assertEquals(true, ((Map<String, Object>) permissions.get("birdcourier.send")).get("default")); |
| 40 | assertEquals(true, ((Map<String, Object>) permissions.get("birdcourier.courier")).get("default")); |
| 41 | assertEquals("op", ((Map<String, Object>) permissions.get("birdcourier.admin")).get("default")); |
| 42 | } |
| 43 | |
| 44 | @Test |
| 45 | @SuppressWarnings("unchecked") |
| 46 | void configYmlHasFlightVisualAndCleanupDefaults() throws IOException { |
| 47 | Map<String, Object> yaml = loadYaml("config.yml"); |
| 48 | |
| 49 | Map<String, Object> visual = (Map<String, Object>) yaml.get("visual"); |
| 50 | assertEquals("PARROT", visual.get("entity-type")); |
| 51 | |
| 52 | Map<String, Object> flight = (Map<String, Object>) yaml.get("flight"); |
| 53 | assertEquals(10.0, ((Number) flight.get("speed-blocks-per-second")).doubleValue(), 0.001); |
| 54 | assertEquals(5.0, ((Number) flight.get("unloaded-speed-multiplier")).doubleValue(), 0.001); |
| 55 | assertEquals(10, ((Number) flight.get("preferred-height-above-ground")).intValue()); |
| 56 | |
| 57 | Map<String, Object> messages = (Map<String, Object>) yaml.get("messages"); |
| 58 | assertTrue(messages.containsKey("sent")); |
| 59 | assertTrue(messages.containsKey("delivered")); |
| 60 | assertTrue(messages.containsKey("target-offline")); |
| 61 | |
| 62 | Map<String, Object> cleanup = (Map<String, Object>) yaml.get("cleanup"); |
| 63 | assertEquals(true, cleanup.get("remove-all-ai-disabled-configured-type")); |
| 64 | assertEquals(false, cleanup.get("remove-configured-type-with-any-courier-flag")); |
| 65 | assertEquals(false, cleanup.get("remove-untagged-courier-like-entities")); |
| 66 | } |
| 67 | |
| 68 | private static Map<String, Object> loadYaml(String name) throws IOException { |
| 69 | try (InputStream in = PluginConfigTest.class.getClassLoader().getResourceAsStream(name)) { |
| 70 | assertNotNull(in, name + " missing on the test classpath"); |
| 71 | Object loaded = new Yaml().load(in); |
| 72 | assertTrue(loaded instanceof Map, name + " should be a mapping"); |
| 73 | @SuppressWarnings("unchecked") |
| 74 | Map<String, Object> map = (Map<String, Object>) loaded; |
| 75 | assertFalse(map.isEmpty()); |
| 76 | return map; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private static String gradleVersion() throws IOException { |
| 81 | Path gradle = Path.of("build.gradle.kts"); |
| 82 | if (!Files.isRegularFile(gradle)) { |
| 83 | gradle = Path.of("..", "build.gradle.kts"); |
| 84 | } |
| 85 | for (String line : Files.readAllLines(gradle, StandardCharsets.UTF_8)) { |
| 86 | line = line.trim(); |
| 87 | if (line.startsWith("version = \"")) { |
| 88 | return line.substring("version = \"".length(), line.length() - 1); |
| 89 | } |
| 90 | } |
| 91 | throw new IOException("version not found in build.gradle.kts"); |
| 92 | } |
| 93 | } |