package org.commcare.cases.ledger.test;

import org.commcare.cases.CaseTestUtils;
import org.commcare.util.mocks.MockDataUtils;
import org.commcare.util.mocks.MockUserDataSandbox;
import org.javarosa.core.model.condition.EvaluationContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Hashtable;

/**
 * Test interplay between ledgers and cases.
 *
 * @author Phillip Mates (pmates@dimagi.com)
 */
public class LedgerWithCaseTest {
    private EvaluationContext evalContext;

    @Before
    public void setUp() {
        MockUserDataSandbox sandbox = MockDataUtils.getStaticStorage();

        // load cases that will be referenced by ledgers
        MockDataUtils.parseIntoSandbox(this.getClass().getResourceAsStream("/create_case_for_ledger.xml"), sandbox);
        CaseTestUtils.loadCaseInstanceIntoSandbox(sandbox);

        // load ledger data
        MockDataUtils.parseIntoSandbox(this.getClass().getResourceAsStream("/ledger_create_basic.xml"), sandbox);
        CaseTestUtils.loadLedgerIntoSandbox(sandbox);

        // create an evaluation context that has ledger and case instances setup
        Hashtable<String, String> instanceRefToId = new Hashtable<>();
        instanceRefToId.put(CaseTestUtils.LEDGER_INSTANCE, "ledger");
        instanceRefToId.put(CaseTestUtils.CASE_INSTANCE, "casedb");
        evalContext =
                MockDataUtils.buildContextWithInstances(sandbox, instanceRefToId);
    }

    @Test
    public void ledgerQueriesWithLedgerData() {
        // case id 'market_basket' exists, and ledger data has been attached to
        // 'market_basket', but the section 'non-existent-section' is
        // non-existent
        Assert.assertTrue(
                CaseTestUtils.xpathEval(evalContext,
                        "instance('ledger')/ledgerdb/ledger[@entity-id='market_basket']/section[@section-id='non-existent-section']",
                        ""));
    }

    @Test
    public void ledgerQueriesWithoutLedgerData() {
        // case id 'star_market' exists but no ledger data has been attached to
        // it
        Assert.assertTrue(
                CaseTestUtils.xpathEval(evalContext,
                        "instance('ledger')/ledgerdb/ledger[@entity-id='star_market']",
                        ""));
        Assert.assertTrue(
                CaseTestUtils.xpathEval(evalContext,
                        "instance('ledger')/ledgerdb/ledger[@entity-id='star_market']/section[@section-id='non-existent-section']",
                        ""));
    }
}

@@ -1989,16 +1989,26 @@
 sWithout
+Referenced
 LedgerDa
@@ -2553,11 +2553,1335 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void ledgerQueriesWithNoLedgerData() %7B%0A        // case id 'star_market' exists but no ledger data been loaded at all%0A        EvaluationContext evalContextWithoutLedgers = createContextWithNoLedgers();%0A        boolean result;%0A        result = CaseTestUtils.xpathEval(evalContextWithoutLedgers,%0A                        %22instance('ledger')/ledgerdb/ledger%5B@entity-id='star_market'%5D%22,%0A                        %22%22);%0A        result = CaseTestUtils.xpathEval(evalContextWithoutLedgers,%0A                        %22instance('ledger')/ledgerdb/ledger%5B@entity-id='star_market'%5D/section%5B@section-id='non-existent-section'%5D%22,%0A                        %22%22);%0A        System.out.print(result);%0A        // TODO PLM: Make this actually fail when 'result' is false%0A    %7D%0A%0A    private EvaluationContext createContextWithNoLedgers() %7B%0A        MockUserDataSandbox sandbox = MockDataUtils.getStaticStorage();%0A%0A        // load cases that will be referenced by ledgers%0A        MockDataUtils.parseIntoSandbox(this.getClass().getResourceAsStream(%22/create_case_for_ledger.xml%22), sandbox);%0A        CaseTestUtils.loadCaseInstanceIntoSandbox(sandbox);%0A%0A        // create an evaluation context that has ledger and case instances setup%0A        return MockDataUtils.getInstanceContexts(sandbox, %22casedb%22, CaseTestUtils.CASE_INSTANCE);%0A    %7D
 %0A%7D%0A

ledger query that refs valid case id but context is missing ledger data