aboutsummaryrefslogtreecommitdiffstats
path: root/jaxrs_client_utils/src/test/java/com/yahoo/vespa/jaxrs/client/HttpPatchTest.java
blob: 3270ac0141e2e293a3775dcf5da2b7b01f015329 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.jaxrs.client;

import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.jaxrs.annotation.PATCH;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;

/**
 * @author bakksjo
 */
public class HttpPatchTest extends JerseyTest {
    private final TestResource testResourceSingleton = new TestResource();

    @Override
    protected Application configure() {
        return new Application() {
            @Override
            public Set<Class<?>> getClasses() {
                return Collections.emptySet();
            }

            @Override
            public Set<Object> getSingletons() {
                return new HashSet<>(Arrays.asList(testResourceSingleton));
            }
        };
    }

    @Override
    protected void configureClient(final ClientConfig config) {
        config.getConfiguration().property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
    }

    private static final String REQUEST_BODY = "Hello there";

    @Test
    public void clientPatchRequest() throws Exception {
        final Response response = target(TestResourceApi.PATH)
                .request()
                .method("PATCH", Entity.text(REQUEST_BODY));
        assertEquals(REQUEST_BODY, testResourceSingleton.invocation.get(60, TimeUnit.SECONDS));
        assertEquals(REQUEST_BODY, response.readEntity(String.class));
    }

    @Test
    public void clientPatchRequestUsingProxyClass() throws Exception {
        final URI targetUri = target(TestResourceApi.PATH).getUri();
        final HostName apiHost = new HostName(targetUri.getHost());
        final int apiPort = targetUri.getPort();
        final String apiPath = targetUri.getPath();

        final JaxRsClientFactory jaxRsClientFactory = new JerseyJaxRsClientFactory();
        final JaxRsStrategyFactory factory = new JaxRsStrategyFactory(
                Collections.singleton(apiHost), apiPort, jaxRsClientFactory, "http");
        final JaxRsStrategy<TestResourceApi> client = factory.apiNoRetries(TestResourceApi.class, apiPath);

        final String responseBody;
        responseBody = client.apply(api -> api.doPatch(REQUEST_BODY));

        assertEquals(REQUEST_BODY, testResourceSingleton.invocation.get(60, TimeUnit.SECONDS));
        assertEquals(REQUEST_BODY, responseBody);
    }

    public interface TestResourceApi {
        String PATH = "test";

        @GET
        String getHello();

        @PATCH
        String doPatch(final String body);
    }

    @Path(TestResourceApi.PATH)
    public static class TestResource implements TestResourceApi {
        public final CompletableFuture<String> invocation = new CompletableFuture<>();

        @GET
        public String getHello() {
            return "Hello World!";
        }

        @PATCH
        public String doPatch(final String body) {
            invocation.complete(body);
            return body;
        }
    }
}