Example usage for org.springframework.hateoas Link expand

List of usage examples for org.springframework.hateoas Link expand

Introduction

In this page you can find the example usage for org.springframework.hateoas Link expand.

Prototype

public Link expand(Map<String, ? extends Object> arguments) 

Source Link

Document

Turns the current template into a Link by expanding it using the given parameters.

Usage

From source file:example.customers.integration.CustomerResourceProcessor.java

@Override
public Resource<Customer> process(Resource<Customer> resource) {

    Customer customer = resource.getContent();
    Location location = customer.getAddress().getLocation();

    if (location == null || !storeIntegration.isStoresAvailable()) {
        return resource;
    }/*w ww  .  j av  a  2 s.com*/

    Link link = storeIntegration.getStoresByLocationLink();

    Map<String, Object> parameters = new HashMap<>();
    parameters.put("location", String.format("%s,%s", location.getLatitude(), location.getLongitude()));
    parameters.put("distance", "50km");

    resource.add(link.expand(parameters).withRel("stores-nearby"));

    return resource;
}

From source file:org.springframework.data.rest.tests.mongodb.MongoWebTests.java

/**
 * @see DATAREST-247/*  www. j  ava  2  s  .c  o m*/
 */
@Test
public void executeQueryMethodWithPrimitiveReturnType() throws Exception {

    Link profiles = client.discoverUnique("profiles");
    Link profileSearches = client.discoverUnique(profiles, "search");
    Link countByTypeLink = client.discoverUnique(profileSearches, "countByType");

    assertThat(countByTypeLink.isTemplated(), is(true));
    assertThat(countByTypeLink.getVariableNames(), hasItem("type"));

    MockHttpServletResponse response = client.request(countByTypeLink.expand("Twitter"));
    assertThat(response.getContentAsString(), is("1"));
}

From source file:org.springframework.data.rest.tests.mongodb.MongoWebTests.java

/**
 * @see DATAREST-511//  w  w  w .  j  a  va2 s . c o  m
 */
@Test
public void invokesQueryResourceReturningAnOptional() throws Exception {

    Profile profile = repository.findAll().iterator().next();

    Link link = client.discoverUnique("profiles", "search", "findById");

    mvc.perform(get(link.expand(profile.getId()).getHref())).//
            andExpect(status().isOk());
}

From source file:org.springframework.data.rest.tests.mongodb.MongoWebTests.java

/**
 * @see DATAREST-517//from w w  w  .  ja  v a2 s. co  m
 */
@Test
public void returnsNotFoundIfQueryExecutionDoesNotReturnResult() throws Exception {

    Link link = client.discoverUnique("profiles", "search", "findById");

    mvc.perform(get(link.expand("").getHref())).//
            andExpect(status().isNotFound());
}

From source file:org.springframework.data.rest.tests.mongodb.MongoWebTests.java

/**
 * @see DATAREST-712/*w w w  .  j  a va  2  s .  c o  m*/
 */
@Test
public void invokesQueryMethodTakingAReferenceCorrectly() throws Exception {

    Link link = client.discoverUnique("users", "search", "findByColleaguesContains");

    User thomas = userRepository.findAll(QUser.user.firstname.eq("Thomas")).iterator().next();
    Link thomasUri = entityLinks.linkToSingleResource(User.class, thomas.id).expand();

    String href = link.expand(thomasUri.getHref()).getHref();

    mvc.perform(get(href)).andExpect(status().isOk());
}

From source file:org.springframework.data.rest.webmvc.jpa.JpaWebTests.java

/**
 * @see DATAREST-311/*from   w  w w . j a  v  a2 s .com*/
 */
@Test
public void onlyLinksShouldAppearWhenExecuteSearchCompact() throws Exception {

    Link peopleLink = client.discoverUnique("people");
    Person daenerys = new Person("Daenerys", "Targaryen");
    String daenerysString = mapper.writeValueAsString(daenerys);

    MockHttpServletResponse createdPerson = postAndGet(peopleLink, daenerysString, MediaType.APPLICATION_JSON);
    Link daenerysLink = client.assertHasLinkWithRel("self", createdPerson);
    assertJsonPathEquals("$.firstName", "Daenerys", createdPerson);

    Link searchLink = client.discoverUnique(peopleLink, "search");
    Link byFirstNameLink = client.discoverUnique(searchLink, "findFirstPersonByFirstName");

    MockHttpServletResponse response = client.request(byFirstNameLink.expand("Daenerys"),
            MediaType.parseMediaType("application/x-spring-data-compact+json"));

    String responseBody = response.getContentAsString();

    JSONArray personLinks = JsonPath.<JSONArray>read(responseBody, "$.links[?(@.rel=='person')].href");

    assertThat(personLinks, hasSize(1));
    assertThat(personLinks.get(0), is((Object) daenerysLink.getHref()));
    assertThat(JsonPath.<JSONArray>read(responseBody, "$.content"), hasSize(0));
}

From source file:org.springframework.data.rest.webmvc.jpa.JpaWebTests.java

/**
 * @see DATAREST-384//from  w w w  . j av a  2s  . co m
 */
@Test
public void execturesSearchThatTakesASort() throws Exception {

    Link booksLink = client.discoverUnique("books");
    Link searchLink = client.discoverUnique(booksLink, "search");
    Link findBySortedLink = client.discoverUnique(searchLink, "find-by-sorted");

    // Assert sort options advertised
    assertThat(findBySortedLink.isTemplated(), is(true));
    assertThat(findBySortedLink.getVariableNames(), hasItems("sort", "projection"));

    // Assert results returned as specified
    client.follow(findBySortedLink.expand("title,desc")).//
            andExpect(jsonPath("$._embedded.books[0].title").value("Spring Data (Second Edition)")).//
            andExpect(jsonPath("$._embedded.books[1].title").value("Spring Data")).//
            andExpect(client.hasLinkWithRel("self"));

    client.follow(findBySortedLink.expand("title,asc")).//
            andExpect(jsonPath("$._embedded.books[0].title").value("Spring Data")).//
            andExpect(jsonPath("$._embedded.books[1].title").value("Spring Data (Second Edition)")).//
            andExpect(client.hasLinkWithRel("self"));
}

From source file:org.springframework.hateoas.LinkUnitTest.java

/**
 * @see #137//  w ww.  ja  va2 s.c om
 */
@Test
public void isTemplatedIfSourceContainsTemplateVariables() {

    Link link = new Link("/foo{?page}");

    assertThat(link.isTemplated(), is(true));
    assertThat(link.getVariableNames(), hasSize(1));
    assertThat(link.getVariableNames(), hasItem("page"));
    assertThat(link.expand("2"), is(new Link("/foo?page=2")));
}