Search in sources :

Example 1 with HttpClientHandler

use of net.morimekta.providence.client.HttpClientHandler in project providence by morimekta.

the class ProvidenceServletTest method testSimpleRequest.

@Test
public void testSimpleRequest() throws IOException, Failure {
    AtomicBoolean called = new AtomicBoolean();
    when(impl.test(any(Request.class))).thenAnswer(i -> {
        called.set(true);
        return new Response("response");
    });
    TestService.Iface client = new TestService.Client(new HttpClientHandler(this::endpoint, factory(), provider));
    Response response = client.test(new Request("request"));
    waitAtMost(Duration.ONE_HUNDRED_MILLISECONDS).untilTrue(called);
    assertNotNull(response);
    assertEquals("{text:\"response\"}", response.asString());
    verify(instrumentation).onComplete(anyDouble(), any(PServiceCall.class), any(PServiceCall.class));
    verifyNoMoreInteractions(instrumentation);
}
Also used : HttpResponse(com.google.api.client.http.HttpResponse) Response(net.morimekta.test.providence.service.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestService(net.morimekta.test.providence.service.TestService) PServiceCall(net.morimekta.providence.PServiceCall) HttpRequest(com.google.api.client.http.HttpRequest) Request(net.morimekta.test.providence.service.Request) HttpClientHandler(net.morimekta.providence.client.HttpClientHandler) Test(org.junit.Test)

Example 2 with HttpClientHandler

use of net.morimekta.providence.client.HttpClientHandler in project providence by morimekta.

the class ProvidenceServletTest method testSimpleRequest_exception.

@Test
public void testSimpleRequest_exception() throws IOException, Failure {
    AtomicBoolean called = new AtomicBoolean();
    when(impl.test(any(Request.class))).thenAnswer(i -> {
        called.set(true);
        throw Failure.builder().setText("failure").build();
    });
    TestService.Iface client = new TestService.Client(new HttpClientHandler(this::endpoint, factory(), provider));
    try {
        client.test(new Request("request"));
        fail("No exception");
    } catch (Failure ex) {
        assertEquals("failure", ex.getText());
    }
    waitAtMost(Duration.ONE_HUNDRED_MILLISECONDS).untilTrue(called);
    verify(instrumentation).onComplete(anyDouble(), any(PServiceCall.class), any(PServiceCall.class));
    verifyNoMoreInteractions(instrumentation);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestService(net.morimekta.test.providence.service.TestService) PServiceCall(net.morimekta.providence.PServiceCall) HttpRequest(com.google.api.client.http.HttpRequest) Request(net.morimekta.test.providence.service.Request) HttpClientHandler(net.morimekta.providence.client.HttpClientHandler) Failure(net.morimekta.test.providence.service.Failure) Test(org.junit.Test)

Example 3 with HttpClientHandler

use of net.morimekta.providence.client.HttpClientHandler in project providence by morimekta.

the class DropWizardIT method testProvidenceServlet.

@Test
public void testProvidenceServlet() throws IOException, CalculateException {
    // This test is just to prove that the providence servlet can be used in dropwizard too.
    Calculator.Iface client = new Calculator.Client(new HttpClientHandler(() -> new GenericUrl(uri("test")), factory(), new DefaultSerializerProvider()));
    Operand result = client.calculate(new Operation(Operator.ADD, list(withNumber(52d), withImaginary(new Imaginary(1d, -1d)), withNumber(15d))));
    assertThat(debugString(result), is(equalTo("{\n" + "  imaginary = {\n" + "    v = 68\n" + "    i = -1\n" + "  }\n" + "}")));
}
Also used : Operand(net.morimekta.test.providence.jax.rs.calculator.Operand) Calculator(net.morimekta.test.providence.jax.rs.calculator.Calculator) DefaultSerializerProvider(net.morimekta.providence.serializer.DefaultSerializerProvider) Operand.withImaginary(net.morimekta.test.providence.jax.rs.calculator.Operand.withImaginary) Imaginary(net.morimekta.test.providence.jax.rs.number.Imaginary) GenericUrl(com.google.api.client.http.GenericUrl) Operation(net.morimekta.test.providence.jax.rs.calculator.Operation) Client(javax.ws.rs.client.Client) HttpClientHandler(net.morimekta.providence.client.HttpClientHandler) Test(org.junit.Test)

Example 4 with HttpClientHandler

use of net.morimekta.providence.client.HttpClientHandler in project providence by morimekta.

the class RPCOptions method getHandler.

public PServiceCallHandler getHandler() {
    Serializer serializer = getSerializer(format);
    URI uri = URI.create(endpoint);
    if (uri.getScheme() == null || uri.getScheme().length() == 0) {
        throw new ArgumentException("No protocol on URI: " + endpoint);
    }
    if (uri.getScheme().startsWith("thrift")) {
        if (// Must have host and port.
        (uri.getPort() < 1) || (uri.getHost() == null || uri.getHost().length() == 0) || // No path, query or fragment.
        (uri.getFragment() != null && uri.getFragment().length() > 0) || (uri.getQuery() != null && uri.getQuery().length() > 0) || (uri.getPath() != null && uri.getPath().length() > 0)) {
            throw new ArgumentException("Illegal thrift URI: " + endpoint);
        }
        InetSocketAddress address = new InetSocketAddress(uri.getHost(), uri.getPort());
        switch(uri.getScheme()) {
            case "thrift":
                return new SocketClientHandler(serializer, address, connect_timeout, read_timeout);
            case "thrift+nonblocking":
                return new NonblockingSocketClientHandler(serializer, address, connect_timeout, read_timeout);
            default:
                throw new ArgumentException("Unknown thrift protocol " + uri.getScheme());
        }
    }
    GenericUrl url = new GenericUrl(endpoint);
    Map<String, String> hdrs = new HashMap<>();
    for (String hdr : headers) {
        String[] parts = hdr.split("[:]", 2);
        if (parts.length != 2) {
            throw new ArgumentException("Invalid headers param: " + hdr);
        }
        hdrs.put(parts[0].trim(), parts[1].trim());
    }
    HttpTransport transport = new NetHttpTransport();
    HttpRequestFactory factory = transport.createRequestFactory(new SetHeadersInitializer(hdrs, connect_timeout, read_timeout));
    SerializerProvider serializerProvider = new ThriftSerializerProvider(serializer.mediaType());
    return new HttpClientHandler(() -> url, factory, serializerProvider);
}
Also used : SocketClientHandler(net.morimekta.providence.thrift.client.SocketClientHandler) NonblockingSocketClientHandler(net.morimekta.providence.thrift.client.NonblockingSocketClientHandler) NonblockingSocketClientHandler(net.morimekta.providence.thrift.client.NonblockingSocketClientHandler) HashMap(java.util.HashMap) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) InetSocketAddress(java.net.InetSocketAddress) GenericUrl(com.google.api.client.http.GenericUrl) URI(java.net.URI) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) SetHeadersInitializer(net.morimekta.providence.tools.rpc.utils.SetHeadersInitializer) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) ArgumentException(net.morimekta.console.args.ArgumentException) ThriftSerializerProvider(net.morimekta.providence.thrift.ThriftSerializerProvider) SerializerProvider(net.morimekta.providence.serializer.SerializerProvider) HttpClientHandler(net.morimekta.providence.client.HttpClientHandler) ThriftSerializerProvider(net.morimekta.providence.thrift.ThriftSerializerProvider) Serializer(net.morimekta.providence.serializer.Serializer)

Aggregations

HttpClientHandler (net.morimekta.providence.client.HttpClientHandler)4 Test (org.junit.Test)3 GenericUrl (com.google.api.client.http.GenericUrl)2 HttpRequest (com.google.api.client.http.HttpRequest)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 PServiceCall (net.morimekta.providence.PServiceCall)2 Request (net.morimekta.test.providence.service.Request)2 TestService (net.morimekta.test.providence.service.TestService)2 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 HttpResponse (com.google.api.client.http.HttpResponse)1 HttpTransport (com.google.api.client.http.HttpTransport)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Client (javax.ws.rs.client.Client)1 ArgumentException (net.morimekta.console.args.ArgumentException)1 DefaultSerializerProvider (net.morimekta.providence.serializer.DefaultSerializerProvider)1 Serializer (net.morimekta.providence.serializer.Serializer)1