/*
 * Copyright 2010-2013 Ning, Inc.
 *
 * Ning licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package org.killbill.billing.jaxrs;

import java.math.BigDecimal;
import java.util.List;

import org.testng.Assert;
import org.testng.annotations.Test;

import org.killbill.billing.client.model.Account;
import org.killbill.billing.client.model.Invoice;
import org.killbill.billing.client.model.Payment;

import static org.testng.Assert.assertEquals;

public class TestOverdue extends TestJaxrsBase {

    @Test(groups = "slow", description = "Can retrieve the account overdue status")
    public void testOverdueStatus() throws Exception {
        // Create an account without a payment method
        final Account accountJson = createAccountNoPMBundleAndSubscriptionAndWaitForFirstInvoice();

        // Get the invoices
        final List<Invoice> invoices = killBillClient.getInvoicesForAccount(accountJson.getAccountId());
        // 2 invoices but look for the non zero dollar one
        assertEquals(invoices.size(), 2);

        // We're still clear - see the configuration
        Assert.assertTrue(killBillClient.getOverdueStateForAccount(accountJson.getAccountId()).getIsClearState());

        clock.addDays(30);
        crappyWaitForLackOfProperSynchonization();
        Assert.assertEquals(killBillClient.getOverdueStateForAccount(accountJson.getAccountId()).getName(), "OD1");

        clock.addDays(10);
        crappyWaitForLackOfProperSynchonization();
        Assert.assertEquals(killBillClient.getOverdueStateForAccount(accountJson.getAccountId()).getName(), "OD2");

        clock.addDays(10);
        crappyWaitForLackOfProperSynchonization();
        Assert.assertEquals(killBillClient.getOverdueStateForAccount(accountJson.getAccountId()).getName(), "OD3");

        // Post external payments
        for (final Invoice invoice : killBillClient.getInvoicesForAccount(accountJson.getAccountId())) {
            if (invoice.getBalance().compareTo(BigDecimal.ZERO) > 0) {
                final Payment payment = new Payment();
                payment.setAccountId(accountJson.getAccountId());
                payment.setInvoiceId(invoice.getInvoiceId());
                payment.setAmount(invoice.getBalance());
                killBillClient.createPayment(payment, true, createdBy, reason, comment);
            }
        }

        // Wait a bit for overdue to pick up the payment events...
        crappyWaitForLackOfProperSynchonization();

        // Verify we're in clear state
        Assert.assertTrue(killBillClient.getOverdueStateForAccount(accountJson.getAccountId()).getIsClearState());
    }
}

@@ -704,73 +704,39 @@
 til.
-List;%0A%0Aimport org.testng.Assert;%0Aimport org.testng.annotations.Te
+Comparator;%0Aimport java.util.Li
 st;%0A
@@ -877,23 +877,180 @@
 t.model.
-Payment
+Invoices;%0Aimport org.killbill.billing.client.model.Payment;%0Aimport org.testng.Assert;%0Aimport org.testng.annotations.Test;%0A%0Aimport com.google.common.collect.Ordering
 ;%0A%0Aimpor
@@ -2457,16 +2457,312 @@
 payments
+, paying the most recent invoice first: this is to avoid a race condition where%0A        // a refresh overdue notification kicks in after the first payment, which makes the account goes CLEAR and%0A        // triggers an AUTO_INVOICE_OFF tag removal (hence adjustment of the other invoices balance).
 %0A       
@@ -2766,13 +2766,8 @@
     
-for (
 fina
@@ -2775,24 +2775,25 @@
  Invoice
+s
  invoice
  : killB
@@ -2788,10 +2788,21 @@
 oice
- :
+sForAccount =
  kil
@@ -2862,16 +2862,438 @@
 untId())
+;%0A        final List%3CInvoice%3E mostRecentInvoiceFirst = Ordering.%3CInvoice%3Efrom(new Comparator%3CInvoice%3E() %7B%0A            @Override%0A            public int compare(final Invoice invoice1, final Invoice invoice2) %7B%0A                return invoice1.getInvoiceDate().compareTo(invoice2.getInvoiceDate());%0A            %7D%0A        %7D).reverse().sortedCopy(invoicesForAccount);%0A        for (final Invoice invoice : mostRecentInvoiceFirst
 ) %7B%0A    

fix race condition in TestOverdue