blob: f8560a68e48a1159eaba7ae9e2b65eea933c2330 [file] [log] [blame]
/*
* Copyright 2015-2016 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package whisk.common
import org.junit.runner.RunWith
import org.scalatest.FlatSpec
import org.scalatest.Matchers
import org.scalatest.junit.JUnitRunner
import common.StreamLogging
@RunWith(classOf[JUnitRunner])
class ConfigTests
extends FlatSpec
with Matchers
with StreamLogging {
"Config" should "gets default value" in {
val config = new Config(Map("a" -> "A"))(Map())
assert(config.isValid && config("a") == "A")
}
it should "get value from environemnt" in {
val config = new Config(Map("a" -> null, "b" -> ""))(Map("A" -> "xyz"))
assert(config.isValid && config("a") == "xyz" && config("b") == "")
}
it should "not be valid when environment does not provide value" in {
val config = new Config(Map("a" -> null))(Map())
assert(!config.isValid && config("a") == null)
}
it should "be invalid if same property is required and optional and still not defined" in {
val config = new Config(Map("a" -> null), optionalProperties = Set("a"))(Map())
assert(!config.isValid)
}
it should "read optional value" in {
val config = new Config(Map("a" -> "A", "x" -> "X"), optionalProperties = Set("b", "c", "x"))(Map("B" -> "B"))
assert(config.isValid)
assert(config("a") == "A")
assert(config("b") == "B")
assert(config("c") == "")
assert(config("x") == "X")
}
it should "override a value with optional value" in {
val config = new Config(Map("a" -> null, "x" -> "X"), optionalProperties = Set("b", "c", "x"))(Map("A" -> "A", "B" -> "B"))
assert(config.isValid && config("a") == "A" && config("b") == "B")
assert(config("a", "b") == "B")
assert(config("a", "c") == "A")
assert(config("c") == "")
assert(config("x") == "X")
assert(config("x", "c") == "X")
assert(config("x", "d") == "X")
assert(config("d", "x") == "X")
assert(config("c", "x") == "X")
assert(config("c", "d") == "")
}
}