Skip to content

Commit

Permalink
Needed to reset beanManager inside the extension for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kenfinnigan authored and asoldano committed Sep 18, 2020
1 parent 6d83e00 commit 1410fdd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public static boolean isCDIActive() {
return manager != null;
}

public static void clearBeanManager() {
manager = null;
}

private static class RestClientData {
private final Class<?> javaClass;
private final Optional<String> baseUri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.junit.AfterClass;
import org.junit.After;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Test;

import javax.inject.Inject;
Expand All @@ -21,18 +21,19 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ClientHeadersDefaultFactoryCDITest {

private static UndertowJaxrsServer server;
private static WeldContainer container;
private UndertowJaxrsServer undertowJaxrsServer;
private WeldContainer weldContainer;

static class Worker {

@Inject
@RestClient
private TestResourceIntf service;
private ClientHeaderParamIntf service;

public String work() {
return service.hello("Stefano");
Expand All @@ -43,7 +44,7 @@ public String work() {
@RegisterRestClient(baseUri="http://localhost:8081")
@RegisterClientHeaders
@ClientHeaderParam(name="IntfHeader", value="intfValue")
public interface TestResourceIntf {
public interface ClientHeaderParamIntf {

@Path("hello/{h}")
@GET
Expand All @@ -52,13 +53,23 @@ public interface TestResourceIntf {
}

@Path("/")
public static class TestResource {
public static class ClientParamResource {

@Path("hello/{h}")
@GET
public String hello(@PathParam("h") String h, @Context HttpHeaders httpHeaders) {
return "IntfHeader: " + httpHeaders.getRequestHeader("IntfHeader").get(0)
+ " - MthdHeader: " + httpHeaders.getRequestHeader("MthdHeader").get(0);
return getValue(httpHeaders, "IntfHeader")
+ " - "
+ getValue(httpHeaders, "MthdHeader");
}

private String getValue(HttpHeaders httpHeaders, String headerName) {
List<String> values = httpHeaders.getRequestHeader(headerName);
if (values.size() > 0) {
return headerName + ": " + values.get(0);
}

return headerName + ": NO VALUE";
}
}

Expand All @@ -68,30 +79,35 @@ public static class MyApp extends Application {
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> classes = new HashSet<Class<?>>();
classes.add(TestResource.class);
classes.add(ClientParamResource.class);
return classes;
}
}

@BeforeClass
public static void init() throws Exception {
@Before
public void init() throws Exception {
Weld weld = new Weld();
weld.addBeanClass(Worker.class);
weld.addBeanClass(TestResourceIntf.class);
container = weld.initialize();
server = new UndertowJaxrsServer().start();
server.deploy(MyApp.class);
weld.addBeanClass(ClientHeaderParamIntf.class);
weldContainer = weld.initialize();
undertowJaxrsServer = new UndertowJaxrsServer().start();
undertowJaxrsServer.deploy(MyApp.class);
}

@AfterClass
public static void stop() throws Exception {
server.stop();
container.shutdown();
@After
public void stop() throws Exception {
if (undertowJaxrsServer != null) {
undertowJaxrsServer.stop();
}
if (weldContainer != null) {
weldContainer.close();
}
RestClientExtension.clearBeanManager();
}

@Test
public void test() {
String result = container.select(Worker.class).get().work();
String result = weldContainer.select(Worker.class).get().work();
Assert.assertTrue(result.contains("IntfHeader: intfValue"));
Assert.assertTrue(result.contains("MthdHeader: hello"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ public static void init() throws Exception {
public static void stop() throws Exception {
server.stop();
container.shutdown();
RestClientExtension.clearBeanManager();
}

@Test
public void test() {
Assert.assertTrue(container.isRunning());
String result = container.select(Worker.class).get().work();
Assert.assertEquals("hello Stefano", result);
Assert.assertEquals(1, Counter.COUNT.get());
Expand Down

0 comments on commit 1410fdd

Please sign in to comment.