Java tutorial
package net.eusashead.hateoas.response.argumentresolver; /* * #[license] * spring-responseentitybuilder * %% * Copyright (C) 2013 Eusa's Head * %% * 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. * %[license] */ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; 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.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; import java.net.URI; import java.nio.charset.Charset; import java.util.Arrays; import java.util.Set; import java.util.TreeSet; import net.eusashead.hateoas.test.model.Entity; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.web.context.WebApplicationContext; import com.fasterxml.jackson.databind.ObjectMapper; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = { WebConfig.class }) public class AsyncEntityControllerITCase { @Autowired private WebApplicationContext context; private MockMvc mockMvc; private ObjectMapper mapper = new ObjectMapper(); @Before public void before() { this.mockMvc = webAppContextSetup(this.context).dispatchOptions(true).build(); } @Test public void testGet() throws Exception { // Expected result HttpHeaders headers = new HttpHeaders(); headers.setETag("W/\"b0c689597eb9dd62c0a1fb90a7c5c5a5\""); ResponseEntity<Entity> expectedResult = new ResponseEntity<Entity>(new Entity("foo"), headers, HttpStatus.OK); // Execute asynchronously MvcResult mvcResult = this.mockMvc .perform(get("http://localhost/async/foo").contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(request().asyncStarted()).andExpect(request().asyncResult(expectedResult)).andReturn(); // Perform asynchronous dispatch this.mockMvc.perform(asyncDispatch(mvcResult)).andDo(print()).andExpect(status().isOk()) .andExpect(header().string("ETag", "W/\"b0c689597eb9dd62c0a1fb90a7c5c5a5\"")) .andExpect(content().contentType(new MediaType("application", "json", Charset.forName("UTF-8")))); } @Test public void testHead() throws Exception { // Expected result HttpHeaders headers = new HttpHeaders(); headers.setETag("W/\"b0c689597eb9dd62c0a1fb90a7c5c5a5\""); ResponseEntity<Entity> expectedResult = new ResponseEntity<Entity>(headers, HttpStatus.NO_CONTENT); // Execute asynchronously MvcResult mvcResult = this.mockMvc .perform(request(HttpMethod.HEAD, "http://localhost/async/foo") .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) .andExpect(request().asyncStarted()).andExpect(request().asyncResult(expectedResult)).andReturn(); // Perform asynchronous dispatch this.mockMvc.perform(asyncDispatch(mvcResult)).andDo(print()).andExpect(status().isNoContent()) .andExpect(header().string("ETag", "W/\"b0c689597eb9dd62c0a1fb90a7c5c5a5\"")); } @Test public void testOptions() throws Exception { // Expected result HttpHeaders headers = new HttpHeaders(); Set<HttpMethod> allowedMethods = new TreeSet<HttpMethod>(); allowedMethods.addAll(Arrays.asList(HttpMethod.GET, HttpMethod.HEAD)); headers.setAllow(allowedMethods); ResponseEntity<Entity> expectedResult = new ResponseEntity<Entity>(new Entity("foo"), headers, HttpStatus.OK); // Execute asynchronously MvcResult mvcResult = this.mockMvc .perform(request(HttpMethod.OPTIONS, "http://localhost/async/foo") .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) .andExpect(request().asyncStarted()).andExpect(request().asyncResult(expectedResult)).andReturn(); // Perform asynchronous dispatch this.mockMvc.perform(asyncDispatch(mvcResult)).andDo(print()).andExpect(status().isOk()) .andExpect(content().contentType(new MediaType("application", "json", Charset.forName("UTF-8")))); } @Test public void testPost() throws Exception { // Expected result HttpHeaders headers = new HttpHeaders(); headers.setLocation(new URI("/path/to/foo")); ResponseEntity<Entity> expectedResult = new ResponseEntity<Entity>(headers, HttpStatus.CREATED); // Execute asynchronously MvcResult mvcResult = this.mockMvc .perform(post("http://localhost/async/foo").content(mapper.writeValueAsBytes(new Entity("foo"))) .contentType(MediaType.APPLICATION_JSON) .characterEncoding(Charset.forName("UTF-8").toString()).accept(MediaType.APPLICATION_JSON)) .andExpect(request().asyncStarted()).andExpect(request().asyncResult(expectedResult)).andReturn(); // Perform asynchronous dispatch this.mockMvc.perform(asyncDispatch(mvcResult)).andDo(print()).andExpect(status().isCreated()) .andExpect(header().string("Location", "/path/to/foo")).andExpect(status().isCreated()); } @Test public void testPut() throws Exception { // Expected result HttpHeaders headers = new HttpHeaders(); headers.setETag("W/\"b0c689597eb9dd62c0a1fb90a7c5c5a5\""); ResponseEntity<Entity> expectedResult = new ResponseEntity<Entity>(headers, HttpStatus.NO_CONTENT); // Execute asynchronously MvcResult mvcResult = this.mockMvc .perform(put("http://localhost/async/foo").content(mapper.writeValueAsBytes(new Entity("foo"))) .contentType(MediaType.APPLICATION_JSON) .characterEncoding(Charset.forName("UTF-8").toString()).accept(MediaType.APPLICATION_JSON)) .andExpect(request().asyncStarted()).andExpect(request().asyncResult(expectedResult)).andReturn(); // Perform asynchronous dispatch this.mockMvc.perform(asyncDispatch(mvcResult)).andDo(print()).andExpect(status().isNoContent()); } @Test public void testDelete() throws Exception { // Expected result ResponseEntity<Entity> expectedResult = new ResponseEntity<Entity>(HttpStatus.NO_CONTENT); // Execute asynchronously MvcResult mvcResult = this.mockMvc .perform(delete("http://localhost/async/foo").contentType(MediaType.APPLICATION_JSON) .characterEncoding(Charset.forName("UTF-8").toString()).accept(MediaType.APPLICATION_JSON)) .andExpect(request().asyncStarted()).andExpect(request().asyncResult(expectedResult)).andReturn(); // Perform asynchronous dispatch this.mockMvc.perform(asyncDispatch(mvcResult)).andDo(print()).andExpect(status().isNoContent()); } }