Class SchemaRegistryStub

java.lang.Object
io.krabka.streams.test.SchemaRegistryStub
All Implemented Interfaces:
AutoCloseable

public final class SchemaRegistryStub extends Object implements AutoCloseable
A small, stateful Confluent Schema Registry test server.

The stub binds an HTTP server to an ephemeral loopback port and implements the subset of the registry REST API that KrabkaSchemaRegistryClient needs: registering a schema, looking a schema up, fetching a subject's latest version, and fetching a schema by ID. Schemas are deduplicated by content, IDs are assigned sequentially from 1, and everything lives in memory until close().

Request counts are recorded per method and path, so tests can assert caching behavior such as "the second deserialization did not fetch the schema again".

Example

try (var registry = new SchemaRegistryStub()) {
    var client = new KrabkaSchemaRegistryClient(registry.uri());
    var cache = new SchemaCache(client);

    serde.registerSubject("orders");
    cache.prewarm().join();

    assertThat(registry.requestCount("POST", "/subjects/orders-value/versions")).isEqualTo(1);
}
  • Constructor Details

    • SchemaRegistryStub

      public SchemaRegistryStub() throws IOException
      Starts the stub on an ephemeral loopback port.
      Throws:
      IOException - if the HTTP server cannot bind
  • Method Details

    • uri

      public URI uri()
      Returns the base URI clients should connect to.
      Returns:
      the loopback URI of the running stub
    • requestCount

      public int requestCount(String method, String path)
      Returns how many requests the stub has served for a method and path.
      Parameters:
      method - the HTTP method, for example "GET"
      path - the raw request path, for example "/schemas/ids/1"
      Returns:
      the number of matching requests served so far
    • close

      public void close()
      Stops the HTTP server and discards all registered schemas.
      Specified by:
      close in interface AutoCloseable