-
Notifications
You must be signed in to change notification settings - Fork 888
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Resteasy 2617] mp rest client context resolver
- Loading branch information
Showing
7 changed files
with
365 additions
and
6 deletions.
There are no files selected for viewing
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
61 changes: 61 additions & 0 deletions
61
...ests/src/test/java/org/jboss/resteasy/test/microprofile/restclient/JsonBindingMPTest.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,61 @@ | ||
package org.jboss.resteasy.test.microprofile.restclient; | ||
|
||
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.microprofile.client.RestClientBuilderImpl; | ||
import org.jboss.resteasy.test.microprofile.restclient.resource.JsonBindingMPService; | ||
import org.jboss.resteasy.test.microprofile.restclient.resource.JsonBindingMPServiceIntf; | ||
import org.jboss.resteasy.test.providers.jsonb.basic.resource.Dog; | ||
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.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.net.URI; | ||
|
||
/** | ||
* @tpSubChapter MicroProfile rest client | ||
* @tpChapter Integration tests | ||
* @tpTestCaseDetails Show JSON-Binding is supported. | ||
* @tpSince RESTEasy 4.6.0 | ||
*/ | ||
@RunWith(Arquillian.class) | ||
@RunAsClient | ||
public class JsonBindingMPTest { | ||
protected static final Logger LOG = Logger.getLogger(JsonBindingMPTest.class.getName()); | ||
private static final String WAR_SERVICE = "jsonBinding_service"; | ||
|
||
@Deployment(name=WAR_SERVICE) | ||
public static Archive<?> serviceDeploy() { | ||
WebArchive war = TestUtil.prepareArchive(WAR_SERVICE); | ||
war.addClasses(JsonBindingMPService.class, | ||
Dog.class); | ||
return TestUtil.finishContainerPrepare(war, null, null); | ||
} | ||
|
||
private static String generateURL(String path, String deployName) { | ||
return PortProviderUtil.generateURL(path, deployName); | ||
} | ||
|
||
@Test | ||
public void testDog() { | ||
RestClientBuilderImpl builder = new RestClientBuilderImpl(); | ||
JsonBindingMPServiceIntf jsonBindingMPServiceIntf = builder | ||
.baseUri(URI.create(generateURL("", WAR_SERVICE))) | ||
.build(JsonBindingMPServiceIntf.class); | ||
|
||
try { | ||
Dog dog = new Dog("Rex", "german shepherd"); | ||
Dog response = jsonBindingMPServiceIntf.getDog(dog); | ||
Assert.assertTrue(response.getName().equals("Jethro")); | ||
Assert.assertTrue(response.getSort().equals("stafford")); | ||
} catch (Exception e) { | ||
Assert.fail("Exception thrown: " + e); | ||
} | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
...tion-tests/src/test/java/org/jboss/resteasy/test/microprofile/restclient/JsonpMPtest.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,128 @@ | ||
package org.jboss.resteasy.test.microprofile.restclient; | ||
|
||
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.microprofile.client.RestClientBuilderImpl; | ||
import org.jboss.resteasy.test.microprofile.restclient.resource.JsonpMPService; | ||
import org.jboss.resteasy.test.microprofile.restclient.resource.JsonpMPServiceIntf; | ||
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.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonArray; | ||
import javax.json.JsonNumber; | ||
import javax.json.JsonObject; | ||
import javax.json.JsonString; | ||
import javax.json.JsonStructure; | ||
import java.net.URI; | ||
|
||
/** | ||
* @tpSubChapter MicroProfile rest client | ||
* @tpChapter Integration tests | ||
* @tpTestCaseDetails Show JSON-P is supported. | ||
* @tpSince RESTEasy 4.6.0 | ||
*/ | ||
@RunWith(Arquillian.class) | ||
@RunAsClient | ||
public class JsonpMPtest { | ||
protected static final Logger LOG = Logger.getLogger(JsonpMPtest.class.getName()); | ||
private static final String WAR_SERVICE = "jsonP_service"; | ||
|
||
@Deployment(name=WAR_SERVICE) | ||
public static Archive<?> serviceDeploy() { | ||
WebArchive war = TestUtil.prepareArchive(WAR_SERVICE); | ||
war.addClasses(JsonpMPService.class); | ||
return TestUtil.finishContainerPrepare(war, null, null); | ||
} | ||
|
||
static JsonpMPServiceIntf jsonpMPServiceIntf; | ||
@Before | ||
public void before() throws Exception { | ||
RestClientBuilderImpl builder = new RestClientBuilderImpl(); | ||
jsonpMPServiceIntf = builder | ||
.baseUri(URI.create(generateURL("", WAR_SERVICE))) | ||
.build(JsonpMPServiceIntf.class); | ||
} | ||
|
||
private static String generateURL(String path, String deployName) { | ||
return PortProviderUtil.generateURL(path, deployName); | ||
} | ||
|
||
@Test | ||
public void testObject() { | ||
|
||
JsonObject obj = Json.createObjectBuilder() | ||
.add("name", "Bill") | ||
.add("id", 10001) | ||
.build(); | ||
|
||
JsonObject response = jsonpMPServiceIntf.object(obj); | ||
Assert.assertTrue("JsonObject from the response doesn't contain field 'name'", | ||
response.containsKey("name")); | ||
Assert.assertEquals("JsonObject from the response doesn't contain correct value for the field 'name'", | ||
response.getJsonString("name").getString(), "Bill"); | ||
Assert.assertTrue("JsonObject from the response doesn't contain field 'id'", | ||
response.containsKey("id")); | ||
Assert.assertEquals("JsonObject from the response doesn't contain correct value for the field 'id'", | ||
response.getJsonNumber("id").longValue(), 10001); | ||
} | ||
|
||
@Test | ||
public void testStructure() { | ||
JsonStructure structure = (JsonStructure) Json.createObjectBuilder().add("name", "Bill").build(); | ||
JsonStructure response = jsonpMPServiceIntf.object(structure); | ||
JsonObject obj = (JsonObject) response; | ||
Assert.assertTrue("JsonObject from the response doesn't contain field 'name'", | ||
obj.containsKey("name")); | ||
Assert.assertEquals("JsonObject from the response doesn't contain correct value for the field 'name'", | ||
obj.getJsonString("name").getString(), "Bill"); | ||
} | ||
|
||
@Test | ||
public void testJsonNumber() { | ||
JsonNumber jsonNumber = Json.createValue(100); | ||
JsonNumber response = jsonpMPServiceIntf.testNumber(jsonNumber); | ||
Assert.assertTrue("JsonNumber object with 200 value is expected", | ||
response.intValue() == 200); | ||
} | ||
|
||
@Test | ||
public void testArray() { | ||
JsonArray array = Json.createArrayBuilder() | ||
.add(Json.createObjectBuilder().add("name", "Bill").build()) | ||
.add(Json.createObjectBuilder().add("name", "Monica").build()) | ||
.build(); | ||
|
||
JsonArray response = jsonpMPServiceIntf.array(array); | ||
Assert.assertEquals("JsonArray from the response doesn't contain two elements as it should", | ||
2, response.size()); | ||
JsonObject obj = response.getJsonObject(0); | ||
Assert.assertTrue("JsonObject[0] from the response doesn't contain field 'name'", | ||
obj.containsKey("name")); | ||
Assert.assertEquals("JsonObject[0] from the response doesn't contain correct value for the field 'name'", | ||
obj.getJsonString("name").getString(), "Bill"); | ||
obj = response.getJsonObject(1); | ||
Assert.assertTrue("JsonObject[1] from the response doesn't contain field 'name'", | ||
obj.containsKey("name")); | ||
Assert.assertEquals("JsonObject[1] from the response doesn't contain correct value for the field 'name'", | ||
obj.getJsonString("name").getString(), "Monica"); | ||
} | ||
|
||
@Test | ||
public void testJsonString() throws Exception { | ||
|
||
JsonString jsonString = Json.createValue("Resteasy"); | ||
JsonString response = jsonpMPServiceIntf.testString(jsonString); | ||
|
||
Assert.assertTrue("JsonString object with Hello Resteasy value is expected", | ||
response.getString().equals("Hello Resteasy")); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...t/java/org/jboss/resteasy/test/microprofile/restclient/resource/JsonBindingMPService.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.microprofile.restclient.resource; | ||
|
||
import org.jboss.resteasy.test.providers.jsonb.basic.resource.Dog; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
|
||
@Path("/jsonBindingService") | ||
public class JsonBindingMPService { | ||
|
||
@Path("/dog") | ||
@POST | ||
@Produces("application/json") | ||
@Consumes("application/json") | ||
public Dog getDog(Dog dog) throws Exception { | ||
dog.setNameAndSort("Jethro", "stafford"); | ||
return dog; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...va/org/jboss/resteasy/test/microprofile/restclient/resource/JsonBindingMPServiceIntf.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,22 @@ | ||
package org.jboss.resteasy.test.microprofile.restclient.resource; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
import org.jboss.resteasy.test.providers.jsonb.basic.resource.Dog; | ||
|
||
import javax.inject.Singleton; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
|
||
@RegisterRestClient(baseUri ="http://localhost:8080/jsonBinding_service") | ||
@Path("/jsonBindingService") | ||
@Singleton | ||
public interface JsonBindingMPServiceIntf { | ||
|
||
@Path("/dog") | ||
@POST | ||
@Produces("application/json") | ||
@Consumes("application/json") | ||
Dog getDog(Dog dog) throws Exception; | ||
} |
82 changes: 82 additions & 0 deletions
82
...rc/test/java/org/jboss/resteasy/test/microprofile/restclient/resource/JsonpMPService.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,82 @@ | ||
package org.jboss.resteasy.test.microprofile.restclient.resource; | ||
|
||
import org.junit.Assert; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonArray; | ||
import javax.json.JsonNumber; | ||
import javax.json.JsonObject; | ||
import javax.json.JsonString; | ||
import javax.json.JsonStructure; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
@Path("/jsonpService") | ||
public class JsonpMPService { | ||
@Path("array") | ||
@POST | ||
@Produces("application/json") | ||
@Consumes("application/json") | ||
public JsonArray array(JsonArray array) { | ||
Assert.assertEquals("The request didn't contain 2 json elements", 2, array.size()); | ||
JsonObject obj = array.getJsonObject(0); | ||
Assert.assertTrue("The field 'name' didn't propagated correctly from the request for object[0]", | ||
obj.containsKey("name")); | ||
Assert.assertEquals("The value of field 'name' didn't propagated correctly from the request for object[0]", | ||
obj.getJsonString("name").getString(), "Bill"); | ||
obj = array.getJsonObject(1); | ||
Assert.assertTrue("The field 'name' didn't propagated correctly from the request for object[1]", | ||
obj.containsKey("name")); | ||
Assert.assertEquals("The value of field 'name' didn't propagated correctly from the request for object[1]", | ||
obj.getJsonString("name").getString(), "Monica"); | ||
return array; | ||
} | ||
|
||
@Path("object") | ||
@POST | ||
@Produces("application/json") | ||
@Consumes("application/json") | ||
public JsonObject object(JsonObject obj) { | ||
Assert.assertTrue("The field 'name' didn't propagated correctly from the request", obj.containsKey("name")); | ||
Assert.assertEquals("The value of field 'name' didn't propagated correctly from the request" | ||
, obj.getJsonString("name").getString(), "Bill"); | ||
if (obj.containsKey("id")) { | ||
Assert.assertEquals("The value of field 'id' didn't propagated correctly from the request" | ||
, obj.getJsonNumber("id").longValue(), 10001); | ||
} | ||
return obj; | ||
} | ||
|
||
@Path("structure") | ||
@POST | ||
@Produces("application/json") | ||
@Consumes("application/json") | ||
public JsonStructure object(JsonStructure struct) { | ||
JsonObject obj = (JsonObject) struct; | ||
Assert.assertTrue("The field 'name' didn't propagated correctly from the request", obj.containsKey("name")); | ||
Assert.assertEquals("The value of field 'name' didn't propagated correctly from the request", | ||
obj.getJsonString("name").getString(), "Bill"); | ||
return obj; | ||
} | ||
|
||
|
||
@Path("number") | ||
@POST | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public JsonNumber testNumber(JsonNumber number) { | ||
return Json.createValue(number.intValue() + 100); | ||
} | ||
|
||
@Path("string") | ||
@POST | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public JsonString testString(JsonString string) { | ||
return Json.createValue("Hello " + string.getString()); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...est/java/org/jboss/resteasy/test/microprofile/restclient/resource/JsonpMPServiceIntf.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,50 @@ | ||
package org.jboss.resteasy.test.microprofile.restclient.resource; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
import javax.inject.Singleton; | ||
import javax.json.JsonArray; | ||
import javax.json.JsonNumber; | ||
import javax.json.JsonObject; | ||
import javax.json.JsonString; | ||
import javax.json.JsonStructure; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
@RegisterRestClient(baseUri ="http://localhost:8080/jsonP_service") | ||
@Path("/jsonpService") | ||
@Singleton | ||
public interface JsonpMPServiceIntf { | ||
@Path("array") | ||
@POST | ||
@Produces("application/json") | ||
@Consumes("application/json") | ||
JsonArray array(JsonArray array); | ||
|
||
@Path("object") | ||
@POST | ||
@Produces("application/json") | ||
@Consumes("application/json") | ||
JsonObject object(JsonObject obj); | ||
|
||
@Path("structure") | ||
@POST | ||
@Produces("application/json") | ||
@Consumes("application/json") | ||
JsonStructure object(JsonStructure struct); | ||
|
||
@Path("number") | ||
@POST | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
JsonNumber testNumber(JsonNumber number); | ||
|
||
@Path("string") | ||
@POST | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
JsonString testString(JsonString string); | ||
} |