package com.marklogic.spring.batch.item.reader;

import com.marklogic.client.DatabaseClient;
import com.marklogic.client.document.DocumentRecord;
import com.marklogic.client.document.XMLDocumentManager;
import com.marklogic.client.io.StringHandle;
import com.marklogic.client.query.QueryManager;
import com.marklogic.client.query.StructuredQueryBuilder;
import com.marklogic.client.query.StructuredQueryDefinition;
import com.marklogic.junit.spring.AbstractSpringTest;
import com.marklogic.spring.batch.config.MarkLogicConfiguration;
import com.marklogic.spring.batch.test.AbstractSpringBatchTest;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;

@ContextConfiguration(classes = MarkLogicConfiguration.class)
@TestPropertySource("classpath:job.properties")
public class MarkLogicItemReaderTest extends AbstractSpringBatchTest {

    private MarkLogicItemReader itemReader;

    @Before
    public void before() {
        DatabaseClient client = getClient();
        QueryManager qm = client.newQueryManager();
        StructuredQueryBuilder sqb = qm.newStructuredQueryBuilder();
        StructuredQueryDefinition query = sqb.directory(true, "/docs/");

        itemReader = new MarkLogicItemReader(getClient(), query);

        StringHandle handle = new StringHandle();
        handle.set("<hello>hello</hello>");
        XMLDocumentManager docMgr = client.newXMLDocumentManager();
        docMgr.write("/docs/doc.xml", handle);
    }

    @Test
    public void readOneDocumentTest() throws Exception {
        itemReader.open(new ExecutionContext());
        DocumentRecord record = itemReader.read();
        assertEquals(record.getUri(), "/docs/doc.xml");
        itemReader.close();
    }
}

@@ -1043,16 +1043,51 @@
 mReader;
+%0A    private DatabaseClient client;
 %0A%0A    @B
@@ -1127,31 +1127,16 @@
         
-DatabaseClient 
 client =
@@ -1415,217 +1415,9 @@
 );%0A%0A
-        StringHandle handle = new StringHandle();%0A        handle.set(%22%3Chello%3Ehello%3C/hello%3E%22);%0A        XMLDocumentManager docMgr = client.newXMLDocumentManager();%0A        docMgr.write(%22/docs/doc.xml%22, handle);
 %0A
+
     
@@ -1486,16 +1486,83 @@
 ption %7B%0A
+        String uri = %22/docs/doc.xml%22;%0A        insertDocument(uri);%0A
         
@@ -1695,51 +1695,776 @@
 (), 
-%22/docs/doc.xml%22);%0A        itemReader.close(
+uri);%0A        itemReader.close();%0A    %7D%0A%0A    @Test%0A    public void readTwoDocuments() throws Exception %7B%0A        String firstUri = %22/docs/a.xml%22;%0A        String secondUri = %22/docs/b.xml%22;%0A        insertDocument(firstUri);%0A        insertDocument(secondUri);%0A%0A        itemReader.open(new ExecutionContext());%0A        DocumentRecord record = itemReader.read();%0A        assertEquals(record.getUri(), firstUri);%0A        record = itemReader.read();%0A        assertEquals(record.getUri(), secondUri);%0A        itemReader.close();%0A    %7D%0A%0A    private void insertDocument(String uri) %7B%0A        StringHandle handle = new StringHandle();%0A        handle.set(%22%3Chello%3Ehello%3C/hello%3E%22);%0A        XMLDocumentManager docMgr = client.newXMLDocumentManager();%0A        docMgr.write(uri, handle
 );%0A 

Add second test to MarkLogicItemReader