ReportBuilderTest.scala 4.18 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
2
3
4
5
6
7
8
9
10
/**
 * Biopet is built on top of GATK Queue for building bioinformatic
 * pipelines. It is mainly intended to support LUMC SHARK cluster which is running
 * SGE. But other types of HPC that are supported by GATK Queue (such as PBS)
 * should also be able to execute Biopet tools and pipelines.
 *
 * Copyright 2014 Sequencing Analysis Support Core - Leiden University Medical Center
 *
 * Contact us at: sasc@lumc.nl
 *
11
 * A dual licensing mode is applied. The source code within this project is freely available for non-commercial use under an AGPL
Peter van 't Hof's avatar
Peter van 't Hof committed
12
13
14
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
15
16
package nl.lumc.sasc.biopet.core.report

Peter van 't Hof's avatar
Peter van 't Hof committed
17
18
19
20
import java.io.File
import java.nio.file.Paths

import com.google.common.io.Files
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb
Peter van 't Hof's avatar
Peter van 't Hof committed
22
23
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
Peter van 't Hof's avatar
Peter van 't Hof committed
24
import org.testng.annotations.{ DataProvider, Test }
Peter van 't Hof's avatar
Peter van 't Hof committed
25
26
27

import scala.concurrent.Await
import scala.concurrent.duration.Duration
Peter van 't Hof's avatar
Peter van 't Hof committed
28
29

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
30
31
 * Created by pjvanthof on 24/02/16.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
32
33
class ReportBuilderTest extends TestNGSuite with Matchers {

Peter van 't Hof's avatar
Peter van 't Hof committed
34
35
36
37
38
39
40
41
42
  private def resourcePath(p: String): String = {
    Paths.get(getClass.getResource(p).toURI).toString
  }

  @DataProvider(name = "testGeneratePages")
  def generatePageProvider = {
    val sample = Array(Some("sampleName"), None)
    val lib = Array(Some("libName"), None)
    val nested = Array(false, true)
Peter van 't Hof's avatar
Peter van 't Hof committed
43
    for (s <- sample; l <- lib; n <- nested if (!(l.isDefined && s.isEmpty))) yield Array(s, l, n)
Peter van 't Hof's avatar
Peter van 't Hof committed
44
45
46
47
48
49
50
51
52
53
  }

  @Test(dataProvider = "testGeneratePages")
  def testGeneratePages(sample: Option[String], lib: Option[String], nested: Boolean): Unit = {
    val builder = new ReportBuilder {
      def reportName: String = "test"
      def indexPage: ReportPage = ReportPage(
        (if (nested) "p1" -> ReportPage(Nil, Nil, Map()) :: Nil else Nil), Nil, Map())
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
54
55
56
57
58
59
60
61
62
63
64
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()
    sample.foreach { sampleName =>
      val sampleId = Await.result(db.createSample(sampleName, 0), Duration.Inf)
      lib.foreach { libName =>
        Await.result(db.createLibrary(libName, 0, sampleId), Duration.Inf)
      }
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
65
66
    val tempDir = Files.createTempDir()
    tempDir.deleteOnExit()
Peter van 't Hof's avatar
Peter van 't Hof committed
67
    val args = Array("-s", dbFile.getAbsolutePath, "-o", tempDir.getAbsolutePath) ++
Peter van 't Hof's avatar
Peter van 't Hof committed
68
69
70
      sample.map(x => Array("-a", s"sampleId=$x")).getOrElse(Array()) ++
      lib.map(x => Array("-a", s"libId=$x")).getOrElse(Array())
    builder.main(args)
Peter van 't Hof's avatar
Peter van 't Hof committed
71
72
    builder.sampleId shouldBe sample.flatMap(s => Await.result(db.getSampleId(0, s), Duration.Inf))
    builder.libId shouldBe lib.flatMap(l => Await.result(db.getLibraryId(0, builder.sampleId.get, l), Duration.Inf))
Peter van 't Hof's avatar
Peter van 't Hof committed
73
74
75
    builder.extFiles.foreach(x => new File(tempDir, "ext" + File.separator + x.targetPath) should exist)
    new File(tempDir, "index.html") should exist
    new File(tempDir, "p1" + File.separator + "index.html").exists() shouldBe nested
Peter van 't Hof's avatar
Peter van 't Hof committed
76
77

    db.close()
Peter van 't Hof's avatar
Peter van 't Hof committed
78
79
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  @Test
  def testCountPages: Unit = {
    ReportBuilder.countPages(ReportPage(Nil, Nil, Map())) shouldBe 1
    ReportBuilder.countPages(ReportPage(
      "p1" -> ReportPage(Nil, Nil, Map()) :: Nil,
      Nil, Map())) shouldBe 2
    ReportBuilder.countPages(ReportPage(
      "p1" -> ReportPage(Nil, Nil, Map()) :: "p2" -> ReportPage(Nil, Nil, Map()) :: Nil,
      Nil, Map())) shouldBe 3
    ReportBuilder.countPages(ReportPage(
      "p1" -> ReportPage("p1" -> ReportPage(Nil, Nil, Map()) :: Nil, Nil, Map()) :: Nil,
      Nil, Map())) shouldBe 3
    ReportBuilder.countPages(ReportPage(
      "p1" -> ReportPage(Nil, Nil, Map()) :: "p2" -> ReportPage("p1" -> ReportPage(Nil, Nil, Map()) :: Nil, Nil, Map()) :: Nil,
      Nil, Map())) shouldBe 4
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
97
98
99
100
101
102
103
104
105
106
  @Test
  def testRenderTemplate: Unit = {
    ReportBuilder.templateCache = Map()
    ReportBuilder.templateCache shouldBe empty
    ReportBuilder.renderTemplate("/template.ssp", Map("arg" -> "test")) shouldBe "test"
    ReportBuilder.templateCache.size shouldBe 1
    ReportBuilder.renderTemplate("/template.ssp", Map("arg" -> "bla")) shouldBe "bla"
    ReportBuilder.templateCache.size shouldBe 1
  }
}