Search in sources :

Example 11 with Invocation

use of org.jmock.api.Invocation in project sling by apache.

the class TopologyRequestValidatorTest method testTrustResponse.

@Test
public void testTrustResponse() throws IOException {
    final HttpServletRequest request = context.mock(HttpServletRequest.class);
    context.checking(new Expectations() {

        {
            allowing(request).getRequestURI();
            will(returnValue("/Test/Uri2"));
        }
    });
    final HttpServletResponse response = context.mock(HttpServletResponse.class);
    final Map<Object, Object> headers = new HashMap<Object, Object>();
    context.checking(new Expectations() {

        {
            allowing(response).setHeader(with(any(String.class)), with(any(String.class)));
            will(new Action() {

                public void describeTo(Description desc) {
                    desc.appendText("Setting header ");
                }

                public Object invoke(Invocation invocation) throws Throwable {
                    headers.put(invocation.getParameter(0), invocation.getParameter(1));
                    return null;
                }
            });
        }
    });
    String clearMessage = "TestMessage2";
    final String message = topologyRequestValidator.encodeMessage(clearMessage);
    topologyRequestValidator.trustMessage(response, request, message);
    final HttpEntity responseEntity = context.mock(HttpEntity.class);
    context.checking(new Expectations() {

        {
            allowing(responseEntity).getContent();
            will(returnValue(new ByteArrayInputStream(message.getBytes())));
        }
    });
    final HttpResponse resp = context.mock(HttpResponse.class);
    context.checking(new Expectations() {

        {
            allowing(resp).getFirstHeader(with(any(String.class)));
            will(new Action() {

                public void describeTo(Description desc) {
                    desc.appendText("Getting (first) header ");
                }

                public Object invoke(Invocation invocation) throws Throwable {
                    return new BasicHeader((String) invocation.getParameter(0), (String) headers.get(invocation.getParameter(0)));
                }
            });
            allowing(resp).getEntity();
            will(returnValue(responseEntity));
        }
    });
    topologyRequestValidator.isTrusted(resp);
    topologyRequestValidator.decodeMessage("/Test/Uri2", resp);
}
Also used : Expectations(org.jmock.Expectations) Action(org.jmock.api.Action) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpResponse(org.apache.http.HttpResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHeader(org.apache.http.message.BasicHeader) Test(org.junit.Test)

Example 12 with Invocation

use of org.jmock.api.Invocation in project sling by apache.

the class StartupFilterImplTest method setProvider.

private void setProvider(final TestProvider provider) throws Exception {
    final BundleContext bundleContext = mockery.mock(BundleContext.class);
    final Action storeStatus = new Action() {

        @Override
        public void describeTo(Description d) {
            d.appendText("Store HTTP response values");
        }

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            lastReturnedStatus = (Integer) invocation.getParameter(0);
            return null;
        }
    };
    messageWriter = new StringWriter();
    final PrintWriter responseWriter = new PrintWriter(messageWriter);
    final Map<String, Object> props = new HashMap<String, Object>();
    props.put(StartupFilterImpl.ACTIVE_BY_DEFAULT_PROP, Boolean.TRUE);
    final ServiceReference[] providerRefs = provider == null ? null : new ServiceReference[] { provider };
    mockery.checking(new Expectations() {

        {
            allowing(bundleContext).createFilter(with(any(String.class)));
            allowing(bundleContext).addServiceListener(with(any(ServiceListener.class)));
            allowing(bundleContext).addServiceListener(with(any(ServiceListener.class)), with(any(String.class)));
            allowing(bundleContext).getServiceReferences(StartupInfoProvider.class.getName(), null);
            will(returnValue(providerRefs));
            allowing(bundleContext).getService(with(any(ServiceReference.class)));
            will(returnValue(provider));
            allowing(bundleContext).getProperty(with("felix.webconsole.manager.root"));
            will(returnValue(CONSOLE_ROOT));
            allowing(bundleContext).registerService(with(equal(Filter.class)), with(any(Filter.class)), with(any(Dictionary.class)));
            will(new DoAllAction(new ChangeInteger(activeFilterCount, true), returnValue(serviceRegistration)));
            allowing(response).setStatus((with(any(Integer.class))));
            will(storeStatus);
            allowing(response).setContentType("text/plain");
            allowing(response).getWriter();
            will(returnValue(responseWriter));
            allowing(response).setCharacterEncoding(with(any(String.class)));
            allowing(serviceRegistration).unregister();
            will(new ChangeInteger(activeFilterCount, false));
            allowing(request).getServletPath();
            will(returnValue(""));
            allowing(request).getPathInfo();
            will(returnValue(getPathInfo()));
            allowing(chain).doFilter(with(any(ServletRequest.class)), with(any(ServletResponse.class)));
        }
    });
    filter.setup(bundleContext, props);
}
Also used : Expectations(org.jmock.Expectations) DoAllAction(org.jmock.lib.action.DoAllAction) Dictionary(java.util.Dictionary) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) DoAllAction(org.jmock.lib.action.DoAllAction) Action(org.jmock.api.Action) Description(org.hamcrest.Description) ServiceListener(org.osgi.framework.ServiceListener) Invocation(org.jmock.api.Invocation) HashMap(java.util.HashMap) ServiceReference(org.osgi.framework.ServiceReference) StringWriter(java.io.StringWriter) Filter(javax.servlet.Filter) BundleContext(org.osgi.framework.BundleContext) PrintWriter(java.io.PrintWriter)

Aggregations

Expectations (org.jmock.Expectations)12 Invocation (org.jmock.api.Invocation)12 Description (org.hamcrest.Description)7 Test (org.junit.Test)7 Action (org.jmock.api.Action)6 CustomAction (org.jmock.lib.action.CustomAction)5 Channel (com.weibo.api.motan.transport.Channel)2 MessageHandler (com.weibo.api.motan.transport.MessageHandler)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)2 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)2 Disposable (com.intellij.openapi.Disposable)1