// 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());
  }
 @Before
 public void setUp() {
   broker = new Broker("Joe");
   now = new DateTime(2011, 1, 10, 0, 0, 0, 0, DateTimeZone.UTC).toInstant();
   timeService.setCurrentTime(now);
   Instant exp = now.plus(TimeService.WEEK * 10);
   TariffSpecification tariffSpec =
       new TariffSpecification(broker, PowerType.CONSUMPTION)
           .withExpiration(exp)
           .withMinDuration(TimeService.WEEK * 8)
           .addRate(new Rate().withValue(0.121));
   tariff = new Tariff(tariffSpec);
   tariff.init();
   customerInfo = new CustomerInfo("Charley", 100);
   customer = new AbstractCustomer(customerInfo);
   reset(mockAccounting);
 }
  // 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)
  }