package tutorial;

import static org.junit.jupiter.api.Assertions.*;

import java.util.*;
import org.junit.jupiter.api.Test;

class LessonTest {

  @Test
  void emptyAndOuterMap() {
    assertEquals(0, Ledger.total(List.of()));
    assertTrue(
      Ledger.group(List.<Ledger.Entry>of(), Ledger.Entry::vehicle).isEmpty()
    );
    assertThrows(UnsupportedOperationException.class, () ->
      Ledger.group(
        List.of(new Ledger.Entry("car", 1)),
        Ledger.Entry::vehicle
      ).clear()
    );
  }

  @Test
  void groupsAndTotals() {
    var rows = List.of(
      new Ledger.Entry("car", 100),
      new Ledger.Entry("car", 200),
      new Ledger.Entry("bike", 50)
    );
    var groups = Ledger.group(rows, Ledger.Entry::vehicle);
    assertEquals(300, Ledger.total(groups.get("car")));
    assertEquals(50, Ledger.total(groups.get("bike")));
    assertThrows(UnsupportedOperationException.class, () ->
      groups.get("car").clear()
    );
  }

  @Test
  void invalidAndOverflow() {
    assertThrows(IllegalArgumentException.class, () ->
      new Ledger.Entry("car", -1)
    );
    assertThrows(ArithmeticException.class, () ->
      Ledger.total(
        List.of(
          new Ledger.Entry("car", Long.MAX_VALUE),
          new Ledger.Entry("car", 1)
        )
      )
    );
  }
}
