Package name.pehl.taoki.security

Source Code of name.pehl.taoki.security.HitchhikingIntegrationTest

package name.pehl.taoki.security;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.IOException;

import name.pehl.taoki.TestComponent;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.restlet.data.Status;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;

/**
* @author $Author: harald.pehl $
* @version $Date: 2012-03-02 08:50:20 -0600 (Fri, 02 Mar 2012) $ $Revision: 150
*          $
*/
public abstract class HitchhikingIntegrationTest
{
    private HitchhikingComponent component;


    @Before
    public void setUp() throws Exception
    {
        component = new HitchhikingComponent(Guice.createInjector(getModule()));
        component.startWith("/hitchhiking", getResourceClass());
    }


    @After
    public void tearDown() throws Exception
    {
        if (component != null)
        {
            component.stop();
        }
    }


    protected abstract AbstractModule getModule();


    protected abstract Class<? extends ServerResource> getResourceClass();


    @Test
    public void getNoAnswer() throws ResourceException, IOException
    {
        ClientResource clientResource = new ClientResource(TestComponent.getBaseUrl() + "/hitchhiking");
        try
        {
            clientResource.get();
            fail("ResourceException expected");
        }
        catch (ResourceException e)
        {
            Status status = e.getStatus();
            assertEquals(Status.CLIENT_ERROR_FORBIDDEN, status);
        }
    }


    @Test
    public void getWrongAnswer() throws ResourceException, IOException
    {
        ClientResource clientResource = new ClientResource(TestComponent.getBaseUrl() + "/hitchhiking?answer=41");
        try
        {
            clientResource.get();
            fail("ResourceException expected");
        }
        catch (ResourceException e)
        {
            Status status = e.getStatus();
            assertEquals(Status.CLIENT_ERROR_FORBIDDEN, status);
        }
    }


    @Test
    public void getAnswer() throws ResourceException, IOException
    {
        ClientResource clientResource = new ClientResource(TestComponent.getBaseUrl() + "/hitchhiking?answer=42");
        String text = clientResource.get().getText();
        assertEquals("Don't panic", text);
    }
}
TOP

Related Classes of name.pehl.taoki.security.HitchhikingIntegrationTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.