OrdersService implements Service { @Override public void start(String bootstrapServers) { producer = startProducer(bootstrapServers, ORDER_VALIDATIONS); streams = startKStreams(bootstrapServers); jettyServer = startJetty(hostInfo.port(), this); log.info("Started Service " + getClass().getSimpleName()); } OrdersService(HostInfo hostInfo); @GET @ManagedAsync @Path("/orders/{id}") @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN}) void getWithTimeout(@PathParam("id") final String id,
      @QueryParam("timeout") @DefaultValue(CALL_TIMEOUT) Long timeout,
      @Suspended final AsyncResponse asyncResponse); @GET @ManagedAsync @Path("orders/{id}/validated") void getPostValidationWithTimeout(@PathParam("id") final String id,
      @QueryParam("timeout") @DefaultValue(CALL_TIMEOUT) Long timeout,
      @Suspended final AsyncResponse asyncResponse); @POST @ManagedAsync @Path("/orders") @Consumes(MediaType.APPLICATION_JSON) void submitOrder(final OrderBean order,
      @QueryParam("timeout") @DefaultValue(CALL_TIMEOUT) final Long timeout,
      @Suspended final AsyncResponse response); @Override void start(String bootstrapServers); @Override void stop(); static void main(String[] args);  }
@Test public void shouldPostOrderAndGetItBack() throws Exception { OrderBean bean = new OrderBean(id(1L), 2L, OrderState.CREATED, Product.JUMPERS, 10, 100d); final Client client = ClientBuilder.newClient(); rest = new OrdersService(new HostInfo("localhost", port)); rest.start(CLUSTER.bootstrapServers()); Response response = client.target(paths.urlPost()) .request(APPLICATION_JSON_TYPE) .post(Entity.json(bean)); assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_CREATED); OrderBean returnedBean = client.target(response.getLocation()) .queryParam("timeout", MIN / 2) .request(APPLICATION_JSON_TYPE) .get(new GenericType<OrderBean>() { }); assertThat(returnedBean).isEqualTo(bean); returnedBean = client.target(paths.urlGet(1)) .queryParam("timeout", MIN / 2) .request(APPLICATION_JSON_TYPE) .get(new GenericType<OrderBean>() { }); assertThat(returnedBean).isEqualTo(bean); }
@Test public void shouldGetValidatedOrderOnRequest() throws Exception { Order orderV1 = new Order(id(1L), 2L, OrderState.CREATED, Product.JUMPERS, 10, 100d); OrderBean beanV1 = OrderBean.toBean(orderV1); final Client client = ClientBuilder.newClient(); rest = new OrdersService( new HostInfo("localhost", port) ); rest.start(CLUSTER.bootstrapServers()); client.target(paths.urlPost()) .request(APPLICATION_JSON_TYPE) .post(Entity.json(beanV1)); MicroserviceTestUtils.sendOrders(asList( newBuilder(orderV1) .setState(OrderState.VALIDATED) .build())); OrderBean returnedBean = client.target(paths.urlGetValidated(beanV1.getId())) .queryParam("timeout", MIN / 2) .request(APPLICATION_JSON_TYPE) .get(new GenericType<OrderBean>() { }); assertThat(returnedBean.getState()).isEqualTo(OrderState.VALIDATED); }
@Test public void shouldTimeoutGetIfNoResponseIsFound() throws Exception { final Client client = ClientBuilder.newClient(); rest = new OrdersService( new HostInfo("localhost", port) ); rest.start(CLUSTER.bootstrapServers()); try { client.target(paths.urlGet(1)) .queryParam("timeout", 100) .request(APPLICATION_JSON_TYPE) .get(new GenericType<OrderBean>() { }); fail("Request should have failed as materialized view has not been updated"); } catch (ServerErrorException e) { assertThat(e.getMessage()).isEqualTo("HTTP 504 Gateway Timeout"); } }
@Test public void shouldGetOrderByIdWhenOnDifferentHost() throws Exception { OrderBean order = new OrderBean(id(1L), 2L, OrderState.VALIDATED, Product.JUMPERS, 10, 100d); int port1 = randomFreeLocalPort(); int port2 = randomFreeLocalPort(); final Client client = ClientBuilder.newClient(); Paths paths1 = new Paths("localhost", port1); Paths paths2 = new Paths("localhost", port2); rest = new OrdersService(new HostInfo("localhost", port1)); rest.start(CLUSTER.bootstrapServers()); rest2 = new OrdersService(new HostInfo("localhost", port2)); rest2.start(CLUSTER.bootstrapServers()); client.target(paths1.urlPost()) .request(APPLICATION_JSON_TYPE) .post(Entity.json(order)); OrderBean returnedOrder = client.target(paths1.urlGet(order.getId())) .queryParam("timeout", MIN / 2) .request(APPLICATION_JSON_TYPE) .get(new GenericType<OrderBean>() { }); assertThat(returnedOrder).isEqualTo(order); returnedOrder = client.target(paths2.urlGet(order.getId())) .queryParam("timeout", MIN / 2) .request(APPLICATION_JSON_TYPE) .get(new GenericType<OrderBean>() { }); assertThat(returnedOrder).isEqualTo(order); }