package oasis.http.testing;

import java.net.URI;

import javax.ws.rs.client.Client;

import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.junit.rules.ExternalResource;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.google.inject.Inject;
import com.google.inject.Injector;

import oasis.http.HttpServer;
import oasis.web.providers.JacksonContextResolver;
import oasis.web.providers.CookieParserRequestFilter;

/**
 * Creates an in-process Resteasy container and client. Depends on Jukito.
 */
public class InProcessResteasy extends ExternalResource {

  public static final URI BASE_URI = URI.create("http://localhost/");

  private final Injector injector;

  private ResteasyDeployment deployment;
  private Client client;

  @Inject
  InProcessResteasy(Injector injector) {
    this.injector = injector;
  }

  public ResteasyDeployment getDeployment() {
    return deployment;
  }

  public Client getClient() {
    return client;
  }

  @Override
  protected void before() throws Throwable {
    ResteasyProviderFactory providerFactory = HttpServer.createResteasyProviderFactory(injector);
    providerFactory.register(JacksonJsonProvider.class);
    providerFactory.register(JacksonContextResolver.class);
    providerFactory.register(CookieParserRequestFilter.class);

    deployment = new ResteasyDeployment();
    deployment.setProviderFactory(providerFactory);

    deployment.start();

    client = new ResteasyClientBuilder()
        .httpEngine(new InProcessClientHttpEngine(deployment.getDispatcher(), BASE_URI))
        .register(JacksonJsonProvider.class)
        .build();
  }

  @Override
  protected void after() {
    deployment.stop();
  }
}

@@ -42,16 +42,48 @@
 net.URI;
+%0Aimport java.security.Principal;
 %0A%0Aimport
@@ -109,16 +109,57 @@
 .Client;
+%0Aimport javax.ws.rs.core.SecurityContext;
 %0A%0Aimport
@@ -525,59 +525,8 @@
 er;%0A
-import oasis.web.providers.JacksonContextResolver;%0A
 impo
@@ -574,16 +574,67 @@
 tFilter;
+%0Aimport oasis.web.providers.JacksonContextResolver;
 %0A%0A/**%0A *
@@ -1587,16 +1587,114 @@
 actory);
+%0A    deployment.getDefaultContextObjects().put(SecurityContext.class, new DummySecurityContext());
 %0A%0A    de
@@ -1945,24 +1945,24 @@
 d after() %7B%0A
-
     deployme
@@ -1975,11 +1975,417 @@
 p();%0A  %7D
+%0A%0A  private static class DummySecurityContext implements SecurityContext %7B%0A    @Override%0A    public Principal getUserPrincipal() %7B%0A      return null;%0A    %7D%0A%0A    @Override%0A    public boolean isUserInRole(String role) %7B%0A      return false;%0A    %7D%0A%0A    @Override%0A    public boolean isSecure() %7B%0A      return false;%0A    %7D%0A%0A    @Override%0A    public String getAuthenticationScheme() %7B%0A      return null;%0A    %7D%0A  %7D
 %0A%7D%0A

Initialize the SecurityContext in the testing infrastructure