Search in sources :

Example 6 with SchemaRegistrationResponse

use of org.springframework.cloud.stream.schema.SchemaRegistrationResponse in project spring-cloud-stream by spring-cloud.

the class ConfluentSchemaRegistryClient method register.

@Override
public SchemaRegistrationResponse register(String subject, String format, String schema) {
    Assert.isTrue("avro".equals(format), "Only Avro is supported");
    String path = String.format("/subjects/%s/versions", subject);
    HttpHeaders headers = new HttpHeaders();
    headers.put("Accept", ACCEPT_HEADERS);
    headers.add("Content-Type", "application/json");
    Integer version = null;
    Integer id = null;
    String payload = null;
    try {
        payload = this.mapper.writeValueAsString(Collections.singletonMap("schema", schema));
    } catch (JsonProcessingException e) {
        throw new RuntimeException("Could not parse schema, invalid JSON format", e);
    }
    try {
        HttpEntity<String> request = new HttpEntity<>(payload, headers);
        ResponseEntity<Map> response = this.template.exchange(this.endpoint + path, HttpMethod.POST, request, Map.class);
        id = (Integer) response.getBody().get("id");
        version = getSubjectVersion(subject, payload);
    } catch (HttpStatusCodeException httpException) {
        throw new RuntimeException(String.format("Failed to register subject %s, server replied with status %d", subject, httpException.getStatusCode().value()), httpException);
    }
    SchemaRegistrationResponse schemaRegistrationResponse = new SchemaRegistrationResponse();
    schemaRegistrationResponse.setId(id);
    schemaRegistrationResponse.setSchemaReference(new SchemaReference(subject, version, "avro"));
    return schemaRegistrationResponse;
}
Also used : SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) SchemaRegistrationResponse(org.springframework.cloud.stream.schema.SchemaRegistrationResponse) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Map(java.util.Map)

Example 7 with SchemaRegistrationResponse

use of org.springframework.cloud.stream.schema.SchemaRegistrationResponse in project spring-cloud-stream by spring-cloud.

the class ConfluentSchemaRegistryClientTests method registerIncompatibleSchema.

@Test
public void registerIncompatibleSchema() {
    this.mockRestServiceServer.expect(requestTo("http://localhost:8081/subjects/user/versions")).andExpect(method(HttpMethod.POST)).andExpect(header("Content-Type", "application/json")).andExpect(header("Accept", "application/vnd.schemaregistry.v1+json")).andRespond(withStatus(HttpStatus.CONFLICT));
    ConfluentSchemaRegistryClient client = new ConfluentSchemaRegistryClient(this.restTemplate);
    Exception expected = null;
    try {
        SchemaRegistrationResponse response = client.register("user", "avro", "{}");
    } catch (Exception e) {
        expected = e;
    }
    Assert.assertTrue(expected instanceof RuntimeException);
    Assert.assertTrue(expected.getCause() instanceof HttpStatusCodeException);
    this.mockRestServiceServer.verify();
}
Also used : SchemaRegistrationResponse(org.springframework.cloud.stream.schema.SchemaRegistrationResponse) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) ConfluentSchemaRegistryClient(org.springframework.cloud.stream.schema.client.ConfluentSchemaRegistryClient) SchemaNotFoundException(org.springframework.cloud.stream.schema.SchemaNotFoundException) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) Test(org.junit.Test)

Example 8 with SchemaRegistrationResponse

use of org.springframework.cloud.stream.schema.SchemaRegistrationResponse in project spring-cloud-stream by spring-cloud.

the class ConfluentSchemaRegistryClientTests method registerWithInvalidJson.

@Test(expected = RuntimeException.class)
public void registerWithInvalidJson() {
    this.mockRestServiceServer.expect(requestTo("http://localhost:8081/subjects/user/versions")).andExpect(method(HttpMethod.POST)).andExpect(header("Content-Type", "application/json")).andExpect(header("Accept", "application/vnd.schemaregistry.v1+json")).andRespond(withBadRequest());
    ConfluentSchemaRegistryClient client = new ConfluentSchemaRegistryClient(this.restTemplate);
    SchemaRegistrationResponse response = client.register("user", "avro", "<>");
}
Also used : SchemaRegistrationResponse(org.springframework.cloud.stream.schema.SchemaRegistrationResponse) ConfluentSchemaRegistryClient(org.springframework.cloud.stream.schema.client.ConfluentSchemaRegistryClient) Test(org.junit.Test)

Example 9 with SchemaRegistrationResponse

use of org.springframework.cloud.stream.schema.SchemaRegistrationResponse in project spring-cloud-stream by spring-cloud.

the class ConfluentSchemaRegistryClientTests method registerSchema.

@Test
public void registerSchema() throws Exception {
    this.mockRestServiceServer.expect(requestTo("http://localhost:8081/subjects/user/versions")).andExpect(method(HttpMethod.POST)).andExpect(header("Content-Type", "application/json")).andExpect(header("Accept", "application/vnd.schemaregistry.v1+json")).andRespond(withSuccess("{\"id\":101}", MediaType.APPLICATION_JSON));
    this.mockRestServiceServer.expect(requestTo("http://localhost:8081/subjects/user")).andExpect(method(HttpMethod.POST)).andExpect(header("Content-Type", "application/json")).andExpect(header("Accept", "application/vnd.schemaregistry.v1+json")).andRespond(withSuccess("{\"version\":1}", MediaType.APPLICATION_JSON));
    ConfluentSchemaRegistryClient client = new ConfluentSchemaRegistryClient(this.restTemplate);
    SchemaRegistrationResponse response = client.register("user", "avro", "{}");
    Assert.assertEquals(1, response.getSchemaReference().getVersion());
    Assert.assertEquals(101, response.getId());
    this.mockRestServiceServer.verify();
}
Also used : SchemaRegistrationResponse(org.springframework.cloud.stream.schema.SchemaRegistrationResponse) ConfluentSchemaRegistryClient(org.springframework.cloud.stream.schema.client.ConfluentSchemaRegistryClient) Test(org.junit.Test)

Aggregations

SchemaRegistrationResponse (org.springframework.cloud.stream.schema.SchemaRegistrationResponse)9 Map (java.util.Map)4 Test (org.junit.Test)4 SchemaReference (org.springframework.cloud.stream.schema.SchemaReference)4 ConfluentSchemaRegistryClient (org.springframework.cloud.stream.schema.client.ConfluentSchemaRegistryClient)4 HttpStatusCodeException (org.springframework.web.client.HttpStatusCodeException)3 HashMap (java.util.HashMap)2 SchemaNotFoundException (org.springframework.cloud.stream.schema.SchemaNotFoundException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TreeMap (java.util.TreeMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Schema (org.apache.avro.Schema)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 ParsedSchema (org.springframework.cloud.stream.schema.ParsedSchema)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1