diff --git a/src/test/java/org/springframework/hateoas/AbstractMarshallingIntegrationTests.java b/src/test/java/org/springframework/hateoas/AbstractMarshallingIntegrationTests.java index aa245ef6..4226bdd1 100644 --- a/src/test/java/org/springframework/hateoas/AbstractMarshallingIntegrationTests.java +++ b/src/test/java/org/springframework/hateoas/AbstractMarshallingIntegrationTests.java @@ -41,4 +41,8 @@ public abstract class AbstractMarshallingIntegrationTests { mapper.writeValue(writer, object); return writer.toString(); } + + protected T read(String source, Class targetType) throws Exception { + return mapper.readValue(source, targetType); + } } diff --git a/src/test/java/org/springframework/hateoas/LinkIntegrationTest.java b/src/test/java/org/springframework/hateoas/LinkIntegrationTest.java new file mode 100644 index 00000000..6e339906 --- /dev/null +++ b/src/test/java/org/springframework/hateoas/LinkIntegrationTest.java @@ -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")); + } +} diff --git a/src/test/java/org/springframework/hateoas/ResourceIntegrationTest.java b/src/test/java/org/springframework/hateoas/ResourceIntegrationTest.java index 045296c9..600f959d 100644 --- a/src/test/java/org/springframework/hateoas/ResourceIntegrationTest.java +++ b/src/test/java/org/springframework/hateoas/ResourceIntegrationTest.java @@ -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)), - is("{\"links\":[],\"firstname\":\"Dave\",\"lastname\":\"Matthews\"}")); + Resource resource = new Resource(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 { + + public PersonResource() { + + } } @JsonAutoDetect(fieldVisibility = Visibility.ANY) diff --git a/src/test/java/org/springframework/hateoas/ResourceSupportIntegrationTest.java b/src/test/java/org/springframework/hateoas/ResourceSupportIntegrationTest.java index 60a502ed..306fd01e 100644 --- a/src/test/java/org/springframework/hateoas/ResourceSupportIntegrationTest.java +++ b/src/test/java/org/springframework/hateoas/ResourceSupportIntegrationTest.java @@ -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"))); } }