Schema deletion additional changes
- fix typo - Return METHOD_NOT_ALLOWED if schema deletion is disabled - Tests dirty context to avoid cross-chatter Signed-off-by: Marius Bogoevici <mbogoevici@pivotal.io>
This commit is contained in:
@@ -1280,7 +1280,7 @@ Spring Cloud Stream provides a schema registry server implementation.
|
||||
In order to use it, you can simply add the `spring-cloud-stream-schema-server` artifact to your project and use the `@EnableSchemaRegistryServer` annotation, adding the schema registry server REST controller to your application.
|
||||
This annotation is intended to be used with Spring Boot web applications, and the listening port of the server is controlled by the `server.port` setting.
|
||||
The `spring.cloud.stream.schema.server.path` setting can be used to control the root path of the schema server (especially when it is embedded in other applications).
|
||||
The `spring.cloud.stream.schema.server.alllowSchemaDeletion` boolean setting enables the deletion of schema. By default this is disabled.
|
||||
The `spring.cloud.stream.schema.server.allowSchemaDeletion` boolean setting enables the deletion of schema. By default this is disabled.
|
||||
|
||||
The schema registry server uses a relational database to store the schemas.
|
||||
By default, it uses an embedded database.
|
||||
|
||||
@@ -36,7 +36,7 @@ public class SchemaServerProperties {
|
||||
/**
|
||||
* Boolean flag to enable/disable schema deletion.
|
||||
*/
|
||||
private boolean alllowSchemaDeletion;
|
||||
private boolean allowSchemaDeletion;
|
||||
|
||||
public String getPath() {
|
||||
return this.path;
|
||||
@@ -46,11 +46,11 @@ public class SchemaServerProperties {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public boolean isAlllowSchemaDeletion() {
|
||||
return alllowSchemaDeletion;
|
||||
public boolean isAllowSchemaDeletion() {
|
||||
return allowSchemaDeletion;
|
||||
}
|
||||
|
||||
public void setAlllowSchemaDeletion(boolean alllowSchemaDeletion) {
|
||||
this.alllowSchemaDeletion = alllowSchemaDeletion;
|
||||
public void setAllowSchemaDeletion(boolean allowSchemaDeletion) {
|
||||
this.allowSchemaDeletion = allowSchemaDeletion;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ public class ServerController {
|
||||
public void delete(@PathVariable("subject") String subject,
|
||||
@PathVariable("format") String format,
|
||||
@PathVariable("version") Integer version) {
|
||||
if (this.schemaServerProperties.isAlllowSchemaDeletion()) {
|
||||
if (this.schemaServerProperties.isAllowSchemaDeletion()) {
|
||||
Schema schema = this.repository.findOneBySubjectAndFormatAndVersion(subject, format,
|
||||
version);
|
||||
deleteSchema(schema);
|
||||
@@ -149,7 +149,7 @@ public class ServerController {
|
||||
|
||||
@RequestMapping(value = "/schemas/{id}", method = RequestMethod.DELETE)
|
||||
public void delete(@PathVariable("id") Integer id) {
|
||||
if (this.schemaServerProperties.isAlllowSchemaDeletion()) {
|
||||
if (this.schemaServerProperties.isAllowSchemaDeletion()) {
|
||||
Schema schema = this.repository.findOne(id);
|
||||
deleteSchema(schema);
|
||||
}
|
||||
@@ -160,13 +160,17 @@ public class ServerController {
|
||||
|
||||
@RequestMapping(value = "/{subject}", method = RequestMethod.DELETE)
|
||||
public void delete(@PathVariable("subject") String subject) {
|
||||
if (this.schemaServerProperties.isAlllowSchemaDeletion()) {
|
||||
if (this.schemaServerProperties.isAllowSchemaDeletion()) {
|
||||
for (Schema schema : this.repository.findAll()) {
|
||||
if (schema.getSubject().equals(subject)) {
|
||||
deleteSchema(schema);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new SchemaDeletionNotAllowedException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void deleteSchema(Schema schema) {
|
||||
@@ -191,4 +195,9 @@ public class ServerController {
|
||||
public void schemaNotFound(SchemaNotFoundException ex) {
|
||||
}
|
||||
|
||||
@ExceptionHandler(SchemaDeletionNotAllowedException.class)
|
||||
@ResponseStatus(value = HttpStatus.METHOD_NOT_ALLOWED, reason = "Schema deletion is not permitted")
|
||||
public void schemaDeletionNotPermitted(SchemaDeletionNotAllowedException ex) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,19 +28,17 @@ import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.cloud.stream.schema.server.config.SchemaServerProperties;
|
||||
import org.springframework.cloud.stream.schema.server.model.Schema;
|
||||
import org.springframework.cloud.stream.schema.server.support.SchemaDeletionNotAllowedException;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD;
|
||||
|
||||
/**
|
||||
* @author Vinicius Carvalho
|
||||
@@ -48,6 +46,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
||||
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
|
||||
public class SchemaRegistryServerAvroTests {
|
||||
|
||||
final String USER_SCHEMA_V1 = "{\"namespace\": \"example.avro\",\n"
|
||||
@@ -174,7 +173,7 @@ public class SchemaRegistryServerAvroTests {
|
||||
ResponseEntity<Schema> response1 = client.postForEntity("http://localhost:8990/",
|
||||
schema, Schema.class);
|
||||
Assert.assertTrue(response1.getStatusCode().is2xxSuccessful());
|
||||
schemaServerProperties.setAlllowSchemaDeletion(true);
|
||||
schemaServerProperties.setAllowSchemaDeletion(true);
|
||||
client.delete("http://localhost:8990/test/avro/v1");
|
||||
ResponseEntity<Schema> response2 = client
|
||||
.getForEntity("http://localhost:8990/test/avro/v1", Schema.class);
|
||||
@@ -193,7 +192,7 @@ public class SchemaRegistryServerAvroTests {
|
||||
ResponseEntity<Schema> response2 = client
|
||||
.getForEntity("http://localhost:8990/test/avro/v1", Schema.class);
|
||||
Assert.assertEquals(HttpStatus.OK, response2.getStatusCode());
|
||||
schemaServerProperties.setAlllowSchemaDeletion(true);
|
||||
schemaServerProperties.setAllowSchemaDeletion(true);
|
||||
client.delete("http://localhost:8990/schemas/1");
|
||||
ResponseEntity<Schema> response3 = client
|
||||
.getForEntity("http://localhost:8990/test/avro/1", Schema.class);
|
||||
@@ -219,7 +218,7 @@ public class SchemaRegistryServerAvroTests {
|
||||
schema2, Schema.class);
|
||||
Assert.assertTrue(response2.getStatusCode().is2xxSuccessful());
|
||||
Assert.assertEquals(HttpStatus.OK, client.getForEntity("http://localhost:8990/test/avro/v2", Schema.class).getStatusCode());
|
||||
schemaServerProperties.setAlllowSchemaDeletion(true);
|
||||
schemaServerProperties.setAllowSchemaDeletion(true);
|
||||
client.delete("http://localhost:8990/test");
|
||||
ResponseEntity<Schema> response4 = client
|
||||
.getForEntity("http://localhost:8990/test/avro/v1", Schema.class);
|
||||
@@ -238,14 +237,15 @@ public class SchemaRegistryServerAvroTests {
|
||||
ResponseEntity<Schema> response1 = client.postForEntity("http://localhost:8990/",
|
||||
schema, Schema.class);
|
||||
Assert.assertTrue(response1.getStatusCode().is2xxSuccessful());
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).defaultRequest(
|
||||
get("/").accept(MediaType.APPLICATION_JSON)).build();
|
||||
try {
|
||||
mockMvc.perform(delete("http://localhost:8990/test/avro/v1"));
|
||||
}
|
||||
catch (Exception e) {
|
||||
Assert.assertTrue(e.getCause() instanceof SchemaDeletionNotAllowedException);
|
||||
}
|
||||
ResponseEntity<Object> deleteBySubjectFormatVersion = client.exchange("http://localhost:8990/test/avro/v1", HttpMethod.DELETE,
|
||||
null, Object.class);
|
||||
assertThat(deleteBySubjectFormatVersion.getStatusCode()).isEqualTo(HttpStatus.METHOD_NOT_ALLOWED);
|
||||
ResponseEntity<Object> deleteBySubject = client.exchange("http://localhost:8990/test", HttpMethod.DELETE,
|
||||
null, Object.class);
|
||||
assertThat(deleteBySubject.getStatusCode()).isEqualTo(HttpStatus.METHOD_NOT_ALLOWED);
|
||||
ResponseEntity<Object> deleteById = client.exchange("http://localhost:8990/schemas/1", HttpMethod.DELETE,
|
||||
null, Object.class);
|
||||
assertThat(deleteById.getStatusCode()).isEqualTo(HttpStatus.METHOD_NOT_ALLOWED);
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
|
||||
Reference in New Issue
Block a user