From b5c85bfa342d7b8b18ec2631b9c74ccd7ba26d56 Mon Sep 17 00:00:00 2001 From: rudikone Date: Sun, 31 Dec 2023 10:27:44 +0300 Subject: [PATCH] test: add Avro Wizard plugin tests --- .gitignore | 7 +- build.gradle.kts | 2 +- example/build.gradle.kts | 6 +- plugin-build/build.gradle.kts | 2 +- plugin-build/plugin/build.gradle.kts | 1 - .../ru/rudikov/plugin/AvroWizardExtension.kt | 21 +++--- .../java/ru/rudikov/plugin/RegisterTask.kt | 19 +++--- .../template/plugin/TemplatePluginTest.kt | 45 ------------- .../plugin/AvroSchemaWizardPluginTest.kt | 67 +++++++++++++++++++ settings.gradle.kts | 3 + 10 files changed, 99 insertions(+), 74 deletions(-) delete mode 100644 plugin-build/plugin/src/test/java/com/ncorti/kotlin/gradle/template/plugin/TemplatePluginTest.kt create mode 100644 plugin-build/plugin/src/test/java/ru/rudikov/plugin/AvroSchemaWizardPluginTest.kt diff --git a/.gitignore b/.gitignore index 39c7972..4fe59a0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,10 +2,5 @@ .DS_Store .gradle /local.properties -/.idea/caches -/.idea/libraries -/.idea/modules.xml -/.idea/workspace.xml -/.idea/navEditor.xml -/.idea/assetWizardSettings.xml +/.idea build diff --git a/build.gradle.kts b/build.gradle.kts index 764ed70..1738d6a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -28,7 +28,7 @@ subprojects { } detekt { - config = rootProject.files("config/detekt/detekt.yml") + config.setFrom(rootProject.files("config/detekt/detekt.yml")) } } diff --git a/example/build.gradle.kts b/example/build.gradle.kts index 62f9a60..7fc820b 100644 --- a/example/build.gradle.kts +++ b/example/build.gradle.kts @@ -1,4 +1,8 @@ plugins { java -// id("ru.rudikov.avroschema-wizard-plugin") + id("ru.rudikov.avroschema-wizard-plugin") +} + +avroWizardConfig { + subjectToSchema = mapOf("my-topic" to "Schema") } diff --git a/plugin-build/build.gradle.kts b/plugin-build/build.gradle.kts index 3ea25d4..adcfe2b 100644 --- a/plugin-build/build.gradle.kts +++ b/plugin-build/build.gradle.kts @@ -31,7 +31,7 @@ allprojects { } detekt { - config = rootProject.files("../config/detekt/detekt.yml") + config.setFrom(rootProject.files("../config/detekt/detekt.yml")) } } diff --git a/plugin-build/plugin/build.gradle.kts b/plugin-build/plugin/build.gradle.kts index 6a8dafd..300f278 100644 --- a/plugin-build/plugin/build.gradle.kts +++ b/plugin-build/plugin/build.gradle.kts @@ -56,4 +56,3 @@ tasks { } } } - diff --git a/plugin-build/plugin/src/main/java/ru/rudikov/plugin/AvroWizardExtension.kt b/plugin-build/plugin/src/main/java/ru/rudikov/plugin/AvroWizardExtension.kt index 6a38d02..0372366 100644 --- a/plugin-build/plugin/src/main/java/ru/rudikov/plugin/AvroWizardExtension.kt +++ b/plugin-build/plugin/src/main/java/ru/rudikov/plugin/AvroWizardExtension.kt @@ -10,16 +10,17 @@ const val DEFAULT_SCHEMA_REGISTRY_URL = "http://localhost:10081" @Suppress("UnnecessaryAbstractClass") abstract class AvroWizardExtension -@Inject -constructor(project: Project) { - private val objects = project.objects + @Inject + constructor(project: Project) { + private val objects = project.objects - val schemaRegistryUrl: Property = objects.property(String::class.java) - .convention(DEFAULT_SCHEMA_REGISTRY_URL) + val schemaRegistryUrl: Property = + objects.property(String::class.java).convention(DEFAULT_SCHEMA_REGISTRY_URL) - val searchAvroFilesPaths: SetProperty = objects.setProperty(String::class.java).convention( - setOf("${project.layout.buildDirectory}") - ) + val searchAvroFilesPaths: SetProperty = + objects.setProperty(String::class.java) + .convention(setOf(project.layout.buildDirectory.get().asFile.absolutePath)) - val subjectToSchema: MapProperty = objects.mapProperty(String::class.java, String::class.java) -} + val subjectToSchema: MapProperty = + objects.mapProperty(String::class.java, String::class.java) + } diff --git a/plugin-build/plugin/src/main/java/ru/rudikov/plugin/RegisterTask.kt b/plugin-build/plugin/src/main/java/ru/rudikov/plugin/RegisterTask.kt index 6600000..859a2aa 100644 --- a/plugin-build/plugin/src/main/java/ru/rudikov/plugin/RegisterTask.kt +++ b/plugin-build/plugin/src/main/java/ru/rudikov/plugin/RegisterTask.kt @@ -56,13 +56,14 @@ abstract class RegisterTask : DefaultTask() { } } - private fun findAvroFileByName(searchPath: String, schemaName: String) = - Files.walk(Paths.get(searchPath)).use { - it.filter { file -> file.isRegularFile() && file.fileName.toString() == "$schemaName.avsc" } - .findFirst() - .getOrNull() - ?.toFile() - ?: error("File $schemaName.avsc not found!") - } - + private fun findAvroFileByName( + searchPath: String, + schemaName: String, + ) = Files.walk(Paths.get(searchPath)).use { + it.filter { file -> file.isRegularFile() && file.fileName.toString() == "$schemaName.avsc" } + .findFirst() + .getOrNull() + ?.toFile() + ?: error("File $schemaName.avsc not found!") + } } diff --git a/plugin-build/plugin/src/test/java/com/ncorti/kotlin/gradle/template/plugin/TemplatePluginTest.kt b/plugin-build/plugin/src/test/java/com/ncorti/kotlin/gradle/template/plugin/TemplatePluginTest.kt deleted file mode 100644 index 9f96ed0..0000000 --- a/plugin-build/plugin/src/test/java/com/ncorti/kotlin/gradle/template/plugin/TemplatePluginTest.kt +++ /dev/null @@ -1,45 +0,0 @@ -package com.ncorti.kotlin.gradle.template.plugin - -import org.gradle.testfixtures.ProjectBuilder -import org.junit.Assert.assertEquals -import org.junit.Assert.assertNotNull -import org.junit.Test -import ru.rudikov.plugin.RegisterTask -import ru.rudikov.plugin.AvroWizardExtension -import java.io.File - -class TemplatePluginTest { - @Test - fun `plugin is applied correctly to the project`() { - val project = ProjectBuilder.builder().build() - project.pluginManager.apply("com.ncorti.kotlin.gradle.template.plugin") - - assert(project.tasks.getByName("templateExample") is RegisterTask) - } - - @Test - fun `extension templateExampleConfig is created correctly`() { - val project = ProjectBuilder.builder().build() - project.pluginManager.apply("com.ncorti.kotlin.gradle.template.plugin") - - assertNotNull(project.extensions.getByName("templateExampleConfig")) - } - - @Test - fun `parameters are passed correctly from extension to task`() { - val project = ProjectBuilder.builder().build() - project.pluginManager.apply("com.ncorti.kotlin.gradle.template.plugin") - val aFile = File(project.projectDir, ".tmp") - (project.extensions.getByName("templateExampleConfig") as AvroWizardExtension).apply { - tag.set("a-sample-tag") - message.set("just-a-message") - outputFile.set(aFile) - } - - val task = project.tasks.getByName("templateExample") as RegisterTask - - assertEquals("a-sample-tag", task.tag.get()) - assertEquals("just-a-message", task.message.get()) - assertEquals(aFile, task.outputFile.get().asFile) - } -} diff --git a/plugin-build/plugin/src/test/java/ru/rudikov/plugin/AvroSchemaWizardPluginTest.kt b/plugin-build/plugin/src/test/java/ru/rudikov/plugin/AvroSchemaWizardPluginTest.kt new file mode 100644 index 0000000..ec6f2d6 --- /dev/null +++ b/plugin-build/plugin/src/test/java/ru/rudikov/plugin/AvroSchemaWizardPluginTest.kt @@ -0,0 +1,67 @@ +package ru.rudikov.plugin + +import org.gradle.testfixtures.ProjectBuilder +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Test + +class AvroSchemaWizardPluginTest { + @Test + fun `plugin is applied correctly to the project`() { + val project = ProjectBuilder.builder().build() + project.pluginManager.apply(PLUGIN_ID) + + assert(project.tasks.getByName(REGISTER_TASK_NAME) is RegisterTask) + } + + @Test + fun `extension avroWizardConfig is created correctly`() { + val project = ProjectBuilder.builder().build() + project.pluginManager.apply(PLUGIN_ID) + + assertNotNull(project.extensions.getByName(EXTENSION_NAME)) + } + + @Test + fun `parameters are passed correctly from extension to task`() { + val project = ProjectBuilder.builder().build() + project.pluginManager.apply(PLUGIN_ID) + + val schemaRegistryUrl = "some_url" + val searchAvroFilesPaths = setOf("some_path") + val subjectToSchema = mapOf("some_subject" to "some_schema") + + (project.extensions.getByName(EXTENSION_NAME) as AvroWizardExtension).apply { + this.schemaRegistryUrl.set(schemaRegistryUrl) + this.searchAvroFilesPaths.set(searchAvroFilesPaths) + this.subjectToSchema.set(subjectToSchema) + } + + val task = project.tasks.getByName(REGISTER_TASK_NAME) as RegisterTask + + assertEquals(schemaRegistryUrl, task.schemaRegistryUrl.get()) + assertEquals(searchAvroFilesPaths, task.searchAvroFilesPaths.get()) + assertEquals(subjectToSchema, task.subjectToSchema.get()) + } + + @Test + fun `parameters by default are passed correctly from extension to task`() { + val project = ProjectBuilder.builder().build() + project.pluginManager.apply(PLUGIN_ID) + + val subjectToSchema = mapOf("some_subject" to "some_schema") + + (project.extensions.getByName(EXTENSION_NAME) as AvroWizardExtension).apply { + this.subjectToSchema.set(subjectToSchema) + } + + val task = project.tasks.getByName(REGISTER_TASK_NAME) as RegisterTask + + assertEquals(DEFAULT_SCHEMA_REGISTRY_URL, task.schemaRegistryUrl.get()) + assertEquals(setOf(project.layout.buildDirectory.get().asFile.absolutePath), task.searchAvroFilesPaths.get()) + } + + companion object { + private const val PLUGIN_ID = "ru.rudikov.avroschema-wizard-plugin" + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 850175e..c6dadeb 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -2,6 +2,9 @@ pluginManagement { repositories { gradlePluginPortal() mavenCentral() + maven { + url = uri("https://packages.confluent.io/maven") + } } }