ConfigUtilsTest.scala 8.15 KB
Newer Older
1
2
package nl.lumc.sasc.biopet.utils

Peter van 't Hof's avatar
Peter van 't Hof committed
3
import java.io.{ PrintWriter, File }
4

Peter van 't Hof's avatar
Peter van 't Hof committed
5
6
import argonaut.Argonaut._
import argonaut.Json
7
8
9
10
11
12
13
14
import nl.lumc.sasc.biopet.core.config.{ ConfigValueIndex, ConfigValue }
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test

/**
 * Created by pjvan_thof on 1/5/15.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
15
class ConfigUtilsTest extends TestNGSuite with Matchers {
16
  import ConfigUtils._
Peter van 't Hof's avatar
Rename    
Peter van 't Hof committed
17
  import ConfigUtilsTest._
18

Peter van 't Hof's avatar
Peter van 't Hof committed
19
20
21
22
23
24
25
26
27
  @Test def testGetValueFromPath: Unit = {
    getValueFromPath(map1, List("dummy")) shouldBe Some(Map("dummy" -> 1))
    getValueFromPath(map1, List("dummy", "dummy")) shouldBe Some(1)
    getValueFromPath(map1, List("nested3", "nested2", "nested1")) shouldBe Some(Map("dummy" -> 1))
    getValueFromPath(map1, List("notexist", "dummy")) shouldBe None
    getValueFromPath(map1, List("dummy", "notexist")) shouldBe None
  }

  @Test def testGetMapFromPath: Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
28
29
    getMapFromPath(map1, List("dummy")) shouldBe Some(Map("dummy" -> 1))
    getMapFromPath(map1, List("nested3", "nested2", "nested1")) shouldBe Some(Map("dummy" -> 1))
Peter van 't Hof's avatar
Peter van 't Hof committed
30
31
32
33
34
    intercept[IllegalStateException] {
      getMapFromPath(map1, List("dummy", "dummy"))
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
35
36
37
  // Merge maps
  @Test def testMergeMaps: Unit = {
    val mergedMap = mergeMaps(map1, map2)
Peter van 't Hof's avatar
Peter van 't Hof committed
38
39
40
    getValueFromPath(mergedMap, List("nested", "1")) shouldBe Some(1)
    getValueFromPath(mergedMap, List("nested", "2")) shouldBe Some(1)
    getValueFromPath(mergedMap, List("nested", "3")) shouldBe Some(2)
Peter van 't Hof's avatar
Peter van 't Hof committed
41
42
43
44
45
46
47
48
49
50
51
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
  }

  // Json to scala values
  @Test def testFileToJson: Unit = {
    fileToJson(file1) shouldBe json1
    fileToJson(file2) shouldBe json2
    intercept[IllegalStateException] {
      fileToJson(corruptFile)
    }
  }

  @Test def testJsonToMap: Unit = {
    jsonToMap(json1) shouldBe map1
    jsonToMap(json2) shouldBe map2
    intercept[IllegalStateException] {
      jsonToMap(Json.jNumberOrString(1337))
    }
  }

  @Test def testFileToConfigMap: Unit = {
    fileToConfigMap(file1) shouldBe map1
    fileToConfigMap(file2) shouldBe map2
  }

  // Any/scala values to Json objects
  @Test def testAnyToJson: Unit = {
    anyToJson("bla") shouldBe jString("bla")
    anyToJson(1337) shouldBe Json.jNumberOrString(1337)
    anyToJson(13.37) shouldBe Json.jNumberOrString(13.37)
    anyToJson(1337L) shouldBe Json.jNumberOrString(1337L)
    anyToJson(13.37f) shouldBe Json.jNumberOrString(13.37f)
    anyToJson(List("bla")) shouldBe Json.array(anyToJson("bla"))
    anyToJson(anyToJson("bla")) shouldBe anyToJson("bla")
    anyToJson(Map()) shouldBe jEmptyObject
    anyToJson(Map("bla" -> 1337)) shouldBe ("bla" := 1337) ->: jEmptyObject
  }

  @Test def testMapToJson: Unit = {
    mapToJson(Map()) shouldBe jEmptyObject
    mapToJson(Map("bla" -> 1337)) shouldBe ("bla" := 1337) ->: jEmptyObject
    mapToJson(Map("bla" -> Map())) shouldBe ("bla" := jEmptyObject) ->: jEmptyObject
    mapToJson(Map("bla" -> Map("nested" -> 1337))) shouldBe ("bla" := (("nested" := 1337) ->: jEmptyObject)) ->: jEmptyObject
  }

  // Any to scala values
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  @Test def testAny2string: Unit = {
    any2string("bla") shouldBe "bla"
    any2string(1337) shouldBe "1337"
    any2string(true) shouldBe "true"
    any2string(13.37) shouldBe "13.37"
  }

  @Test def testAny2int: Unit = {
    any2int(1337) shouldBe 1337
    any2int("1337") shouldBe 1337
    any2int(13.37) shouldBe 13
    intercept[IllegalStateException] {
      any2int(new Object)
    }
  }

  @Test def testAny2long: Unit = {
    any2long(1337L) shouldBe 1337L
    any2long(1337) shouldBe 1337L
    any2long("1337") shouldBe 1337L
    any2long(13.37) shouldBe 13L
    intercept[IllegalStateException] {
      any2long(new Object)
    }
  }

  @Test def testAny2double: Unit = {
    any2double(13.37) shouldBe 13.37d
    any2double("1337") shouldBe 1337d
    any2double(1337) shouldBe 1337d
    any2double(1337L) shouldBe 1337d
    any2double(1337f) shouldBe 1337d
    intercept[IllegalStateException] {
      any2double(new Object)
    }
  }

  @Test def testAny2float: Unit = {
    any2float(1337d) shouldBe 1337f
    any2float("1337") shouldBe 1337f
    any2float(1337) shouldBe 1337f
    any2float(1337L) shouldBe 1337f
    any2float(13.37f) shouldBe 13.37f
    intercept[IllegalStateException] {
      any2float(new Object)
    }
  }

  @Test def testAny2boolean: Unit = {
    any2boolean(true) shouldBe true
    any2boolean("false") shouldBe false
    any2boolean("true") shouldBe true
    any2boolean(0) shouldBe false
    any2boolean(1) shouldBe true
    intercept[IllegalStateException] {
      any2boolean(new Object)
    }
  }

  @Test def testAny2list: Unit = {
    any2list(Nil) shouldBe Nil
    any2list(List(1, 2, 3, 4)) shouldBe List(1, 2, 3, 4)
    any2list(List(1337)) shouldBe List(1337)
    any2list(1337) shouldBe List(1337)
  }

  @Test def testAny2stringList: Unit = {
    any2stringList(Nil) shouldBe Nil
    any2stringList(List("1337")) shouldBe List("1337")
    any2stringList(List(1337)) shouldBe List("1337")
    any2stringList(1337) shouldBe List("1337")
  }

  @Test def testAny2map: Unit = {
    any2map(Map()) shouldBe Map()
    any2map(Map("bla" -> 1337)) shouldBe Map("bla" -> 1337)
    any2map(null) shouldBe null
    intercept[IllegalStateException] {
      any2map(new Object)
    }
  }

  @Test def testImplicits: Unit = {
    val index = ConfigValueIndex("test", Nil, "test")
    new ImplicitConversions {
      configValue2list(ConfigValue(index, index, List(""))) shouldBe List("")
      configValue2stringList(ConfigValue(index, index, List(""))) shouldBe List("")
      configValue2stringSet(ConfigValue(index, index, List(""))) shouldBe Set("")

      var int: Int = ConfigValue(index, index, 1)
      var long: Long = ConfigValue(index, index, 1)
      var double: Double = ConfigValue(index, index, 1)
      var float: Float = ConfigValue(index, index, 1)
      var boolean: Boolean = ConfigValue(index, index, true)
      var intOption: Option[Int] = ConfigValue(index, index, 1)
      var longOption: Option[Long] = ConfigValue(index, index, 1)
      var doubleOption: Option[Double] = ConfigValue(index, index, 1)
      var floatOption: Option[Float] = ConfigValue(index, index, 1)
      var booleanOption: Option[Boolean] = ConfigValue(index, index, true)
      var string: String = ConfigValue(index, index, "test")
      var file: File = ConfigValue(index, index, "test")
Peter van 't Hof's avatar
Peter van 't Hof committed
187
188

      //TODO: test BiopetQScript error message
189
190
191
    }
  }
}
Peter van 't Hof's avatar
Rename    
Peter van 't Hof committed
192
object ConfigUtilsTest {
Peter van 't Hof's avatar
Peter van 't Hof committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  def writeTemp(text: String): File = {
    val file = File.createTempFile("TestConfigUtils.", ".json")
    val w = new PrintWriter(file)
    w.write(text)
    w.close()
    return file
  }

  val jsonText1 =
    """
       |{
       | "int": 1337,
       | "double": 13.37,
       | "string": "bla",
       | "nested": {
       |   "1": 1,
       |   "2": 1
       | },
       | "list": ["a", "b", "c"],
Peter van 't Hof's avatar
Peter van 't Hof committed
212
213
       | "dummy": { "dummy": 1},
       | "nested3": { "nested2": { "nested1": { "dummy": 1 } } }
Peter van 't Hof's avatar
Peter van 't Hof committed
214
215
216
217
218
219
220
221
222
223
224
225
       |}
     """.stripMargin

  val file1 = writeTemp(jsonText1)

  val json1 = {
    ("int" := 1337) ->:
      ("double" := 13.37) ->:
      ("string" := "bla") ->:
      ("nested" := (("1" := 1) ->: ("2" := 1) ->: jEmptyObject)) ->:
      ("list" := List("a", "b", "c")) ->:
      ("dummy" := ("dummy" := 1) ->: jEmptyObject) ->:
Peter van 't Hof's avatar
Peter van 't Hof committed
226
      ("nested3" := ("nested2" := ("nested1" := ("dummy" := 1) ->: jEmptyObject) ->: jEmptyObject) ->: jEmptyObject) ->:
Peter van 't Hof's avatar
Peter van 't Hof committed
227
228
229
230
231
232
233
234
      jEmptyObject
  }

  val map1 = Map("int" -> 1337,
    "double" -> 13.37,
    "string" -> "bla",
    "nested" -> Map("1" -> 1, "2" -> 1),
    "list" -> List("a", "b", "c"),
Peter van 't Hof's avatar
Peter van 't Hof committed
235
236
    "dummy" -> Map("dummy" -> 1),
    "nested3" -> Map("nested2" -> Map("nested1" -> Map("dummy" -> 1))))
Peter van 't Hof's avatar
Peter van 't Hof committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

  val jsonText2 =
    """
       |{
       | "int": 7331,
       | "nested": {
       |   "2": 2,
       |   "3": 2
       | },
       | "dummy": 1
       |}
     """.stripMargin

  val file2 = writeTemp(jsonText2)

  val json2 = {
    ("int" := 7331) ->:
      ("nested" := (("2" := 2) ->: ("3" := 2) ->: jEmptyObject)) ->:
      ("dummy" := 1) ->:
      jEmptyObject
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
259
  val map2: Map[String, Any] = Map("int" -> 7331,
Peter van 't Hof's avatar
Peter van 't Hof committed
260
261
262
263
264
265
266
267
268
269
270
271
272
    "nested" -> Map("2" -> 2, "3" -> 2),
    "dummy" -> 1)

  val corruptJson =
    """
       |{
       | "int": 1337
       | "double": 13.37
       |}
     """.stripMargin

  val corruptFile = writeTemp(corruptJson)
}