net.eusashead.hateoas.hal.http.converter.module.HalHttpMessageConverterModuleTest.java Source code

Java tutorial

Introduction

Here is the source code for net.eusashead.hateoas.hal.http.converter.module.HalHttpMessageConverterModuleTest.java

Source

package net.eusashead.hateoas.hal.http.converter.module;

/*
 * #[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.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import net.eusashead.hateoas.hal.http.converter.HalHttpMessageConverter;
import net.eusashead.hateoas.hal.http.converter.HalTestUtils;

import org.junit.Assert;
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.mock.http.MockHttpInputMessage;
import org.springframework.mock.http.MockHttpOutputMessage;

import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
import com.theoryinpractise.halbuilder.api.RepresentationFactory;
import com.theoryinpractise.halbuilder.standard.StandardRepresentationFactory;

@RunWith(JUnit4.class)
public class HalHttpMessageConverterModuleTest {

    private RepresentationFactory factory = new StandardRepresentationFactory();

    private HalHttpMessageConverter converter = new HalHttpMessageConverter(factory);

    @Test
    public void testRegisterModule() {

        // Module list should be initialised
        Assert.assertNotNull(converter.getModules());

        // Should be empty
        Assert.assertEquals(0, converter.getModules().size());

        // Add a module
        FooModule module = addModule();

        // Check that it was added
        Assert.assertEquals(1, converter.getModules().size());
        Assert.assertTrue(converter.getModules().contains(module));

    }

    @Test
    public void testReadWithModule() throws IOException {

        // Should be unable to read without module
        Assert.assertFalse(converter.canRead(Foo.class, HalHttpMessageConverter.HAL_JSON));

        // Add the module
        addModule();

        // Should be able to read
        Assert.assertTrue(converter.canRead(Foo.class, HalHttpMessageConverter.HAL_JSON));

        // Create a test message and read it
        InputStream is = new FileInputStream(new File("src/test/resources/foo.json"));
        HttpInputMessage inputMessage = new MockHttpInputMessage(is);
        Object obj = converter.read(Foo.class, inputMessage);
        Assert.assertNotNull(obj);
        Assert.assertEquals(Foo.class, obj.getClass());

    }

    @Test
    public void testWriteWithModule() throws IOException {

        // Should be unable to write without module
        Assert.assertFalse(converter.canWrite(Foo.class, HalHttpMessageConverter.HAL_JSON));

        // Add module
        addModule();

        // Should be able to write
        Assert.assertTrue(converter.canWrite(Foo.class, HalHttpMessageConverter.HAL_JSON));

        // Create a test output message
        HttpOutputMessage outputMessage = new MockHttpOutputMessage();
        converter.write(new Foo(), HalHttpMessageConverter.HAL_JSON, outputMessage);
        Assert.assertNotNull(outputMessage.getBody());
        String expected = HalTestUtils.stringFromFile("src/test/resources/foo.json");
        Assert.assertEquals(expected, outputMessage.getBody().toString());
    }

    private FooModule addModule() {
        FooModule module = new FooModule();
        converter.addModule(module);
        return module;
    }

}

class Foo {
}

class FooModule implements HalHttpMessageConverterModule {

    @Override
    public boolean canRead(Class<?> type) {
        return type.equals(Foo.class);
    }

    @Override
    public boolean canWrite(Class<?> type) {
        return type.equals(Foo.class);
    }

    @Override
    public ReadableRepresentation write(Object target, RepresentationFactory factory) {
        return factory.newRepresentation("/foo").withProperty("foo", "foo");
    }

    @Override
    public Object read(ReadableRepresentation representation, Class<?> type) {
        return new Foo();
    }

}