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;
}
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();
}
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", "<>");
}
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();
}
Aggregations