ConfigTest.scala 5.32 KB
Newer Older
bow's avatar
bow committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 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
 *
 * A dual licensing mode is applied. The source code within this project that are
 * not part of GATK Queue is freely available for non-commercial use under an AGPL
 * 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
16
package nl.lumc.sasc.biopet.utils.config
17

Peter van 't Hof's avatar
Peter van 't Hof committed
18
import nl.lumc.sasc.biopet.utils.{ ConfigUtils, ConfigUtilsTest }
19
20
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
21
import org.testng.annotations.{ DataProvider, Test }
22
23

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
24
25
 * Test class for [[Config]]
 *
26
27
 * Created by pjvan_thof on 1/8/15.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
28
class ConfigTest extends TestNGSuite with Matchers with ConfigUtils.ImplicitConversions {
Peter van 't Hof's avatar
Peter van 't Hof committed
29
  @Test def testLoadConfigFile(): Unit = {
30
31
32
33
34
35
36
    val config = new Config
    config.loadConfigFile(ConfigTest.file)
    config.map shouldBe ConfigTest.map
    config.loadConfigFile(ConfigTest.file)
    config.map shouldBe ConfigTest.map
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
37
  @Test def testContains(): Unit = {
38
39
40
41
42
43
44
    ConfigTest.config.contains("m1") shouldBe true
    ConfigTest.config.contains("notexist") shouldBe false
    ConfigTest.config.contains(new ConfigValueIndex("m1", Nil, "k1")) shouldBe true
    ConfigTest.config.contains(new ConfigValueIndex("notexist", Nil, "k1")) shouldBe true
    ConfigTest.config.contains(new ConfigValueIndex("notexist", Nil, "k1", false)) shouldBe false
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
45
  @Test def testApply(): Unit = {
46
47
48
49
    ConfigTest.config("m1", Nil, "k1").asString shouldBe "v2"
    ConfigTest.config("m1", Nil, "notexist", default = "default").asString shouldBe "default"
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
50
  @Test def testMergeConfigs(): Unit = {
51
52
53
54
55
    val map1 = Map("1" -> 1)
    val map2 = Map("2" -> 2)
    Config.mergeConfigs(new Config(map1), new Config(map2)).map shouldBe ConfigUtils.mergeMaps(map1, map2)
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
56
  @Test def testToString(): Unit = {
57
58
59
60
    val map1 = Map("1" -> 1)
    new Config(map1).toString() shouldBe map1.toString()
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
61
  @Test def testSkipNested(): Unit = {
62
63
64
65
66
67
    val map = Map("1" -> Map("2" -> Map("4" -> Map("5" -> Map("k1" -> "v1")))))
    Config.getValueFromMap(map, new ConfigValueIndex("5", List("1", "2", "4", "5"), "k1")).get.asString shouldBe "v1"
    Config.getValueFromMap(map, new ConfigValueIndex("5", List("1", "2", "3", "4", "5"), "k1")).get.asString shouldBe "v1"
    Config.getValueFromMap(map, new ConfigValueIndex("5", List("1", "2", "3", "dummy", "dummy", "4", "5"), "k1")).get.asString shouldBe "v1"
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
68
  @DataProvider(name = "testGetValueFromMapProvider")
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  def testGetValueFromMapProvider() = {
    Array(
      Array("m1", Nil, "k1", true, "v2"),
      Array("m1", List("bla"), "k1", true, "v2"),
      Array("m1", List("bla", "bla2"), "k1", true, "v2"),
      Array("m2", List("m1"), "k1", true, "v4"),
      Array("m2", Nil, "k1", true, "v3"),
      Array("notexist", Nil, "k1", true, "v1"),

      // With Freevar
      Array("notexist", Nil, "notexist", true, None),
      Array("m1", Nil, "notexist", true, None),
      Array("m2", Nil, "notexist", true, None),
      Array("m1", List("m2"), "notexist", true, None),
      Array("m3", Nil, "k2", true, "v5"),
      Array("m4", Nil, "k2", true, None),
      Array("m5", Nil, "k2", true, None),
      Array("m6", Nil, "k2", true, None),
      Array("m4", List("m3"), "k2", true, "v6"),
      Array("m5", List("m3"), "k2", true, "v5"),
      Array("m6", List("m3"), "k2", true, "v5"),
      Array("m5", List("m3", "m4"), "k2", true, "v7"),
      Array("m6", List("m3", "m4"), "k2", true, "v6"),
      Array("m6", List("m3", "m4", "m5"), "k2", true, "v8"),

      // Without freeVar
      Array("m3", Nil, "k2", false, "v5"),
      Array("m4", Nil, "k2", false, None),
      Array("m5", Nil, "k2", false, None),
      Array("m6", Nil, "k2", false, None),
      Array("m4", List("m3"), "k2", false, "v6"),
      Array("m5", List("m3"), "k2", false, None),
      Array("m6", List("m3"), "k2", false, None),
      Array("m5", List("m3", "m4"), "k2", false, "v7"),
      Array("m6", List("m3", "m4"), "k2", false, None),
      Array("m6", List("m3", "m4", "m5"), "k2", false, "v8")
    )
  }
107

108
109
110
111
112
113
114
115
116
  @Test(dataProvider = "testGetValueFromMapProvider")
  def testGetValueFromMap(module: String, path: List[String], key: String, freeVar: Boolean, expected: Any): Unit = {
    val map = ConfigTest.map
    val index = new ConfigValueIndex(module, path, key, freeVar)
    val value = Config.getValueFromMap(map, index)
    value match {
      case Some(x) => x.value shouldBe expected
      case None    => value shouldBe expected
    }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  }
}

object ConfigTest {
  val map = Map(
    "k1" -> "v1",
    "m1" -> Map(
      "k1" -> "v2",
      "m2" -> Map(
        "k1" -> "v4"
      )
    ),
    "m2" -> Map(
      "k1" -> "v3"
    ),
    "m3" -> Map(
      "k2" -> "v5",
      "m4" -> Map(
        "k2" -> "v6",
        "m5" -> Map(
          "k2" -> "v7",
          "m6" -> Map(
            "k2" -> "v8"
          )
        )
      )
    )
  )

Peter van 't Hof's avatar
Peter van 't Hof committed
146
  val file = ConfigUtilsTest.writeTemp(ConfigUtils.mapToJson(map).spaces2, "json")
147
148
149
150

  val config = new Config
  config.loadConfigFile(file)
}