Skip to content

Commit c35f365

Browse files
committed
add patch methods for stub RestClient
1 parent 496b699 commit c35f365

4 files changed

Lines changed: 68 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
- Add ApacheTestClientFactory: useful to support PATCH methods on jdk > 16
55
- Add `useApacheClient` (shortcut) configuration into `@TestGuiceyApp` and `@TestDropwizardApp`
66
to simplify usage of ApacheTestClientFactory with annotations
7-
- Add `useApacheClient()` (shortcut) method into extension and generic builders
7+
- Add `useApacheClient()` (shortcut) method into extension and generic builders
8+
* @StubRest RestClient:
9+
- Add patch method shortcuts
810

911
### 7.2.1 (2025-05-12)
1012
* Fix NoClassDefFoundError on guicey startup due to junit classes leak into core (#428)

dropwizard-guicey/src/main/java/ru/vyarus/dropwizard/guice/test/rest/RestClient.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ru.vyarus.dropwizard.guice.test.rest;
22

33
import jakarta.annotation.Nullable;
4+
import jakarta.ws.rs.HttpMethod;
45
import jakarta.ws.rs.client.Client;
56
import jakarta.ws.rs.client.Entity;
67
import jakarta.ws.rs.client.Invocation;
@@ -64,6 +65,7 @@
6465
* @author Vyacheslav Rusakov
6566
* @since 20.02.2025
6667
*/
68+
@SuppressWarnings("PMD.TooManyMethods")
6769
public class RestClient {
6870

6971
private static final List<Integer> DEFAULT_OK = Arrays.asList(HttpStatus.OK_200, HttpStatus.NO_CONTENT_204);
@@ -327,6 +329,54 @@ public <T> T put(final String path, final Entity<?> body, final @Nullable Class<
327329
}
328330
}
329331

332+
/**
333+
* Simple PATCH call shortcut. Provided path should include only the target rest path.
334+
* Body object assumed to be a json entity (would be serialized as json). For file sending use method with generic
335+
* entity {@link #put(String, jakarta.ws.rs.client.Entity, Class)}.
336+
* <p>
337+
* To provide additional headers and query params see {@link #defaultHeader(String, String)}
338+
* ({@link #defaultAccept(String...)}) and {@link #defaultQueryParam(String, String)}.
339+
* For void responses (result class null) checks response status correctness (see {@link #defaultOk(Integer...)}).
340+
* <p>
341+
* For response headers validation, use raw {@code Response res = request("/path/").put(Entity.json(body))}.
342+
* <p>
343+
* IMPORTANT: default jersey client would not support PATCH calls on jdk > 16 because it uses
344+
* {@link org.glassfish.jersey.client.HttpUrlConnectorProvider} which supports only HTTP 1.1. Use apache
345+
* client to workaround this problem on jdk > 16 (see useApacheClient extension option)
346+
*
347+
* @param path target path, relative to rest root
348+
* @param body put body object (serialized as json)
349+
* @param result result type (when null, accepts any 200 or 204 responses)
350+
* @param <T> result type
351+
* @return mapped result object or null (if class not declared)
352+
*/
353+
public <T> T patch(final String path, final @Nullable Object body, final @Nullable Class<T> result) {
354+
return patch(path, Entity.json(body), result);
355+
}
356+
357+
/**
358+
* Same as {@link #patch(String, Object, Class)}, but accepts generic entity. Useful for sending multipart
359+
* requests.
360+
* <p>
361+
* IMPORTANT: default jersey client would not support PATCH calls on jdk > 16 because it uses
362+
* {@link org.glassfish.jersey.client.HttpUrlConnectorProvider} which supports only HTTP 1.1. Use apache
363+
* client to workaround this problem on jdk > 16 (see useApacheClient extension option)
364+
*
365+
* @param path target path, relative to rest root
366+
* @param body post body object (serialized as json)
367+
* @param result result type (when null, accepts any 200 or 204 responses)
368+
* @param <T> result type
369+
* @return mapped result object or null (if class not declared)
370+
*/
371+
public <T> T patch(final String path, final Entity<?> body, final @Nullable Class<T> result) {
372+
if (result != null) {
373+
return request(path).method(HttpMethod.PATCH, body, result);
374+
} else {
375+
checkVoidResponse(() -> request(path).method(HttpMethod.PATCH, body));
376+
return null;
377+
}
378+
}
379+
330380
/**
331381
* Simple DELETE call shortcut. Provided path should include only the target rest path.
332382
* <p>

dropwizard-guicey/src/test/groovy/ru/vyarus/dropwizard/guice/test/jupiter/setup/rest/RestClientTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ru.vyarus.dropwizard.guice.test.jupiter.setup.rest;
22

33
import jakarta.ws.rs.WebApplicationException;
4+
import jakarta.ws.rs.client.Entity;
45
import org.junit.jupiter.api.Assertions;
56
import org.junit.jupiter.api.Test;
67
import ru.vyarus.dropwizard.guice.test.jupiter.TestGuiceyApp;
@@ -12,7 +13,7 @@
1213
* @author Vyacheslav Rusakov
1314
* @since 25.02.2025
1415
*/
15-
@TestGuiceyApp(RestStubApp.class)
16+
@TestGuiceyApp(value = RestStubApp.class, useApacheClient = true)
1617
public class RestClientTest {
1718

1819
@StubRest(disableDropwizardExceptionMappers = true)
@@ -38,6 +39,12 @@ void testClient() {
3839
rest.post("/1/foo", null, null);
3940
res = rest.put("/1/foo", "something", String.class);
4041
Assertions.assertEquals("foo", res);
42+
43+
// patch body can't be null
44+
rest.patch("/1/foo", Entity.text("sample"), null);
45+
res = rest.patch("/1/foo", "something", String.class);
46+
Assertions.assertEquals("foo", res);
47+
4148
rest.delete("/1/bar", null);
4249
}
4350

dropwizard-guicey/src/test/groovy/ru/vyarus/dropwizard/guice/test/rest/support/Resource1.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.base.Preconditions;
44
import jakarta.ws.rs.DELETE;
55
import jakarta.ws.rs.GET;
6+
import jakarta.ws.rs.PATCH;
67
import jakarta.ws.rs.POST;
78
import jakarta.ws.rs.PUT;
89
import jakarta.ws.rs.Path;
@@ -37,6 +38,12 @@ public String put(@PathParam("foo") String foo) {
3738
return foo;
3839
}
3940

41+
@PATCH
42+
@Path("/{foo}")
43+
public String patch(@PathParam("foo") String foo) {
44+
return foo;
45+
}
46+
4047
@DELETE
4148
@Path("/{foo}")
4249
public void delete(@PathParam("foo") String foo) {

0 commit comments

Comments
 (0)