From 329f91b60fca44183551f631d7d2fbe556280628 Mon Sep 17 00:00:00 2001 From: AlexisSouquiere Date: Wed, 17 Jan 2024 15:44:29 +0100 Subject: [PATCH] Adding test --- .../akhq/controllers/SchemaController.java | 7 ++-- .../controllers/SchemaControllerTest.java | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/akhq/controllers/SchemaController.java b/src/main/java/org/akhq/controllers/SchemaController.java index 650df9bdd..824c9ea69 100644 --- a/src/main/java/org/akhq/controllers/SchemaController.java +++ b/src/main/java/org/akhq/controllers/SchemaController.java @@ -6,6 +6,7 @@ import io.micronaut.http.HttpResponse; import io.micronaut.http.annotation.*; import io.swagger.v3.oas.annotations.Operation; +import jakarta.annotation.Nullable; import org.akhq.configs.security.Role; import org.akhq.middlewares.SchemaComparator; import org.akhq.models.Schema; @@ -157,19 +158,19 @@ private Schema registerSchema(String cluster, @Body Schema schema) throws IOExce } @Get("api/{cluster}/schema/id/{id}") - @Operation(tags = {"schema registry"}, summary = "Find a schema by id") + @Operation(tags = {"schema registry"}, summary = "Find a subject by the schema id") public Schema getSubjectBySchemaIdAndTopic( HttpRequest request, String cluster, Integer id, - @QueryValue String topic + @Nullable @QueryValue String topic ) throws IOException, RestClientException { // TODO Do the check on the subject name too checkIfClusterAllowed(cluster); return this.schemaRepository.getSubjectsBySchemaId(cluster, id) .stream() - .filter(s -> s.getSubject().contains(topic)) + .filter(s -> topic == null || s.getSubject().contains(topic)) .findFirst() .orElse(null); } diff --git a/src/test/java/org/akhq/controllers/SchemaControllerTest.java b/src/test/java/org/akhq/controllers/SchemaControllerTest.java index f9b0eaf52..9bfd3bc49 100644 --- a/src/test/java/org/akhq/controllers/SchemaControllerTest.java +++ b/src/test/java/org/akhq/controllers/SchemaControllerTest.java @@ -104,4 +104,43 @@ void deleteNotExistApi() { ); assertTrue(e.getMessage().contains("doesn't exist")); } + + @Test + void findSubjectBySchemaId() { + // Create 3 subjects (including 2 with the same schema) + var subject1Schema1 = new Schema("subjectTopic1-value", SchemaRegistryRepositoryTest.SCHEMA_1_V1, + Schema.Config.CompatibilityLevelConfig.FORWARD); + var subject1Schema1V2 = new Schema("subjectTopic1-value", SchemaRegistryRepositoryTest.SCHEMA_1_V2, + Schema.Config.CompatibilityLevelConfig.FORWARD); + var subject2Schema1 = new Schema("subjectTopic2-value", SchemaRegistryRepositoryTest.SCHEMA_1_V1, + Schema.Config.CompatibilityLevelConfig.FORWARD); + + var subject1Schema1Response = this.retrieve(HttpRequest.POST(BASE_URL, subject1Schema1), Schema.class); + var subject1Schema1V2Response = this.retrieve(HttpRequest.POST(BASE_URL + "/subjectTopic1-value", subject1Schema1V2), Schema.class); + var subject2Schema1Response = this.retrieve(HttpRequest.POST(BASE_URL, subject2Schema1), Schema.class); + + // Subject v1 and v2 should be different + assertNotEquals(subject1Schema1Response.getId(), subject1Schema1V2Response.getId()); + assertNotEquals(subject1Schema1Response.getSchema(), subject1Schema1V2Response.getSchema()); + + // Subject 1 and 2 should have the same ID, schema but different subject + assertEquals(subject1Schema1Response.getId(), subject2Schema1Response.getId()); + assertEquals(subject1Schema1Response.getSchema(), subject2Schema1Response.getSchema()); + assertNotEquals(subject1Schema1Response.getSubject(), subject2Schema1Response.getSubject()); + + // Searching subject by schema ID should give the right subject depending on the topic + var subject1FromSchemaIdAndTopic = + this.retrieve(HttpRequest.GET(BASE_URL + "/id/" + subject1Schema1Response.getId() + "?topic=subjectTopic1"), Schema.class); + assertEquals(subject1Schema1Response.getId(), subject1FromSchemaIdAndTopic.getId()); + assertEquals(subject1Schema1Response.getSubject(), subject1FromSchemaIdAndTopic.getSubject()); + + var subject2FromSchemaIdAndTopic = + this.retrieve(HttpRequest.GET(BASE_URL + "/id/" + subject1Schema1Response.getId() + "?topic=subjectTopic2"), Schema.class); + assertEquals(subject2Schema1Response.getId(), subject2FromSchemaIdAndTopic.getId()); + assertEquals(subject2Schema1Response.getSubject(), subject2FromSchemaIdAndTopic.getSubject()); + + // Clean + this.exchange(HttpRequest.DELETE(BASE_URL + "/subjectTopic1-value")); + this.exchange(HttpRequest.DELETE(BASE_URL + "/subjectTopic2-value")); + } }