diff --git a/src/main/java/org/springframework/hateoas/UriTemplate.java b/src/main/java/org/springframework/hateoas/UriTemplate.java index cd14c33a..06171107 100644 --- a/src/main/java/org/springframework/hateoas/UriTemplate.java +++ b/src/main/java/org/springframework/hateoas/UriTemplate.java @@ -278,8 +278,8 @@ public class UriTemplate implements Iterable, Serializable { if (value == null) { if (variable.isRequired()) { - throw new IllegalArgumentException(String.format("Template variable %s is required but no value was given!", - variable.getName())); + throw new IllegalArgumentException( + String.format("Template variable %s is required but no value was given!", variable.getName())); } return; diff --git a/src/main/java/org/springframework/hateoas/client/Traverson.java b/src/main/java/org/springframework/hateoas/client/Traverson.java index 1c1e6de2..1c300587 100644 --- a/src/main/java/org/springframework/hateoas/client/Traverson.java +++ b/src/main/java/org/springframework/hateoas/client/Traverson.java @@ -15,7 +15,7 @@ */ package org.springframework.hateoas.client; -import static org.springframework.http.HttpMethod.*; +import static org.springframework.http.HttpMethod.GET; import java.net.URI; import java.nio.charset.Charset; @@ -61,6 +61,8 @@ import com.jayway.jsonpath.JsonPath; * @author Dietrich Schulten * @author Greg Turnquist * @author Tom Bunting + * @author Manish Misra + * @author Michael Wirth * @since 0.11 */ public class Traverson { @@ -397,6 +399,7 @@ public class Traverson { private Link traverseToLink(boolean expandFinalUrl) { Assert.isTrue(rels.size() > 0, "At least one rel needs to be provided!"); + return new Link(expandFinalUrl ? traverseToExpandedFinalUrl().toString() : traverseToFinalUrl(), rels.get(rels.size() - 1).getRel()); } diff --git a/src/test/java/org/springframework/hateoas/client/Server.java b/src/test/java/org/springframework/hateoas/client/Server.java index 7760ef2f..3da3f8ac 100644 --- a/src/test/java/org/springframework/hateoas/client/Server.java +++ b/src/test/java/org/springframework/hateoas/client/Server.java @@ -15,15 +15,8 @@ */ package org.springframework.hateoas.client; -import static net.jadler.Jadler.*; -import static org.hamcrest.Matchers.*; - -import java.io.Closeable; -import java.io.IOException; -import java.nio.charset.Charset; -import java.util.Collections; -import java.util.UUID; - +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; import org.springframework.hateoas.Link; @@ -38,14 +31,24 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.StreamUtils; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.Closeable; +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.Collections; +import java.util.UUID; + +import static net.jadler.Jadler.closeJadler; +import static net.jadler.Jadler.initJadler; +import static net.jadler.Jadler.onRequest; +import static net.jadler.Jadler.port; +import static org.hamcrest.Matchers.equalTo; /** * Helper class for integration tests. * * @author Oliver Gierke * @author Greg Turnquist + * @author Michael Wirth */ public class Server implements Closeable { @@ -99,14 +102,18 @@ public class Server implements Closeable { onRequest(). // havingPathEqualTo("/github-with-template"). // respond(). // - withBody("{ \"foo_url_templated\" : \"" + rootResource() + "/github/{issue}\"}"). // + withBody("{ \"_links\" : { \"rel_to_templated_link\" : { \"href\" : \"/github/{issue}\" }}}"). // withContentType(MediaTypes.HAL_JSON.toString()); // Sample traversal of HAL docs based on Spring-a-Gram showcase - org.springframework.core.io.Resource springagramRoot = resourceLoader.getResource("classpath:springagram-root.json"); - org.springframework.core.io.Resource springagramItems = resourceLoader.getResource("classpath:springagram-items.json"); - org.springframework.core.io.Resource springagramItem = resourceLoader.getResource("classpath:springagram-item.json"); - org.springframework.core.io.Resource springagramItemWithoutImage = resourceLoader.getResource("classpath:springagram-item-without-image.json"); + org.springframework.core.io.Resource springagramRoot = resourceLoader + .getResource("classpath:springagram-root.json"); + org.springframework.core.io.Resource springagramItems = resourceLoader + .getResource("classpath:springagram-items.json"); + org.springframework.core.io.Resource springagramItem = resourceLoader + .getResource("classpath:springagram-item.json"); + org.springframework.core.io.Resource springagramItemWithoutImage = resourceLoader + .getResource("classpath:springagram-item-without-image.json"); String springagramRootTemplate; String springagramItemsTemplate; @@ -124,7 +131,8 @@ public class Server implements Closeable { } String springagramRootHalDocument = String.format(springagramRootTemplate, rootResource(), rootResource()); - String springagramItemsHalDocument = String.format(springagramItemsTemplate, rootResource(), rootResource(), rootResource()); + String springagramItemsHalDocument = String.format(springagramItemsTemplate, rootResource(), rootResource(), + rootResource()); String springagramItemHalDocument = String.format(springagramItemTemplate, rootResource(), rootResource()); String springagramItemWithoutImageHalDocument = String.format(springagramItemWithoutImageTemplate, rootResource()); diff --git a/src/test/java/org/springframework/hateoas/client/TraversonTest.java b/src/test/java/org/springframework/hateoas/client/TraversonTest.java index 6865dbc1..d43f0edd 100644 --- a/src/test/java/org/springframework/hateoas/client/TraversonTest.java +++ b/src/test/java/org/springframework/hateoas/client/TraversonTest.java @@ -15,10 +15,6 @@ */ package org.springframework.hateoas.client; -import static net.jadler.Jadler.*; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import static org.springframework.hateoas.client.Hop.*; import java.io.IOException; import java.net.URI; @@ -48,11 +44,23 @@ import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; +import static net.jadler.Jadler.verifyThatRequest; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.springframework.hateoas.client.Hop.rel; + /** * Integration tests for {@link Traverson}. * * @author Oliver Gierke * @author Greg Turnquist + * @author Michael Wirth * @since 0.11 */ public class TraversonTest { @@ -239,23 +247,22 @@ public class TraversonTest { assertThat(link.isTemplated(), is(false)); } - @Test + @Test // #971 public void returnsTemplatedRequiredLinkIfRequested() { - TraversalBuilder follow = new Traverson(URI.create(server.rootResource().concat("/github-with-template")), MediaTypes.HAL_JSON) - .follow("foo_url_templated"); + Link templatedLink = new Traverson(URI.create(server.rootResource() + "/github-with-template"), MediaTypes.HAL_JSON) // + .follow("rel_to_templated_link") // + .asTemplatedLink(); - Link link = follow.asTemplatedLink(); + assertThat(templatedLink.isTemplated(), is(true)); + assertThat(templatedLink.getVariableNames(), contains("issue")); - assertThat(link.isTemplated()).isTrue(); - assertThat(link.getVariableNames()).contains("template"); + Link expandedLink = templatedLink.expand("42"); - link = follow.asLink(); - - assertThat(link.isTemplated()).isFalse(); + assertThat(expandedLink.isTemplated(), is(false)); + assertThat(expandedLink.getHref(), equalTo("/github/42")); } - /** * @see #258 */