|
| 1 | +package com.marklogic.client.test; |
| 2 | + |
| 3 | +import com.marklogic.client.DatabaseClient; |
| 4 | +import com.marklogic.client.DatabaseClientFactory; |
| 5 | +import com.marklogic.client.datamovement.LineSplitter; |
| 6 | +import com.marklogic.client.io.Format; |
| 7 | +import com.marklogic.client.io.StringHandle; |
| 8 | +import org.junit.After; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import java.io.BufferedReader; |
| 13 | +import java.io.File; |
| 14 | +import java.io.FileInputStream; |
| 15 | +import java.io.InputStreamReader; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Paths; |
| 18 | +import java.util.stream.Stream; |
| 19 | +import java.util.zip.GZIPInputStream; |
| 20 | + |
| 21 | +import static org.junit.Assert.*; |
| 22 | + |
| 23 | +public class LineSplitterTest { |
| 24 | + static final private String jsonlFile = "src/test/resources/data" + File.separator + "line-delimited.jsonl"; |
| 25 | + static final private String jsonlGzipFile = "src/test/resources/data" + File.separator + "line-delimited.jsonl.gz"; |
| 26 | + static final private String xmlFile = "src/test/resources/data" + File.separator + "line-delimited.txt"; |
| 27 | + private DatabaseClient client; |
| 28 | + |
| 29 | + @Before |
| 30 | + public void setUp() { |
| 31 | + client = DatabaseClientFactory.newClient("localhost", 8012, |
| 32 | + new DatabaseClientFactory.DigestAuthContext("rest-admin", "x")); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testSplitter() throws Exception { |
| 37 | + LineSplitter splitter = new LineSplitter(); |
| 38 | + Stream<StringHandle> contentStream = splitter.split(new FileInputStream(jsonlFile)); |
| 39 | + assertNotNull(contentStream); |
| 40 | + |
| 41 | + StringHandle[] result = contentStream.toArray(size -> new StringHandle[size]); |
| 42 | + assertNotNull(result); |
| 43 | + assertEquals(result.length, Files.lines(Paths.get(jsonlFile)).count()); |
| 44 | + |
| 45 | + String[] originalResult = Files.lines(Paths.get(jsonlFile)) |
| 46 | + .toArray(size -> new String[size]); |
| 47 | + |
| 48 | + for (int i = 0; i < result.length && i < originalResult.length; i++) { |
| 49 | + assertNotNull(result[i].get()); |
| 50 | + assertEquals(result[i].get(), originalResult[i]); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testSplitterGzip() throws Exception { |
| 56 | + LineSplitter splitter = new LineSplitter(); |
| 57 | + GZIPInputStream gzipStream = new GZIPInputStream(new FileInputStream(jsonlGzipFile)); |
| 58 | + Stream<StringHandle> contentStream = splitter.split(gzipStream); |
| 59 | + assertNotNull(contentStream); |
| 60 | + |
| 61 | + StringHandle[] result = contentStream.toArray(size -> new StringHandle[size]); |
| 62 | + assertNotNull(result); |
| 63 | + |
| 64 | + gzipStream = new GZIPInputStream(new FileInputStream(jsonlGzipFile)); |
| 65 | + String[] originalResult = new BufferedReader(new InputStreamReader(gzipStream)) |
| 66 | + .lines() |
| 67 | + .toArray(size -> new String[size]); |
| 68 | + assertEquals(result.length, originalResult.length); |
| 69 | + |
| 70 | + for (int i = 0; i < result.length && i < originalResult.length; i++) { |
| 71 | + assertNotNull(result[i].get()); |
| 72 | + assertEquals(result[i].get(), originalResult[i]); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testSplitterXML() throws Exception { |
| 78 | + LineSplitter splitter = new LineSplitter(); |
| 79 | + splitter.setFormat(Format.XML); |
| 80 | + Stream<StringHandle> contentStream = splitter.split(new FileInputStream(xmlFile)); |
| 81 | + assertNotNull(contentStream); |
| 82 | + |
| 83 | + StringHandle[] result = contentStream.toArray(size -> new StringHandle[size]); |
| 84 | + assertNotNull(result); |
| 85 | + assertEquals(result.length, Files.lines(Paths.get(xmlFile)).count()); |
| 86 | + |
| 87 | + String[] originalResult = Files.lines(Paths.get(xmlFile)) |
| 88 | + .toArray(size -> new String[size]); |
| 89 | + assertEquals(result[0].getFormat(), Format.XML); |
| 90 | + for (int i = 0; i < result.length && i < originalResult.length; i++) { |
| 91 | + assertNotNull(result[i].get()); |
| 92 | + assertEquals(result[i].get(), originalResult[i]); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @After |
| 97 | + public void closeSetUp() { |
| 98 | + client.release(); |
| 99 | + } |
| 100 | +} |
0 commit comments