Java tutorial
package net.eusashead.hateoas.response.impl; /* * #[license] * spring-halbuilder * %% * 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.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request; 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.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; import java.io.StringReader; import java.nio.charset.Charset; import java.util.List; import net.eusashead.hateoas.hal.http.converter.HalHttpMessageConverter; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; 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.theoryinpractise.halbuilder.api.Link; import com.theoryinpractise.halbuilder.api.ReadableRepresentation; import com.theoryinpractise.halbuilder.standard.StandardRepresentationFactory; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = { PageWebConfig.class }) public class HalPageResponseBuilderITCase { @Autowired private WebApplicationContext context; private MockMvc mockMvc; @Before public void before() { this.mockMvc = webAppContextSetup(context).dispatchOptions(true).build(); } @Test public void testGet() throws Exception { // Execute controller method MvcResult result = this.mockMvc .perform(get("http://localhost/customer/?page=1&size=15") .contentType(HalHttpMessageConverter.HAL_JSON).accept(HalHttpMessageConverter.HAL_JSON)) .andExpect(status().isOk()).andExpect(header().string("ETag", "W/\"123456789\"")) .andExpect( content().contentType(new MediaType("application", "hal+json", Charset.forName("UTF-8")))) .andReturn(); // Check result ReadableRepresentation returned = new StandardRepresentationFactory() .readRepresentation(new StringReader(result.getResponse().getContentAsString())); Assert.assertNotNull(returned); // Self link Link self = returned.getLinkByRel("self"); Assert.assertNotNull(self); Assert.assertEquals("/customer/?page=1&size=15", self.getHref()); // Self link Link next = returned.getLinkByRel("next"); Assert.assertNotNull(next); Assert.assertEquals("/customer/?page=2&size=15", next.getHref()); // Previous link Link previous = returned.getLinkByRel("previous"); Assert.assertNotNull(previous); Assert.assertEquals("/customer/?page=0&size=15", previous.getHref()); // Pagination parameters Object size = returned.getValue("size"); Assert.assertEquals(Integer.valueOf(15).toString(), size); Object page = returned.getValue("page"); Assert.assertEquals(Integer.valueOf(1).toString(), page); Object numberOfElements = returned.getValue("numberOfElements"); Assert.assertEquals(Integer.valueOf(15).toString(), numberOfElements); Object totalElements = returned.getValue("totalElements"); Assert.assertEquals(Integer.valueOf(95).toString(), totalElements); // Check the body List<? extends ReadableRepresentation> content = returned.getResourcesByRel("content"); Assert.assertNotNull(content); Assert.assertEquals(Integer.valueOf(15), Integer.valueOf(content.size())); } @Test public void testHead() throws Exception { // Execute controller method this.mockMvc .perform(request(HttpMethod.HEAD, "http://localhost/customer/?page=1&size=15") .contentType(HalHttpMessageConverter.HAL_JSON).accept(HalHttpMessageConverter.HAL_JSON)) .andExpect(status().isNoContent()).andExpect(header().string("ETag", "W/\"123456789\"")) .andReturn(); } @Test public void tesGetNotModified() throws Exception { // Execute controller method this.mockMvc .perform(request(HttpMethod.GET, "http://localhost/customer/?page=1&size=15") .contentType(HalHttpMessageConverter.HAL_JSON).accept(HalHttpMessageConverter.HAL_JSON) .header("If-None-Match", "W/\"123456789\"")) .andExpect(status().isNotModified()).andReturn(); } @Test public void testHeadNotModified() throws Exception { // Execute controller method this.mockMvc .perform(request(HttpMethod.HEAD, "http://localhost/customer/?page=1&size=15") .contentType(HalHttpMessageConverter.HAL_JSON).accept(HalHttpMessageConverter.HAL_JSON) .header("If-None-Match", "W/\"123456789\"")) .andExpect(status().isNotModified()).andReturn(); } }