@Test public void testRegisterOverCapacity() throws Exception { RestService restService = createMock(RestService.class); CachedSchemaRegistryClient client = new CachedSchemaRegistryClient(restService, 1); // capacity is just one String schema1 = "{\"type\": \"record\", \"name\": \"Blah\", \"fields\": [{ \"name\": \"name\", \"type\": \"string\" }]}"; Schema avroSchema1 = new Schema.Parser().parse(schema1); String schema2 = "{\"type\": \"record\", \"name\": \"Blah\", \"fields\": [" + "{ \"name\": \"name\", \"type\": \"string\" }," + "{ \"name\": \"blah\", \"type\": \"string\" }" + "]}"; Schema avroSchema2 = new Schema.Parser().parse(schema2); String subject = "foo"; int id = 25; // Expect one call to register schema (the second one will fail) expect(restService.registerSchema(anyString(), eq(subject))).andReturn(id); replay(restService); assertEquals(id, client.register(subject, avroSchema1)); try { // This call should exceed the identityMapCapacity assertEquals(id, client.register(subject, avroSchema2)); fail(); } catch (IllegalStateException e) { } verify(restService); }
@Test public void testIdCache() throws Exception { RestService restService = createMock(RestService.class); CachedSchemaRegistryClient client = new CachedSchemaRegistryClient(restService, 20); String schema = "{\"type\": \"record\", \"name\": \"Blah\", \"fields\": [{ \"name\": \"name\", \"type\": \"string\" }]}"; Schema avroSchema = new Schema.Parser().parse(schema); String subject = "foo"; int id = 25; expect(restService.registerSchema(anyString(), eq(subject))).andReturn(id); // Expect only one call to getId (the rest should hit the cache) expect(restService.getId(id)).andReturn(new SchemaString(schema)); replay(restService); assertEquals(id, client.register(subject, avroSchema)); assertEquals(avroSchema, client.getByID(id)); assertEquals(avroSchema, client.getByID(id)); // hit the cache verify(restService); }
@Test public void testVersionCache() throws Exception { RestService restService = createMock(RestService.class); CachedSchemaRegistryClient client = new CachedSchemaRegistryClient(restService, 20); String schema = "{\"type\": \"record\", \"name\": \"Blah\", \"fields\": [{ \"name\": \"name\", \"type\": \"string\" }]}"; Schema avroSchema = new Schema.Parser().parse(schema); String subject = "foo"; int id = 25; int version = 7; expect(restService.registerSchema(anyString(), eq(subject))).andReturn(id); // Expect only one call to lookup the subject (the rest should hit the cache) expect(restService.lookUpSubjectVersion(anyString(), eq(subject))) .andReturn( new io.confluent.kafka.schemaregistry.client.rest.entities.Schema( subject, version, id, schema)); replay(restService); assertEquals(id, client.register(subject, avroSchema)); assertEquals(version, client.getVersion(subject, avroSchema)); assertEquals(version, client.getVersion(subject, avroSchema)); // hit the cache verify(restService); }