Reputation: 5452
In a spark project we use Typesafe Config. We have a big config file with some (necessary) redundancy. It is quite easy to refer the wrong branch of the config json and sometimes the error slips to production code.
I need to verify that all the calls of config.getString(x) with x literal will not fail for a given config file.
I'd like to write a unit test that checks every string used in my application to obtain a config value.
A possible solution we found is to preload all the config values to a case class, so
val rawPath = config.getString("comp1.data.files.rawData")
val coresNumber = config.getLong("comp1.setup.cores")
would become
case class ConfigData(rawPath:String, coresNumber:Long)
def initConfig():ConfigData ={
val rawPath = config.getString("comp1.data.files.rawData")
val coresNumber = config.getLong("comp1.setup.cores")
ConfigData(rawPat,coresNumber)
}
val conf = initConfig()
val rawPath = conf.rawPath
val coresNumner = conf.coresNumber
and then simply call initData()
to test for config load errors.
I've also thought of using scala reflection but I'd need to find all the places in my code where config.getString(x)
is called and then obtain the x
to test their existence in the config file, but I can't find a way to get all the instances of a method call and create a test on the parameter.
Is there anything I haven't thought of?
Upvotes: 1
Views: 802
Reputation: 14803
One solution is to provide the configuration in a Singleton, which you initialise when starting the server - so the Server starts only if the configuration is correct. Or run the configurations in Unit Tests.
In this Singleton you load your Configuration in single values
or case classes
depending on amount of values.
We use pureconfig which makes it pretty simple to map the configuration directly into case classes
.
Here is an Example without Pure Script: DemoAdapterContext and AdaptersContext.
Let me know if you need more infos.
Upvotes: 1