Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch latest version for references, if not specified #128

Merged
merged 3 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ schemaRegistry {
.addLocalReference("localAvroSubject", "/a/local/path.avsc")
subject('avroWithRemoteReferences', '/absolutPath/dependent/path.avsc', "AVRO")
.addReference('avroSubject', 'avroSubjectType', 1)
.addReference('avroSubjectLatestVersion', 'avroSubjectLatestVersionType')
.addReference('avroSubjectLatestVersionExplicit', 'avroSubjectLatestVersionExplicitType', -1)
subject('protoWithReferences', 'dependent/path.proto', "PROTOBUF").addReference('protoSubject', 'protoSubjectType', 1)
subject('jsonWithReferences', 'dependent/path.json', "JSON").addReference('jsonSubject', 'jsonSubjectType', 1)
}
Expand All @@ -124,7 +126,11 @@ schemaRegistry {
You have to list all the (subject, avsc file path) pairs that you want to test.

If you have references with other schemas stored in the registry that are required before the compatibility check,
you can call the `addReference("name", "subject", version)`, this will add a reference to fetch dynamically from the registry.
you can call the `addReference("name", "subject", version)`,
this will add a reference to use from the registry.
A convenience method, `addReference("name", "subject")`,
uses the latest version of the schema in the registry.
You can also specify `-1` explicitly to use the latest version.
The addReference calls can be chained.

If you have local references to add before calling the compatibility in the registry,
Expand Down Expand Up @@ -175,13 +181,19 @@ schemaRegistry {
.addLocalReference("localAvroSubject", "/a/local/path.avsc")
subject('avroWithRemoteReferences', '/absolutPath/dependent/path.avsc', "AVRO")
.addReference('avroSubject', 'avroSubjectType', 1)
.addReference('avroSubjectLatestVersion', 'avroSubjectLatestVersionType')
.addReference('avroSubjectLatestVersionExplicit', 'avroSubjectLatestVersionExplicitType', -1)
subject('protoWithReferences', 'dependent/path.proto', "PROTOBUF").addReference('protoSubject', 'protoSubjectType', 1)
subject('jsonWithReferences', 'dependent/path.json', "JSON").addReference('jsonSubject', 'jsonSubjectType', 1)
}
}
```
If you have references to other schemas required before the register,
you can call the `addReference("name", "subject", version)`, this will add a reference to use from the registry.
you can call the `addReference("name", "subject", version)`,
this will add a reference to use from the registry.
A convenience method, `addReference("name", "subject")`,
uses the latest version of the schema in the registry.
You can also specify `-1` explicitly to use the latest version.
The addReference calls can be chained.

If you have local references to add before calling the register,
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/com/github/imflog/schema/registry/Subject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ data class Subject(
return this
}

fun addReference(name: String, subject:String): Subject {
references.add(SchemaReference(name, subject, -1))
return this
}

fun addLocalReference(name: String, path: String): Subject {
localReferences.add(LocalReference(name, path))
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RegisterTaskAction(
fun run(): Int {
var errorCount = 0
writeOutputFileHeader()
subjects.forEach { (subject, path, type, references, localReferences) ->
subjects.forEach { (subject, path, type, references: List<SchemaReference>, localReferences) ->
try {
val schemaId = registerSchema(subject, path, type.toSchemaType(), references, localReferences)
writeRegisteredSchemaOutput(subject, path, schemaId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,29 @@ package com.github.imflog.schema.registry
import com.github.imflog.schema.registry.tasks.download.DownloadTask
import org.assertj.core.api.Assertions
import org.gradle.api.Project
import org.gradle.internal.impldep.org.junit.rules.TemporaryFolder
import org.gradle.testfixtures.ProjectBuilder
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.UnexpectedBuildFailure
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.File
import java.nio.file.Files
import java.nio.file.Path

class SchemaRegistryPluginTest {
lateinit var project: Project
lateinit var folderRule: TemporaryFolder
@TempDir
lateinit var folderRule: Path
lateinit var buildFile: File

private val subject = "test-subject"

@BeforeEach
fun init() {
folderRule = TemporaryFolder()
project = ProjectBuilder.builder().build()
project.pluginManager.apply(SchemaRegistryPlugin::class.java)
}

@AfterEach
fun tearDown() {
folderRule.delete()
Files.createFile(folderRule.resolve("build.gradle"))
}

@Test
Expand All @@ -41,8 +38,7 @@ class SchemaRegistryPluginTest {

@Test
fun `plugin should fail with wrong url extension configuration`() {
folderRule.create()
buildFile = folderRule.newFile("build.gradle")
buildFile = File(folderRule.toFile(), "build.gradle")
buildFile.writeText(
"""
plugins {
Expand All @@ -61,7 +57,7 @@ class SchemaRegistryPluginTest {
try {
GradleRunner.create()
.withGradleVersion("6.7.1")
.withProjectDir(folderRule.root)
.withProjectDir(folderRule.toFile())
.withArguments(DownloadTask.TASK_NAME)
.withPluginClasspath()
.withDebug(true)
Expand All @@ -74,8 +70,7 @@ class SchemaRegistryPluginTest {

@Test
fun `plugin should fail with wrong credentials extension configuration`() {
folderRule.create()
buildFile = folderRule.newFile("build.gradle")
buildFile = File(folderRule.toFile(), "build.gradle")
buildFile.writeText(
"""
plugins {
Expand All @@ -99,7 +94,7 @@ class SchemaRegistryPluginTest {
try {
GradleRunner.create()
.withGradleVersion("6.7.1")
.withProjectDir(folderRule.root)
.withProjectDir(folderRule.toFile())
.withArguments(DownloadTask.TASK_NAME)
.withPluginClasspath()
.withDebug(true)
Expand All @@ -112,8 +107,7 @@ class SchemaRegistryPluginTest {

@Test
fun `plugin should only parse nested extensions`() {
folderRule.create()
buildFile = folderRule.newFile("build.gradle")
buildFile = File(folderRule.toFile(), "build.gradle")
buildFile.writeText(
"""
plugins {
Expand All @@ -136,7 +130,7 @@ class SchemaRegistryPluginTest {
try {
GradleRunner.create()
.withGradleVersion("6.7.1")
.withProjectDir(folderRule.root)
.withProjectDir(folderRule.toFile())
.withArguments(DownloadTask.TASK_NAME)
.withPluginClasspath()
.withDebug(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@ package com.github.imflog.schema.registry.parser
import com.github.imflog.schema.registry.LocalReference
import io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient
import org.assertj.core.api.Assertions
import org.gradle.internal.impldep.org.junit.rules.TemporaryFolder
import org.json.JSONObject
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.io.TempDir
import java.nio.file.Path

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class AvroSchemaParserTest {

private val schemaRegistryClient = MockSchemaRegistryClient()
private val folderRule: TemporaryFolder = TemporaryFolder().apply { create() }
private val parser = AvroSchemaParser(
schemaRegistryClient,
folderRule.root
)
@TempDir
lateinit var folderRule: Path

companion object {
private const val ADDRESS_REFERENCE_NAME = "Address"
Expand All @@ -39,14 +36,10 @@ class AvroSchemaParserTest {
}"""
}

@AfterAll
fun tearDown() {
folderRule.delete()
}

@Test
fun `Should format local references correctly`() {
// Given
val parser = AvroSchemaParser(schemaRegistryClient, folderRule.toFile())
val aLocalReference = givenALocalReference()

// When
Expand All @@ -61,7 +54,7 @@ class AvroSchemaParserTest {
}

private fun givenALocalReference(): LocalReference {
val addressLocalFile = folderRule.root.resolve("Address.avsc")
val addressLocalFile = folderRule.resolve("Address.avsc").toFile()
addressLocalFile.writeText(ADDRESS_SCHEMA)
return LocalReference(ADDRESS_REFERENCE_NAME, addressLocalFile.path)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@ package com.github.imflog.schema.registry.parser
import com.github.imflog.schema.registry.LocalReference
import io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient
import org.assertj.core.api.Assertions
import org.gradle.internal.impldep.org.junit.rules.TemporaryFolder
import org.json.JSONObject
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.io.TempDir
import java.nio.file.Path

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class JsonSchemaParserTest {

private val schemaRegistryClient = MockSchemaRegistryClient()
private val folderRule: TemporaryFolder = TemporaryFolder().apply { create() }
private val parser = JsonSchemaParser(
schemaRegistryClient,
folderRule.root
)
@TempDir
lateinit var folderRule: Path

companion object {
private const val ADDRESS_REFERENCE_NAME = "Address"
Expand All @@ -43,14 +40,10 @@ class JsonSchemaParserTest {
}"""
}

@AfterAll
fun tearDown() {
folderRule.delete()
}

@Test
fun `Should format local references correctly`() {
// Given
val parser = JsonSchemaParser(schemaRegistryClient, folderRule.toFile())
val aLocalReference = givenALocalReference()

// When
Expand All @@ -65,7 +58,7 @@ class JsonSchemaParserTest {
}

private fun givenALocalReference(): LocalReference {
val addressLocalFile = folderRule.root.resolve("Address.json")
val addressLocalFile = folderRule.resolve("Address.json").toFile()
addressLocalFile.writeText(ADDRESS_SCHEMA)
return LocalReference(ADDRESS_REFERENCE_NAME, addressLocalFile.path)
}
Expand Down
Loading