Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RESTEASY-1998] Prepare test for resteasy.preferJacksonOverJsonB prop…
…erty for client on deployment
- Loading branch information
Showing
2 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
...est/java/org/jboss/resteasy/test/providers/jackson2/PreferJacksonOverJsonBClientTest.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,124 @@ | ||
package org.jboss.resteasy.test.providers.jackson2; | ||
|
||
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.plugins.server.servlet.ResteasyContextParameters; | ||
import org.jboss.resteasy.test.providers.jackson2.resource.MyEntity; | ||
import org.jboss.resteasy.test.providers.jackson2.resource.PreferJacksonOverJsonBClientResource; | ||
import org.jboss.resteasy.utils.PortProviderUtil; | ||
import org.jboss.resteasy.utils.TestUtil; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
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.WebTarget; | ||
import javax.ws.rs.core.Response; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.core.Is.is; | ||
|
||
/** | ||
* @tpSubChapter Check that resteasy.preferJacksonOverJsonB property works correctly on client used on server deployment. | ||
* @tpChapter Integration test | ||
* @tpSince RESTEasy 3.3 | ||
*/ | ||
@RunWith(Arquillian.class) | ||
@RunAsClient | ||
public class PreferJacksonOverJsonBClientTest { | ||
|
||
protected static final Logger LOG = Logger.getLogger(PreferJacksonOverJsonBClientTest.class.getName()); | ||
|
||
static Client client; | ||
|
||
private static final String WAR_WITH_JSONB = "war_with_jsonb"; | ||
private static final String WAR_WITH_JACKSON2 = "war_with_jackson2"; | ||
|
||
/** | ||
* Prepare deployment with resteasy.preferJacksonOverJsonB = false | ||
*/ | ||
@Deployment(name = WAR_WITH_JSONB) | ||
public static Archive<?> deployWithJsonB() { | ||
return deploy(WAR_WITH_JSONB, false); | ||
} | ||
|
||
/** | ||
* Prepare deployment with resteasy.preferJacksonOverJsonB = true | ||
*/ | ||
@Deployment(name = WAR_WITH_JACKSON2) | ||
public static Archive<?> deployWithoutJsonB() { | ||
return deploy(WAR_WITH_JACKSON2, true); | ||
} | ||
|
||
|
||
/** | ||
* Prepare deployment with specific archive name and specific resteasy.preferJacksonOverJsonB value | ||
*/ | ||
public static Archive<?> deploy(String archiveName, Boolean useJackson) { | ||
WebArchive war = TestUtil.prepareArchive(archiveName); | ||
war.addClass(MyEntity.class); | ||
Map<String, String> contextParams = new HashMap<>(); | ||
contextParams.put(ResteasyContextParameters.RESTEASY_PREFER_JACKSON_OVER_JSONB, useJackson.toString()); | ||
war.setManifest(new StringAsset("Manifest-Version: 1.0\n" | ||
+ "Dependencies: org.jboss.resteasy.resteasy-json-binding-provider services\n")); | ||
return TestUtil.finishContainerPrepare(war, contextParams, PreferJacksonOverJsonBClientResource.class); | ||
} | ||
|
||
@BeforeClass | ||
public static void setup() { | ||
client = ClientBuilder.newClient(); | ||
} | ||
|
||
@AfterClass | ||
public static void cleanup() { | ||
client.close(); | ||
} | ||
|
||
/** | ||
* @tpTestDetails Set resteasy.preferJacksonOverJsonB=false, check that JsonB is used on client | ||
* @tpSince RESTEasy 3.6.1.Final | ||
*/ | ||
@Test | ||
public void testJsonB() { | ||
test(WAR_WITH_JSONB); | ||
} | ||
|
||
/** | ||
* @tpTestDetails Set resteasy.preferJacksonOverJsonB=true, check that Jackson is used on client | ||
* @tpSince RESTEasy 3.6.1.Final | ||
*/ | ||
@Test | ||
public void testJackson() { | ||
test(WAR_WITH_JACKSON2); | ||
} | ||
|
||
|
||
/** | ||
* Perform HTTP call, end-point performs another HTTP call and receives Date object, converts it to String and send back in plain text | ||
* Json-B returns "2018-10-12T15:40:30.485Z[UTC]" but Jackson2 does not support Date so it returns unix time in seconds e.g. 1539358801324 | ||
*/ | ||
private void test(String deployment) { | ||
|
||
WebTarget target = client.target(PortProviderUtil.generateURL("/call", deployment)); | ||
Response response = target.request() | ||
.header("clientURL", PortProviderUtil.generateURL("/core", deployment)) | ||
.get(); | ||
String responseText = response.readEntity(String.class); | ||
LOG.info("Response: " + responseText); | ||
|
||
if (deployment.equals(WAR_WITH_JACKSON2)) { | ||
Assert.assertThat("Jackson2 not used.", responseText.matches("^[0-9]*$"), is(true)); | ||
} else { | ||
Assert.assertThat("Json-B not used.", responseText.matches("^[0-9]*$"), is(false)); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...jboss/resteasy/test/providers/jackson2/resource/PreferJacksonOverJsonBClientResource.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,47 @@ | ||
package org.jboss.resteasy.test.providers.jackson2.resource; | ||
|
||
|
||
import org.jboss.resteasy.spi.HttpResponseCodes; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.HeaderParam; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.client.Client; | ||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.client.WebTarget; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import java.util.Date; | ||
|
||
@Path("/") | ||
public class PreferJacksonOverJsonBClientResource { | ||
|
||
@GET | ||
@Path("core") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public Date core() { | ||
return new Date(); | ||
} | ||
|
||
@GET | ||
@Path("call") | ||
@Produces("text/plain") | ||
public String call(@HeaderParam("clientURL") String clientURL) throws Exception { | ||
|
||
Client client = ClientBuilder.newClient(); | ||
try { | ||
WebTarget target = client.target(clientURL); | ||
Response response = target.request().get(); | ||
|
||
if (response.getStatus() != HttpResponseCodes.SC_OK) { | ||
throw new Exception("Client in deployment received wrong response code"); | ||
} | ||
|
||
String responseText = response.readEntity(String.class); | ||
return responseText; | ||
} finally { | ||
client.close(); | ||
} | ||
} | ||
} |