ConfigTest.scala 4.56 KB
Newer Older
1
2
3
4
5
package nl.lumc.sasc.biopet.core.config

import nl.lumc.sasc.biopet.utils.{ ConfigUtilsTest, ConfigUtils }
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
6
import org.testng.annotations.{ DataProvider, Test }
7
8
9
10

/**
 * Created by pjvan_thof on 1/8/15.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
11
class ConfigTest extends TestNGSuite with Matchers with ConfigUtils.ImplicitConversions {
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
  @Test def testLoadConfigFile: Unit = {
    val config = new Config
    config.loadConfigFile(ConfigTest.file)
    config.map shouldBe ConfigTest.map
    config.loadConfigFile(ConfigTest.file)
    config.map shouldBe ConfigTest.map
  }

  @Test def testContains: Unit = {
    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
  }

  @Test def testApply: Unit = {
    ConfigTest.config("m1", Nil, "k1").asString shouldBe "v2"
    ConfigTest.config("m1", Nil, "notexist", default = "default").asString shouldBe "default"
  }

  @Test def testMergeConfigs: Unit = {
    val map1 = Map("1" -> 1)
    val map2 = Map("2" -> 2)
    Config.mergeConfigs(new Config(map1), new Config(map2)).map shouldBe ConfigUtils.mergeMaps(map1, map2)
  }

  @Test def testToString: Unit = {
    val map1 = Map("1" -> 1)
    new Config(map1).toString() shouldBe map1.toString()
  }

44
45
46
47
48
49
50
  @Test def testSkipNested: Unit = {
    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
51
  @DataProvider(name = "testGetValueFromMapProvider")
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  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")
    )
  }
90

91
92
93
94
95
96
97
98
99
  @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
    }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  }
}

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"
          )
        )
      )
    )
  )

  val file = ConfigUtilsTest.writeTemp(ConfigUtils.mapToJson(map).spaces2)

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