Search in sources :

Example 1 with Request

use of net.morimekta.test.providence.service.Request in project providence by morimekta.

the class ProvidenceServletTest method testBadRequest.

@Test
public void testBadRequest() throws IOException, Failure {
    HttpResponse response;
    response = post("text/url-encoded", "method=test");
    assertThat(response.getStatusCode(), is(HttpServletResponse.SC_BAD_REQUEST));
    assertThat(response.getStatusMessage(), is("Unknown content-type: text/url-encoded"));
    response = post("application/json", "{\"not\":\"usable\"}");
    assertThat(response.getStatusCode(), is(HttpServletResponse.SC_OK));
    assertThat(IOUtils.readString(response.getContent()), is("[\"\",\"exception\",0,{\"message\":\"Expected service call start ('['): but found '{'\",\"id\":\"PROTOCOL_ERROR\"}]"));
    response = post("application/json", "[\"foo\", 1, 1, {}]");
    assertThat(response.getStatusCode(), is(HttpServletResponse.SC_OK));
    assertThat(IOUtils.readString(response.getContent()), is("[\"foo\",\"exception\",1,{\"message\":\"No such method foo on service.TestService\",\"id\":\"UNKNOWN_METHOD\"}]"));
    response = post("application/json", "[\"test\", 2, 2, {}]");
    assertThat(response.getStatusCode(), is(HttpServletResponse.SC_OK));
    assertThat(IOUtils.readString(response.getContent()), is("[\"test\",\"exception\",2,{\"message\":\"Invalid service request call type: REPLY\",\"id\":\"INVALID_MESSAGE_TYPE\"}]"));
    response = post("application/json", "[\"test\", \"blurb\", 3, {}]");
    assertThat(response.getStatusCode(), is(HttpServletResponse.SC_OK));
    assertThat(IOUtils.readString(response.getContent()), is("[\"test\",\"exception\",0,{\"message\":\"Service call type \\\"blurb\\\" is not valid\",\"id\":\"INVALID_MESSAGE_TYPE\"}]"));
    reset(impl);
    doAnswer(i -> Response.builder().setText("reply").build()).when(impl).test(any(Request.class));
    // and simple OK, just in case.
    response = post("application/json", "[\"test\", \"call\", 3, {\"request\":{}}]");
    assertThat(response.getStatusCode(), is(HttpServletResponse.SC_OK));
    assertThat(IOUtils.readString(response.getContent()), is("[\"test\",\"reply\",3,{\"success\":{\"text\":\"reply\"}}]"));
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) Request(net.morimekta.test.providence.service.Request) HttpResponse(com.google.api.client.http.HttpResponse) Test(org.junit.Test)

Example 2 with Request

use of net.morimekta.test.providence.service.Request 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 3 with Request

use of net.morimekta.test.providence.service.Request 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)

Aggregations

HttpRequest (com.google.api.client.http.HttpRequest)3 Request (net.morimekta.test.providence.service.Request)3 Test (org.junit.Test)3 HttpResponse (com.google.api.client.http.HttpResponse)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 PServiceCall (net.morimekta.providence.PServiceCall)2 HttpClientHandler (net.morimekta.providence.client.HttpClientHandler)2 TestService (net.morimekta.test.providence.service.TestService)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Failure (net.morimekta.test.providence.service.Failure)1 Response (net.morimekta.test.providence.service.Response)1