Search in sources :

Example 1 with OrderBean

use of io.confluent.examples.streams.microservices.domain.beans.OrderBean in project kafka-streams-examples by confluentinc.

the class EndToEndTest method shouldProcessManyValidOrdersEndToEnd.

@Test
public void shouldProcessManyValidOrdersEndToEnd() throws Exception {
    client = getClient();
    // Add inventory required by the inventory service
    List<KeyValue<Product, Integer>> inventory = asList(new KeyValue<>(UNDERPANTS, 75), new KeyValue<>(JUMPERS, 10));
    sendInventory(inventory, Topics.WAREHOUSE_INVENTORY);
    // Send ten orders in succession
    for (int i = 0; i < 10; i++) {
        OrderBean inputOrder = new OrderBean(id(i), 2L, OrderState.CREATED, Product.JUMPERS, 1, 1d);
        startTimer();
        // POST & GET order
        client.target(path.urlPost()).request(APPLICATION_JSON_TYPE).post(Entity.json(inputOrder));
        returnedBean = client.target(path.urlGetValidated(i)).queryParam("timeout", MIN).request(APPLICATION_JSON_TYPE).get(newBean());
        endTimer();
        assertThat(returnedBean).isEqualTo(new OrderBean(inputOrder.getId(), inputOrder.getCustomerId(), OrderState.VALIDATED, inputOrder.getProduct(), inputOrder.getQuantity(), inputOrder.getPrice()));
    }
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) OrderBean(io.confluent.examples.streams.microservices.domain.beans.OrderBean) Test(org.junit.Test)

Example 2 with OrderBean

use of io.confluent.examples.streams.microservices.domain.beans.OrderBean in project kafka-streams-examples by confluentinc.

the class OrdersService method fetchFromOtherHost.

private void fetchFromOtherHost(final String path, AsyncResponse asyncResponse, long timeout) {
    log.info("Chaining GET to a different instance: " + path);
    try {
        OrderBean bean = client.target(path).queryParam("timeout", timeout).request(MediaType.APPLICATION_JSON_TYPE).get(new GenericType<OrderBean>() {
        });
        asyncResponse.resume(bean);
    } catch (Exception swallowed) {
    }
}
Also used : OrderBean(io.confluent.examples.streams.microservices.domain.beans.OrderBean) URISyntaxException(java.net.URISyntaxException) InvalidStateStoreException(org.apache.kafka.streams.errors.InvalidStateStoreException)

Example 3 with OrderBean

use of io.confluent.examples.streams.microservices.domain.beans.OrderBean in project kafka-streams-examples by confluentinc.

the class EndToEndTest method shouldCreateNewOrderAndGetBackValidatedOrder.

@Test
public void shouldCreateNewOrderAndGetBackValidatedOrder() throws Exception {
    OrderBean inputOrder = new OrderBean(id(1L), 2L, OrderState.CREATED, Product.JUMPERS, 1, 1d);
    client = getClient();
    // Add inventory required by the inventory service with enough items in stock to pass validation
    List<KeyValue<Product, Integer>> inventory = asList(new KeyValue<>(UNDERPANTS, 75), new KeyValue<>(JUMPERS, 10));
    sendInventory(inventory, Topics.WAREHOUSE_INVENTORY);
    // When we POST order and immediately GET on the returned location
    client.target(path.urlPost()).request(APPLICATION_JSON_TYPE).post(Entity.json(inputOrder));
    returnedBean = client.target(path.urlGetValidated(1)).queryParam("timeout", MIN).request(APPLICATION_JSON_TYPE).get(newBean());
    // Then
    assertThat(returnedBean.getState()).isEqualTo(OrderState.VALIDATED);
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) OrderBean(io.confluent.examples.streams.microservices.domain.beans.OrderBean) Test(org.junit.Test)

Example 4 with OrderBean

use of io.confluent.examples.streams.microservices.domain.beans.OrderBean in project kafka-streams-examples by confluentinc.

the class EndToEndTest method shouldProcessManyInvalidOrdersEndToEnd.

@Test
public void shouldProcessManyInvalidOrdersEndToEnd() throws Exception {
    client = getClient();
    // Add inventory required by the inventory service
    List<KeyValue<Product, Integer>> inventory = asList(new KeyValue<>(UNDERPANTS, 75000), // ***nothing in stock***
    new KeyValue<>(JUMPERS, 0));
    sendInventory(inventory, Topics.WAREHOUSE_INVENTORY);
    // Send ten orders one after the other
    for (int i = 0; i < 10; i++) {
        OrderBean inputOrder = new OrderBean(id(i), 2L, OrderState.CREATED, Product.JUMPERS, 1, 1d);
        startTimer();
        // POST & GET order
        client.target(path.urlPost()).request(APPLICATION_JSON_TYPE).post(Entity.json(inputOrder));
        returnedBean = client.target(path.urlGetValidated(i)).queryParam("timeout", MIN).request(APPLICATION_JSON_TYPE).get(newBean());
        endTimer();
        assertThat(returnedBean).isEqualTo(new OrderBean(inputOrder.getId(), inputOrder.getCustomerId(), OrderState.FAILED, inputOrder.getProduct(), inputOrder.getQuantity(), inputOrder.getPrice()));
    }
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) OrderBean(io.confluent.examples.streams.microservices.domain.beans.OrderBean) Test(org.junit.Test)

Example 5 with OrderBean

use of io.confluent.examples.streams.microservices.domain.beans.OrderBean in project kafka-streams-examples by confluentinc.

the class OrdersServiceTest method shouldTimeoutGetIfNoResponseIsFound.

@Test
public void shouldTimeoutGetIfNoResponseIsFound() {
    final Client client = ClientBuilder.newClient();
    // Start the rest interface
    rest = new OrdersService("localhost");
    rest.start(CLUSTER.bootstrapServers());
    Paths paths = new Paths("localhost", rest.port());
    // Then GET order should timeout
    try {
        client.target(paths.urlGet(1)).queryParam("timeout", // Lower the request 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");
    }
}
Also used : Paths(io.confluent.examples.streams.microservices.util.Paths) ServerErrorException(javax.ws.rs.ServerErrorException) Client(javax.ws.rs.client.Client) OrderBean(io.confluent.examples.streams.microservices.domain.beans.OrderBean) Test(org.junit.Test)

Aggregations

OrderBean (io.confluent.examples.streams.microservices.domain.beans.OrderBean)8 Test (org.junit.Test)7 Paths (io.confluent.examples.streams.microservices.util.Paths)4 Client (javax.ws.rs.client.Client)4 KeyValue (org.apache.kafka.streams.KeyValue)3 GenericType (javax.ws.rs.core.GenericType)2 Order (io.confluent.examples.streams.avro.microservices.Order)1 URISyntaxException (java.net.URISyntaxException)1 ServerErrorException (javax.ws.rs.ServerErrorException)1 Response (javax.ws.rs.core.Response)1 InvalidStateStoreException (org.apache.kafka.streams.errors.InvalidStateStoreException)1