ConfigUtilsTest.scala 10.9 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.
 */
16
17
package nl.lumc.sasc.biopet.utils

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

Peter van 't Hof's avatar
Peter van 't Hof committed
20
21
import argonaut.Argonaut._
import argonaut.Json
Peter van 't Hof's avatar
Peter van 't Hof committed
22
import nl.lumc.sasc.biopet.utils.config.{ ConfigValue, ConfigValueIndex }
23
24
25
26
27
import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
28
29
 * Test class for [[ConfigUtils]]
 *
30
31
 * Created by pjvan_thof on 1/5/15.
 */
Peter van 't Hof's avatar
Peter van 't Hof committed
32
class ConfigUtilsTest extends TestNGSuite with Matchers {
33
  import ConfigUtils._
Peter van 't Hof's avatar
Rename    
Peter van 't Hof committed
34
  import ConfigUtilsTest._
35

Peter van 't Hof's avatar
Peter van 't Hof committed
36
  @Test def testGetValueFromPath(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
37
38
39
40
41
42
43
    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
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
44
  @Test def testGetMapFromPath(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
45
46
    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
47
48
49
50
51
    intercept[IllegalStateException] {
      getMapFromPath(map1, List("dummy", "dummy"))
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
52
  // Merge maps
Peter van 't Hof's avatar
Peter van 't Hof committed
53
  @Test def testMergeMaps(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
54
    val mergedMap = mergeMaps(map1, map2)
Peter van 't Hof's avatar
Peter van 't Hof committed
55
56
57
    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
58
59
60
  }

  // Json to scala values
Peter van 't Hof's avatar
Peter van 't Hof committed
61
  @Test def testFileToJson(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
62
63
64
65
66
67
68
    fileToJson(file1) shouldBe json1
    fileToJson(file2) shouldBe json2
    intercept[IllegalStateException] {
      fileToJson(corruptFile)
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
69
  @Test def testJsonToMap(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
70
71
72
73
74
75
76
    jsonToMap(json1) shouldBe map1
    jsonToMap(json2) shouldBe map2
    intercept[IllegalStateException] {
      jsonToMap(Json.jNumberOrString(1337))
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
77
  @Test def testFileToConfigMap(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
78
79
80
81
82
    fileToConfigMap(file1) shouldBe map1
    fileToConfigMap(file2) shouldBe map2
  }

  // Any/scala values to Json objects
Peter van 't Hof's avatar
Peter van 't Hof committed
83
  @Test def testAnyToJson(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
84
85
86
87
88
89
90
91
92
93
94
    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
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
95
  @Test def testMapToJson(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
96
97
98
99
100
101
102
    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
Peter van 't Hof's avatar
Peter van 't Hof committed
103
  @Test def testAny2string(): Unit = {
104
105
106
107
108
109
    any2string("bla") shouldBe "bla"
    any2string(1337) shouldBe "1337"
    any2string(true) shouldBe "true"
    any2string(13.37) shouldBe "13.37"
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
110
  @Test def testAny2int(): Unit = {
111
112
113
114
115
116
117
118
    any2int(1337) shouldBe 1337
    any2int("1337") shouldBe 1337
    any2int(13.37) shouldBe 13
    intercept[IllegalStateException] {
      any2int(new Object)
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
119
  @Test def testAny2long(): Unit = {
120
121
122
123
124
125
126
127
128
    any2long(1337L) shouldBe 1337L
    any2long(1337) shouldBe 1337L
    any2long("1337") shouldBe 1337L
    any2long(13.37) shouldBe 13L
    intercept[IllegalStateException] {
      any2long(new Object)
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
129
  @Test def testAny2double(): Unit = {
130
131
132
133
134
135
136
137
138
139
    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)
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
140
  @Test def testAny2float(): Unit = {
141
142
143
144
145
146
147
148
149
150
    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)
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
151
  @Test def testAny2boolean(): Unit = {
152
153
154
155
156
157
158
159
160
161
    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)
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
162
  @Test def testAny2list(): Unit = {
163
164
165
166
167
168
    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)
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
169
  @Test def testAny2stringList(): Unit = {
170
171
172
173
174
175
    any2stringList(Nil) shouldBe Nil
    any2stringList(List("1337")) shouldBe List("1337")
    any2stringList(List(1337)) shouldBe List("1337")
    any2stringList(1337) shouldBe List("1337")
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
176
  @Test def testAny2map(): Unit = {
177
178
179
180
181
182
183
184
    any2map(Map()) shouldBe Map()
    any2map(Map("bla" -> 1337)) shouldBe Map("bla" -> 1337)
    any2map(null) shouldBe null
    intercept[IllegalStateException] {
      any2map(new Object)
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
185
  @Test def testImplicits(): Unit = {
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    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
204
205

      //TODO: test BiopetQScript error message
206
207
    }
  }
208
209
210
211
212
213
214
215
216

  @Test def testMergeConflict: Unit = {
    val map1 = Map("c" -> "1")
    val map2 = Map("c" -> "2")
    mergeMaps(map1, map2) shouldBe Map("c" -> "1")
    mergeMaps(map1, map2, (a, b, k) => a.toString + b.toString) shouldBe Map("c" -> "12")
    mergeMaps(map2, map1, (a, b, k) => a.toString + b.toString) shouldBe Map("c" -> "21")
    mergeMaps(map2, map2, (a, b, k) => a.toString + b.toString) shouldBe Map("c" -> "22")
  }
217
218
219

  @Test def testNestedMergeConflict: Unit = {
    val map1 = Map("c" -> Map("x" -> "1"))
Peter van 't Hof's avatar
Peter van 't Hof committed
220
    val map2 = Map("c" -> Map("x" -> "2"))
221
222
223
224
225
    mergeMaps(map1, map2) shouldBe Map("c" -> Map("x" -> "1"))
    mergeMaps(map1, map2, (a, b, k) => a.toString + b.toString) shouldBe Map("c" -> Map("x" -> "12"))
    mergeMaps(map2, map1, (a, b, k) => a.toString + b.toString) shouldBe Map("c" -> Map("x" -> "21"))
    mergeMaps(map2, map2, (a, b, k) => a.toString + b.toString) shouldBe Map("c" -> Map("x" -> "22"))
  }
226
227
228
229
230

  @Test def testFilterEmtpyMapValues: Unit = {
    ConfigUtils.filterEmtpyMapValues(Map("bla" -> "bla")) shouldBe Map("bla" -> "bla")
    ConfigUtils.filterEmtpyMapValues(Map("bla" -> Map())) shouldBe Map()
    ConfigUtils.filterEmtpyMapValues(Map("bla" -> Map("bla" -> "bla"))) shouldBe Map("bla" -> Map("bla" -> "bla"))
Peter van 't Hof's avatar
Peter van 't Hof committed
231
    ConfigUtils.filterEmtpyMapValues(Map("bla" -> Map("bla" -> Map()))) shouldBe Map("bla" -> Map("bla" -> Map()))
232
233
234
235
    ConfigUtils.filterEmtpyMapValues(Map("bla" -> Map("bla" -> "bla"), "bla2" -> "bla")) shouldBe Map("bla" -> Map("bla" -> "bla"), "bla2" -> "bla")
  }

  @Test def testUniqeKeys: Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
236
237
238
239
    ConfigUtils.uniqueKeys(Map("bla" -> "bla"), Map("bla" -> "bla")) shouldBe Map()
    ConfigUtils.uniqueKeys(Map("bla" -> "bla"), Map()) shouldBe Map("bla" -> "bla")
    ConfigUtils.uniqueKeys(Map("bla" -> Map("bla" -> "bla")), Map("bla" -> Map("bla" -> "bla"))) shouldBe Map()
    ConfigUtils.uniqueKeys(Map("bla" -> Map("bla" -> "bla")), Map("bla" -> Map())) shouldBe Map("bla" -> Map("bla" -> "bla"))
240
  }
241
}
242

Peter van 't Hof's avatar
Rename    
Peter van 't Hof committed
243
object ConfigUtilsTest {
Peter van 't Hof's avatar
Peter van 't Hof committed
244
245
  def writeTemp(text: String, extension: String): File = {
    val file = File.createTempFile("TestConfigUtils.", extension)
246
    file.deleteOnExit()
Peter van 't Hof's avatar
Peter van 't Hof committed
247
248
249
    val w = new PrintWriter(file)
    w.write(text)
    w.close()
Peter van 't Hof's avatar
Peter van 't Hof committed
250
    file
Peter van 't Hof's avatar
Peter van 't Hof committed
251
252
253
254
255
256
257
258
259
260
261
262
263
  }

  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
264
265
       | "dummy": { "dummy": 1},
       | "nested3": { "nested2": { "nested1": { "dummy": 1 } } }
Peter van 't Hof's avatar
Peter van 't Hof committed
266
267
268
       |}
     """.stripMargin

Peter van 't Hof's avatar
Peter van 't Hof committed
269
  val file1 = writeTemp(jsonText1, ".json")
Peter van 't Hof's avatar
Peter van 't Hof committed
270
271
272
273
274
275
276
277

  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
278
      ("nested3" := ("nested2" := ("nested1" := ("dummy" := 1) ->: jEmptyObject) ->: jEmptyObject) ->: jEmptyObject) ->:
Peter van 't Hof's avatar
Peter van 't Hof committed
279
280
281
282
283
284
285
286
      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
287
288
    "dummy" -> Map("dummy" -> 1),
    "nested3" -> Map("nested2" -> Map("nested1" -> Map("dummy" -> 1))))
Peter van 't Hof's avatar
Peter van 't Hof committed
289
290
291
292
293
294
295
296
297
298
299
300
301

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

Peter van 't Hof's avatar
Peter van 't Hof committed
302
  val file2 = writeTemp(jsonText2, ".yaml")
Peter van 't Hof's avatar
Peter van 't Hof committed
303
304
305
306
307
308
309
310

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

Peter van 't Hof's avatar
Peter van 't Hof committed
311
  val map2: Map[String, Any] = Map("int" -> 7331,
Peter van 't Hof's avatar
Peter van 't Hof committed
312
313
314
315
316
317
318
319
320
321
322
    "nested" -> Map("2" -> 2, "3" -> 2),
    "dummy" -> 1)

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

Peter van 't Hof's avatar
Peter van 't Hof committed
323
  val corruptFile = writeTemp(corruptJson, ".json")
Peter van 't Hof's avatar
Peter van 't Hof committed
324
}