|
| 1 | +package datadog.gradle.plugin.config |
| 2 | + |
| 3 | +import org.gradle.testkit.runner.BuildResult |
| 4 | +import org.gradle.testkit.runner.GradleRunner |
| 5 | +import org.gradle.testkit.runner.TaskOutcome |
| 6 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 7 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 8 | +import org.junit.jupiter.api.Test |
| 9 | +import org.junit.jupiter.api.io.TempDir |
| 10 | +import java.io.File |
| 11 | +import java.nio.file.Paths |
| 12 | + |
| 13 | +class ParseV2SupportedConfigurationsTest { |
| 14 | + @Test |
| 15 | + fun `should generate Java file from JSON configuration`(@TempDir projectDir: File) { |
| 16 | + val (buildResult, generatedFile) = runGradleTask(projectDir) |
| 17 | + |
| 18 | + assertEquals(TaskOutcome.SUCCESS, buildResult.task(":generateSupportedConfigurations")?.outcome) |
| 19 | + |
| 20 | + assertTrue(generatedFile.exists(), "Generated Java file should exist") |
| 21 | + |
| 22 | + val content = generatedFile.readText() |
| 23 | + assertTrue(content.contains("package datadog.test;")) |
| 24 | + assertTrue(content.contains("public final class TestGeneratedSupportedConfigurations {")) |
| 25 | + |
| 26 | + assertTrue(content.contains("public static final Map<String, List<SupportedConfiguration>> SUPPORTED;")) |
| 27 | + assertTrue(content.contains("public static final Map<String, List<String>> ALIASES;")) |
| 28 | + assertTrue(content.contains("public static final Map<String, String> ALIAS_MAPPING;")) |
| 29 | + assertTrue(content.contains("public static final Map<String, String> DEPRECATED;")) |
| 30 | + assertTrue(content.contains("public static final Map<String, String> REVERSE_PROPERTY_KEYS_MAP;")) |
| 31 | + |
| 32 | + assertTrue(content.contains("private static Map<String, List<SupportedConfiguration>> initSupported()")) |
| 33 | + assertTrue(content.contains("private static void initSupported1(Map<String, List<SupportedConfiguration>> supportedMap)")) |
| 34 | + assertTrue(content.contains("private static void initSupported2(Map<String, List<SupportedConfiguration>> supportedMap)")) |
| 35 | + assertTrue(content.contains("private static Map<String, List<String>> initAliases()")) |
| 36 | + assertTrue(content.contains("private static Map<String, String> initAliasMapping()")) |
| 37 | + assertTrue(content.contains("private static Map<String, String> initDeprecated()")) |
| 38 | + assertTrue(content.contains("private static Map<String, String> initReversePropertyKeysMap()")) |
| 39 | + |
| 40 | + assertContainsSupportedConfig( |
| 41 | + content, |
| 42 | + key = "DD_ACTION_EXECUTION_ID", |
| 43 | + version = "A", |
| 44 | + type = "string", |
| 45 | + default = "null", |
| 46 | + aliases = emptyList(), |
| 47 | + propertyKeys = listOf("property.key") |
| 48 | + ) |
| 49 | + |
| 50 | + assertContainsSupportedConfig( |
| 51 | + content, |
| 52 | + key = "DD_AGENTLESS_LOG_SUBMISSION_ENABLED", |
| 53 | + version = "A", |
| 54 | + type = "boolean", |
| 55 | + default = "false", |
| 56 | + aliases = emptyList() |
| 57 | + ) |
| 58 | + |
| 59 | + assertContainsSupportedConfig( |
| 60 | + content, |
| 61 | + key = "DD_AGENTLESS_LOG_SUBMISSION_ENABLED", |
| 62 | + version = "B", |
| 63 | + type = "boolean", |
| 64 | + default = "true", |
| 65 | + aliases = listOf("DD_ALIAS") |
| 66 | + ) |
| 67 | + |
| 68 | + assertTrue(content.contains("""aliasesMap.put("DD_ACTION_EXECUTION_ID", Collections.unmodifiableList(Arrays.asList()))""")) |
| 69 | + assertTrue(content.contains("""aliasesMap.put("DD_AGENTLESS_LOG_SUBMISSION_ENABLED", Collections.unmodifiableList(Arrays.asList("DD_ALIAS")))""")) |
| 70 | + |
| 71 | + assertTrue(content.contains("""aliasMappingMap.put("DD_ALIAS", "DD_AGENTLESS_LOG_SUBMISSION_ENABLED")""")) |
| 72 | + |
| 73 | + assertTrue(content.contains("""deprecatedMap.put("old.config", "Use test.config instead")""")) |
| 74 | + assertTrue(content.contains("""deprecatedMap.put("legacy.setting", "No longer supported")""")) |
| 75 | + |
| 76 | + assertTrue(content.contains("""reversePropertyKeysMapping.put("property.key", "DD_ACTION_EXECUTION_ID")""")) |
| 77 | + } |
| 78 | + |
| 79 | + private fun runGradleTask(projectDir: File): Pair<BuildResult, File> { |
| 80 | + val jsonFile = file(projectDir, "test-supported-configurations.json") |
| 81 | + jsonFile.writeText( |
| 82 | + """ |
| 83 | + { |
| 84 | + "supportedConfigurations": { |
| 85 | + "DD_ACTION_EXECUTION_ID": [ |
| 86 | + { |
| 87 | + "version": "A", |
| 88 | + "type": "string", |
| 89 | + "default": null, |
| 90 | + "aliases": [], |
| 91 | + "propertyKeys": ["property.key"] |
| 92 | + } |
| 93 | + ], |
| 94 | + "DD_AGENTLESS_LOG_SUBMISSION_ENABLED": [ |
| 95 | + { |
| 96 | + "version": "A", |
| 97 | + "type": "boolean", |
| 98 | + "default": "false", |
| 99 | + "aliases": [] |
| 100 | + }, |
| 101 | + { |
| 102 | + "version": "B", |
| 103 | + "type": "boolean", |
| 104 | + "default": "true", |
| 105 | + "aliases": ["DD_ALIAS"] |
| 106 | + } |
| 107 | + ] |
| 108 | + }, |
| 109 | + "deprecations": { |
| 110 | + "old.config": "Use test.config instead", |
| 111 | + "legacy.setting": "No longer supported" |
| 112 | + } |
| 113 | + } |
| 114 | + """.trimIndent() |
| 115 | + ) |
| 116 | + |
| 117 | + setupGradleProject(projectDir) |
| 118 | + |
| 119 | + val buildResult = GradleRunner.create() |
| 120 | + .forwardOutput() |
| 121 | + .withPluginClasspath() |
| 122 | + .withArguments("generateSupportedConfigurations") |
| 123 | + .withProjectDir(projectDir) |
| 124 | + .build() |
| 125 | + |
| 126 | + val generatedFile = file(projectDir, "build", "generated", "supportedConfigurations", "datadog", "test", "TestGeneratedSupportedConfigurations.java") |
| 127 | + return Pair(buildResult, generatedFile) |
| 128 | + } |
| 129 | + |
| 130 | + private fun setupGradleProject(projectDir: File) { |
| 131 | + file(projectDir, "settings.gradle.kts").writeText( |
| 132 | + """ |
| 133 | + rootProject.name = "test-config-project" |
| 134 | + """.trimIndent() |
| 135 | + ) |
| 136 | + |
| 137 | + file(projectDir, "build.gradle.kts").writeText( |
| 138 | + """ |
| 139 | + plugins { |
| 140 | + id("java") |
| 141 | + id("dd-trace-java.supported-config-generator") |
| 142 | + } |
| 143 | + |
| 144 | + group = "datadog.config.test" |
| 145 | + |
| 146 | + supportedTracerConfigurations { |
| 147 | + jsonFile.set(file("test-supported-configurations.json")) |
| 148 | + destinationDirectory.set(file("build/generated/supportedConfigurations")) |
| 149 | + className.set("datadog.test.TestGeneratedSupportedConfigurations") |
| 150 | + } |
| 151 | + """.trimIndent() |
| 152 | + ) |
| 153 | + } |
| 154 | + |
| 155 | + private fun file(projectDir: File, vararg parts: String, makeDirectory: Boolean = false): File { |
| 156 | + val f = Paths.get(projectDir.absolutePath, *parts).toFile() |
| 157 | + |
| 158 | + if (makeDirectory) { |
| 159 | + f.parentFile.mkdirs() |
| 160 | + } |
| 161 | + |
| 162 | + return f |
| 163 | + } |
| 164 | + |
| 165 | + private fun assertContainsSupportedConfig( |
| 166 | + content: String, |
| 167 | + key: String, |
| 168 | + version: String, |
| 169 | + type: String, |
| 170 | + default: String, |
| 171 | + aliases: List<String>, |
| 172 | + propertyKeys: List<String> = emptyList() |
| 173 | + ) { |
| 174 | + val aliasesArray = aliases.joinToString(", ") { "\"$it\"" } |
| 175 | + val propertyKeysArray = propertyKeys.joinToString(", ") { "\"$it\"" } |
| 176 | + |
| 177 | + assertTrue( |
| 178 | + content.contains("""supportedMap.put("$key""""), |
| 179 | + "Should contain supportedMap.put for key: $key" |
| 180 | + ) |
| 181 | + |
| 182 | + val expectedPattern = buildString { |
| 183 | + append("new SupportedConfiguration(") |
| 184 | + append("\"$version\", ") |
| 185 | + append("\"$type\", ") |
| 186 | + append(if (default == "null") "null" else "\"$default\"") |
| 187 | + append(", ") |
| 188 | + append("Arrays.asList($aliasesArray)") |
| 189 | + append(", ") |
| 190 | + append("Arrays.asList($propertyKeysArray)") |
| 191 | + append(")") |
| 192 | + } |
| 193 | + |
| 194 | + assertTrue( |
| 195 | + content.contains(expectedPattern), |
| 196 | + "Should contain SupportedConfiguration: $expectedPattern" |
| 197 | + ) |
| 198 | + } |
| 199 | +} |
0 commit comments