package com.example;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.net.URI;

import org.junit.Before;
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.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.client.Traverson;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DemoApplication.class)
@WebIntegrationTest(randomPort=true)
public class DemoApplicationTests {

    @Value("${local.server.port}")
    private int port;

    @Autowired
    private WebApplicationContext context;

    private MockMvc mvc;

    @Before
    public void setup() {
        mvc = MockMvcBuilders
                .webAppContextSetup(context)
                .build();
    }

    @Test
    public void restTest() throws Exception {
        Traverson traverson = new Traverson(URI.create("http://localhost:" + port + "/"), MediaTypes.HAL_JSON);
        Link link = traverson.follow("mice").asLink();
        mvc.perform(post(link.expand().getHref()).content(
                new ObjectMapper().writeValueAsString(ImmutableMap.of("name", "My Mouse"))
        ))
        .andExpect(status().isCreated())
        .andDo(print())
        .andDo((a) -> {
            // follow the location header.
            mvc.perform(get(a.getResponse().getHeader("Location")))
            .andDo(print())
            .andExpect(status().isOk());
        });
    }

}

@@ -15,16 +15,73 @@
 ample;%0A%0A
+import static org.hamcrest.core.StringEndsWith.endsWith;%0A
 import s
@@ -242,18 +242,17 @@
 ilders.p
-os
+u
 t;%0Aimpor
@@ -333,16 +333,104 @@
 .print;%0A
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;%0A
 import s
@@ -514,30 +514,8 @@
 s;%0A%0A
-import java.net.URI;%0A%0A
 impo
@@ -843,149 +843,8 @@
 st;%0A
-import org.springframework.hateoas.Link;%0Aimport org.springframework.hateoas.MediaTypes;%0Aimport org.springframework.hateoas.client.Traverson;%0A
 impo
@@ -1730,215 +1730,68 @@
     
-Traverson traverson = new Traverson(URI.create(%22http://localhost:%22 + port + %22/%22), MediaTypes.HAL_JSON);%0A        Link link = traverson.follow(%22mice%22).asLink();%0A        mvc.perform(post(link.expand().getHref()
+mvc.perform(put(%22/mice/0f629be4-e6cc-11e5-bb3b-3bf5fa6b2828%22
 ).co
@@ -1936,24 +1936,128 @@
 sCreated())%0A
+        .andExpect(header().string(%22Location%22, endsWith(%22/mice/0f629be4-e6cc-11e5-bb3b-3bf5fa6b2828%22)))%0A
         .and

test PUT method