// Check production transactions
  @Test
  public void testProduction() {
    Instant exp = now.plus(TimeService.WEEK * 10);
    TariffSpecification tariffSpec =
        new TariffSpecification(broker, PowerType.PRODUCTION)
            .withExpiration(exp)
            .withMinDuration(TimeService.WEEK * 4)
            .withSignupPayment(-34.2)
            .withEarlyWithdrawPayment(35.0)
            .addRate(new Rate().withValue(0.102));
    tariff = new Tariff(tariffSpec);
    tariff.init();

    // subscribe and consume in the first timeslot
    TariffSubscription tsub = tariffMarketService.subscribeToTariff(tariff, customer, 4);
    assertEquals("four customers committed", 4, tsub.getCustomersCommitted());
    tsub.usePower(-244.6); // production
    assertEquals("correct total usage", -244.6 / 4, tsub.getTotalUsage(), 1e-6);
    assertEquals("correct realized price", 0.102, tariff.getRealizedPrice(), 1e-6);
    // def txs = TariffTransaction.findAllByPostedTime(timeService.currentTime);
    // assertEquals("two transactions", 2, txs.size())
    // TariffTransaction ttx = TariffTransaction.findByPostedTimeAndTxType(timeService.currentTime,
    // TariffTransactionType.SIGNUP)
    // assertNotNull("found signup tx", ttx)
    // assertEquals("correct charge", -34.2 * 4, ttx.charge, 1e-6)
    // ttx = TariffTransaction.findByPostedTimeAndTxType(timeService.currentTime,
    // TariffTransactionType.PRODUCE)
    // assertNotNull("found production tx", ttx)
    // assertEquals("correct amount", -244.6, ttx.quantity)
    // assertEquals("correct charge", -0.102 * 244.6, ttx.charge, 1e-6)
  }
 // create a Subscription from a Tariff
 @Test
 public void testSimpleSub() {
   TariffSubscription ts = tariffMarketService.subscribeToTariff(tariff, customer, 3);
   assertNotNull("non-null subscription", ts);
   assertEquals("correct customer", customer, ts.getCustomer());
   assertEquals("correct tariff", tariff, ts.getTariff());
   assertEquals("correct customer count", 3, ts.getCustomersCommitted());
 }
  // subscription withdrawal without and with penalty
  @Test
  public void testEarlyWithdraw() {
    Instant exp = now.plus(TimeService.WEEK * 10);
    TariffSpecification tariffSpec =
        new TariffSpecification(broker, PowerType.CONSUMPTION)
            .withExpiration(exp)
            .withMinDuration(TimeService.WEEK * 4)
            .withSignupPayment(-33.2)
            .withEarlyWithdrawPayment(42.1)
            .addRate(new Rate().withValue(0.121));
    tariff = new Tariff(tariffSpec);
    tariff.init();
    TariffSubscription tsub = tariffMarketService.subscribeToTariff(tariff, customer, 5);

    // move time forward 2 weeks, withdraw 2 customers
    Instant wk2 = now.plus(TimeService.WEEK * 2);
    timeService.setCurrentTime(wk2);
    tsub.unsubscribe(2);
    verify(mockAccounting)
        .addTariffTransaction(
            TariffTransaction.Type.WITHDRAW, tariff, customer.getCustomerInfo(), 2, 0.0, 42.1 * 2);
    // def txs = TariffTransaction.findAllByPostedTime(wk2)
    // assertEquals("one transaction", 1, txs.size())
    // assertEquals("correct txType", TariffTransactionType.WITHDRAW, txs[0].txType)
    // assertEquals("correct charge", 42.1*2, txs[0].charge)
    assertEquals("three customers committed", 3, tsub.getCustomersCommitted());

    // move time forward another week, add 4 customers and drop 1
    Instant wk3 = now.plus(TimeService.WEEK * 2 + TimeService.HOUR * 6);
    timeService.setCurrentTime(wk3);
    TariffSubscription tsub1 = tariffMarketService.subscribeToTariff(tariff, customer, 4);
    assertEquals("same subscription", tsub, tsub1);
    tsub1.unsubscribe(1);
    // txs = TariffTransaction.findAllByPostedTime(wk3)
    // assertEquals("two transactions", 2, txs.size())
    // TariffTransaction ttx = TariffTransaction.findByPostedTimeAndTxType(timeService.currentTime,
    //
    // TariffTransactionType.SIGNUP)
    // assertNotNull("found signup tx", ttx)
    // assertEquals("correct charge", -33.2 * 4, ttx.charge)
    // ttx = TariffTransaction.findByPostedTimeAndTxType(timeService.currentTime,
    //                                                  TariffTransactionType.WITHDRAW)
    // assertNotNull("found withdraw tx", ttx)
    // assertEquals("correct charge", 42.1, ttx.charge)
    assertEquals("six customers committed", 6, tsub1.getCustomersCommitted());
  }
  // Check consumption transactions
  @Test
  public void testConsumption() {
    Instant exp = now.plus(TimeService.WEEK * 10);
    TariffSpecification tariffSpec =
        new TariffSpecification(broker, PowerType.CONSUMPTION)
            .withExpiration(exp)
            .withMinDuration(TimeService.WEEK * 4)
            .withSignupPayment(-33.2)
            .addRate(new Rate().withValue(0.121));
    tariff = new Tariff(tariffSpec);
    tariff.init();

    // subscribe and consume in the first timeslot
    TariffSubscription tsub = tariffMarketService.subscribeToTariff(tariff, customer, 4);
    assertEquals("four customers committed", 4, tsub.getCustomersCommitted());
    tsub.usePower(24.4); // consumption
    assertEquals("correct total usage", 24.4 / 4, tsub.getTotalUsage(), 1e-6);
    assertEquals("correct realized price", 0.121, tariff.getRealizedPrice(), 1e-6);
    // def txs = TariffTransaction.findAllByPostedTime(timeService.getCurrentTime());
    // assertEquals("two transactions", 2, txs.size())
    // TariffTransaction ttx =
    //    TariffTransaction.findByPostedTimeAndTxType(timeService.currentTime,
    // TariffTransactionType.SIGNUP)
    // assertNotNull("found signup tx", ttx)
    // assertEquals("correct charge", -33.2 * 4, ttx.charge, 1e-6)
    // ttx = TariffTransaction.findByPostedTimeAndTxType(timeService.currentTime,
    // TariffTransactionType.CONSUME)
    // assertNotNull("found consumption tx", ttx)
    // assertEquals("correct amount", 24.4, ttx.quantity)
    // assertEquals("correct charge", 0.121 * 24.4, ttx.charge, 1e-6)

    // just consume in the second timeslot
    Instant hour = now.plus(TimeService.HOUR);
    timeService.setCurrentTime(hour);
    tsub.usePower(32.8); // consumption
    assertEquals("correct total usage", (24.4 + 32.8) / 4, tsub.getTotalUsage(), 1e-6);
    assertEquals("correct realized price", 0.121, tariff.getRealizedPrice(), 1e-6);
    // txs = TariffTransaction.findAllByPostedTime(timeService.getCurrentTime())
    // assertEquals("one transaction", 1, txs.size())
    // ttx = TariffTransaction.findByPostedTimeAndTxType(timeService.getCurrentTime(),
    // TariffTransactionType.CONSUME)
    // assertNotNull("found consumption tx", ttx)
    // assertEquals("correct amount", 32.8, ttx.quantity)
    // assertEquals("correct charge", 0.121 * 32.8, ttx.charge, 1e-6)
  }
  // subscription with non-zero signup bonus
  @Test
  public void testSignupBonus() {
    Instant exp = now.plus(TimeService.WEEK * 10);
    TariffSpecification tariffSpec =
        new TariffSpecification(broker, PowerType.CONSUMPTION)
            .withExpiration(exp)
            .withMinDuration(TimeService.WEEK * 4)
            .withSignupPayment(-33.2)
            .addRate(new Rate().withValue(0.121));
    tariff = new Tariff(tariffSpec);
    tariff.init();

    TariffSubscription tsub = tariffMarketService.subscribeToTariff(tariff, customer, 5);
    assertNotNull("non-null subscription", tsub);
    assertEquals("five customers committed", 5, tsub.getCustomersCommitted());
    verify(mockAccounting)
        .addTariffTransaction(
            TariffTransaction.Type.SIGNUP, tariff, customer.getCustomerInfo(), 5, 0.0, -33.2 * 5);
  }
  // Check two-part tariff
  @Test
  public void testTwoPart() {
    Instant exp = now.plus(TimeService.WEEK * 10);
    TariffSpecification tariffSpec =
        new TariffSpecification(broker, PowerType.CONSUMPTION)
            .withExpiration(exp)
            .withMinDuration(TimeService.WEEK * 4)
            .withSignupPayment(-31.2)
            .withPeriodicPayment(1.3)
            .addRate(new Rate().withValue(0.112));
    tariff = new Tariff(tariffSpec);
    tariff.init();

    // subscribe and consume in the first timeslot
    TariffSubscription tsub = tariffMarketService.subscribeToTariff(tariff, customer, 6);
    assertEquals("six customers committed", 6, tsub.getCustomersCommitted());
    tsub.usePower(28.8); // consumption
    assertEquals("correct total usage", 28.8 / 6, tsub.getTotalUsage(), 1e-6);
    assertEquals(
        "correct realized price", (0.112 * 28.8 + 6 * 1.3) / 28.8, tariff.getRealizedPrice(), 1e-6);
    // def txs = TariffTransaction.findAllByPostedTime(timeService.currentTime);
    // assertEquals("two transactions", 3, txs.size())
    // TariffTransaction ttx = TariffTransaction.findByPostedTimeAndTxType(timeService.currentTime,
    // TariffTransactionType.SIGNUP)
    // assertNotNull("found signup tx", ttx)
    // assertEquals("correct charge", -31.2 * 6, ttx.charge, 1e-6)
    // ttx = TariffTransaction.findByPostedTimeAndTxType(timeService.currentTime,
    // TariffTransactionType.CONSUME)
    // assertNotNull("found consumption tx", ttx)
    // assertEquals("correct amount", 28.8, ttx.quantity)
    // assertEquals("correct charge", 0.112 * 28.8, ttx.charge, 1e-6)
    // ttx = TariffTransaction.findByPostedTimeAndTxType(timeService.currentTime,
    // TariffTransactionType.PERIODIC)
    // assertNotNull("found periodoc tx", ttx)
    // assertEquals("correct charge", 6 * 1.3, ttx.charge, 1e-6)
  }