|
| 1 | +package com.marklogic.spring.batch.item.writer; |
| 2 | + |
| 3 | +import com.marklogic.client.DatabaseClient; |
| 4 | +import com.marklogic.client.admin.TransformExtensionsManager; |
| 5 | +import com.marklogic.client.document.*; |
| 6 | +import com.marklogic.client.io.*; |
| 7 | +import com.marklogic.client.spring.BasicConfig; |
| 8 | +import com.marklogic.junit.Fragment; |
| 9 | +import com.marklogic.junit.spring.AbstractSpringTest; |
| 10 | +import com.marklogic.spring.batch.item.MarkLogicItemWriter; |
| 11 | +import org.junit.After; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | +import org.springframework.core.io.Resource; |
| 15 | +import org.springframework.test.context.ContextConfiguration; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +@ContextConfiguration(classes = {BasicConfig.class}) |
| 24 | +public class MarkLogicItemWriterTest extends AbstractSpringTest { |
| 25 | + |
| 26 | + public String xml; |
| 27 | + public String transformName = "simple"; |
| 28 | + DatabaseClient client; |
| 29 | + TransformExtensionsManager transMgr; |
| 30 | + XMLDocumentManager docMgr; |
| 31 | + |
| 32 | + @Before |
| 33 | + public void setup() throws IOException { |
| 34 | + Resource transform = getApplicationContext().getResource("classpath:/transforms/simple.xqy"); |
| 35 | + client = getClientProvider().getDatabaseClient(); |
| 36 | + TransformExtensionsManager transMgr = client.newServerConfigManager().newTransformExtensionsManager(); |
| 37 | + FileHandle fileHandle = new FileHandle(transform.getFile()); |
| 38 | + fileHandle.setFormat(Format.XML); |
| 39 | + transMgr.writeXQueryTransform(transformName, fileHandle); |
| 40 | + xml = "<hello>world</hello>"; |
| 41 | + docMgr = client.newXMLDocumentManager(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void writeDocumentWithTransformNoParametersTest() { |
| 46 | + MarkLogicItemWriter writer = new MarkLogicItemWriter(client); |
| 47 | + writer.setTransform(Format.XML, transformName, null); |
| 48 | + DocumentWriteOperation writeOp = new MarkLogicWriteHandle("hello.xml", new DocumentMetadataHandle(), new StringHandle(xml)); |
| 49 | + List<DocumentWriteOperation> writeOps = new ArrayList<DocumentWriteOperation>(); |
| 50 | + writeOps.add(writeOp); |
| 51 | + try { |
| 52 | + writer.write(writeOps); |
| 53 | + } catch (Exception e) { |
| 54 | + e.printStackTrace(); |
| 55 | + } |
| 56 | + StringHandle handle = docMgr.read("hello.xml", new StringHandle()); |
| 57 | + Fragment frag = new Fragment(handle.toString()); |
| 58 | + frag.assertElementExists("//hello[text() = 'world']"); |
| 59 | + frag.assertElementExists("//transform"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void writeDocumentWithTransformWithParametersTest() { |
| 64 | + MarkLogicItemWriter writer = new MarkLogicItemWriter(client); |
| 65 | + Map<String, String> parameters = new HashMap<String, String>(); |
| 66 | + parameters.put("monster", "grover"); |
| 67 | + parameters.put("trash-can", "oscar"); |
| 68 | + writer.setTransform(Format.XML, transformName, parameters); |
| 69 | + DocumentWriteOperation writeOp = new MarkLogicWriteHandle("hello.xml", new DocumentMetadataHandle(), new StringHandle(xml)); |
| 70 | + List<DocumentWriteOperation> writeOps = new ArrayList<DocumentWriteOperation>(); |
| 71 | + writeOps.add(writeOp); |
| 72 | + try { |
| 73 | + writer.write(writeOps); |
| 74 | + } catch (Exception e) { |
| 75 | + e.printStackTrace(); |
| 76 | + } |
| 77 | + StringHandle handle = docMgr.read("hello.xml", new StringHandle()); |
| 78 | + Fragment frag = new Fragment(handle.toString()); |
| 79 | + frag.assertElementExists("//transform"); |
| 80 | + frag.assertElementExists("//monster[text() = 'grover']"); |
| 81 | + frag.assertElementExists("//trash-can[text() = 'oscar']"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void writeBatchDocumentsWithTransformTest() { |
| 86 | + GenericDocumentManager docMgr = client.newDocumentManager(); |
| 87 | + docMgr.setContentFormat(Format.XML); |
| 88 | + DocumentWriteSet batch = docMgr.newWriteSet(); |
| 89 | + batch.add("/hello.xml", new DocumentMetadataHandle(), new StringHandle("<hello />")); |
| 90 | + batch.add("/hello2.xml", new DocumentMetadataHandle(), new StringHandle("<hello2 />")); |
| 91 | + ServerTransform serverTransform = new ServerTransform("simple"); |
| 92 | + docMgr.write(batch, serverTransform); |
| 93 | + } |
| 94 | + |
| 95 | + @After |
| 96 | + public void cleanup() { |
| 97 | +// transMgr.deleteTransform(transformName); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | +} |
0 commit comments