Java tutorial
package net.eusashead.hateoas.hal.http.converter; /* * #[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 net.eusashead.hateoas.hal.http.converter.HalHttpMessageConverter.HAL_JSON; import static net.eusashead.hateoas.hal.http.converter.HalHttpMessageConverter.HAL_XML; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.mock.http.MockHttpInputMessage; import org.springframework.mock.http.MockHttpOutputMessage; 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 HalHttpMessageConverterTest { // Test HAL resources private static final String BASKET_JSON = "src/test/resources/basket.json"; private static final String BASKET_XML = "src/test/resources/basket.xml"; // Test subject private HalHttpMessageConverter converter; @Before public void before() { RepresentationFactory factory = new StandardRepresentationFactory(); this.converter = new HalHttpMessageConverter(factory); } @Test public void testSupportedMediaTypes() { List<MediaType> mediaTypes = converter.getSupportedMediaTypes(); Assert.assertTrue(mediaTypes.contains(HAL_JSON)); Assert.assertTrue(mediaTypes.contains(HAL_XML)); } @Test(expected = UnsupportedOperationException.class) public void testSupportsRepresentation() { Assert.assertTrue(converter.supports(Object.class)); } @Test public void testCanReadRepresentation() { Assert.assertTrue(converter.canRead(Representation.class, HAL_JSON)); Assert.assertTrue(converter.canRead(Representation.class, HAL_XML)); } @Test public void testCantReadOther() { Assert.assertFalse(converter.canRead(Object.class, HAL_JSON)); Assert.assertFalse(converter.canRead(Object.class, HAL_XML)); } @Test public void testCanWriteRepresentation() { Assert.assertTrue(converter.canWrite(Representation.class, HAL_JSON)); Assert.assertTrue(converter.canWrite(Representation.class, HAL_XML)); } @Test public void testCantWriteOther() { Assert.assertFalse(converter.canWrite(Object.class, HAL_JSON)); Assert.assertFalse(converter.canWrite(Object.class, HAL_XML)); } @Test public void testWriteRepresentationJson() throws HttpMessageNotWritableException, IOException { ReadableRepresentation rep = HalTestUtils.halFromFile(BASKET_JSON); HttpOutputMessage message = new MockHttpOutputMessage(); converter.write(rep, HAL_JSON, message); Assert.assertEquals(HalTestUtils.stringFromFile(BASKET_JSON), message.getBody().toString()); } @Test public void testWriteRepresentationXml() throws HttpMessageNotWritableException, IOException { ReadableRepresentation rep = HalTestUtils.halFromFile(BASKET_XML); HttpOutputMessage message = new MockHttpOutputMessage(); converter.write(rep, HAL_XML, message); Assert.assertEquals(HalTestUtils.stringFromFile(BASKET_XML), message.getBody().toString()); } @Test(expected = IllegalArgumentException.class) public void testWriteOtherJson() throws HttpMessageNotWritableException, IOException { converter.write(new Object(), HAL_JSON, new MockHttpOutputMessage()); } @Test(expected = IllegalArgumentException.class) public void testWriteOtherXml() throws HttpMessageNotWritableException, IOException { converter.write(new Object(), HAL_XML, new MockHttpOutputMessage()); } @Test public void testReadRepresentationJson() throws HttpMessageNotWritableException, IOException { // Create a test input message InputStream is = new FileInputStream(new File(BASKET_JSON)); HttpInputMessage message = new MockHttpInputMessage(is); Object object = converter.read(Representation.class, message); // Compare converted with expected ReadableRepresentation rep = HalTestUtils.halFromFile(BASKET_JSON); Assert.assertEquals(rep, object); } @Test public void testReadRepresentationXml() throws HttpMessageNotWritableException, IOException { // Create a test input message InputStream is = new FileInputStream(new File(BASKET_XML)); HttpInputMessage message = new MockHttpInputMessage(is); Object object = converter.read(Representation.class, message); // Compare converted with expected ReadableRepresentation rep = HalTestUtils.halFromFile(BASKET_XML); Assert.assertEquals(rep, object); } @Test(expected = IllegalArgumentException.class) public void testReadOtherJson() throws HttpMessageNotWritableException, IOException { InputStream is = new FileInputStream(new File(BASKET_JSON)); HttpInputMessage message = new MockHttpInputMessage(is); converter.read(Object.class, message); } @Test(expected = IllegalArgumentException.class) public void testReadOtherXml() throws HttpMessageNotWritableException, IOException { InputStream is = new FileInputStream(new File(BASKET_XML)); HttpInputMessage message = new MockHttpInputMessage(is); converter.read(Object.class, message); } }