Skip to content

Commit

Permalink
[Resteasy 2617] mp rest client context resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
rsearls authored and asoldano committed Sep 24, 2020
1 parent 87f03cd commit 86fe7a4
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 6 deletions.
7 changes: 1 addition & 6 deletions resteasy-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<version.microprofile.restclient>2.0-RC2</version.microprofile.restclient>

<!-- todo rls move this property when updating all versions for MP-rest-client 2.0
and return version to ${version.microprofile.restclient}
-->
<version.microprofile.restclient.tck>1.4.0</version.microprofile.restclient.tck>

Expand Down Expand Up @@ -641,12 +642,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.eclipse.microprofile.rest.client</groupId>
<artifactId>microprofile-rest-client-tck</artifactId>
<version>${version.microprofile.restclient}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config</artifactId>
Expand Down
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);
}
}
}
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"));
}
}
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;
}
}
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;
}
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());
}

}
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);
}

0 comments on commit 86fe7a4

Please sign in to comment.