Skip to content

Instantly share code, notes, and snippets.

@tobyclemson
Last active December 15, 2015 20:00
Show Gist options
  • Save tobyclemson/5315742 to your computer and use it in GitHub Desktop.
Save tobyclemson/5315742 to your computer and use it in GitHub Desktop.
Service testing wiring
class ARealService[ARealServiceConfiguration] extends Service[T] {
def initialize(bootstrap: Bootstrap[ARealServiceConfiguration]) {
// Initialize bundles here
}
def run(configuration: ARealServiceConfiguration, environment: Environment) {
// Here's where DBI gets constructed
// Wire up resources and configure object mapper here
}
}
class NullService[T <: Configuration] extends Service[T] {
def initialize(bootstrap: Bootstrap[T]) {}
def run(configuration: T, environment: Environment) {}
}
class ServiceSpecification
extends ResourceTest
with Specification {
def setUpResources() {
val configuration = ConfigurationLoader.load(getValidator, getObjectMapperFactory)
val environment = new TestEnvironment(this, configuration, getValidator, getObjectMapperFactory)
val bootstrap = new TestBootstrap[ARealServiceConfiguration](this, configuration, environment, getObjectMapperFactory)
ARealService.initialize(bootstrap)
ARealService.run(configuration, environment)
environment.resources.foreach(addResource(_))
environment.resourceClasses.foreach(addResource(_))
}
}
class TestBootstrap[C <: Configuration](
private val test: ResourceTest,
private val configuration: C,
private val environment: Environment,
private val objectMapperFactory: ObjectMapperFactory)
extends Bootstrap[C](new NullService[C]) {
override def addBundle(bundle: Bundle) {
bundle.initialize(this)
bundle.run(environment)
}
override def addBundle(bundle: ConfiguredBundle[_ >: C]) {
bundle.initialize(this)
bundle.run(configuration, environment)
}
override def getObjectMapperFactory: ObjectMapperFactory = objectMapperFactory
override def addCommand(command: Command) {}
override def addCommand(command: ConfiguredCommand[C]) {}
override def runWithBundles(configuration: C, environment: Environment) {}
override def getCommands: ImmutableList[Command] = ImmutableList.builder().build()
override def getName: String = "test-service"
override def setName(name: String) {}
}
class TestEnvironment(
private val test: ResourceTest,
private val configuration: Configuration,
private val validator: Validator,
private val objectMapperFactory: ObjectMapperFactory)
extends Environment("test-environment", configuration, objectMapperFactory, validator) {
val resources = new mutable.MutableList[Any]()
val resourceClasses = new mutable.MutableList[Class[_]]()
override def addProvider(provider: Any) {
test.addProvider(provider)
}
override def addProvider(klass: Class[_]) {
test.addProvider(klass)
}
override def addResource(resource: Any) {
resources += resource
}
override def addResource(klass: Class[_]) {
resourceClasses += klass
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment