Java tutorial
package net.eusashead.hateoas.hal.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 java.math.BigDecimal; import java.util.Date; import java.util.List; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.mock.web.MockHttpServletRequest; import com.theoryinpractise.halbuilder.api.ReadableRepresentation; import com.theoryinpractise.halbuilder.api.Representation; import com.theoryinpractise.halbuilder.api.RepresentationFactory; import com.theoryinpractise.halbuilder.standard.StandardRepresentationFactory; @RunWith(JUnit4.class) public class HalResponseBuilderImplTest { private static final Date MODIFIED_DATE = new Date(123456789l); private RepresentationFactory representationFactory = new StandardRepresentationFactory(); private MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path/to/resource"); @Test public void testSetRepresentation() throws Exception { // Build a Representation with the factory Representation representation = representationFactory.newRepresentation("/order/123") .withProperty("string", "String value").withProperty("date", new Date(123456789l)) .withProperty("int", 34).withProperty("decimal", BigDecimal.valueOf(123.32d)) .withProperty("boolean", true).withRepresentation("customer", representationFactory .newRepresentation("/customer").withProperty("email", "test@domain.com")); // Create a HalGetResponseBuilder HalResponseBuilderImpl builder = new HalResponseBuilderImpl(representationFactory, request); // Build the response with the existing Representation ResponseEntity<Representation> response = builder.representation(representation).etag(MODIFIED_DATE) .lastModified(MODIFIED_DATE).expireIn(1000000).build(); // Check the headers assertHeaders(response); // Check the body Assert.assertEquals("/order/123", response.getBody().getLinkByRel("self").getHref()); Assert.assertEquals("String value", response.getBody().getValue("string").toString()); Assert.assertEquals(new Date(123456789l).toString(), response.getBody().getValue("date").toString()); Assert.assertEquals("34", response.getBody().getValue("int").toString()); Assert.assertEquals("123.32", response.getBody().getValue("decimal").toString()); Assert.assertEquals("true", response.getBody().getValue("boolean").toString()); List<? extends ReadableRepresentation> customer = response.getBody().getResourcesByRel("customer"); Assert.assertNotNull(customer); Assert.assertEquals(Integer.valueOf(1), Integer.valueOf(customer.size())); ReadableRepresentation cust = customer.get(0); Assert.assertEquals("test@domain.com", cust.getValue("email").toString()); } @Test public void testConstructRepresentation() throws Exception { // Create a HalGetResponseBuilder HalResponseBuilderImpl builder = new HalResponseBuilderImpl(representationFactory, request); // Create a response with a Representation ResponseEntity<Representation> response = builder.withProperty("string", "String value") .withProperty("date", new Date(123456789l)).withProperty("int", 34) .withProperty("decimal", BigDecimal.valueOf(123.32d)).withProperty("boolean", true) .withRepresentation("customer", representationFactory.newRepresentation("/customer").withProperty("email", "test@domain.com")) .etag(MODIFIED_DATE).lastModified(MODIFIED_DATE).expireIn(1000000).build(); // Check the headers assertHeaders(response); // Check the body Assert.assertEquals("/path/to/resource", response.getBody().getLinkByRel("self").getHref()); Assert.assertEquals("String value", response.getBody().getValue("string").toString()); Assert.assertEquals(new Date(123456789l).toString(), response.getBody().getValue("date").toString()); Assert.assertEquals("34", response.getBody().getValue("int").toString()); Assert.assertEquals("123.32", response.getBody().getValue("decimal").toString()); Assert.assertEquals("true", response.getBody().getValue("boolean").toString()); List<? extends ReadableRepresentation> customer = response.getBody().getResourcesByRel("customer"); Assert.assertNotNull(customer); Assert.assertEquals(Integer.valueOf(1), Integer.valueOf(customer.size())); ReadableRepresentation cust = customer.get(0); Assert.assertEquals("test@domain.com", cust.getValue("email").toString()); } @Test public void testDefaultConvert() throws Exception { // Create a test object Order order = new Order(1, BigDecimal.valueOf(12.32d), new Date(123456789l)); // Create a HalGetResponseBuilder HalResponseBuilderImpl builder = new HalResponseBuilderImpl(representationFactory, request); // Create a response with a Representation ResponseEntity<Representation> response = builder.convert(order).etag(order.getDate()) .lastModified(order.getDate()).expireIn(1000000).build(); // Check the headers assertHeaders(response); // Check the body Assert.assertEquals("/path/to/resource", response.getBody().getLinkByRel("self").getHref()); Assert.assertEquals("1", response.getBody().getValue("id").toString()); Assert.assertEquals("12.32", response.getBody().getValue("total").toString()); Assert.assertEquals(new Date(123456789l).toString(), response.getBody().getValue("date").toString()); } @Test public void testConvertWithRepresentationWriter() throws Exception { // Create a test object Order order = new Order(1, BigDecimal.valueOf(12.32d), new Date(123456789l)); // Create a HalGetResponseBuilder HalResponseBuilderImpl builder = new HalResponseBuilderImpl(representationFactory, request); // Create a response with a Representation ResponseEntity<Representation> response = builder.convert(order, new OrderRepresentationWriter()) .etag(order.getDate()).lastModified(order.getDate()).expireIn(1000000).build(); // Check the headers assertHeaders(response); // Check the body Assert.assertEquals("/order/1", response.getBody().getLinkByRel("self").getHref()); Assert.assertEquals("1", response.getBody().getValue("id").toString()); Assert.assertEquals("12.32", response.getBody().getValue("total").toString()); Assert.assertEquals(new Date(123456789l).toString(), response.getBody().getValue("date").toString()); } @Test public void testHead() { // Create a HalGetResponseBuilder HalResponseBuilderImpl builder = new HalResponseBuilderImpl(representationFactory, new MockHttpServletRequest("HEAD", "/path/to/resource")); // Create a response with a Representation ResponseEntity<Representation> response = builder.withProperty("string", "String value") .etag(new Date(123456789l)).lastModified(new Date(123456789l)).expireIn(1000000).build(); // Check we get a 204 Assert.assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); // Check headers assertHeaders(response); // Check body is null Assert.assertNull(response.getBody()); } @Test public void testGetNotModified() { // Create a HEAD with an If-None-Match header MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path/to/resource"); request.addHeader("If-None-Match", "W/\"123456789\""); // Create a HalGetResponseBuilder HalResponseBuilderImpl builder = new HalResponseBuilderImpl(representationFactory, request); // Create a response with a Representation ResponseEntity<Representation> response = builder.withProperty("string", "String value") .etag(new Date(123456789l)).lastModified(new Date(123456789l)).expireIn(1000000).build(); // Check we get a 304 Assert.assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode()); } @Test public void testHeadNotModified() { // Create a HEAD with an If-None-Match header MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/path/to/resource"); request.addHeader("If-None-Match", "W/\"123456789\""); // Create a HalGetResponseBuilder HalResponseBuilderImpl builder = new HalResponseBuilderImpl(representationFactory, request); // Create a response with a Representation ResponseEntity<Representation> response = builder.withProperty("string", "String value") .etag(new Date(123456789l)).lastModified(new Date(123456789l)).expireIn(1000000).build(); // Check we get a 304 Assert.assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode()); } // Assert the headers private void assertHeaders(ResponseEntity<Representation> response) { HttpHeaders headers = response.getHeaders(); Assert.assertNotNull(headers.getETag()); Assert.assertEquals("W/\"123456789\"", headers.getETag()); Assert.assertEquals(123456000, headers.getLastModified()); Assert.assertNotNull(headers.getDate()); Assert.assertNotNull(headers.getExpires()); Assert.assertEquals(headers.getDate() + 1000000, headers.getExpires()); Assert.assertNotNull(headers.getCacheControl()); Assert.assertEquals("public, must-revalidate, proxy-revalidate, max-age=1000", headers.getCacheControl()); } }