Search in sources :

Example 11 with RestService

use of io.confluent.kafka.schemaregistry.client.rest.RestService in project schema-registry by confluentinc.

the class CachedSchemaRegistryClientTest method testGetSchemas.

@Test
public void testGetSchemas() throws Exception {
    expect(restService.registerSchema(anyString(), anyString(), anyObject(List.class), anyString(), anyBoolean())).andReturn(ID_25).andReturn(26).andReturn(27).andReturn(28).andReturn(29);
    List<Schema> schemas = IntStream.range(0, 5).mapToObj(idx -> new Schema(SUBJECT_0, 7, idx + 25, AvroSchema.TYPE, Collections.emptyList(), avroSchemaString(idx))).collect(Collectors.toList());
    expect(restService.getSchemas(anyString(), anyBoolean(), anyBoolean())).andReturn(schemas);
    replay(restService);
    for (int i = 0; i != CACHE_CAPACITY; ++i) {
        // Each one results in new id.
        client.register(SUBJECT_0, avroSchema(i));
    }
    List<ParsedSchema> parsedSchemas = client.getSchemas(SUBJECT_0, false, true);
    assertEquals(5, parsedSchemas.size());
    IntStream.range(0, 5).forEach(idx -> {
        assertEquals(new AvroSchema(avroSchemaString(idx)), parsedSchemas.get(idx));
        assertEquals(AvroSchema.TYPE, parsedSchemas.get(idx).schemaType());
    });
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) RestService(io.confluent.kafka.schemaregistry.client.rest.RestService) HashMap(java.util.HashMap) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) EasyMock.reset(org.easymock.EasyMock.reset) Map(java.util.Map) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) EasyMock.replay(org.easymock.EasyMock.replay) Assert.fail(org.junit.Assert.fail) EasyMock.eq(org.easymock.EasyMock.eq) Before(org.junit.Before) EasyMock.anyObject(org.easymock.EasyMock.anyObject) FakeTicker(com.google.common.testing.FakeTicker) EasyMock.anyString(org.easymock.EasyMock.anyString) ModeUpdateRequest(io.confluent.kafka.schemaregistry.client.rest.entities.requests.ModeUpdateRequest) Assert.assertNotNull(org.junit.Assert.assertNotNull) Mode(io.confluent.kafka.schemaregistry.client.rest.entities.Mode) Test(org.junit.Test) IOException(java.io.IOException) EasyMock.expect(org.easymock.EasyMock.expect) Collectors(java.util.stream.Collectors) ConfigException(org.apache.kafka.common.config.ConfigException) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) TimeUnit(java.util.concurrent.TimeUnit) EasyMock.expectLastCall(org.easymock.EasyMock.expectLastCall) List(java.util.List) EasyMock.anyBoolean(org.easymock.EasyMock.anyBoolean) EasyMock.createNiceMock(org.easymock.EasyMock.createNiceMock) EasyMock.verify(org.easymock.EasyMock.verify) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) List(java.util.List) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) Test(org.junit.Test)

Example 12 with RestService

use of io.confluent.kafka.schemaregistry.client.rest.RestService in project schema-registry by confluentinc.

the class RestApp method start.

public void start() throws Exception {
    restApp = new SchemaRegistryRestApplication(prop);
    restServer = restApp.createServer();
    restServer.start();
    restConnect = restServer.getURI().toString();
    if (restConnect.endsWith("/"))
        restConnect = restConnect.substring(0, restConnect.length() - 1);
    restClient = new RestService(restConnect);
}
Also used : SchemaRegistryRestApplication(io.confluent.kafka.schemaregistry.rest.SchemaRegistryRestApplication) RestService(io.confluent.kafka.schemaregistry.client.rest.RestService)

Example 13 with RestService

use of io.confluent.kafka.schemaregistry.client.rest.RestService in project schema-registry by confluentinc.

the class MetricsTest method testSchemaCreatedCount.

@Test
public void testSchemaCreatedCount() throws Exception {
    RestService service = restApp.restClient;
    String subject = "testTopic1";
    int schemaCount = 3;
    List<String> schemas = TestUtils.getRandomCanonicalAvroString(schemaCount);
    // test registering and verifying new schemas in subject1
    int schemaIdCounter = 1;
    for (int i = 0; i < schemaCount; i++) {
        String schema = schemas.get(i);
        TestUtils.registerAndVerifySchema(service, schema, schemaIdCounter++, subject);
    }
    assertEquals(schemaCount, container.getSchemasCreated().get());
    assertEquals(schemaCount, container.getSchemasCreated(AvroSchema.TYPE).get());
    // Re-registering schemas should not increase metrics.
    for (int i = 0; i < schemaCount; i++) {
        String schemaString = schemas.get(i);
        service.registerSchema(schemaString, subject);
    }
    assertEquals(schemaCount, container.getSchemasCreated().get());
    assertEquals(schemaCount, container.getSchemasCreated(AvroSchema.TYPE).get());
    for (Integer i = 1; i < schemaIdCounter; i++) {
        assertEquals(i, service.deleteSchemaVersion(RestService.DEFAULT_REQUEST_PROPERTIES, subject, i.toString()));
    }
    // Deleting schemas should not modify create count.
    assertEquals(schemaCount, container.getSchemasCreated().get());
    assertEquals(schemaCount, container.getSchemasCreated(AvroSchema.TYPE).get());
    assertEquals(schemaCount, container.getSchemasDeleted().get());
    assertEquals(schemaCount, container.getSchemasDeleted(AvroSchema.TYPE).get());
}
Also used : RestService(io.confluent.kafka.schemaregistry.client.rest.RestService) Test(org.junit.Test)

