Skip to content

Commit

Permalink
Adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisSouquiere committed Jan 17, 2024
1 parent 22d6a57 commit 329f91b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/main/java/org/akhq/controllers/SchemaController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/akhq/controllers/SchemaControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit 329f91b

Please sign in to comment.