// 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)
  }
  // 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)
  }
 private void usePower(Timeslot timeslot) {
   for (CapacityBundle bundle : capacityBundles) {
     List<TariffSubscription> subscriptions =
         getTariffSubscriptionRepo().findActiveSubscriptionsForCustomer(bundle.getCustomerInfo());
     double totalCapacity = 0.0;
     double totalUsageCharge = 0.0;
     for (TariffSubscription subscription : subscriptions) {
       double usageSign = bundle.getPowerType().isConsumption() ? +1 : -1;
       double currCapacity = usageSign * useCapacity(subscription, bundle);
       if (Config.getInstance().isUsageChargesLogging()) {
         double charge =
             subscription
                 .getTariff()
                 .getUsageCharge(currCapacity, subscription.getTotalUsage(), false);
         totalUsageCharge += charge;
       }
       subscription.usePower(currCapacity);
       totalCapacity += currCapacity;
     }
     log.info(
         bundle.getName()
             + ": Total "
             + bundle.getPowerType()
             + " capacity for timeslot "
             + timeslot.getSerialNumber()
             + " = "
             + totalCapacity);
     logUsageCharges(
         bundle.getName()
             + ": Total "
             + bundle.getPowerType()
             + " usage charge for timeslot "
             + timeslot.getSerialNumber()
             + " = "
             + totalUsageCharge);
   }
 }
  // 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)
  }