TestEndToEnd.java :  » MVC » fulworx » org » fulworx » core » rest » Java Open Source

Java Open Source » MVC » fulworx 
fulworx » org » fulworx » core » rest » TestEndToEnd.java
package org.fulworx.core.rest;

import com.opensymphony.xwork2.spring.SpringObjectFactory;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fulworx.core.FulworxServletRequest;
import org.fulworx.core.XMLUtil;
import org.fulworx.core.config.spring.FulworxSpringObjectFactory;
import org.fulworx.core.config.xwork.FulworxObjectFactory;
import org.fulworx.core.config.xwork.FulworxObjectFactoryBuilder;
import org.fulworx.core.rest.restlet.RootApplication;
import org.fulworx.core.util.cache.ActiveRefreshCache;
import org.fulworx.core.util.cache.TimeoutRefreshCache;
import org.restlet.Application;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.data.ChallengeResponse;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.FileRepresentation;
import org.restlet.resource.InputRepresentation;
import org.restlet.resource.Representation;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.ByteArrayInputStream;

/**
 * @version $Id: $
 */
public class TestEndToEnd extends TestCase
{
    private Restlet restlet;


    protected void setUp() throws
                           Exception
    {
        SpringObjectFactory springObjectFactory = new FulworxSpringObjectFactory();
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                new String[]{"testResourceContext.xml"});
        springObjectFactory.setApplicationContext(applicationContext);

        FulworxObjectFactoryBuilder builder = new FulworxObjectFactoryBuilder();
        builder.setObjectFactory(springObjectFactory);
        FulworxObjectFactory factory = builder.build();
        factory.createActionContext(new FulworxServletRequest());

        Application application = (Application) factory.buildBean(RootApplication.class, null);
        application.setContext(new Context());

        ActiveRefreshCache cache = (ActiveRefreshCache) factory.buildBean(ActiveRefreshCache.class, null);
        TimeoutRefreshCache tcache = (TimeoutRefreshCache) factory.buildBean(TimeoutRefreshCache.class, null);
        if (cache != null && tcache != null)
        {
            tcache.getTimer().cancel();
            cache.getTimer().cancel();
        }

