Skip to content

Instantly share code, notes, and snippets.

@fpopic
Last active February 4, 2019 17:59
Show Gist options
  • Select an option

  • Save fpopic/38087eac33abee7f3aaba41f831c5407 to your computer and use it in GitHub Desktop.

Select an option

Save fpopic/38087eac33abee7f3aaba41f831c5407 to your computer and use it in GitHub Desktop.
import org.apache.beam.sdk.testing.{NeedsRunner, PAssert, TestPipeline}
import org.apache.beam.sdk.transforms.DoFn.ProcessElement
import org.apache.beam.sdk.transforms.{Create, DoFn, ParDo}
import org.junit.experimental.categories.Category
import org.junit.{Rule, Test}
import org.scalatest.junit.JUnitSuite
import scala.annotation.meta.getter
import scala.collection.JavaConverters._
class TimesTwoDoFn extends DoFn[Int, Int] {
@ProcessElement
def processElement(c: ProcessContext): Unit = {
c.output(c.element * 2)
}
}
class TimesFiveDoFn extends DoFn[Int, Int] {
@ProcessElement
def processElement(c: ProcessContext): Unit = {
c.output(c.element * 5)
}
}
class MinimalPipelineTest extends JUnitSuite {
@(Rule@getter) // The @Rule 'pipeline' must be public.
val pipeline: TestPipeline = TestPipeline.create()
@Test
@Category(Array(classOf[NeedsRunner]))
def myTimesTenPipelineTest(): Unit = {
val input = Iterable(1, 2, 3).asJava
val actualOutput = pipeline.apply(Create.of(input))
.apply("x2", ParDo.of(new TimesTwoDoFn()))
.apply("x5", ParDo.of(new TimesFiveDoFn()))
val expectedOutput = Iterable(10, 20, 30).asJava
PAssert that actualOutput containsInAnyOrder expectedOutput
pipeline.run().waitUntilFinish()
}
}
@fpopic

fpopic commented Jan 22, 2018

Copy link
Copy Markdown
Author

For testing the whole pipeline ensure the following in build.sbt:

 "junit" % "junit" % "4.12" % Test,
  "org.hamcrest" % "hamcrest-all" % "1.3" % Test,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment