forked from resteasy/resteasy
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tomas Terem
committed
May 17, 2019
1 parent
d66c8cf
commit 944dc50
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
...tegration-tests/src/test/java/org/jboss/resteasy/test/interceptor/DynamicFeatureTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.jboss.resteasy.test.interceptor; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.container.test.api.RunAsClient; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.logging.Logger; | ||
import org.jboss.resteasy.test.interceptor.resource.AddDynamicFeature; | ||
import org.jboss.resteasy.test.interceptor.resource.DynamicFeatureResource; | ||
import org.jboss.resteasy.test.interceptor.resource.GreetingInterceptor; | ||
import org.jboss.resteasy.utils.PortProviderUtil; | ||
import org.jboss.resteasy.utils.TestUtil; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.junit.AfterClass; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import javax.ws.rs.client.Client; | ||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.client.Entity; | ||
import javax.ws.rs.client.WebTarget; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
/** | ||
* @tpSubChapter Resteasy-client | ||
* @tpChapter Integration tests | ||
* @tpTestCaseDetails Test for DynamicFeature | ||
* @tpSince RESTEasy 3.8.0 | ||
*/ | ||
@RunWith(Arquillian.class) | ||
@RunAsClient | ||
public class DynamicFeatureTest { | ||
|
||
private static final Logger LOG = Logger.getLogger(DynamicFeatureTest.class.getName()); | ||
|
||
static Client client; | ||
|
||
@Deployment | ||
public static Archive<?> deploy() { | ||
WebArchive war = TestUtil.prepareArchive(DynamicFeatureTest.class.getSimpleName()); | ||
war.addClasses(GreetingInterceptor.class); | ||
return TestUtil.finishContainerPrepare(war, null, DynamicFeatureResource.class, AddDynamicFeature.class); | ||
} | ||
|
||
@BeforeClass | ||
public static void setup() { | ||
client = ClientBuilder.newClient(); | ||
} | ||
|
||
@AfterClass | ||
public static void cleanup() { | ||
client.close(); | ||
} | ||
|
||
/** | ||
* @tpTestDetails Test dynamic feature. | ||
* @tpSince RESTEasy 3.8.0 | ||
*/ | ||
@Test | ||
public void testDynamicFeature() { | ||
WebTarget target = client.target(PortProviderUtil.generateURL("/dynamic-feature/hello", DynamicFeatureTest.class.getSimpleName())); | ||
Entity<String> entity = Entity.entity( "Tomas", MediaType.TEXT_PLAIN); | ||
String response = target.request().post(entity, String.class); | ||
Assert.assertEquals("Hello Tomas !", response); | ||
LOG.info("Response: " + response); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...n-tests/src/test/java/org/jboss/resteasy/test/interceptor/resource/AddDynamicFeature.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.jboss.resteasy.test.interceptor.resource; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import javax.ws.rs.container.DynamicFeature; | ||
import javax.ws.rs.container.ResourceInfo; | ||
import javax.ws.rs.core.FeatureContext; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
@Provider | ||
public class AddDynamicFeature implements DynamicFeature { | ||
|
||
private static final Logger LOG = Logger.getLogger(AddDynamicFeature.class.getName()); | ||
|
||
@Override | ||
public void configure(ResourceInfo resourceInfo, FeatureContext context) { | ||
if (resourceInfo.getResourceMethod().getName().equals("hello")) { | ||
context.register(GreetingInterceptor.class); | ||
LOG.error("This should be happening exactly once"); | ||
} | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...ts/src/test/java/org/jboss/resteasy/test/interceptor/resource/DynamicFeatureResource.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.jboss.resteasy.test.interceptor.resource; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
|
||
@Path("/dynamic-feature") | ||
public class DynamicFeatureResource { | ||
|
||
@Path("/hello") | ||
@GET | ||
@POST | ||
@Produces("text/plain") | ||
@Consumes("text/plain") | ||
public String hello(String name) { | ||
return name; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...tests/src/test/java/org/jboss/resteasy/test/interceptor/resource/GreetingInterceptor.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.jboss.resteasy.test.interceptor.resource; | ||
|
||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.ext.WriterInterceptor; | ||
import javax.ws.rs.ext.WriterInterceptorContext; | ||
import java.io.IOException; | ||
|
||
public class GreetingInterceptor implements WriterInterceptor { | ||
|
||
@Override | ||
public void aroundWriteTo(WriterInterceptorContext context) | ||
throws IOException, WebApplicationException { | ||
String entity = (String) context.getEntity(); | ||
if (entity != null) { | ||
context.setEntity("Hello " + entity + " !"); | ||
} | ||
context.proceed(); | ||
} | ||
|
||
} |