        restlet = application.createRoot();
    }


    public void testActionInvocation() throws
                                       Exception
    {
        Request request = new Request();
        request.setMethod(Method.GET);
        request.setResourceRef("/someResource");
        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        XMLUtil.contains("<SomeEntity><Firstname>testFirst</Firstname><Lastname>testLast</Lastname><Value>1</Value></SomeEntity>",
                         representation.getText());
    }

    public void testSimpleGuardedActionInvocation() throws
                                                    Exception
    {
        Request request = new Request();
        request.setMethod(Method.GET);
        request.setResourceRef("/someResource");

        // Add the client authentication to the call  
        ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC;
        ChallengeResponse authentication = new ChallengeResponse(scheme,
                                                                 "scott", "tiger");
        request.setChallengeResponse(authentication);

        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        XMLUtil.contains("<SomeEntity><Firstname>testFirst</Firstname><Lastname>testLast</Lastname><Value>1</Value></SomeEntity>",
                         representation.getText());
    }

    public void testEntityActionInvocation() throws
                                             Exception
    {
        Request request = new Request();
        request.setMethod(Method.PUT);
        request.setResourceRef("/someResource");
        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        XMLUtil.contains(
                "<SomeEntity><Firstname>testFirst</Firstname><Lastname>testLast</Lastname><Address><Street1>testStreet1</Street1><Street2>testStreet2</Street2></Address><Value>2</Value></SomeEntity>",
                representation.getText());
    }


    public void testEmptyGet() throws
                               Exception
    {
        Request request = new Request();
        request.setMethod(Method.GET);
        request.setResourceRef("/someResource");
        Representation rep = new InputRepresentation(new ByteArrayInputStream(new byte[]{}),
                                                     MediaType.APPLICATION_XML);
        rep.setSize(0);
        request.setEntity(rep);
        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        XMLUtil.contains(
                "<SomeEntity><Firstname>testFirst</Firstname><Lastname>testLast</Lastname><Value>1</Value></SomeEntity>",
                representation.getText());
    }


    public void testNewInstance() throws
                                  Exception
    {
        Request request = new Request();
        request.setMethod(Method.PUT);
        request.setResourceRef("/someResource");
        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        XMLUtil.contains(
                "<SomeEntity><Firstname>testFirst</Firstname><Lastname>testLast</Lastname><Address><Street1>testStreet1</Street1><Street2>testStreet2</Street2></Address><Value>2</Value></SomeEntity>",
                representation.getText());

        request = new Request();
        request.setMethod(Method.PUT);
        request.setResourceRef("/someResource");
        response = new Response(request);
        restlet.handle(request, response);

        //validate response
        representation = response.getEntity();
        XMLUtil.contains(
                "<SomeEntity><Firstname>testFirst</Firstname><Lastname>testLast</Lastname><Address><Street1>testStreet1</Street1><Street2>testStreet2</Street2></Address><Value>2</Value></SomeEntity>",
                representation.getText());
    }

    public void testPutAsParam() throws
                                 Exception
    {
        Request request = new Request();
        request.setMethod(Method.POST);
        request.setResourceRef("/someResource?method=PUT");
        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        XMLUtil.contains(
                "<SomeEntity><Firstname>testFirst</Firstname><Lastname>testLast</Lastname><Address><Street1>testStreet1</Street1><Street2>testStreet2</Street2></Address><Value>2</Value></SomeEntity>",
                representation.getText());
    }


    public void testErrorActionInvocation() throws
                                            Exception
    {
        Request request = new Request();
        request.setMethod(Method.POST);
        request.setResourceRef("/someResource");
        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        XMLUtil.contains(
                "<ErrorList>" +
                "<ResponseCode>404</ResponseCode>" +
                "<Error><errorCode>111</errorCode><detail>Default error message someAction error</detail></Error></ErrorList>",
                representation.getText());
        assertEquals(404, response.getStatus().getCode());
    }


    public void testInvalidAction() throws
                                    Exception
    {
        Request request = new Request();
        request.setMethod(Method.DELETE);
        request.setResourceRef("/someResource");
        request.setEntity(
                "<SomeEntity><Firstname>delFirst</Firstname><Lastname>delLast</Lastname><Address><Street1>delStreet1</Street1><Street2>delStreet2</Street2></Address></SomeEntity>");
        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        XMLUtil.contains(
                "<ErrorList>" +
                "<ResponseCode>500</ResponseCode>" +
                "<Error><errorCode>error.operation.unsupported</errorCode><detail>Operation DELETE is not supported for entity deleteSomeAction</detail></Error></ErrorList>",
                representation.getText());
        assertEquals(500, response.getStatus().getCode());
    }


    public void testUnexpectedError() throws
                                      Exception
    {
        LOG.info("Testing delete with no submitted entity");
        Request request = new Request();
        request.setMethod(Method.DELETE);
        request.setResourceRef("/someDelResource");
        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        XMLUtil.contains(
                "<ErrorList>" +
                "<ResponseCode>500</ResponseCode>" +
                "<Error><errorCode>unknown</errorCode><detail>Error invoking action</detail></Error></ErrorList>",
                representation.getText());
        assertEquals(500, response.getStatus().getCode());
    }


    public void testEntityParameters() throws
                                       Exception
    {
        String content = "<?xml version='1.0' encoding='utf-8'?><SomeEntity><Firstname>delFirst</Firstname><Lastname>delLast</Lastname><Address><Street1>delStreet1</Street1><Street2>delStreet2</Street2></Address></SomeEntity>";
        Representation rep = new InputRepresentation(
                new ByteArrayInputStream(
                        content.getBytes()),
                MediaType.APPLICATION_XML);
        rep.setSize(content.length());

        Request request = new Request();
        request.setMethod(Method.DELETE);
        request.setResourceRef("/someDelResource");
        request.setEntity(rep);
        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        //notice <value> doesn't increment in this action, it's not supposed to
        XMLUtil.contains(
                "<SomeEntity><Firstname>isdeleted</Firstname><Lastname>isdeleted</Lastname><Address><Street1>delStreet1</Street1><Street2>delStreet2</Street2></Address><Value>1</Value></SomeEntity>",
                representation.getText());
    }


    public void testRequestParameters() throws
                                        Exception
    {
        String content = "<?xml version='1.0' encoding='utf-8'?><SomeEntity><Firstname>delFirst</Firstname><Lastname>delLast</Lastname><Address><Street1>delStreet1</Street1><Street2>delStreet2</Street2></Address></SomeEntity>";
        Representation rep = new InputRepresentation(
                new ByteArrayInputStream(
                        content.getBytes()),
                MediaType.APPLICATION_XML);
        rep.setSize(content.length());

        Request request = new Request();
        request.setMethod(Method.DELETE);
        request.setResourceRef("/anotherDelResource/user/user123");
        request.setEntity(rep);
        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        //notice <value> doesn't increment in this action, it's not supposed to
        XMLUtil.contains(
                "<SomeEntity><Firstname>user123</Firstname><Lastname>isdeleted</Lastname><Address><Street1>delStreet1</Street1><Street2>delStreet2</Street2></Address><Value>1</Value></SomeEntity>",
                representation.getText());
    }

    public void testDeleteWithParam() throws
                                      Exception
    {
        String content = "<?xml version='1.0' encoding='utf-8'?><SomeEntity><Firstname>delFirst</Firstname><Lastname>delLast</Lastname><Address><Street1>delStreet1</Street1><Street2>delStreet2</Street2></Address></SomeEntity>";
        Representation rep = new InputRepresentation(
                new ByteArrayInputStream(
                        content.getBytes()),
                MediaType.APPLICATION_XML);
        rep.setSize(content.length());

        Request request = new Request();
        request.setMethod(Method.POST);
        request.setResourceRef("/anotherDelResource/user/user123?method=DELETE");
        request.setEntity(rep);
        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        //notice <value> doesn't increment in this action, it's not supposed to
        XMLUtil.contains(
                "<SomeEntity><Firstname>user123</Firstname><Lastname>isdeleted</Lastname><Address><Street1>delStreet1</Street1><Street2>delStreet2</Street2></Address><Value>1</Value></SomeEntity>",
                representation.getText());
    }


    public void testSchemaRetrieval() throws
                                      Exception
    {
        Request request = new Request();
        request.setMethod(Method.GET);
        request.setResourceRef("/someResource.xsd");
        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        XMLUtil.contains(
                "<xs:schema version=\"1.0\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
                "\n" +
                "  <xs:element name=\"SomeEntity\">\n" +
                "    <xs:complexType>\n" +
                "      <xs:sequence>\n" +
                "        <xs:element name=\"Firstname\" type=\"xs:string\"/>\n" +
                "        <xs:element name=\"Lastname\" type=\"xs:string\"/>\n" +
                "        <xs:element name=\"Address\" type=\"Address\"/>\n" +
                "        <xs:element name=\"Value\" type=\"xs:int\" minOccurs=\"0\"/>\n" +
                "        <xs:element name=\"StringValue\" type=\"xs:string\" minOccurs=\"0\"/>\n" +
                "      </xs:sequence>\n" +
                "    </xs:complexType>\n" +
                "  </xs:element>\n" +
                "\n" +
                "  <xs:complexType name=\"Address\">\n" +
                "    <xs:sequence>\n" +
                "      <xs:element name=\"Street1\" type=\"xs:string\"/>\n" +
                "      <xs:element name=\"Street2\" type=\"xs:string\" minOccurs=\"0\"/>\n" +
                "    </xs:sequence>\n" +
                "  </xs:complexType>\n" +
                "</xs:schema>\n" +
                "\n",
                representation.getText());
    }


    private static final Log LOG = LogFactory.getLog(TestEndToEnd.class);


    public void testFileRead() throws
                               Exception
    {
        Request request = new Request();
        request.setMethod(Method.GET);

        request.setResourceRef("/fileresource/endtoend.response.txt");
        Response response = new Response(request);
        restlet.handle(request, response);

        //validate response
        Representation representation = response.getEntity();
        assertTrue(representation instanceof FileRepresentation);

        FileRepresentation fileRepresentation = (FileRepresentation) representation;
        assertEquals("Hooray", fileRepresentation.getText());
    }


    //todo: get submitted with "boundary" in the content type.
    public void testFileCreate() throws
                                 Exception
    {
//        Request request = new Request();
//        request.setMethod(Method.PUT);
//
//        request.setResourceRef("/fileresource/newFile");
//        Response response = new Response(request);
//
//        String content = "file contents";
//        Representation rep = new InputRepresentation(
//                new ByteArrayInputStream(
//                        content.getBytes()),
//                MediaType.MULTIPART_ALL);
//        rep.setSize(content.length());
//        request.setEntity(rep);
//        restlet.handle(request, response);
//
//        //validate response
//        Representation representation = response.getEntity();
//        assertTrue(representation instanceof FileRepresentation);
//
//        FileRepresentation fileRepresentation = (FileRepresentation) representation;
//        assertEquals("file contents", fileRepresentation.getText());
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.