/**
   * This integration test, tests that an exception is thrown if the format of the key used to
   * retrieve a cache identity is invalid.
   */
  public void testCSSServletWithInvalidKey() throws Exception {

    //
    // EXPECTATIONS
    //

    //
    // Setup a usable servlet environment using mocks.
    //
    ServletHelper.setExpectedResults(
        expectations, servletConfigMock, servletContextMock, CSSServlet.class);

    //
    // Create a writer for the HttpServletResponse to use
    //

    StringWriter stringWriter = new StringWriter();

    PrintWriter printWriter = new PrintWriter(stringWriter);

    //
    // We don't need a fully initialised Volantis bean so we can
    // return a MarinerServletApplicationMock.
    //
    servletContextMock.expects.getAttribute("marinerApplication").returns(marinerApplicationMock);

    //
    // We need to register our Version of the Volantis bean against our
    // MarinerServketApplicationMock as we cannot overide the
    // getVolantisBean() method which is package protected.
    //

    ApplicationInternals.setVolantisBean(marinerApplicationMock, volantis);
    //        marinerApplicationMock
    //                .expects
    //                .getVolantisBean()
    //                .returns(volantis).any();

    httpServletRequestMock.expects.getParameter("key").returns("rubbish");

    CSSServlet cssServlet = new CSSServlet();

    cssServlet.init(servletConfigMock);

    boolean exceptionCaught = false;
    try {
      cssServlet.doGet(httpServletRequestMock, httpServletResponseMock);
    } catch (IllegalArgumentException e) {
      assertEquals(
          e.getMessage(),
          "base64Key is not a valid encoding of createTime" + ", expiresTime and sequenceNo");
      exceptionCaught = true;
    }
    assertEquals(exceptionCaught, true);
  }
  /**
   * This integration test, tests that an entry placed into the cache via the Volantis bean can be
   * retrieved using the returned key from the CSSServlet and that the servlet writes the content
   * out with the correct content type.
   */
  public void testCSSServletWithValidKey() throws Exception {

    //
    // SETUP
    //
    WritableCSSEntityMock writableCSSEntityMock =
        new WritableCSSEntityMock("writableCSSEntityMock", expectations);

    //
    // We need to initialise the cache and get the one and hopefully only
    // instance of it.
    //
    Cache cache = volantis.getCSSCache();

    //
    // Store our WritableCSSEntityMock in the cache and get the identity
    // that can be used to retrieve the entry.
    //
    CacheIdentity identity = cache.store(new CSSCacheEntry(writableCSSEntityMock));

    //
    // EXPECTATIONS
    //

    //
    // Setup a usable servlet environment using mocks.
    //
    ServletHelper.setExpectedResults(
        expectations, servletConfigMock, servletContextMock, CSSServlet.class);

    //
    // Create a writer for the HttpServletResponse to use
    //

    StringWriter stringWriter = new StringWriter();

    PrintWriter printWriter = new PrintWriter(stringWriter);

    //
    // We don't need a fully initialised Volantis bean so we can
    // return a MarinerServletApplicationMock.
    //
    servletContextMock.expects.getAttribute("marinerApplication").returns(marinerApplicationMock);

    //
    // We need to register our Version of the Volantis bean against our
    // MarinerServketApplicationMock as we cannot overide the
    // getVolantisBean() method which is package protected.
    //

    ApplicationInternals.setVolantisBean(marinerApplicationMock, volantis);
    //        marinerApplicationMock
    //                .expects
    //                .getVolantisBean()
    //                .returns(volantis).any();

    //
    // For piece of mind we will get our WritableCSSEntityMock to actually
    // write something to its supplied writer. That way we know for sure
    // that the servlet works end to end.
    //
    writableCSSEntityMock
        .expects
        .write(printWriter)
        .does(
            new MethodAction() {
              public Object perform(MethodActionEvent event) throws Throwable {
                Writer pw = (Writer) event.getArgument(Writer.class);
                pw.write("foo");
                return null;
              }
            });

    httpServletRequestMock.expects.getParameter("key").returns(identity.getBase64KeyAsString());

    httpServletResponseMock.expects.getWriter().returns(printWriter);

    httpServletResponseMock.expects.setContentType("text/css");

    httpServletResponseMock
        .fuzzy
        .addHeader(mockFactory.expectsAny(), mockFactory.expectsAny())
        .returns()
        .any();

    httpServletResponseMock
        .fuzzy
        .setDateHeader(mockFactory.expectsAny(), mockFactory.expectsAny())
        .returns()
        .any();

    CSSServlet cssServlet = new CSSServlet();

    cssServlet.init(servletConfigMock);

    cssServlet.doGet(httpServletRequestMock, httpServletResponseMock);

    assertEquals("foo", stringWriter.toString());
  }
  /**
   * This integration test, tests that a 404 response is returned from the servlet if a cache key is
   * not found.
   */
  public void testCSSServletWithMissingKey() throws Exception {

    //
    // SETUP
    //

    WritableCSSEntityMock writableCSSEntityMock =
        new WritableCSSEntityMock("writableCSSEntityMock", expectations);

    //
    // We need to initialise the cache and get the one and hopefully only
    // instance of it.
    //
    Cache cache = volantis.getCSSCache();

    //
    // Store our WritableCSSEntityMock in the cache and get the identity
    // that can be used to retrieve the entry.
    //
    CacheIdentity identity = cache.store(new CSSCacheEntry(writableCSSEntityMock));
    //
    // Now remove all entries so we get a cache miss.
    //
    cache.clear();

    //
    // EXPECTATIONS
    //

    //
    // Setup a usable servlet environment using mocks.
    //
    ServletHelper.setExpectedResults(
        expectations, servletConfigMock, servletContextMock, CSSServlet.class);

    //
    // We don't need a fully initialised Volantis bean so we can
    // return a MarinerServletApplicationMock.
    //
    servletContextMock.expects.getAttribute("marinerApplication").returns(marinerApplicationMock);

    //
    // We need to register our Version of the Volantis bean against our
    // MarinerServketApplicationMock as we cannot overide the
    // getVolantisBean() method which is package protected.
    //

    ApplicationInternals.setVolantisBean(marinerApplicationMock, volantis);
    //        marinerApplicationMock
    //                .expects
    //                .getVolantisBean()
    //                .returns(volantis).any();

    httpServletRequestMock.expects.getParameter("key").returns(identity.getBase64KeyAsString());

    httpServletResponseMock.expects.sendError(404);

    CSSServlet cssServlet = new CSSServlet();

    cssServlet.init(servletConfigMock);

    cssServlet.doGet(httpServletRequestMock, httpServletResponseMock);
  }