Last active
February 13, 2018 04:35
-
-
Save mageddo/782e3c89f36531aae999dc91a4e0409c to your computer and use it in GitHub Desktop.
RestEasy Test example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CepCrawlerDAORestTest { | |
| @ClassRule | |
| public static final InMemoryRestServer server = new InMemoryRestServer(CepSearchProxy.class); | |
| @Path("/") | |
| public static class CepSearchProxy { | |
| @Path(CepCrawlerDAORest.CEP_SEARCH) | |
| @GET | |
| public Response buscaCep(){ | |
| return Response.status(509).build(); | |
| } | |
| } | |
| @Test | |
| public void crawByCep_successful() { | |
| Assert.assertEquals(509, server.target().path(CepCrawlerDAORest.CEP_SEARCH).request().get().getStatus()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import io.undertow.Undertow; | |
| import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; | |
| import org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer; | |
| import org.jboss.resteasy.spi.ResteasyDeployment; | |
| import org.junit.rules.ExternalResource; | |
| import javax.ws.rs.client.Client; | |
| import javax.ws.rs.client.WebTarget; | |
| import javax.ws.rs.core.Application; | |
| import java.io.IOException; | |
| import java.net.ServerSocket; | |
| import java.util.HashMap; | |
| import java.util.HashSet; | |
| import java.util.Map; | |
| import java.util.Set; | |
| public class InMemoryRestServer extends ExternalResource { | |
| public static final String HOST = "localhost"; | |
| private static Map<String, String> contextParams = new HashMap<>(); | |
| private static Map<String, String> initParams = new HashMap<>(); | |
| private final Set<Class<?>> classes = new HashSet<>(); | |
| private Application application = null; | |
| private int port; | |
| private UndertowJaxrsServer server; | |
| private final Client client = ResteasyClientBuilder.newClient(); | |
| private WebTarget target; | |
| public InMemoryRestServer(Application application) { | |
| this.application = application; | |
| } | |
| public InMemoryRestServer(Class... objects) { | |
| for (Class clazz : objects) { | |
| classes.add(clazz); | |
| } | |
| } | |
| public static int findFreePort() throws IOException { | |
| ServerSocket server = new ServerSocket(0); | |
| int port = server.getLocalPort(); | |
| server.close(); | |
| return port; | |
| } | |
| @Override | |
| protected void before() throws Throwable { | |
| port = findFreePort(); | |
| server = new UndertowJaxrsServer().start(Undertow.builder().addHttpListener(port, HOST)); | |
| ResteasyDeployment deployment = new ResteasyDeployment(); | |
| deployment.setDeploymentSensitiveFactoryEnabled(true); | |
| if(application == null){ | |
| deployment.setApplication(new Application(){ | |
| @Override | |
| public Set<Class<?>> getClasses() { | |
| return classes; | |
| } | |
| }); | |
| }else{ | |
| deployment.setApplication(application); | |
| } | |
| deployment.start(); | |
| contextParams.put("contextKey1", "contextValue1"); | |
| contextParams.put("contextKey2", "contextValue2"); | |
| initParams.put("initKey1", "initValue1"); | |
| initParams.put("initKey2", "initValue2"); | |
| initParams.put("resteasy.servlet.context.deployment", "false"); | |
| server.deploy(deployment, "/", contextParams, initParams); | |
| server.start(); | |
| target = client.target(String.format("http://%s:%s", HOST, port)); | |
| } | |
| @Override | |
| protected void after() { | |
| server.stop(); | |
| client.close(); | |
| } | |
| public Client getClient() { | |
| return client; | |
| } | |
| public WebTarget target(){ | |
| return target; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment