#14 - Added test cases showing that de-/serialization is working.

This commit is contained in:
Oliver Gierke
2012-09-17 17:42:10 +02:00
parent 9e728b4660
commit e89242c9f0
4 changed files with 98 additions and 5 deletions

View File

@@ -41,4 +41,8 @@ public abstract class AbstractMarshallingIntegrationTests {
mapper.writeValue(writer, object);
return writer.toString();
}
protected <T> T read(String source, Class<T> targetType) throws Exception {
return mapper.readValue(source, targetType);
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Integration tests for {@link Link} marshaling.
*
* @author Oliver Gierke
*/
public class LinkIntegrationTest extends AbstractMarshallingIntegrationTests {
private static final String REFERENCE = "{\"rel\":\"something\",\"href\":\"location\"}";
/**
* @see #14
*/
@Test
public void writesLinkCorrectly() throws Exception {
assertThat(write(new Link("location", "something")), is(REFERENCE));
}
/**
* @see #14
*/
@Test
public void readsLinkCorrectly() throws Exception {
Link result = read(REFERENCE, Link.class);
assertThat(result.getHref(), is("location"));
assertThat(result.getRel(), is("something"));
}
}

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.hateoas;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.codehaus.jackson.annotate.JsonAutoDetect;
@@ -29,6 +29,8 @@ import org.junit.Test;
*/
public class ResourceIntegrationTest extends AbstractMarshallingIntegrationTests {
static final String REFERENCE = "{\"links\":[{\"rel\":\"self\",\"href\":\"localhost\"}],\"firstname\":\"Dave\",\"lastname\":\"Matthews\"}";
@Test
public void inlinesContent() throws Exception {
@@ -36,8 +38,31 @@ public class ResourceIntegrationTest extends AbstractMarshallingIntegrationTests
person.firstname = "Dave";
person.lastname = "Matthews";
assertThat(write(new Resource<Person>(person)),
is("{\"links\":[],\"firstname\":\"Dave\",\"lastname\":\"Matthews\"}"));
Resource<Person> resource = new Resource<Person>(person);
resource.add(new Link("localhost"));
assertThat(write(resource), is(REFERENCE));
}
/**
* @see #14
*/
@Test
public void readsResourceSupportCorrectly() throws Exception {
PersonResource result = read(REFERENCE, PersonResource.class);
assertThat(result.getLinks(), hasSize(1));
assertThat(result.getLinks(), hasItem(new Link("localhost")));
assertThat(result.getContent().firstname, is("Dave"));
assertThat(result.getContent().lastname, is("Matthews"));
}
static class PersonResource extends Resource<Person> {
public PersonResource() {
}
}
@JsonAutoDetect(fieldVisibility = Visibility.ANY)

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.hateoas;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
@@ -27,6 +27,8 @@ import org.junit.Test;
*/
public class ResourceSupportIntegrationTest extends AbstractMarshallingIntegrationTests {
static final String REFERENCE = "{\"links\":[{\"rel\":\"self\",\"href\":\"localhost\"}]}";
/**
* @see #7
*/
@@ -36,6 +38,18 @@ public class ResourceSupportIntegrationTest extends AbstractMarshallingIntegrati
ResourceSupport resourceSupport = new ResourceSupport();
resourceSupport.add(new Link("localhost"));
assertThat(write(resourceSupport), is("{\"links\":[{\"rel\":\"self\",\"href\":\"localhost\"}]}"));
assertThat(write(resourceSupport), is(REFERENCE));
}
/**
* @see #14
*/
@Test
public void readResourceSupportCorrectly() throws Exception {
ResourceSupport result = read(REFERENCE, ResourceSupport.class);
assertThat(result.getLinks(), hasSize(1));
assertThat(result.getLinks(), hasItem(new Link("localhost")));
}
}