From 72e033e640676a294122401f6feeaa622bc5a5f4 Mon Sep 17 00:00:00 2001 From: r0bturner Date: Tue, 29 Jun 2021 09:32:26 +0100 Subject: [PATCH] feat: allow specific schema version to be deleted (#197) * feat: allow specific schema version to be deleted * Should be name here * Fix linting Co-authored-by: Rob.Turner --- .../main/schemaregistry/SchemaViewModel.kt | 2 ++ .../views/main/schemaregistry/SchemaView.kt | 7 +++++++ .../main/schemaregistry/SchemaViewModelTest.kt | 13 +++++++++++++ .../main/kotlin/insulator/kafka/SchemaRegistry.kt | 3 +++ .../kotlin/insulator/kafka/SchemaRegistryTest.kt | 12 ++++++++++++ 5 files changed, 37 insertions(+) diff --git a/app/src/main/kotlin/insulator/viewmodel/main/schemaregistry/SchemaViewModel.kt b/app/src/main/kotlin/insulator/viewmodel/main/schemaregistry/SchemaViewModel.kt index 5d5f7e68..f774da5c 100644 --- a/app/src/main/kotlin/insulator/viewmodel/main/schemaregistry/SchemaViewModel.kt +++ b/app/src/main/kotlin/insulator/viewmodel/main/schemaregistry/SchemaViewModel.kt @@ -35,4 +35,6 @@ class SchemaViewModel @Inject constructor( } fun delete() = schemaRegistry?.deleteSubject(nameProperty.value) + + fun deleteSchemaVersion() = schemaRegistry?.deleteSchemaVersion(nameProperty.value, selectedVersionProperty.value?.version!!) } diff --git a/app/src/main/kotlin/insulator/views/main/schemaregistry/SchemaView.kt b/app/src/main/kotlin/insulator/views/main/schemaregistry/SchemaView.kt index 05b4b020..8094f8be 100644 --- a/app/src/main/kotlin/insulator/views/main/schemaregistry/SchemaView.kt +++ b/app/src/main/kotlin/insulator/views/main/schemaregistry/SchemaView.kt @@ -34,6 +34,7 @@ class SchemaView @Inject constructor( hbox(alignment = Pos.CENTER_LEFT) { fieldName("Schema") schemaComboBox() + deleteSchemaVersionButton() } jsonView(viewModel.schemaProperty, formatter) } @@ -51,4 +52,10 @@ class SchemaView @Inject constructor( viewModel.delete() closeTab() }.hideOnReadonly() + + private fun EventTarget.deleteSchemaVersionButton() = + confirmationButton("Delete", "The schema \"${viewModel.nameProperty.value}\" version ${viewModel.selectedVersionProperty.value.version} will be removed.") { + viewModel.deleteSchemaVersion() + closeTab() + }.hideOnReadonly() } diff --git a/app/src/test/kotlin/insulator/viewmodel/main/schemaregistry/SchemaViewModelTest.kt b/app/src/test/kotlin/insulator/viewmodel/main/schemaregistry/SchemaViewModelTest.kt index e7d1fc52..45492441 100644 --- a/app/src/test/kotlin/insulator/viewmodel/main/schemaregistry/SchemaViewModelTest.kt +++ b/app/src/test/kotlin/insulator/viewmodel/main/schemaregistry/SchemaViewModelTest.kt @@ -27,6 +27,19 @@ class SchemaViewModelTest : StringSpec({ sut.error.value shouldBe null } } + + "happy path delete schema version" { + SchemaViewModelTestFixture().use { + // arrange + val subject = Subject(name = it.targetSubject, schemas = listOf(Schema("{}", 1, 4))) + val sut = SchemaViewModel(it.cluster, subject, it.mockSchemaRegistry) + // act + sut.deleteSchemaVersion() + // assert + verify(exactly = 1) { it.mockSchemaRegistry.deleteSchemaVersion(it.targetSubject, subject.schemas[0].version) } + sut.error.value shouldBe null + } + } }) private class SchemaViewModelTestFixture : FxContext() { diff --git a/lib/kafka/src/main/kotlin/insulator/kafka/SchemaRegistry.kt b/lib/kafka/src/main/kotlin/insulator/kafka/SchemaRegistry.kt index 99422560..be3675d8 100644 --- a/lib/kafka/src/main/kotlin/insulator/kafka/SchemaRegistry.kt +++ b/lib/kafka/src/main/kotlin/insulator/kafka/SchemaRegistry.kt @@ -22,6 +22,9 @@ class SchemaRegistry(private val client: SchemaRegistryClient) { fun deleteSubject(subject: String) = client.runCatchingE { deleteSubject(subject) }.map { Unit } + fun deleteSchemaVersion(subject: String, version: Int) = + client.runCatchingE { deleteSchemaVersion(subject, version.toString()) }.map { Unit } + fun getAllSubjects(): Either> = client.runCatchingE { allSubjects.sorted() } diff --git a/lib/kafka/src/test/kotlin/insulator/kafka/SchemaRegistryTest.kt b/lib/kafka/src/test/kotlin/insulator/kafka/SchemaRegistryTest.kt index 1325a9df..c70cff4c 100644 --- a/lib/kafka/src/test/kotlin/insulator/kafka/SchemaRegistryTest.kt +++ b/lib/kafka/src/test/kotlin/insulator/kafka/SchemaRegistryTest.kt @@ -73,6 +73,18 @@ class SchemaRegistryTest : StringSpec({ res shouldBeRight { } } + "happy path deleteSchemaVersion" { + // arrange + val mockSchema = mockk { + every { deleteSchemaVersion(any(), any()) } returns 1 + } + val sut = SchemaRegistry(mockSchema) + // act + val res = sut.deleteSchemaVersion("subject1", 1) + // assert + res shouldBeRight { } + } + "register an invalid schema return left" { // arrange val mockSchema = mockk()