Example 14 with RestService

use of io.confluent.kafka.schemaregistry.client.rest.RestService in project akhq by tchiotludo.

the class KafkaModule method getRegistryRestClient.

public RestService getRegistryRestClient(String clusterId) {
    Connection connection = this.getConnection(clusterId);
    if (connection.getSchemaRegistry() != null) {
        RestService restService = new RestService(connection.getSchemaRegistry().getUrl());
        if (connection.getSchemaRegistry().getProperties() != null && !connection.getSchemaRegistry().getProperties().isEmpty()) {
            Map<String, Object> sslConfigs = connection.getSchemaRegistry().getProperties().entrySet().stream().filter(e -> e.getKey().startsWith("schema.registry.")).collect(Collectors.toMap(e -> e.getKey().substring("schema.registry.".length()), Map.Entry::getValue));
            SslFactory sslFactory = new SslFactory(sslConfigs);
            if (sslFactory != null && sslFactory.sslContext() != null) {
                restService.setSslSocketFactory(sslFactory.sslContext().getSocketFactory());
            }
        }
        restService.setHttpHeaders(Collections.singletonMap("Accept", "application/json"));
        if (connection.getSchemaRegistry().getBasicAuthUsername() != null) {
            BasicAuthCredentialProvider basicAuthCredentialProvider = BasicAuthCredentialProviderFactory.getBasicAuthCredentialProvider(new UserInfoCredentialProvider().alias(), ImmutableMap.of("schema.registry.basic.auth.user.info", connection.getSchemaRegistry().getBasicAuthUsername() + ":" + connection.getSchemaRegistry().getBasicAuthPassword()));
            restService.setBasicAuthCredentialProvider(basicAuthCredentialProvider);
        }
        if (connection.getSchemaRegistry().getProperties() != null) {
            restService.configure(connection.getSchemaRegistry().getProperties());
        }
        return restService;
    }
    return null;
}
Also used : AbstractProperties(org.akhq.configs.AbstractProperties) java.util(java.util) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) RestService(io.confluent.kafka.schemaregistry.client.rest.RestService) BasicAuthCredentialProvider(io.confluent.kafka.schemaregistry.client.security.basicauth.BasicAuthCredentialProvider) ProtobufSchemaProvider(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider) Default(org.akhq.configs.Default) AdminClient(org.apache.kafka.clients.admin.AdminClient) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) ByteArraySerializer(org.apache.kafka.common.serialization.ByteArraySerializer) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) URIBuilder(org.codehaus.httpcache4j.uri.URIBuilder) JsonSchemaProvider(io.confluent.kafka.schemaregistry.json.JsonSchemaProvider) ByteArrayDeserializer(org.apache.kafka.common.serialization.ByteArrayDeserializer) Configuration(org.sourcelab.kafka.connect.apiclient.Configuration) ImmutableMap(com.google.common.collect.ImmutableMap) KafkaConnectClient(org.sourcelab.kafka.connect.apiclient.KafkaConnectClient) Singleton(jakarta.inject.Singleton) AvroSchemaProvider(io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider) Collectors(java.util.stream.Collectors) File(java.io.File) BasicAuthCredentialProviderFactory(io.confluent.kafka.schemaregistry.client.security.basicauth.BasicAuthCredentialProviderFactory) Connection(org.akhq.configs.Connection) UserInfoCredentialProvider(io.confluent.kafka.schemaregistry.client.security.basicauth.UserInfoCredentialProvider) CachedSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient) Inject(jakarta.inject.Inject) SslFactory(io.confluent.kafka.schemaregistry.client.security.SslFactory) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) BasicAuthCredentialProvider(io.confluent.kafka.schemaregistry.client.security.basicauth.BasicAuthCredentialProvider) UserInfoCredentialProvider(io.confluent.kafka.schemaregistry.client.security.basicauth.UserInfoCredentialProvider) Connection(org.akhq.configs.Connection) RestService(io.confluent.kafka.schemaregistry.client.rest.RestService) ImmutableMap(com.google.common.collect.ImmutableMap) SslFactory(io.confluent.kafka.schemaregistry.client.security.SslFactory)

Aggregations

RestService (io.confluent.kafka.schemaregistry.client.rest.RestService)14 CachedSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient)7 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 AvroSchemaProvider (io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider)2 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)2 SchemaString (io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString)2 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)2 BasicAuthCredentialProvider (io.confluent.kafka.schemaregistry.client.security.basicauth.BasicAuthCredentialProvider)2 JsonSchemaProvider (io.confluent.kafka.schemaregistry.json.JsonSchemaProvider)2 ProtobufSchemaProvider (io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider)2 Collectors (java.util.stream.Collectors)2 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 FakeTicker (com.google.common.testing.FakeTicker)1 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)1 SchemaProvider (io.confluent.kafka.schemaregistry.SchemaProvider)1 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)1 Mode (io.confluent.kafka.schemaregistry.client.rest.entities.Mode)1 Schema (io.confluent.kafka.schemaregistry.client.rest.entities.Schema)1