confluentinc / confluentinc/examples

FraudService solution has NPE and also doesn't aggregate first order in the method simpleMerge

Open
#281 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.1k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

The solution for the FraudService causes an NPE in the aggregate because total.getValue() could be null.

https://github.com/confluentinc/examples/blob/5.3.1-post/microservices-orders/exercises/FraudService.java

Solution:

```
final KTable, OrderValue> aggregate = orders
.groupBy((id, order) -> order.getCustomerId(), Grouped.with(Serdes.Long(), Schemas.Topics.ORDERS.getValueSerde()))
.windowedBy(SessionWindows.with(Duration.ofHours(1)))
.aggregate(OrderValue::new,
(custId, order, total) -> {
return new OrderValue(order, (total.getValue() == null ? 0 : total.getValue()) + order.getQuantity() * order.getPrice());
},
(k, a, b) -> simpleMerge(a, b),
Materialized.with(null, new JsonSerdes(OrderValue.class)));
```

The simpleMerge also doesn't aggregate the first order because it sets the value to 0 if a is null.

```
private OrderValue simpleMerge(final OrderValue a, final OrderValue b) {
return new OrderValue(b.getOrder(), (a == null ? 0D : a.getValue()) + b.getValue());
}
```

should be:

```
private OrderValue simpleMerge(final OrderValue a, final OrderValue b) {
return new OrderValue(b.getOrder(), ((a == null || a.getValue() == null) ? b.getValue() : a.getValue() + b.getValue()));
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.