Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.hamcrest.CoreMatchers;
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.resteasy.category.ExpectedFailing;
import org.jboss.resteasy.category.Jaxrs21;
import org.jboss.resteasy.test.client.resource.JAXRS21SyncInvokeResource;
import org.jboss.resteasy.util.HttpResponseCodes;
Expand Down Expand Up @@ -46,6 +49,10 @@ public void after() throws Exception {
client.close();
}

/**
* @tpTestDetails Client sends http PATCH request with invoke() method
* @tpSince RESTEasy 3.5.0
*/
@Test
public void testMethods() throws Exception {
{
Expand All @@ -60,6 +67,10 @@ public void testMethods() throws Exception {
}
}

/**
* @tpTestDetails Client sends http PATCH request with method() method
* @tpSince RESTEasy 3.5.0
*/
@Test
public void testInvoke() throws Exception {
{
Expand All @@ -73,4 +84,40 @@ public void testInvoke() throws Exception {
Assert.assertEquals("patch hello", entity);
}
}

/**
* @tpTestDetails Check that PATCH is present in OPTIONS response if the resource supports PATCH method
* @tpSince RESTEasy 3.5.0
*/
@Test
public void testOptionsContainsAllowPatch() throws Exception {
Response res = client.target(generateURL("/test")).request().options();
Assert.assertThat(res.getHeaderString("Allow"), CoreMatchers.containsString("PATCH"));
res.close();
}

/**
* @tpTestDetails Check that OPTIONS response contains Accept-Patch header with supported PATCH format descriptors
* @tpSince RESTEasy 3.5.0
*/
@Test
@Category(ExpectedFailing.class) // RESTEASY-1822
public void testOptionsContainsAcceptPatch() throws Exception {
Response res = client.target(generateURL("/test")).request().options();
Assert.assertEquals("text/plain;charset=UTF-8", res.getHeaderString("Accept-Patch"));
res.close();
}

/**
* @tpTestDetails Check http headers in the response after successful PATCH request
* @tpSince RESTEasy 3.5.0
*/
@Test
public void testPatchHeaders() throws Exception {
Response res = client.target(generateURL("/test")).request().method(HttpMethod.PATCH, Entity.text("hello"));
MultivaluedMap<String, String> stringHeaders = res.getStringHeaders();
stringHeaders.forEach((k,v)-> {
if (k.equals("Content-type")) Assert.assertEquals("text/plain;charset=UTF-8", v);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package org.jboss.resteasy.test.resource.patch;

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.resteasy.category.ExpectedFailing;
import org.jboss.resteasy.category.Jaxrs21;
import org.jboss.resteasy.util.HttpResponseCodes;
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.experimental.categories.Category;
import org.junit.runner.RunWith;

import javax.json.Json;
import javax.ws.rs.HttpMethod;
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;
import javax.ws.rs.core.Response;

@RunWith(Arquillian.class)
@RunAsClient
@Category(Jaxrs21.class)
public class PatchErrorHandlingTest {
static Client client;

@BeforeClass
public static void setup() {
client = ClientBuilder.newClient();
}

@AfterClass
public static void close() {
client.close();
client = null;
}

@Deployment
public static Archive<?> deploy() {
WebArchive war = TestUtil.prepareArchive(PatchErrorHandlingTest.class.getSimpleName());
return TestUtil.finishContainerPrepare(war, null, StudentResource.class, Student.class);
}

private String generateURL(String path) {
return PortProviderUtil.generateURL(path, PatchErrorHandlingTest.class.getSimpleName());
}

/**
* @tpTestDetails When the server determines that the patch document provided by the client is not properly formatted,
* it SHOULD return a 400 (Bad Request) response.
* @tpSince RESTEasy 3.5.0
*/
@Test
@Category(ExpectedFailing.class) // RESTEASY-1823
public void testMalformedPatchDocument() throws Exception {
WebTarget base = client.target(generateURL("/students"));
Student newStudent = new Student().setId(1L).setFirstName("Taylor").setSchool("school1");
Response response = base.request().post(Entity.<Student>entity(newStudent, MediaType.APPLICATION_JSON_TYPE));

WebTarget patchTarget = client.target(generateURL("/students/1"));
javax.json.JsonArray patchRequest = Json.createArrayBuilder()
.add(Json.createObjectBuilder().add("op", "copyyy").add("from", "/firstName").add("path", "/lastName").build())
.build();
Response res = patchTarget.request().build(HttpMethod.PATCH, Entity.entity(patchRequest, MediaType.APPLICATION_JSON_PATCH_JSON)).invoke();
Assert.assertEquals(HttpResponseCodes.SC_BAD_REQUEST, res.getStatus());
}

/**
* @tpTestDetails Client sends PATCH request in the format which is not supported for the resource.
* Server should return 415 (Unsupported media type)
* @tpSince RESTEasy 3.5.0
*/
@Test
public void testUnsupportedPatchDocument() throws Exception {
WebTarget patchTarget = client.target(generateURL("/students/1"));
Student student = new Student().setFirstName("test");
Response res = patchTarget.request().build(HttpMethod.PATCH, Entity.entity(student, MediaType.APPLICATION_JSON_TYPE)).invoke();
Assert.assertEquals(HttpResponseCodes.SC_UNSUPPORTED_MEDIA_TYPE, res.getStatus());
}

/**
* @tpTestDetails Client sends valid Patch request with with valid format descriptor, but the resource doesn't exists.
* Server should return 404 (Not found).
* @tpSince RESTEasy 3.5.0
*/
@Test
@Category(ExpectedFailing.class) // RESTEASY-1823
public void testResourceNotFound() throws Exception {
WebTarget base = client.target(generateURL("/students"));
Student newStudent = new Student().setId(1L).setFirstName("Taylor").setSchool("school1");
Response response = base.request().post(Entity.<Student>entity(newStudent, MediaType.APPLICATION_JSON_TYPE));

WebTarget patchTarget = client.target(generateURL("/students/1088"));
javax.json.JsonArray patchRequest = Json.createArrayBuilder()
.add(Json.createObjectBuilder().add("op", "copy").add("from", "/firstName").add("path", "/lastName").build())
.build();
Response res = patchTarget.request().build(HttpMethod.PATCH, Entity.entity(patchRequest, MediaType.APPLICATION_JSON_PATCH_JSON)).invoke();
Assert.assertEquals(HttpResponseCodes.SC_NOT_FOUND, res.getStatus());
}

/**
* @tpTestDetails Client sends Patch request to patch property of the object which doesn't exists.
* Server should return 409 (Conflict)
* @tpSince RESTEasy 3.5.0
*/
@Test
@Category(ExpectedFailing.class) // RESTEASY-1823
public void testConflictingState() throws Exception {
WebTarget base = client.target(generateURL("/students"));
Student newStudent = new Student().setId(1L).setFirstName("Taylor").setSchool("school1");
Response response = base.request().post(Entity.<Student>entity(newStudent, MediaType.APPLICATION_JSON_TYPE));

WebTarget patchTarget = client.target(generateURL("/students/1"));
javax.json.JsonArray patchRequest = Json.createArrayBuilder()
.add(Json.createObjectBuilder().add("op", "replace").add("path", "/wrongProperty").add("value", "John").build())
.build();
Response res = patchTarget.request().build(HttpMethod.PATCH, Entity.entity(patchRequest, MediaType.APPLICATION_JSON_PATCH_JSON)).invoke();
Assert.assertEquals(HttpResponseCodes.SC_CONFLICT, res.getStatus());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,70 +29,64 @@
@RunWith(Arquillian.class)
@RunAsClient
@Category(Jaxrs21.class)
public class StudentPatchTest
{
public class StudentPatchTest {

static Client client;
static Client client;

@BeforeClass
public static void setup()
{
client = ClientBuilder.newClient();
}
@BeforeClass
public static void setup() {
client = ClientBuilder.newClient();
}

@AfterClass
public static void close()
{
client.close();
client = null;
}
@AfterClass
public static void close() {
client.close();
client = null;
}

@Deployment
public static Archive<?> deploy()
{
WebArchive war = TestUtil.prepareArchive(StudentPatchTest.class.getSimpleName());
return TestUtil.finishContainerPrepare(war, null, StudentResource.class, Student.class);
}
@Deployment
public static Archive<?> deploy() {
WebArchive war = TestUtil.prepareArchive(StudentPatchTest.class.getSimpleName());
return TestUtil.finishContainerPrepare(war, null, StudentResource.class, Student.class);
}

private String generateURL(String path)
{
return PortProviderUtil.generateURL(path, StudentPatchTest.class.getSimpleName());
}
private String generateURL(String path) {
return PortProviderUtil.generateURL(path, StudentPatchTest.class.getSimpleName());
}

@Test
//@Ignore
public void testPatchStudent() throws Exception
{
ResteasyClient client = new ResteasyClientBuilder().connectionPoolSize(10).build();

WebTarget base = client.target(generateURL("/students"));
//add a student, first name is Taylor and school is school1, other fields is null.
Student newStudent = new Student().setId(1L).setFirstName("Taylor").setSchool("school1");
Response response = base.request().post(Entity.<Student> entity(newStudent, MediaType.APPLICATION_JSON_TYPE));
Student s = response.readEntity(Student.class);
Assert.assertNotNull("Add student failed", s);
Assert.assertEquals("Taylor", s.getFirstName());
Assert.assertNull("Last name is not null", s.getLastName());
Assert.assertEquals("school1", s.getSchool());
Assert.assertNull("Gender is not null", s.getGender());
@Test
//@Ignore
public void testPatchStudent() throws Exception {
ResteasyClient client = new ResteasyClientBuilder().connectionPoolSize(10).build();

//patch a student, after patch we can get a male student named John Taylor and school is null.
WebTarget patchTarget = client.target(generateURL("/students/1"));
javax.json.JsonArray patchRequest = Json.createArrayBuilder()
.add(Json.createObjectBuilder().add("op", "copy").add("from", "/firstName").add("path", "/lastName").build())
.add(Json.createObjectBuilder().add("op", "replace").add("path", "/firstName").add("value", "John").build())
.add(Json.createObjectBuilder().add("op", "remove").add("path", "/school").build())
.add(Json.createObjectBuilder().add("op", "add").add("path", "/gender").add("value", "male").build())
.build();
patchTarget.request().build(HttpMethod.PATCH, Entity.entity(patchRequest, MediaType.APPLICATION_JSON_PATCH_JSON)).invoke();
WebTarget base = client.target(generateURL("/students"));
//add a student, first name is Taylor and school is school1, other fields is null.
Student newStudent = new Student().setId(1L).setFirstName("Taylor").setSchool("school1");
Response response = base.request().post(Entity.<Student>entity(newStudent, MediaType.APPLICATION_JSON_TYPE));
Student s = response.readEntity(Student.class);
Assert.assertNotNull("Add student failed", s);
Assert.assertEquals("Taylor", s.getFirstName());
Assert.assertNull("Last name is not null", s.getLastName());
Assert.assertEquals("school1", s.getSchool());
Assert.assertNull("Gender is not null", s.getGender());

//verify the patch update result
WebTarget getTarget = client.target(generateURL("/students/1"));
Response getResponse = getTarget.request().get();
Student patchedStudent = getResponse.readEntity(Student.class);
Assert.assertEquals("Expected lastname is changed to Taylor", "Taylor", patchedStudent.getLastName());
Assert.assertEquals("Expected firstname is replaced from Taylor to John", "John", patchedStudent.getFirstName());
Assert.assertEquals("Expected school is null", null, patchedStudent.getSchool());
Assert.assertEquals("Add gender", "male", patchedStudent.getGender());
}
//patch a student, after patch we can get a male student named John Taylor and school is null.
WebTarget patchTarget = client.target(generateURL("/students/1"));
javax.json.JsonArray patchRequest = Json.createArrayBuilder()
.add(Json.createObjectBuilder().add("op", "copy").add("from", "/firstName").add("path", "/lastName").build())
.add(Json.createObjectBuilder().add("op", "replace").add("path", "/firstName").add("value", "John").build())
.add(Json.createObjectBuilder().add("op", "remove").add("path", "/school").build())
.add(Json.createObjectBuilder().add("op", "add").add("path", "/gender").add("value", "male").build())
.build();
patchTarget.request().build(HttpMethod.PATCH, Entity.entity(patchRequest, MediaType.APPLICATION_JSON_PATCH_JSON)).invoke();

//verify the patch update result
WebTarget getTarget = client.target(generateURL("/students/1"));
Response getResponse = getTarget.request().get();
Student patchedStudent = getResponse.readEntity(Student.class);
Assert.assertEquals("Expected lastname is changed to Taylor", "Taylor", patchedStudent.getLastName());
Assert.assertEquals("Expected firstname is replaced from Taylor to John", "John", patchedStudent.getFirstName());
Assert.assertEquals("Expected school is null", null, patchedStudent.getSchool());
Assert.assertEquals("Add gender", "male", patchedStudent.getGender());
}
}