Fixing and adding tests.

This commit is contained in:
Jon Brisbin
2012-07-27 09:40:50 -05:00
parent 7f8abc5aa8
commit 6be6de34d3
5 changed files with 113 additions and 6 deletions

View File

@@ -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

View File

@@ -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
}
}

View File

@@ -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")
}
}

View File

@@ -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"
}
}

View File

@@ -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();
}