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);
}
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);
}
Aggregations