Re-vamp tests to accommodate new controller.

This commit is contained in:
Jon Brisbin
2012-04-09 10:38:31 -05:00
parent ecb81f21fa
commit 80df6c01a8
5 changed files with 80 additions and 132 deletions

View File

@@ -1,4 +1,4 @@
archivesBaseName = "rest-${name}"
archivesBaseName = "spring-data-rest-${name}"
dependencies {

View File

@@ -1,4 +1,4 @@
archivesBaseName = "rest-${name}"
archivesBaseName = "spring-data-rest-${name}"
dependencies {

View File

@@ -1,7 +1,7 @@
//apply plugin: "war"
//apply plugin: "jetty"
archivesBaseName = "rest-${name}"
archivesBaseName = "spring-data-rest-${name}"
dependencies {

View File

@@ -572,26 +572,31 @@ public class RepositoryRestController implements InitializingBean {
PluralAttribute plAttr = (PluralAttribute) attr;
switch (plAttr.getCollectionType()) {
case COLLECTION:
case LIST:
case LIST: {
Collection c = new ArrayList();
if (request.getMethod() == HttpMethod.POST) {
c.addAll((Collection) typeMeta.entityMetadata.get(property, entity));
Collection current = (Collection) typeMeta.entityMetadata.get(property, entity);
if (request.getMethod() == HttpMethod.POST && null != current) {
c.addAll(current);
}
c.add(childEntity);
typeMeta.entityMetadata.set(property, c, entity);
break;
case SET:
}
break;
case SET: {
Set s = new HashSet();
if (request.getMethod() == HttpMethod.POST) {
s.addAll((Set) typeMeta.entityMetadata.get(property, entity));
Set current = (Set) typeMeta.entityMetadata.get(property, entity);
if (request.getMethod() == HttpMethod.POST && null != current) {
s.addAll(current);
}
s.add(childEntity);
typeMeta.entityMetadata.set(property, s, entity);
break;
case MAP:
}
break;
case MAP: {
Map m = new HashMap();
if (request.getMethod() == HttpMethod.POST) {
m.putAll((Map) typeMeta.entityMetadata.get(property, entity));
Map current = (Map) typeMeta.entityMetadata.get(property, entity);
if (request.getMethod() == HttpMethod.POST && null != current) {
m.putAll(current);
}
String key = rel.get();
if (null == key) {
@@ -601,7 +606,8 @@ public class RepositoryRestController implements InitializingBean {
m.put(rel.get(), childEntity);
typeMeta.entityMetadata.set(property, m, entity);
}
break;
}
break;
}
} else if (attr instanceof SingularAttribute) {
typeMeta.entityMetadata.set(property, childEntity, entity);
@@ -618,7 +624,9 @@ public class RepositoryRestController implements InitializingBean {
while (null != (line = in.readLine())) {
String sLinkUri = line.trim();
Object o = resolveTopLevelResource(baseUri, sLinkUri);
entityHandler.handle(o);
if (null != o) {
entityHandler.handle(o);
}
}
} else if (jsonMediaType.equals(incomingMediaType)) {
final Map<String, List<Map<String, String>>> incoming = readIncoming(request, incomingMediaType, Map.class);
@@ -626,7 +634,9 @@ public class RepositoryRestController implements InitializingBean {
String sLinkUri = link.get("href");
Object o = resolveTopLevelResource(baseUri, sLinkUri);
rel.set(link.get("rel"));
entityHandler.handle(o);
if (null != o) {
entityHandler.handle(o);
}
}
}

View File

@@ -5,6 +5,7 @@ import org.codehaus.jackson.map.ser.CustomSerializerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.rest.core.SimpleLink
import org.springframework.data.rest.core.util.FluentBeanSerializer
import org.springframework.data.rest.test.webmvc.Address
import org.springframework.data.rest.webmvc.RepositoryRestConfiguration
import org.springframework.data.rest.webmvc.RepositoryRestController
import org.springframework.data.rest.webmvc.RepositoryRestMvcConfiguration
@@ -14,7 +15,7 @@ import org.springframework.mock.web.MockHttpServletRequest
import org.springframework.test.context.ContextConfiguration
import org.springframework.transaction.annotation.Transactional
import org.springframework.ui.ExtendedModelMap
import org.springframework.ui.Model
import org.springframework.web.util.UriComponentsBuilder
import spock.lang.Shared
import spock.lang.Specification
@@ -24,6 +25,8 @@ import spock.lang.Specification
@ContextConfiguration(classes = [RepositoryRestConfiguration, RepositoryRestMvcConfiguration])
class RepositoryRestControllerSpec extends Specification {
@Shared
UriComponentsBuilder uriBuilder
@Shared
ObjectMapper mapper = new ObjectMapper()
@Autowired
@@ -39,147 +42,82 @@ class RepositoryRestControllerSpec extends Specification {
)
}
Model GET(String path) {
def request = createRequest("GET", path)
def model = new ExtendedModelMap()
controller.get(new ServletServerHttpRequest(request), model)
return model
}
Model POST(String path, m) {
def request = createRequest("POST", path)
request.contentType = "application/json"
request.setContent(mapper.writeValueAsBytes(m))
def model = new ExtendedModelMap()
controller.createOrUpdate(new ServletServerHttpRequest(request), model)
return model
}
Model PUT(String path, m) {
def request = createRequest("PUT", path)
request.contentType = "application/json"
request.setContent(mapper.writeValueAsBytes(m))
def model = new ExtendedModelMap()
controller.createOrUpdate(new ServletServerHttpRequest(request), model)
return model
}
Model DELETE(String path) {
def request = createRequest("DELETE", path)
def model = new ExtendedModelMap()
controller.delete(new ServletServerHttpRequest(request), model)
return model
}
def setupSpec() {
uriBuilder = UriComponentsBuilder.fromUriString("http://localhost:8080/data")
def customSerializerFactory = new CustomSerializerFactory()
customSerializerFactory.addSpecificMapping(SimpleLink, new FluentBeanSerializer(SimpleLink))
mapper.setSerializerFactory(customSerializerFactory)
}
@Transactional
def "responds to GET"() {
def "API Test"() {
when:
def repos = GET("")
def reposLinks = repos.resource?._links
given:
def model = new ExtendedModelMap()
when: "listing available repositories"
controller.listRepositories(uriBuilder, model)
def reposLinks = model.resource?.links
then:
repos.status == HttpStatus.OK
model.status == HttpStatus.OK
reposLinks?.size() == 3
when:
def persons = GET("person")
def personsLinks = persons.resource?._links
when: "adding an entity"
model.clear()
def req = createRequest("POST", "person")
def data = mapper.writeValueAsBytes([name: "John Doe"])
req.content = data
controller.create(new ServletServerHttpRequest(req), uriBuilder, "person", model)
then:
persons.status == HttpStatus.OK
model.status == HttpStatus.CREATED
when: "listing available entities"
model.clear()
controller.listEntities(uriBuilder, "person", model)
def personsLinks = model.resource?.links
then:
model.status == HttpStatus.OK
personsLinks[0].href().toString() == "http://localhost:8080/data/person/1"
when:
def person = GET("person/1")
when: "getting specific entity"
model.clear()
req = createRequest("GET", "person/1")
controller.entity(new ServletServerHttpRequest(req), uriBuilder, "person", "1", model)
then:
person?.resource?.name == "John Doe"
model.resource?.name == "John Doe"
when:
def profiles = GET("person/1/profiles")
def profilesLinks = profiles.resource?.profiles
when: "creating child entity"
model.clear()
req = createRequest("POST", "address")
data = mapper.writeValueAsBytes(new Address(["1 W. 1st St."] as String[], "Univille", "ST", "12345"))
req.content = data
controller.create(new ServletServerHttpRequest(req), uriBuilder, "address", model)
then:
profilesLinks.size() == 2
model.status == HttpStatus.CREATED
}
@Transactional
def "responds to POST with ID"() {
when:
def created = POST("person/3", [name: "James Doe"])
when: "linking child to parent entity"
model.clear()
req = createRequest("POST", "person/1/addresses")
req.contentType = "text/uri-list"
data = "http://localhost:8080/data/address/1".bytes
req.content = data
controller.updateLinks(new ServletServerHttpRequest(req), uriBuilder, "person", "1", "addresses", model)
then:
created.status == HttpStatus.CREATED
model.status == HttpStatus.CREATED
}
@Transactional
def "responds to PUT"() {
given:
POST("person/3", [name: "James Doe"])
when:
def updated = PUT("person/3", [name: "James Doe Jr."])
def getUpdated = GET("person/3")
when: "getting property of entity"
model.clear()
controller.propertyOfEntity(uriBuilder, "person", "1", "addresses", model)
def addrLinks = model.resource?.links
then:
updated.status == HttpStatus.NO_CONTENT
getUpdated.status == HttpStatus.OK
getUpdated.resource?.name == "James Doe Jr."
}
@Transactional
def "updates links"() {
given:
POST("person/3", [name: "James Doe"])
when:
def link = POST("person/3/addresses", [[href: "$baseUri/address/1".toString()]])
def getUpdated = GET("person/3/addresses")
then:
link.status == HttpStatus.CREATED
getUpdated.status == HttpStatus.OK
getUpdated.resource?.size() == 1
}
@Transactional
def "responds to DELETE"() {
given:
POST("person/3", [name: "James Doe"])
POST("person/3/addresses", [[href: "$baseUri/address/1".toString()]])
when:
def deleted = DELETE("person/3/addresses/1")
def getUpdated = GET("person/3/addresses")
then:
deleted.status == HttpStatus.NO_CONTENT
getUpdated.status == HttpStatus.OK
getUpdated.resource?._links?.size() == 0
when:
def delEntity = DELETE("person/3")
def getUpdEntity = GET("person/3")
then:
delEntity.status == HttpStatus.NO_CONTENT
getUpdEntity.status == HttpStatus.NOT_FOUND
addrLinks.size() == 1
}