SchemaMetadataInfo schemaMetadataInfo = schemaRegistry.getSchemaMetadataInfo(subject); if (schemaMetadataInfo == null) { SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(subject) .type(AvroSchemaProvider.TYPE) .schemaGroup("Kafka")
private void _testSerDes(Long id, Number serdesProtocolVersion) throws Exception { SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("random-" + System.currentTimeMillis()) .schemaGroup("custom") .type(AvroSchemaProvider.TYPE)
/** * Return true if the schema creation failed as expected */ private void testCreate(SchemaRegistryClient client) { boolean failedAsExpected = false; SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(name).type(type).schemaGroup(group). description("description").build(); try { client.registerSchemaMetadata(schemaMetadata); } catch (BadRequestException ex) { Response resp = ex.getResponse(); Assert.assertEquals(test + " - http response unexpected", expectedHttpResponse.getStatusCode(), resp.getStatus()); CatalogResponse catalogResponse = SchemaRegistryClient.readCatalogResponse(resp.readEntity(String.class)); Assert.assertEquals(test + " - catalog response unexpected", expectedCatalogResponse.getCode(), catalogResponse.getResponseCode()); failedAsExpected = true; } Assert.assertTrue(test + " - did not fail as expected", failedAsExpected); } }
private void doTestSchemaOps(SchemaValidationLevel validationLevel) throws IOException, InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException { String testName = TEST_NAME_RULE.getMethodName(); SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(testName + "-schema") .type(AvroSchemaProvider.TYPE) .schemaGroup(testName + "-group") SchemaMetadata schemaMetadataToUpdateTo = new SchemaMetadata.Builder(currentSchemaMetadata).validationLevel(SchemaValidationLevel.LATEST) .build(); SchemaMetadataInfo updatedSchemaMetadata = schemaRegistryClient.updateSchemaMetadata(schemaName, schemaMetadataToUpdateTo);
@Test public void testRegistrySchemaOps() throws Exception { SchemaCompatibility compatibility = SchemaCompatibility.BOTH; SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(schemaName) .type(AvroSchemaProvider.TYPE) .description("devices schema")
private void doTestSchemaVersionLifeCycleStates(SchemaValidationLevel validationLevel) throws InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, IOException, SchemaLifecycleException, SchemaBranchNotFoundException { SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(TEST_NAME_RULE.getMethodName() + "-schema") .type(AvroSchemaProvider.TYPE) .schemaGroup("group")
@Test public void testSchemaVersionStatesThroughIds() throws Exception { SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(TEST_NAME_RULE.getMethodName() + "-schema") .type(AvroSchemaProvider.TYPE) .schemaGroup("group")
@Test public void testSerDes() throws Exception { SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("msgCtx-" + System.currentTimeMillis()) .schemaGroup("custom") .type(AvroSchemaProvider.TYPE)
@Test public void testHASanity() throws Exception { SchemaRegistryTestServerClientWrapper followerServer = followerSchemaRegistryServer(); SchemaRegistryClient schemaRegistryClient = followerServer.getClient(); // registering schema metadata on follower, this should have been redirected to leader. String schemaName = "foo"; SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(schemaName).type("avro").build(); Long schemaId = schemaRegistryClient.registerSchemaMetadata(schemaMetadata); Assert.assertNotNull(schemaId); // registering a new schema on follower, this should have been redirected to leader. String schema1 = IOUtils.toString(LocalRegistryServerTest.class.getResourceAsStream("/schema-1.avsc"), "UTF-8"); SchemaIdVersion v1 = schemaRegistryClient.addSchemaVersion(schemaName, new SchemaVersion(schema1, "Initial version of the schema")); // retrieve schema on leader as the schema data is stored in memory in leader. this data does not exist on // followers as the storage is inmemory. SchemaRegistryTestServerClientWrapper leaderServer = leaderSchemaRegistryServer(); int leaderPort = leaderServer.getLocalPort(); SchemaRegistryClient leaderClient = leaderServer.getClient(); SchemaMetadataInfo schemaMetadataInfo = leaderClient.getSchemaMetadataInfo(schemaName); Assert.assertEquals(schemaMetadata, schemaMetadataInfo.getSchemaMetadata()); // stop the leader server leaderServer.stopTestServer(); // get the new leader server and run operations. SchemaRegistryTestServerClientWrapper newLeaderServer = leaderSchemaRegistryServer(); Assert.assertNotEquals(leaderPort, newLeaderServer.getLocalPort()); leaderClient = newLeaderServer.getClient(); String receivedSchema = leaderClient.getSchemaVersionInfo(new SchemaVersionKey(schemaName, v1.getVersion())).getSchemaText(); Assert.assertEquals(schema1, receivedSchema); }
@Test public void testValidationLevels() throws Exception { SchemaMetadata schemaMetadata = createSchemaMetadata(TEST_NAME_RULE.getMethodName(), SchemaCompatibility.BOTH); String schemaName = schemaMetadata.getName(); Long id = SCHEMA_REGISTRY_CLIENT.registerSchemaMetadata(schemaMetadata); SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-1.avsc"), "Initial version of the schema")); SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-2.avsc"), "Second version of the schema")); SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-3.avsc"), "Third version of the schema, removes name field")); try { SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-4.avsc"), "Forth version of the schema, adds back name field, but different type")); Assert.fail("Should throw IncompatibleSchemaException as check against all schema's would find name field is not compatible with v1 and v2"); } catch (IncompatibleSchemaException ise) { //expected } SchemaMetadata currentSchemaMetadata = SCHEMA_REGISTRY_CLIENT.getSchemaMetadataInfo(schemaName) .getSchemaMetadata(); SchemaMetadata schemaMetadataToUpdateTo = new SchemaMetadata.Builder(currentSchemaMetadata).validationLevel(SchemaValidationLevel.LATEST) .build(); SchemaMetadataInfo updatedSchemaMetadata = SCHEMA_REGISTRY_CLIENT.updateSchemaMetadata(schemaName, schemaMetadataToUpdateTo); Assert.assertEquals(SchemaValidationLevel.LATEST, updatedSchemaMetadata.getSchemaMetadata() .getValidationLevel()); try { SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-4.avsc"), "Forth version of the schema, adds back name field, but different type")); } catch (IncompatibleSchemaException ise) { Assert.fail("Should not throw IncompatibleSchemaException as check against only latest schema as such should ignore v1 and v2"); } }
@Test public void testSanity() throws Exception { SchemaRegistryTestServerClientWrapper schemaRegistryTestServerClientWrapper = new SchemaRegistryTestServerClientWrapper(SCHEMA_REGISTRY_TEST_CONFIGURATION); schemaRegistryTestServerClientWrapper.startTestServer(); SchemaRegistryClient schemaRegistryClient = schemaRegistryTestServerClientWrapper.getClient(); // registering schema metadata SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("foo").type("avro").build(); Long schemaId = schemaRegistryClient.registerSchemaMetadata(schemaMetadata); Assert.assertNotNull(schemaId); // registering a new schema String schemaName = schemaMetadata.getName(); String schema1 = IOUtils.toString(LocalRegistryServerTest.class.getResourceAsStream("/schema-1.avsc"), "UTF-8"); SchemaIdVersion v1 = schemaRegistryClient.addSchemaVersion(schemaName, new SchemaVersion(schema1, "Initial version of the schema")); schemaRegistryTestServerClientWrapper.stopTestServer(); } }
public SchemaMetadata getSchemaKey(String topic, boolean isKey) { String name = topic; if(isKey) { if (schemaNameKeySuffix != null ) { name += schemaNameKeySuffix; } } else { if (schemaNameValueSuffix != null ) { name += schemaNameValueSuffix; } } // there wont be any naming collisions as kafka does not allow character `:` in a topic name. return new SchemaMetadata.Builder(name).type(AvroSchemaProvider.TYPE).schemaGroup(schemaGroup).build(); }
private SchemaMetadata getSchemaKey(String topic, boolean isKey) { String name = isKey ? topic+":k" : topic; return new SchemaMetadata.Builder(name).type(AvroSchemaProvider.TYPE).schemaGroup("kafka").build(); }
private SchemaMetadata createSchemaMetadata(String name) { return new SchemaMetadata.Builder(name) .type(AvroSchemaProvider.TYPE) .schemaGroup("sample-group") .description("Sample schema") .compatibility(SchemaCompatibility.BACKWARD) .build(); }
private SchemaMetadata createSchemaInfo(String testName, SchemaCompatibility compatibility) { return new SchemaMetadata.Builder(testName + "-schema") .type(AvroSchemaProvider.TYPE) .schemaGroup(testName + "-group") .description("Schema for " + testName) .compatibility(compatibility) .build(); }
private SchemaMetadata createSchemaMetadata(String topic) { SchemaMetadata schemaMetadata = getSchemaKey(topic, isKey); String description = "Schema registered by KafkaAvroSerializer for topic: [" + topic + "] iskey: [" + isKey + "]"; return new SchemaMetadata.Builder(schemaMetadata).description(description).compatibility(compatibility).build(); }
private SchemaMetadata buildSchemaMetadata(String schemaDesc, SchemaCompatibility compatibility) { return new SchemaMetadata.Builder(schemaDesc + "-schema") .type(AvroSchemaProvider.TYPE) .schemaGroup(schemaDesc + "-group") .description("Schema for " + schemaDesc) .compatibility(compatibility) .build(); } }
@Test public void testSchemaMetadataOps() throws Exception { for (SchemaCompatibility schemaCompatibility : SchemaCompatibility.values()) { SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("compatibility-" + schemaCompatibility) .type(AvroSchemaProvider.TYPE) .description("devices schema") .compatibility(schemaCompatibility) .schemaGroup(SCHEMA_GROUP).build(); Long schemaMetadataId = schemaRegistry.registerSchemaMetadata(schemaMetadata); SchemaMetadata schemaMetadataReturned = schemaRegistry.getSchemaMetadataInfo(schemaMetadataId).getSchemaMetadata(); Assert.assertEquals(schemaMetadata, schemaMetadataReturned); } }
public SchemaMetadata toSchemaMetadata() { return new SchemaMetadata.Builder(getName()) .type(getType()) .schemaGroup(getSchemaGroup()) .compatibility(getCompatibility()) .validationLevel(getValidationLevel()) .description(getDescription()) .evolve(getEvolve()) .build(); }
private SchemaMetadata createSchemaMetadata(String schemaDesc, SchemaCompatibility compatibility) { return new SchemaMetadata.Builder(schemaDesc + "-schema") .type(AvroSchemaProvider.TYPE) .schemaGroup(schemaDesc + "-group") .description("Schema for " + schemaDesc) .compatibility(compatibility) .build(); }