package org.commcare.xml;

import static org.junit.Assert.assertEquals;

import org.commcare.suite.model.PostRequest;
import org.commcare.suite.model.QueryData;
import org.commcare.suite.model.RemoteRequestEntry;
import org.javarosa.xml.ElementParser;
import org.javarosa.xml.util.InvalidStructureException;
import org.javarosa.xml.util.UnfullfilledRequirementsException;
import org.junit.Test;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

public class RemoteRequestEntryParserTest {

    @Test
    public void testParse() throws IOException, UnfullfilledRequirementsException, InvalidStructureException,
            XmlPullParserException, NoSuchFieldException, IllegalAccessException {
        String query = "<remote-request>\n"
                + "  <post url=\"https://www.fake.com/claim_patient/\">\n"
                + "    <data key=\"case_id\" ref=\"instance('session')/session/data/case_id\"/>\n"
                + "  </post>\n"
                + "  <command id=\"search\">\n"
                + "    <display>\n"
                + "      <text>Search</text>\n"
                + "    </display>\n"
                + "  </command>\n"
                + "</remote-request>";
        ByteArrayInputStream inputStream = new ByteArrayInputStream(query.getBytes("UTF-8"));
        KXmlParser parser = ElementParser.instantiateParser(inputStream);
        EntryParser entryParser = EntryParser.buildRemoteSyncParser(parser);
        RemoteRequestEntry entry = (RemoteRequestEntry)entryParser.parse();
        PostRequest post = entry.getPostRequest();
        Field paramsField = PostRequest.class.getDeclaredField("params");
        paramsField.setAccessible(true);
        List<QueryData> params = (List<QueryData>)paramsField.get(post);
        assertEquals(1, params.size());
        assertEquals("case_id", params.get(0).getKey());
    }
}

@@ -77,139 +77,499 @@
 ort 
-org.commcare.suite.model.PostRequest;%0Aimport org.commcare.suite.model.QueryData;%0Aimport org.commcare.suite.model.RemoteRequestEntry
+com.google.common.collect.ImmutableList;%0A%0Aimport org.commcare.data.xml.SimpleNode;%0Aimport org.commcare.data.xml.TreeBuilder;%0Aimport org.commcare.suite.model.PostRequest;%0Aimport org.commcare.suite.model.QueryData;%0Aimport org.commcare.suite.model.RemoteRequestEntry;%0Aimport org.javarosa.core.model.condition.EvaluationContext;%0Aimport org.javarosa.core.model.instance.DataInstance;%0Aimport org.javarosa.core.model.instance.TreeElement;%0Aimport org.javarosa.core.model.instance.VirtualDataInstance
 ;%0Aim
@@ -924,16 +924,74 @@
 .Field;%0A
+import java.util.Collections;%0Aimport java.util.Hashtable;%0A
 import j
@@ -1456,13 +1456,8 @@
 ion/
-data/
 case
@@ -2394,21 +2394,697 @@
 ).getKey());
+%0A%0A        Hashtable%3CString, String%3E evaluatedParams = post.getEvaluatedParams(%0A                new EvaluationContext(null, buildSessionInstance()));%0A        assertEquals(%22bang%22, evaluatedParams.get(%22case_id%22));%0A%0A    %7D%0A%0A    private Hashtable%3CString, DataInstance%3E buildSessionInstance() %7B%0A        List%3CSimpleNode%3E nodes = ImmutableList.of(%0A            SimpleNode.textNode(%22case_id%22, Collections.emptyMap(), %22bang%22)%0A        );%0A        TreeElement root = TreeBuilder.buildTree(%22session%22, %22session%22, nodes);%0A        Hashtable%3CString, DataInstance%3E instances = new Hashtable%3C%3E();%0A        instances.put(%22session%22, new VirtualDataInstance(%22session%22, root));%0A        return instances;
 %0A    %7D%0A%7D%0A

test evaluated params