Java Code Examples for io.confluent.kafka.schemaregistry.client.SchemaRegistryClient#getAllVersions()

The following examples show how to use io.confluent.kafka.schemaregistry.client.SchemaRegistryClient#getAllVersions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfluentClientTest.java    From apicurio-registry with Apache License 2.0 4 votes vote down vote up
@Test
    public void testSimpleOps() throws Exception {
        SchemaRegistryClient client = buildClient();
        final String subject = generateArtifactId();

        Schema schema1 = new Schema.Parser().parse("{\"type\":\"record\",\"name\":\"myrecord1\",\"fields\":[{\"name\":\"f1\",\"type\":\"string\"}]}");
        int id1 = client.register(subject, schema1);
        
        // Reset the client cache so that the next line actually does what we want.
        client.reset();

        ParsedSchema ps1 = TestUtils.retry(() -> client.getSchemaById(id1));

        Schema schema2 = new Schema.Parser().parse("{\"type\":\"record\",\"name\":\"myrecord2\",\"fields\":[{\"name\":\"f2\",\"type\":\"string\"}]}");
        int id2 = client.register(subject, schema2);

        ParsedSchema ps2 = TestUtils.retry(() -> client.getSchemaById(id2));

        Schema schema = client.getById(id1);
        Assertions.assertNotNull(schema);

        client.reset();

        Assertions.assertTrue(client.testCompatibility(subject, schema2));

        // global id can be mapped async
        retry(() -> {
            Schema schema3 = client.getById(id2);
            Assertions.assertNotNull(schema3);
            return schema3;
        });

        Collection<String> subjects = client.getAllSubjects();
        Assertions.assertTrue(subjects.contains(subject));

        List<Integer> versions = client.getAllVersions(subject);
        Assertions.assertTrue(versions.contains(1));
        Assertions.assertTrue(versions.contains(2));

        // TODO -- match per schema!
        //int v1 = client.getVersion(subject, schema1);
        //Assertions.assertEquals(1, v1);

        int v2 = client.getVersion(subject, schema2);
        Assertions.assertEquals(2, v2);

        int d1 = client.deleteSchemaVersion(subject, "1");
        Assertions.assertEquals(1, d1);
        int d2 = client.deleteSchemaVersion(subject, "2");
        Assertions.assertEquals(2, d2);
        //int dl = client.deleteSchemaVersion(subject, "latest");
        //Assertions.assertEquals(2, dl);

        // TODO: discuss with Ales: both versions of the schema were deleted above.  should the subject be deleted when all versions are deleted?
//        versions = client.deleteSubject(subject);
        // TODO: why would this work?  deleting the subject would return the already-deleted versions?
//        Assertions.assertTrue(versions.contains(1));
//        Assertions.assertTrue(versions.contains(2));
    }
 
Example 2
Source File: SerdeMixTest.java    From apicurio-registry with Apache License 2.0 4 votes vote down vote up
@RegistryServiceTest
public void testVersions(Supplier<RegistryService> supplier) throws Exception {
    SchemaRegistryClient confClient = buildClient();
    RegistryService apicurioClient = supplier.get();

    String subject = generateArtifactId();

    Schema schema = new Schema.Parser().parse("{\"type\":\"record\",\"name\":\"myrecord5\",\"fields\":[{\"name\":\"bar\",\"type\":\"string\"}]}");
    int id = confClient.register(subject, schema);
    confClient.reset();

    this.waitForArtifact(subject);

    this.waitForGlobalId(id);
    Schema schema2 = confClient.getById(id);
    Assertions.assertNotNull(schema2);

    CompletionStage<ArtifactMetaData> cs = apicurioClient.updateArtifact(subject, ArtifactType.AVRO, new ByteArrayInputStream(IoUtil.toBytes(schema.toString())));
    ArtifactMetaData amd1 = ConcurrentUtil.result(cs);
    this.waitForGlobalId(amd1.getGlobalId());

    apicurioClient.getArtifactMetaDataByGlobalId(amd1.getGlobalId());

    cs = apicurioClient.updateArtifact(subject, ArtifactType.AVRO, new ByteArrayInputStream(IoUtil.toBytes(schema.toString())));
    ArtifactMetaData amd2 = ConcurrentUtil.result(cs);
    this.waitForGlobalId(amd2.getGlobalId());

    apicurioClient.getArtifactMetaDataByGlobalId(amd2.getGlobalId());

    List<Integer> versions1 = confClient.getAllVersions(subject);
    Assertions.assertEquals(3, versions1.size());
    Assertions.assertTrue(versions1.contains(1));
    Assertions.assertTrue(versions1.contains(2));
    Assertions.assertTrue(versions1.contains(3));

    List<Long> versions2 = apicurioClient.listArtifactVersions(subject);
    Assertions.assertEquals(3, versions2.size());
    Assertions.assertTrue(versions2.contains(1L));
    Assertions.assertTrue(versions2.contains(2L));
    Assertions.assertTrue(versions2.contains(3L));

    confClient.deleteSchemaVersion(subject, "1");

    TestUtils.retry(() -> {
        try {
            apicurioClient.reset();
            apicurioClient.getArtifactVersionMetaData(1, subject);
            Assertions.fail("Expected apicurioClient.getArtifactVersionMetaData() to fail");
        } catch (Exception ignored) {
        }
        return null;
    });

    versions1 = confClient.getAllVersions(subject);
    Assertions.assertEquals(2, versions1.size());
    Assertions.assertFalse(versions1.contains(1));
    Assertions.assertTrue(versions1.contains(2));
    Assertions.assertTrue(versions1.contains(3));

    versions2 = apicurioClient.listArtifactVersions(subject);
    Assertions.assertEquals(2, versions2.size());
    Assertions.assertFalse(versions2.contains(1L));
    Assertions.assertTrue(versions2.contains(2L));
    Assertions.assertTrue(versions2.contains(3L));
}