diff --git a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/BaseSpec.groovy b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/BaseSpec.groovy index e5cd2bb74..8dcedc104 100644 --- a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/BaseSpec.groovy +++ b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/BaseSpec.groovy @@ -58,15 +58,15 @@ abstract class BaseSpec extends Specification { } def createJsonRequest(method, path, query, obj) { - createRequest(method, path, null, "application/json", mapper.writeValueAsString(obj)) + createRequest(method, path, query, "application/json", mapper.writeValueAsString(obj)) } def createUriListRequest(method, path, query, obj) { - createRequest(method, path, null, "text/uri-list", obj.join("\n")) + createRequest(method, path, query, "text/uri-list", obj.join("\n")) } def createRequest(method, path, query) { - createRequest(method, path, null, null, null) + createRequest(method, path, query, null, null) } def createRequest(method, path, query, contentType, content) { @@ -76,9 +76,7 @@ abstract class BaseSpec extends Specification { method: method ) if (query) { - req.queryString = URLEncoder.encode( - query.collect {k, v -> "$k=$v"}.join("&") - ) + query.collect {k, v -> req.addParameter(k, v)} } if (contentType) { req.contentType = contentType diff --git a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/DiscoverySpec.groovy b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/DiscoverySpec.groovy new file mode 100644 index 000000000..44b36662e --- /dev/null +++ b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/DiscoverySpec.groovy @@ -0,0 +1,47 @@ +package org.springframework.data.rest.webmvc.spec + +import org.springframework.data.domain.PageRequest +import org.springframework.data.rest.webmvc.PagingAndSorting +import org.springframework.data.rest.webmvc.RepositoryRestConfiguration +import org.springframework.http.HttpStatus + +/** + * @author Jon Brisbin + */ +class DiscoverySpec extends BaseSpec { + + def "exposes configured repositories for discovery"() { + + given: + def request = createRequest("GET", "", null) + + when: + def response = controller.listRepositories(request, baseUri) + + then: + response.statusCode == HttpStatus.OK + + when: + def links = readJson(response)._links + + then: + links.size() == 4 + + } + + def "lists entities for discovery"() { + + given: + (1..20).each { newPerson() } + def pageSort = new PagingAndSorting(RepositoryRestConfiguration.DEFAULT, new PageRequest(0, 10)) + def request = createRequest("GET", "people", null) + + when: + def response = controller.listEntities(request, pageSort, baseUri, "people") + + then: + response.statusCode == HttpStatus.OK + + } + +} diff --git a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/JsonpSpec.groovy b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/JsonpSpec.groovy new file mode 100644 index 000000000..15512d7a8 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/JsonpSpec.groovy @@ -0,0 +1,26 @@ +package org.springframework.data.rest.webmvc.spec + +import org.springframework.http.HttpStatus + +/** + * @author Jon Brisbin + */ +class JsonpSpec extends BaseSpec { + + def "wraps response with JSONP"() { + + given: + def person = newPerson() + def request = createRequest("GET", "people/${person.id}", ["callback": "jsonp_callback"]) + + when: + def response = controller.entity(request, baseUri, "people", "${person.id}") + def body = new String(response.body) + + then: + response.statusCode == HttpStatus.OK + body?.startsWith("jsonp_callback") + + } + +} diff --git a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/TopLevelEntitySpec.groovy b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/TopLevelEntitySpec.groovy index 35088ac2d..02139e276 100644 --- a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/TopLevelEntitySpec.groovy +++ b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/TopLevelEntitySpec.groovy @@ -36,4 +36,34 @@ class TopLevelEntitySpec extends BaseSpec { } + def "updates top-level entity"() { + + given: + def person = newPerson() + person.name = "Johnnie Doe" + person = people.save(person) + def persId = person.id + def request = createJsonRequest("PUT", "people/$persId", null, person) + def retrReq = createRequest("GET", "people/$persId", null) + + when: + def response = controller.createOrUpdate(request, baseUri, "people", "$persId") + + then: + response.statusCode == HttpStatus.NO_CONTENT + + when: + response = controller.entity(retrReq, baseUri, "people", "$persId") + + then: + response.statusCode == HttpStatus.OK + + when: + def pers = readJson(response) + + then: + pers.name == "Johnnie Doe" + + } + } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/ApplicationConfig.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/ApplicationConfig.java index a2909fa3d..6277f2ec9 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/ApplicationConfig.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/ApplicationConfig.java @@ -7,6 +7,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.data.rest.webmvc.RepositoryRestConfiguration; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.orm.jpa.JpaDialect; @@ -57,6 +58,11 @@ public class ApplicationConfig { return txManager; } + @Bean public RepositoryRestConfiguration repositoryRestConfiguration() { + return new RepositoryRestConfiguration() + .setJsonpOnErrParamName("errback"); + } + @Bean public TestRepositoryEventListener testRepositoryEventListener() { return new TestRepositoryEventListener(); }