aboutsummaryrefslogtreecommitdiffstats
path: root/scalding/src/test/scala/sandcrawler/HBaseMimeCountTest.scala
blob: dfcfcdbb3c686713965f4c4f52b0df542990cc2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package sandcrawler

import cascading.tuple.{Tuple, Fields}
import com.twitter.scalding.{JobTest, Tsv, TupleConversions}
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.util.Bytes
import org.junit.runner.RunWith
import org.scalatest.FunSpec
import org.scalatest.junit.JUnitRunner
import org.slf4j.LoggerFactory
import parallelai.spyglass.hbase.HBaseSource
import parallelai.spyglass.hbase.HBaseConstants.SourceMode
import scala._

@RunWith(classOf[JUnitRunner])
class HBaseMimeCountTest extends FunSpec with TupleConversions {

  val output = "/tmp/testOutput"
  val (test_table, test_host) = ("test-table", "dummy-host:2181")

  val log = LoggerFactory.getLogger(this.getClass.getName)

  val mimeType1 = "text/html"
  val mimeType2 = "application/pdf"

  val sampleData = List(
    List("sha1:K2DKSSVTXWPRMFDTWSTCQW3RVWRIOV3Q", mimeType1),
    List("sha1:C3YNNEGH5WAG5ZAAXWAEBNXJWT6CZ3WU", mimeType1),
    List("sha1:SDKUVHC3YNNEGH5WAG5ZAAXWAEBNX4WT", mimeType2),
    List("sha1:35985C3YNNEGH5WAG5ZAAXWAEBNXJW56", mimeType2),
    List("sha1:885C3YNNEGH5WAG5ZAAXWA8BNXJWT6CZ", mimeType2),
    List("sha1:00904C3YNNEGH5WAG5ZA9XWAEBNXJWT6", mimeType2),
    List("sha1:249C3YNNEGH5WAG5ZAAXWAEBNXJWT6CZ", mimeType1),
    List("sha1:095893C3YNNEGH5WAG5ZAAXWAEBNXJWT", mimeType2)
  )

  val mimeType1Count = sampleData.count(lst => lst(1) == mimeType1)
  val mimeType2Count = sampleData.count(lst => lst(1) == mimeType2)

  JobTest("sandcrawler.HBaseMimeCountJob")
    .arg("test", "")
    .arg("app.conf.path", "app.conf")
    .arg("output", output)
    .arg("hbase-table", test_table)
    .arg("zookeeper-hosts", test_host)
    .arg("debug", "true")
    .source[Tuple](HBaseMimeCountJob.getHBaseSource(test_table, test_host),
      sampleData.map(l => new Tuple(l.map(s => {new ImmutableBytesWritable(Bytes.toBytes(s))}):_*)))
      .sink[Tuple](Tsv(output)) {
        outputBuffer =>
        it("should return a 2-element list.") {
          assert(outputBuffer.size === 2)
        }

        // Convert List[Tuple] to Map[String, Integer].
        val counts = outputBuffer.map(t => (t.getString(0), t.getInteger(1))).toMap
        it("should have the appropriate number of each mime type") {
          assert(counts(mimeType1) == mimeType1Count)
          assert(counts(mimeType2) == mimeType2Count)
        }
    }
    .run
    .finish
}