|
| 1 | +package com.baeldung.crunch; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertIterableEquals; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.util.Calendar; |
| 11 | + |
| 12 | +import org.apache.crunch.PCollection; |
| 13 | +import org.apache.crunch.Pipeline; |
| 14 | +import org.apache.crunch.Source; |
| 15 | +import org.apache.crunch.Target; |
| 16 | +import org.apache.crunch.impl.mem.MemPipeline; |
| 17 | +import org.apache.crunch.io.From; |
| 18 | +import org.apache.crunch.io.To; |
| 19 | +import org.junit.Ignore; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +public class MemPipelineUnitTest { |
| 23 | + |
| 24 | + private static final String INPUT_FILE_PATH = "src/test/resources/crunch/input.txt"; |
| 25 | + |
| 26 | + @Test |
| 27 | + public void givenPipeLineAndSource_whenSourceRead_thenExpectedNumberOfRecordsRead() { |
| 28 | + Pipeline pipeline = MemPipeline.getInstance(); |
| 29 | + Source<String> source = From.textFile(INPUT_FILE_PATH); |
| 30 | + |
| 31 | + PCollection<String> lines = pipeline.read(source); |
| 32 | + |
| 33 | + assertEquals(21, lines.asCollection() |
| 34 | + .getValue() |
| 35 | + .size()); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void givenPipeLine_whenTextFileRead_thenExpectedNumberOfRecordsRead() { |
| 40 | + Pipeline pipeline = MemPipeline.getInstance(); |
| 41 | + |
| 42 | + PCollection<String> lines = pipeline.readTextFile(INPUT_FILE_PATH); |
| 43 | + |
| 44 | + assertEquals(21, lines.asCollection() |
| 45 | + .getValue() |
| 46 | + .size()); |
| 47 | + } |
| 48 | + |
| 49 | + private String createOutputPath() throws IOException { |
| 50 | + Path path = Files.createTempDirectory("test"); |
| 51 | + final String outputFilePath = path.toString() + File.separatorChar |
| 52 | + + "output.text"; |
| 53 | + return outputFilePath; |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @Ignore("Requires Hadoop binaries") |
| 58 | + public void givenCollection_whenWriteCalled_fileWrittenSuccessfully() |
| 59 | + throws IOException { |
| 60 | + PCollection<String> inputStrings = MemPipeline.collectionOf("Hello", |
| 61 | + "Apache", "Crunch", Calendar.getInstance() |
| 62 | + .toString()); |
| 63 | + final String outputFilePath = createOutputPath(); |
| 64 | + Target target = To.textFile(outputFilePath); |
| 65 | + |
| 66 | + inputStrings.write(target); |
| 67 | + |
| 68 | + Pipeline pipeline = MemPipeline.getInstance(); |
| 69 | + PCollection<String> lines = pipeline.readTextFile(outputFilePath); |
| 70 | + assertIterableEquals(inputStrings.materialize(), lines.materialize()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + @Ignore("Requires Hadoop binaries") |
| 75 | + public void givenPipeLine_whenWriteTextFileCalled_fileWrittenSuccessfully() |
| 76 | + throws IOException { |
| 77 | + Pipeline pipeline = MemPipeline.getInstance(); |
| 78 | + PCollection<String> inputStrings = MemPipeline.collectionOf("Hello", |
| 79 | + "Apache", "Crunch", Calendar.getInstance() |
| 80 | + .toString()); |
| 81 | + final String outputFilePath = createOutputPath(); |
| 82 | + |
| 83 | + pipeline.writeTextFile(inputStrings, outputFilePath); |
| 84 | + |
| 85 | + PCollection<String> lines = pipeline.readTextFile(outputFilePath); |
| 86 | + assertIterableEquals(inputStrings.materialize(), lines.materialize()); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments