Java tutorial
package net.eusashead.hateoas.springhalbuilder.controller; /* * #[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.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 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.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.Date; import net.eusashead.hateoas.hal.http.converter.HalHttpMessageConverter; import net.eusashead.hateoas.springhalbuilder.config.WebConfig; import net.eusashead.hateoas.springhalbuilder.model.Customer; 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.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext.ClassMode; 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; import com.theoryinpractise.halbuilder.api.Link; import com.theoryinpractise.halbuilder.api.ReadableRepresentation; import com.theoryinpractise.halbuilder.standard.StandardRepresentationFactory; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = { WebConfig.class }) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class CustomerControllerITCase { @Autowired private WebApplicationContext context; private ObjectMapper mapper = new ObjectMapper(); private MockMvc mockMvc; @Before public void before() { this.mockMvc = webAppContextSetup(context).dispatchOptions(true).build(); } @Test public void testOptions() throws Exception { this.mockMvc .perform(request(HttpMethod.OPTIONS, "http://localhost/customer/1") .contentType(HalHttpMessageConverter.HAL_JSON).accept(HalHttpMessageConverter.HAL_JSON)) .andExpect(status().isOk()).andExpect(header().string("Allow", "GET,HEAD")).andReturn(); } @Test public void testHead() throws Exception { this.mockMvc .perform(request(HttpMethod.HEAD, "http://localhost/customer/1") .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNoContent()).andExpect(header().string("ETag", "\"3\"")) .andExpect(header().string("Last-Modified", "Mon, 29 Jul 2013 23:00:00 GMT")).andReturn(); } @Test public void testGet() throws Exception { // Execute controller method MvcResult result = this.mockMvc .perform(get("http://localhost/customer/1").contentType(HalHttpMessageConverter.HAL_JSON) .accept(HalHttpMessageConverter.HAL_JSON)) .andExpect(status().isOk()).andExpect(header().string("ETag", "\"3\"")) .andExpect(header().string("Last-Modified", "Mon, 29 Jul 2013 23:00:00 GMT")) .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); Link linkByRel = returned.getLinkByRel("self"); Assert.assertNotNull(linkByRel); Assert.assertEquals("/customer/1", linkByRel.getHref()); Assert.assertEquals("John", returned.getValue("first_name").toString()); Assert.assertEquals("Citizen", returned.getValue("last_name").toString()); Assert.assertEquals("14/01/1970", returned.getValue("birthday").toString()); } @Test public void testPut() throws Exception { // Get the ETag this.mockMvc .perform(request(HttpMethod.HEAD, "http://localhost/customer/1") .contentType(HalHttpMessageConverter.HAL_JSON).accept(HalHttpMessageConverter.HAL_JSON)) .andExpect(status().isNoContent()).andExpect(header().string("ETag", "\"3\"")).andReturn(); // Create the request body Customer customer = new Customer(); customer.setBirthday(new Date(0L)); customer.setCustomerId(1); customer.setFirstName("New"); customer.setLastName("Last"); customer.setEmail("new.last@email.com"); customer.setVersion(3); // Perform the update this.mockMvc .perform(put("http://localhost/customer/1").content(mapper.writeValueAsBytes(customer)) .header("If-Match", "\"3\"").contentType(MediaType.APPLICATION_JSON) .characterEncoding(Charset.forName("UTF-8").toString()).accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNoContent()).andReturn(); } @Test public void testPutConcurrent() throws Exception { // Get the ETag this.mockMvc .perform(request(HttpMethod.HEAD, "http://localhost/customer/1") .contentType(HalHttpMessageConverter.HAL_JSON).accept(HalHttpMessageConverter.HAL_JSON)) .andExpect(status().isNoContent()).andExpect(header().string("ETag", "\"3\"")).andReturn(); // Create the request body Customer customer = new Customer(); customer.setBirthday(new Date(0L)); customer.setCustomerId(1); customer.setFirstName("New"); customer.setLastName("Last"); customer.setEmail("new.last@email.com"); customer.setVersion(2); // Perform the update this.mockMvc .perform(put("http://localhost/customer/1").content(mapper.writeValueAsBytes(customer)) .header("If-Match", "\"2\"").contentType(MediaType.APPLICATION_JSON) .characterEncoding(Charset.forName("UTF-8").toString()).accept(MediaType.APPLICATION_JSON)) .andExpect(status().isPreconditionFailed()).andReturn(); } @Test public void testDelete() throws Exception { // We need to get the ETag first this.mockMvc .perform(request(HttpMethod.HEAD, "http://localhost/customer/4") .contentType(HalHttpMessageConverter.HAL_JSON).accept(HalHttpMessageConverter.HAL_JSON)) .andExpect(status().isNoContent()).andExpect(header().string("ETag", "\"1\"")).andReturn(); // Delete this.mockMvc.perform(delete("http://localhost/customer/4").contentType(HalHttpMessageConverter.HAL_JSON) .header("If-Match", "\"1\"").characterEncoding(Charset.forName("UTF-8").toString()) .accept(HalHttpMessageConverter.HAL_JSON)).andExpect(status().isNoContent()).andReturn(); } }