From 019d35078421a33e579992aca13dbe6f14ca67bf Mon Sep 17 00:00:00 2001 From: gjoranv Date: Tue, 24 Oct 2023 11:31:21 +0200 Subject: Add bill status logic. --- .../api/integration/billing/StatusHistoryTest.java | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/integration/billing/StatusHistoryTest.java (limited to 'controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/integration/billing/StatusHistoryTest.java') diff --git a/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/integration/billing/StatusHistoryTest.java b/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/integration/billing/StatusHistoryTest.java new file mode 100644 index 00000000000..46a4c7e199c --- /dev/null +++ b/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/integration/billing/StatusHistoryTest.java @@ -0,0 +1,89 @@ +package com.yahoo.vespa.hosted.controller.api.integration.billing; + +import org.junit.jupiter.api.Test; + +import java.time.Clock; +import java.time.ZonedDateTime; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * @author gjoranv + */ +public class StatusHistoryTest { + + private final Clock clock = Clock.systemUTC(); + + @Test + void open_can_change_to_any_status() { + var history = StatusHistory.open(clock); + history.checkValidTransition(BillStatus.FROZEN); + history.checkValidTransition(BillStatus.CLOSED); + history.checkValidTransition(BillStatus.VOID); + } + + @Test + void frozen_cannot_change_to_open() { + var history = new StatusHistory(historyWith(BillStatus.FROZEN)); + + history.checkValidTransition(BillStatus.CLOSED); + history.checkValidTransition(BillStatus.VOID); + + assertThrows(IllegalArgumentException.class, () -> history.checkValidTransition(BillStatus.OPEN)); + } + + @Test + void closed_cannot_change() { + var history = new StatusHistory(historyWith(BillStatus.CLOSED)); + + assertThrows(IllegalArgumentException.class, () -> history.checkValidTransition(BillStatus.OPEN)); + assertThrows(IllegalArgumentException.class, () -> history.checkValidTransition(BillStatus.FROZEN)); + assertThrows(IllegalArgumentException.class, () -> history.checkValidTransition(BillStatus.VOID)); + } + + @Test + void void_cannot_change() { + var history = new StatusHistory(historyWith(BillStatus.VOID)); + + assertThrows(IllegalArgumentException.class, () -> history.checkValidTransition(BillStatus.OPEN)); + assertThrows(IllegalArgumentException.class, () -> history.checkValidTransition(BillStatus.FROZEN)); + assertThrows(IllegalArgumentException.class, () -> history.checkValidTransition(BillStatus.CLOSED)); + } + + @Test + void any_status_can_change_to_itself() { + var history = new StatusHistory(historyWith(BillStatus.OPEN)); + history.checkValidTransition(BillStatus.OPEN); + + history = new StatusHistory(historyWith(BillStatus.FROZEN)); + history.checkValidTransition(BillStatus.FROZEN); + + history = new StatusHistory(historyWith(BillStatus.CLOSED)); + history.checkValidTransition(BillStatus.CLOSED); + + history = new StatusHistory(historyWith(BillStatus.VOID)); + history.checkValidTransition(BillStatus.VOID); + } + + @Test + void it_validates_status_history_in_constructor() { + assertThrows(IllegalArgumentException.class, () -> new StatusHistory(historyWith(BillStatus.FROZEN, BillStatus.OPEN))); + assertThrows(IllegalArgumentException.class, () -> new StatusHistory(historyWith(BillStatus.CLOSED, BillStatus.OPEN))); + assertThrows(IllegalArgumentException.class, () -> new StatusHistory(historyWith(BillStatus.CLOSED, BillStatus.FROZEN))); + assertThrows(IllegalArgumentException.class, () -> new StatusHistory(historyWith(BillStatus.CLOSED, BillStatus.VOID))); + assertThrows(IllegalArgumentException.class, () -> new StatusHistory(historyWith(BillStatus.VOID, BillStatus.OPEN))); + assertThrows(IllegalArgumentException.class, () -> new StatusHistory(historyWith(BillStatus.VOID, BillStatus.FROZEN))); + assertThrows(IllegalArgumentException.class, () -> new StatusHistory(historyWith(BillStatus.VOID, BillStatus.CLOSED))); + } + + private SortedMap historyWith(BillStatus... statuses) { + var history = new TreeMap<>(Map.of(ZonedDateTime.now(clock), BillStatus.OPEN)); + for (var status : statuses) { + history.put(ZonedDateTime.now(clock), status); + } + return history; + } +} -- cgit v1.2.3