Skip to content

Instantly share code, notes, and snippets.

@wicksome
Created August 13, 2019 05:07
Show Gist options
  • Save wicksome/53b77aa31b3a6de4fdbca90892470b6f to your computer and use it in GitHub Desktop.
Save wicksome/53b77aa31b3a6de4fdbca90892470b6f to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import org.junit.Test;
import org.slf4j.Logger;
import org.springframework.web.client.AsyncRestTemplate;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LocalTest2 {
private Logger log = LOG;
AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate();
UserMapper userMapper = new UserMapper();
Connector accountConnector = new Connector();
Connector cardConnector = new Connector();
Connector pointConnector = new Connector();
@Test
public void example2() {
UserDetail user = get("user-1");
System.out.println(user);
}
public UserDetail get(String id) {
// validate();
try {
return Stream.of(id)
.map(userMapper::get)
.map(user -> {
final String externalId = user.getId();
final CompletableFuture<Accounts> accountsFuture = accountConnector.getAccounts(externalId);
final CompletableFuture<Cards> cardsFuture = cardConnector.getCards(externalId);
final CompletableFuture<Point> pointFuture = pointConnector.getPoint(externalId);
return CompletableFuture.allOf(accountsFuture, cardsFuture, pointFuture)
.thenApply(ignore -> UserDetail.builder()
.user(user)
.accounts(accountsFuture.join())
.cards(cardsFuture.join())
.point(pointFuture.join())
.build())
.exceptionally(err -> {
throw new ConnectorException(err);
});
})
.map(CompletableFuture::join)
.findFirst()
.orElse(null);
} catch (InvalidException | ConnectorException e) {
log.debug(e.getMessage(), e);
throw e;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw e;
}
}
}
class Connector {
CompletableFuture<Accounts> getAccounts(String id) {
return CompletableFuture.supplyAsync(() -> {
return new Accounts(Arrays.asList(new Account()));
});
}
CompletableFuture<Cards> getCards(String id) {
return CompletableFuture.supplyAsync(() -> {
return new Cards(Arrays.asList(new Card()));
});
}
CompletableFuture<Point> getPoint(String id) {
return CompletableFuture.supplyAsync(() -> {
return new Point();
});
}
}
class ConnectorException extends RuntimeException {
public ConnectorException(Throwable throwable) {
super(throwable);
}
}
class InvalidException extends RuntimeException {
}
class UserMapper {
public User get(String id) {
return new User();
}
}
@Data
@Builder
class UserDetail {
@JsonUnwrapped
private final User user;
private final Accounts accounts;
private final Cards cards;
private final Point point;
}
@Data
class Account {
}
@Getter
@AllArgsConstructor
class Accounts {
private List<Account> accounts;
}
@Data
class Card {
}
@Getter
@AllArgsConstructor
class Cards {
private List<Card> accounts;
}
@Data
class Point {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment