Skip to content

Instantly share code, notes, and snippets.

@davidmc24
Last active August 29, 2015 14:13
Show Gist options
  • Save davidmc24/b9ce3535cf630a698970 to your computer and use it in GitHub Desktop.
Save davidmc24/b9ce3535cf630a698970 to your computer and use it in GitHub Desktop.
ModuleConfigurationSpec
/*
* Copyright 2015 the original author or authors.
*
* 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 ratpack.guice
import com.google.inject.AbstractModule
import groovy.transform.NotYetImplemented
import ratpack.exec.Promise
import ratpack.groovy.handling.GroovyContext
import ratpack.groovy.handling.GroovyHandler
import ratpack.groovy.test.embed.GroovyEmbeddedApp
import ratpack.http.HttpUrlBuilder
import ratpack.http.client.HttpClient
import ratpack.server.ServerConfig
import spock.lang.Specification
import javax.inject.Inject
/**
* Demonstrations of different ways to get configuration into modules.
*/
class ModuleConfigurationSpec extends Specification {
def "plain modules can depend on injected objects declared after"() {
expect:
GroovyEmbeddedApp.fromServerFunction { builder ->
builder.registry(Guice.registry { bindings ->
bindings.add(new SampleServiceModule()).add(new SampleServiceConfigModule("that"))
}).handler(SampleServiceHandler)
}.test { client ->
assert client.text == "Fuck that. - Me"
}
}
def "plain modules can depend on injected objects declared before"() {
expect:
GroovyEmbeddedApp.fromServerFunction { builder ->
builder.registry(Guice.registry { bindings ->
bindings.add(new SampleServiceConfigModule("that")).add(new SampleServiceModule())
}).handler(SampleServiceHandler)
}.test { client ->
assert client.text == "Fuck that. - Me"
}
}
def "plain modules can use a JIT binding"() {
expect:
GroovyEmbeddedApp.fromServerFunction { builder ->
builder.registry(Guice.registry { bindings ->
bindings.add(new SampleServiceModule())
}).handler(SampleServiceHandler)
}.test { client ->
assert client.text == "Fuck everything. - Me"
}
}
def "configurable modules can use a default object"() {
expect:
GroovyEmbeddedApp.fromServerFunction { builder ->
builder.registry(Guice.registry { bindings ->
bindings.add(ConfigurableSampleServiceModule)
}).handler(SampleServiceHandler)
}.test { client ->
assert client.text == "Fuck everything. - Me"
}
}
def "configurable modules can override config object creation"() {
expect:
GroovyEmbeddedApp.fromServerFunction { builder ->
builder.registry(Guice.registry { bindings ->
bindings.add(MyConfigurableSampleServiceModule)
}).handler(SampleServiceHandler)
}.test { client ->
assert client.text == "Fuck my life. - Me"
}
}
def "configurable modules can use a configuration action"() {
expect:
GroovyEmbeddedApp.fromServerFunction { builder ->
builder.registry(Guice.registry { bindings ->
bindings.add(ConfigurableSampleServiceModule) { config ->
config.endpoint = "everyone"
}
}).handler(SampleServiceHandler)
}.test { client ->
assert client.text == "Everyone can go and fuck off. - Me"
}
}
def "configurable modules can NOT use an injected object if declared before"() {
expect:
GroovyEmbeddedApp.fromServerFunction { builder ->
builder.registry(Guice.registry { bindings ->
bindings.add(new SampleServiceConfigModule("that")).add(ConfigurableSampleServiceModule)
}).handler(SampleServiceHandler)
}.test { client ->
assert client.text == "Fuck everything. - Me"
}
}
def "configurable modules can use an injected object if declared after"() {
expect:
GroovyEmbeddedApp.fromServerFunction { builder ->
builder.registry(Guice.registry { bindings ->
bindings.add(ConfigurableSampleServiceModule).add(new SampleServiceConfigModule("that"))
}).handler(SampleServiceHandler)
}.test { client ->
assert client.text == "Fuck that. - Me"
}
}
@NotYetImplemented
def "configurable modules can use a specified config object instance with config action"() {
def configObject = new SampleServiceConfig("everyone")
expect:
GroovyEmbeddedApp.fromServer { builder ->
builder.registry(Guice.registry { bindings ->
bindings.add(ConfigurableSampleServiceModule, configObject) { config ->
config.defaultFrom = "Nostradamus"
}
}).handler(SampleServiceHandler)
}.test { client ->
assert client.text == "Everyone can go and fuck off. - Me"
}
}
def "configurable modules can NOT use an injected object with config action"() {
expect:
GroovyEmbeddedApp.fromServerFunction { builder ->
builder.registry(Guice.registry { bindings ->
bindings.add(ConfigurableSampleServiceModule, { config ->
config.defaultFrom = "Nostradamus"
}).add(new SampleServiceConfigModule("that"))
}).handler(SampleServiceHandler)
}.test { client ->
assert client.text == "Fuck that. - Me"
}
}
static class SampleServiceClient {
@Inject
HttpClient httpClient
@Inject
SampleServiceConfig config
Promise<String> callService(String from) {
httpClient.get(HttpUrlBuilder.base(config.uri).path(config.endpoint).path(from).build()).map { it.body.text }
}
}
static class SampleServiceConfigModule extends AbstractModule {
private final String endpoint
SampleServiceConfigModule(String endpoint) {
this.endpoint = endpoint
}
@Override
protected void configure() {
bind(SampleServiceConfig).toInstance(new SampleServiceConfig(endpoint))
}
}
static class SampleServiceModule extends AbstractModule {
@Override
protected void configure() {
bind(SampleServiceClient)
bind(SampleServiceHandler)
}
}
static class SampleServiceHandler extends GroovyHandler {
@Inject
SampleServiceClient client
@Inject
SampleServiceConfig config
@Override
protected void handle(GroovyContext context) {
def from = context.request.queryParams.get("from") ?: config.defaultFrom
client.callService(from).then { content ->
context.response.send(content)
}
}
}
static class ConfigurableSampleServiceModule extends ConfigurableModule<SampleServiceConfig> {
@Override
protected void configure() {
bind(SampleServiceClient)
bind(SampleServiceHandler)
}
}
static class MyConfigurableSampleServiceModule extends ConfigurableSampleServiceModule {
@Override
protected SampleServiceConfig createConfig(ServerConfig serverConfig) {
def config = super.createConfig(serverConfig)
config.endpoint = "life"
config
}
}
static class SampleServiceConfig {
URI uri = URI.create("http://ratpack-foaas.herokuapp.com/")
String defaultFrom = "Me"
String endpoint = "everything"
private ServerConfig serverConfig
SampleServiceConfig(String endpoint) {
this.endpoint = endpoint
}
@Inject
SampleServiceConfig(ServerConfig serverConfig) {
this.serverConfig = serverConfig
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment