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\"}}]"));
}
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);
}
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);
}
Aggregations