Skip to content

Commit

Permalink
test: add Avro Wizard plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rudikone committed Dec 31, 2023
1 parent 8bd626a commit b5c85bf
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 74 deletions.
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ subprojects {
}

detekt {
config = rootProject.files("config/detekt/detekt.yml")
config.setFrom(rootProject.files("config/detekt/detekt.yml"))
}
}

Expand Down
6 changes: 5 additions & 1 deletion example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 1 addition & 1 deletion plugin-build/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ allprojects {
}

detekt {
config = rootProject.files("../config/detekt/detekt.yml")
config.setFrom(rootProject.files("../config/detekt/detekt.yml"))
}
}

Expand Down
1 change: 0 additions & 1 deletion plugin-build/plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,3 @@ tasks {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = objects.property(String::class.java)
.convention(DEFAULT_SCHEMA_REGISTRY_URL)
val schemaRegistryUrl: Property<String> =
objects.property(String::class.java).convention(DEFAULT_SCHEMA_REGISTRY_URL)

val searchAvroFilesPaths: SetProperty<String> = objects.setProperty(String::class.java).convention(
setOf("${project.layout.buildDirectory}")
)
val searchAvroFilesPaths: SetProperty<String> =
objects.setProperty(String::class.java)
.convention(setOf(project.layout.buildDirectory.get().asFile.absolutePath))

val subjectToSchema: MapProperty<String, String> = objects.mapProperty(String::class.java, String::class.java)
}
val subjectToSchema: MapProperty<String, String> =
objects.mapProperty(String::class.java, String::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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"
}
}
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
maven {
url = uri("https://packages.confluent.io/maven")
}
}
}

Expand Down

0 comments on commit b5c85bf

Please sign in to comment.