diff --git a/spring-cloud-stream-schema-server/pom.xml b/spring-cloud-stream-schema-server/pom.xml index f10084061..ca8321557 100644 --- a/spring-cloud-stream-schema-server/pom.xml +++ b/spring-cloud-stream-schema-server/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 spring-cloud-stream-schema-server diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/config/SchemaServerConfiguration.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/config/SchemaServerConfiguration.java index def498eb9..2528cea85 100644 --- a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/config/SchemaServerConfiguration.java +++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/config/SchemaServerConfiguration.java @@ -43,18 +43,6 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @EnableConfigurationProperties(SchemaServerProperties.class) public class SchemaServerConfiguration { - @Bean - public ServerController serverController(SchemaRepository repository, SchemaServerProperties schemeServerProperties) { - return new ServerController(repository, schemaValidators(), schemeServerProperties); - } - - @Bean - public Map schemaValidators() { - Map validatorMap = new HashMap<>(); - validatorMap.put("avro", new AvroSchemaValidator()); - return validatorMap; - } - @Bean public static BeanFactoryPostProcessor entityScanPackagesPostProcessor() { return new BeanFactoryPostProcessor() { @@ -69,4 +57,16 @@ public class SchemaServerConfiguration { }; } + @Bean + public ServerController serverController(SchemaRepository repository, SchemaServerProperties schemeServerProperties) { + return new ServerController(repository, schemaValidators(), schemeServerProperties); + } + + @Bean + public Map schemaValidators() { + Map validatorMap = new HashMap<>(); + validatorMap.put("avro", new AvroSchemaValidator()); + return validatorMap; + } + } diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/controllers/ServerController.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/controllers/ServerController.java index a46566bb2..cbe8d51af 100644 --- a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/controllers/ServerController.java +++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/controllers/ServerController.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -133,6 +133,15 @@ public class ServerController { return new ResponseEntity<>(schema, HttpStatus.OK); } + @RequestMapping(method = RequestMethod.GET, produces = "application/json", path = "/{subject}/{format}") + public ResponseEntity> findBySubjectAndVersion(@PathVariable("subject") String subject, @PathVariable("format") String format) { + List schemas = repository.findBySubjectAndFormatOrderByVersion(subject, format); + if (schemas == null || schemas.size() == 0) { + throw new SchemaNotFoundException(String.format("No schemas found for subject %s and format %s", subject, format)); + } + return new ResponseEntity>(schemas, HttpStatus.OK); + } + @RequestMapping(value = "/{subject}/{format}/v{version}", method = RequestMethod.DELETE) public void delete(@PathVariable("subject") String subject, @PathVariable("format") String format, diff --git a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/repository/SchemaRepository.java b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/repository/SchemaRepository.java index 0b9c39358..88cff4060 100644 --- a/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/repository/SchemaRepository.java +++ b/spring-cloud-stream-schema-server/src/main/java/org/springframework/cloud/stream/schema/server/repository/SchemaRepository.java @@ -30,6 +30,7 @@ public interface SchemaRepository extends PagingAndSortingRepository findBySubjectAndFormatOrderByVersion(String subject, String format); + @Transactional Schema findOneBySubjectAndFormatAndVersion(String subject, String format, Integer version); diff --git a/spring-cloud-stream-schema-server/src/test/java/org/springframework/cloud/stream/schema/server/SchemaRegistryServerAvroTests.java b/spring-cloud-stream-schema-server/src/test/java/org/springframework/cloud/stream/schema/server/SchemaRegistryServerAvroTests.java index 4198cae42..787b59feb 100644 --- a/spring-cloud-stream-schema-server/src/test/java/org/springframework/cloud/stream/schema/server/SchemaRegistryServerAvroTests.java +++ b/spring-cloud-stream-schema-server/src/test/java/org/springframework/cloud/stream/schema/server/SchemaRegistryServerAvroTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ import org.springframework.boot.test.context.SpringBootTest; 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.core.ParameterizedTypeReference; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; @@ -246,4 +247,30 @@ public class SchemaRegistryServerAvroTests { assertThat(deleteById.getStatusCode()).isEqualTo(HttpStatus.METHOD_NOT_ALLOWED); } + @Test + public void testFindSchemaBySubject() throws Exception { + Schema v1 = new Schema(); + v1.setFormat("avro"); + v1.setSubject("test"); + v1.setDefinition(USER_SCHEMA_V1); + ResponseEntity response1 = client.postForEntity("http://localhost:8990/", + v1, Schema.class); + Assert.assertTrue(response1.getStatusCode().is2xxSuccessful()); + + Schema v2 = new Schema(); + v2.setFormat("avro"); + v2.setSubject("test"); + v2.setDefinition(USER_SCHEMA_V2); + + ResponseEntity response2 = client.postForEntity("http://localhost:8990/", + v2, Schema.class); + Assert.assertTrue(response2.getStatusCode().is2xxSuccessful()); + + ResponseEntity> schemaResponse = client.exchange("http://localhost:8990/test/avro", HttpMethod.GET, null, new ParameterizedTypeReference>() { + }); + + Assert.assertTrue(schemaResponse.getStatusCode().is2xxSuccessful()); + Assert.assertEquals(2, schemaResponse.getBody().size()); + } + }