use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString in project schema-registry by confluentinc.
the class LeaderElectorTest method verifyIdAndSchema.
private void verifyIdAndSchema(final RestService restService, final int expectedId, final String expectedSchemaString, String errMsg) {
waitUntilIdExists(restService, expectedId, errMsg);
String schemaString = null;
try {
schemaString = restService.getId(expectedId).getSchemaString();
} catch (IOException e) {
fail(errMsg);
} catch (RestClientException e) {
fail(errMsg);
}
assertEquals(errMsg, expectedSchemaString, schemaString);
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString in project schema-registry by confluentinc.
the class RestApiTest method testBasic.
@Test
public void testBasic() throws Exception {
String subject1 = "testTopic1";
String subject2 = "testTopic2";
int schemasInSubject1 = 10;
List<Integer> allVersionsInSubject1 = new ArrayList<Integer>();
List<String> allSchemasInSubject1 = getRandomJsonSchemas(schemasInSubject1);
int schemasInSubject2 = 5;
List<Integer> allVersionsInSubject2 = new ArrayList<Integer>();
List<String> allSchemasInSubject2 = getRandomJsonSchemas(schemasInSubject2);
List<String> allSubjects = new ArrayList<String>();
// test getAllSubjects with no existing data
assertEquals("Getting all subjects should return empty", allSubjects, restApp.restClient.getAllSubjects());
// test registering and verifying new schemas in subject1
int schemaIdCounter = 1;
for (int i = 0; i < schemasInSubject1; i++) {
String schema = allSchemasInSubject1.get(i);
int expectedVersion = i + 1;
registerAndVerifySchema(restApp.restClient, schema, schemaIdCounter, subject1);
schemaIdCounter++;
allVersionsInSubject1.add(expectedVersion);
}
allSubjects.add(subject1);
// test re-registering existing schemas
for (int i = 0; i < schemasInSubject1; i++) {
int expectedId = i + 1;
String schemaString = allSchemasInSubject1.get(i);
int foundId = restApp.restClient.registerSchema(schemaString, JsonSchema.TYPE, Collections.emptyList(), subject1);
assertEquals("Re-registering an existing schema should return the existing version", expectedId, foundId);
}
// test registering schemas in subject2
for (int i = 0; i < schemasInSubject2; i++) {
String schema = allSchemasInSubject2.get(i);
int expectedVersion = i + 1;
registerAndVerifySchema(restApp.restClient, schema, schemaIdCounter, subject2);
schemaIdCounter++;
allVersionsInSubject2.add(expectedVersion);
}
allSubjects.add(subject2);
// test getAllVersions with existing data
assertEquals("Getting all versions from subject1 should match all registered versions", allVersionsInSubject1, restApp.restClient.getAllVersions(subject1));
assertEquals("Getting all versions from subject2 should match all registered versions", allVersionsInSubject2, restApp.restClient.getAllVersions(subject2));
// test getAllSubjects with existing data
assertEquals("Getting all subjects should match all registered subjects", allSubjects, restApp.restClient.getAllSubjects());
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString in project schema-registry by confluentinc.
the class LeaderElectorTest method testImportOnLeaderFollowerClusters.
@Test
public /**
* Test import mode and registration of schemas with version and id when a 'leader cluster' and
* 'follower cluster' is present. (Follower cluster == all nodes have leaderEligibility false)
*
* If only followers are alive, registration should fail. If both follower and leader cluster are
* alive, registration should succeed.
*
* Fetching by id should succeed in all configurations.
*/
void testImportOnLeaderFollowerClusters() throws Exception {
int numFollowers = 4;
int numLeaders = 4;
int numSchemas = 5;
String subject = "testSubject";
List<String> schemas = TestUtils.getRandomCanonicalAvroString(numSchemas);
List<Integer> ids = new ArrayList<Integer>();
Properties props = new Properties();
props.setProperty(SchemaRegistryConfig.MODE_MUTABILITY, "true");
int newId = 100000;
int newVersion = 100;
Set<RestApp> followerApps = new HashSet<RestApp>();
RestApp aFollower = null;
for (int i = 0; i < numFollowers; i++) {
RestApp follower = new RestApp(choosePort(), zkConnect(), bootstrapServers(), KAFKASTORE_TOPIC, CompatibilityLevel.NONE.name, false, props);
followerApps.add(follower);
follower.start();
aFollower = follower;
}
// Sanity check
assertNotNull(aFollower);
// Try to register schemas to a follower - should fail
boolean successfullyRegistered = false;
try {
aFollower.restClient.registerSchema(schemas.get(0), subject, newVersion++, newId++);
successfullyRegistered = true;
} catch (RestClientException e) {
// registration should fail
}
assertFalse("Should not be possible to register with no leaders present.", successfullyRegistered);
// Make a leader-eligible 'cluster'
final Set<RestApp> leaderApps = new HashSet<RestApp>();
RestApp aLeader = null;
for (int i = 0; i < numLeaders; i++) {
RestApp leader = new RestApp(choosePort(), zkConnect(), bootstrapServers(), KAFKASTORE_TOPIC, CompatibilityLevel.NONE.name, true, props);
leaderApps.add(leader);
leader.start();
aLeader = leader;
}
assertNotNull(aLeader);
// Enter import mode
try {
aLeader.restClient.setMode(Mode.IMPORT.toString());
} catch (RestClientException e) {
fail("It should be possible to set mode when a leader cluster is present.");
}
// Try to register to a leader cluster node - should succeed
try {
for (String schema : schemas) {
ids.add(aLeader.restClient.registerSchema(schema, subject, newVersion++, newId++));
}
} catch (RestClientException e) {
fail("It should be possible to register schemas when a leader cluster is present. " + "Error: " + e.getMessage());
}
// Try to register to a follower cluster node - should succeed
String anotherSchema = TestUtils.getRandomCanonicalAvroString(1).get(0);
try {
ids.add(aFollower.restClient.registerSchema(anotherSchema, subject, newVersion++, newId++));
} catch (RestClientException e) {
fail("Should be possible register a schema through follower cluster.");
}
// Verify all ids can be fetched
try {
for (int id : ids) {
waitUntilIdExists(aFollower.restClient, id, String.format("Should be possible to fetch id %d from this follower.", id));
waitUntilIdExists(aLeader.restClient, id, String.format("Should be possible to fetch id %d from this leader.", id));
SchemaString followerResponse = aFollower.restClient.getId(id);
SchemaString leaderResponse = aLeader.restClient.getId(id);
assertEquals("Leader and follower responded with different schemas when queried with the same id.", followerResponse.getSchemaString(), leaderResponse.getSchemaString());
}
} catch (RestClientException e) {
fail("Expected ids were not found in the schema registry.");
}
// Stop everything in the leader cluster
while (leaderApps.size() > 0) {
RestApp leader = findLeader(leaderApps);
leaderApps.remove(leader);
leader.stop();
waitUntilLeaderElectionCompletes(leaderApps);
}
// Try to register a new schema - should fail
anotherSchema = TestUtils.getRandomCanonicalAvroString(1).get(0);
successfullyRegistered = false;
try {
aFollower.restClient.registerSchema(anotherSchema, subject, newVersion++, newId++);
successfullyRegistered = true;
} catch (RestClientException e) {
// should fail
}
assertFalse("Should not be possible to register with no leaders present.", successfullyRegistered);
// Try fetching preregistered ids from followers - should succeed
try {
for (int id : ids) {
SchemaString schemaString = aFollower.restClient.getId(id);
}
List<Integer> versions = aFollower.restClient.getAllVersions(subject);
assertEquals("Number of ids should match number of versions.", ids.size(), versions.size());
} catch (RestClientException e) {
fail("Should be possible to fetch registered schemas even with no leaders present.");
}
for (RestApp follower : followerApps) {
follower.stop();
}
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString in project schema-registry by confluentinc.
the class RestApiTest method testSchemaReferences.
@Test
public void testSchemaReferences() throws Exception {
List<String> schemas = TestUtils.getAvroSchemaWithReferences();
String subject = "reference";
TestUtils.registerAndVerifySchema(restApp.restClient, schemas.get(0), 1, subject);
RegisterSchemaRequest request = new RegisterSchemaRequest();
request.setSchema(schemas.get(1));
SchemaReference ref = new SchemaReference("otherns.Subrecord", "reference", 1);
request.setReferences(Collections.singletonList(ref));
int registeredId = restApp.restClient.registerSchema(request, "referrer", false);
assertEquals("Registering a new schema should succeed", 2, registeredId);
SchemaString schemaString = restApp.restClient.getId(2);
// the newly registered schema should be immediately readable on the leader
assertEquals("Registered schema should be found", schemas.get(1), schemaString.getSchemaString());
assertEquals("Schema references should be found", Collections.singletonList(ref), schemaString.getReferences());
List<Integer> refs = restApp.restClient.getReferencedBy("reference", 1);
assertEquals(2, refs.get(0).intValue());
ns.MyRecord myrecord = new ns.MyRecord();
AvroSchema schema = new AvroSchema(AvroSchemaUtils.getSchema(myrecord));
// Note that we pass an empty list of refs since SR will perform a deep equality check
Schema registeredSchema = restApp.restClient.lookUpSubjectVersion(schema.canonicalString(), AvroSchema.TYPE, Collections.emptyList(), "referrer", false);
assertEquals("Registered schema should be found", 2, registeredSchema.getId().intValue());
try {
restApp.restClient.deleteSchemaVersion(RestService.DEFAULT_REQUEST_PROPERTIES, "reference", String.valueOf(1));
fail("Deleting reference should fail with " + Errors.REFERENCE_EXISTS_ERROR_CODE);
} catch (RestClientException rce) {
assertEquals("Reference found", Errors.REFERENCE_EXISTS_ERROR_CODE, rce.getErrorCode());
}
assertEquals((Integer) 1, restApp.restClient.deleteSchemaVersion(RestService.DEFAULT_REQUEST_PROPERTIES, "referrer", "1"));
refs = restApp.restClient.getReferencedBy("reference", 1);
assertTrue(refs.isEmpty());
assertEquals((Integer) 1, restApp.restClient.deleteSchemaVersion(RestService.DEFAULT_REQUEST_PROPERTIES, "reference", "1"));
}
use of io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString in project schema-registry by confluentinc.
the class RestApiTest method testRegisterInvalidSchemaBadReference.
@Test
public void testRegisterInvalidSchemaBadReference() throws Exception {
String subject = "testSubject";
// Invalid Reference
SchemaReference invalidReference = new SchemaReference("invalid.schema", "badSubject", 1);
String schemaString = "{\"type\":\"record\"," + "\"name\":\"myrecord\"," + "\"fields\":" + "[{\"type\":\"string\",\"name\":\"field1\"}]}";
try {
restApp.restClient.registerSchema(schemaString, "AVRO", Collections.singletonList(invalidReference), subject);
fail("Registering schema with invalid reference should fail with " + Errors.INVALID_SCHEMA_ERROR_CODE + " (invalid schema)");
} catch (RestClientException rce) {
assertEquals("Invalid schema", Errors.INVALID_SCHEMA_ERROR_CODE, rce.getErrorCode());
}
}
Aggregations