From 43c0bcda98514ca6de82eef10ca5765aa92a8b24 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sun, 17 May 2015 17:30:03 +0200 Subject: [PATCH] #85 - Added a test case to show how to use a RestTempalte to interact with a HAL service. --- rest/starbucks/pom.xml | 2 +- .../java/example/stores/StarbucksClient.java | 61 +++++++++++++++++-- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/rest/starbucks/pom.xml b/rest/starbucks/pom.xml index 7d315df5..9ca07b0e 100644 --- a/rest/starbucks/pom.xml +++ b/rest/starbucks/pom.xml @@ -13,7 +13,7 @@ - 0.17.0.RELEASE + 0.18.0.BUILD-SNAPSHOT diff --git a/rest/starbucks/src/test/java/example/stores/StarbucksClient.java b/rest/starbucks/src/test/java/example/stores/StarbucksClient.java index 6f2f551e..e8c885de 100644 --- a/rest/starbucks/src/test/java/example/stores/StarbucksClient.java +++ b/rest/starbucks/src/test/java/example/stores/StarbucksClient.java @@ -15,26 +15,42 @@ */ package example.stores; +import static org.springframework.hateoas.MediaTypes.*; +import static org.springframework.http.HttpMethod.*; + import java.net.URI; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.stream.StreamSupport; import lombok.extern.slf4j.Slf4j; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.context.annotation.Configuration; +import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.context.annotation.Bean; +import org.springframework.hateoas.Link; +import org.springframework.hateoas.Links; import org.springframework.hateoas.MediaTypes; import org.springframework.hateoas.PagedResources; import org.springframework.hateoas.PagedResources.PageMetadata; import org.springframework.hateoas.Resource; +import org.springframework.hateoas.Resources; import org.springframework.hateoas.client.Traverson; import org.springframework.hateoas.client.Traverson.TraversalBuilder; import org.springframework.hateoas.mvc.TypeReferences.PagedResourcesType; +import org.springframework.hateoas.mvc.TypeReferences.ResourceType; +import org.springframework.hateoas.mvc.TypeReferences.ResourcesType; +import org.springframework.http.HttpHeaders; +import org.springframework.http.RequestEntity; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.client.RestOperations; +import org.springframework.web.client.RestTemplate; /** * A test case to discover the search resource and execute a predefined search with it. @@ -42,18 +58,28 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * @author Oliver Gierke */ @RunWith(SpringJUnit4ClassRunner.class) +@WebIntegrationTest(randomPort = true) @SpringApplicationConfiguration @Slf4j public class StarbucksClient { - @Configuration - static class Config {} + @SpringBootApplication + static class Config { + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + } + + @Value("${local.server.port}") String port; + + private static final String SERVICE_URI = "http://localhost:%s/api"; @Test - @Ignore public void discoverStoreSearch() { - Traverson traverson = new Traverson(URI.create("http://localhost:8080/api"), MediaTypes.HAL_JSON); + Traverson traverson = new Traverson(URI.create(String.format(SERVICE_URI, port)), MediaTypes.HAL_JSON); // Set up path traversal TraversalBuilder builder = traverson. // @@ -81,6 +107,29 @@ public class StarbucksClient { forEach(store -> log.info("{} - {}", store.name, store.address)); } + @Autowired RestOperations restOperations; + + @Test + public void accessServiceUsingRestTemplate() { + + HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Arrays.asList(HAL_JSON)); + + // Access root resource + + RequestEntity request = new RequestEntity<>(headers, GET, URI.create(String.format(SERVICE_URI, port))); + Resource rootLinks = restOperations.exchange(request, new ResourceType() {}).getBody(); + Links links = new Links(rootLinks.getLinks()); + + // Follow stores link + + Link storesLink = links.getLink("stores").expand(); + request = new RequestEntity<>(headers, GET, URI.create(storesLink.getHref())); + Resources stores = restOperations.exchange(request, new ResourcesType() {}).getBody(); + + stores.getContent().forEach(store -> log.info("{} - {}", store.name, store.address)); + } + static class Store { public String name;