Skip to content

Commit

Permalink
Merge pull request #2 from beckje01/sendTest
Browse files Browse the repository at this point in the history
Send Message Test
  • Loading branch information
beckje01 committed Jun 23, 2016
2 parents 6971a90 + 75661cc commit 662edf0
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 5 deletions.
Expand Up @@ -9,6 +9,7 @@
import ratpack.service.StopEvent;

import java.io.IOException;
import java.util.Optional;

public class RabbitConnectionService implements Service {

Expand Down Expand Up @@ -37,8 +38,8 @@ public void onStop(StopEvent event) throws Exception {
connection.close(config.getCloseTimeoutMilliseconds());
}

public Channel getNewChannel() throws IOException {
return connection.createChannel();
public Optional<Channel> getNewChannel() throws IOException {
return Optional.ofNullable(connection.createChannel());
}

}
Expand Up @@ -20,13 +20,12 @@ public RabbitQueueService(RabbitConnectionService rabbitConnectionService) {
this.rabbitConnectionService = rabbitConnectionService;
}

abstract ChannelConfig getChannelConfig();
public abstract ChannelConfig getChannelConfig();

@Override
public void onStart(StartEvent event) throws Exception {
channel = rabbitConnectionService.getNewChannel();
channel = rabbitConnectionService.getNewChannel().get();
ChannelConfig config = getChannelConfig();

channel.queueDeclare(config.getQueueName(), config.getDurable(), config.getExclusive(), config.getAutoDelete(), config.getArguments());
}

Expand Down
@@ -0,0 +1,26 @@
package smartthings.ratpack.rabbit

import ratpack.test.ApplicationUnderTest
import ratpack.test.http.TestHttpClient
import smartthings.ratpack.rabbit.example.ExampleApp
import spock.lang.Shared
import spock.lang.Specification

class SimpleSendSpec extends Specification{

@Shared
ApplicationUnderTest aut = ExampleApp.getExampleApp()

@Delegate
TestHttpClient client = aut.httpClient

def "Simple Send"(){
when:
def resp = get("/")

then:
resp.body.text == "working"
resp.statusCode == 200

}
}
@@ -0,0 +1,49 @@
package smartthings.ratpack.rabbit.example

import groovy.util.logging.Slf4j
import ratpack.config.ConfigData
import ratpack.groovy.test.embed.GroovyEmbeddedApp
import ratpack.guice.Guice
import smartthings.ratpack.rabbit.RabbitProducerModule

@Slf4j
class ExampleApp {

static String getTestRabbitServer() {
return System.getenv("RABBIT_SERVER") ?: '192.168.99.100'
}

static def getConfig() {
def config = new RabbitProducerModule.Config()
config.setHostName(getTestRabbitServer())
config.setUsername("guest")
config.setPassword("guest")
config.setPortNumber(5672)
config.setVirtualHost("/")
return config
}

static def getExampleApp() {
return GroovyEmbeddedApp.of {
registry Guice.registry {
it.moduleConfig(RabbitProducerModule, getConfig())
it.bind(TestQueueService)
}

handlers {
all {
TestQueueService testQueueService = registry.get(TestQueueService)
def helloWorld = "Hello World".bytes
testQueueService.send(helloWorld).onError({ t ->
response.status(500)
t.printStackTrace()
render t.toString()
}).then {
render "working"
}
}
}

}
}
}
@@ -0,0 +1,29 @@
package smartthings.ratpack.rabbit.example

import com.google.inject.Inject
import smartthings.ratpack.rabbit.ChannelConfig
import smartthings.ratpack.rabbit.RabbitConnectionService
import smartthings.ratpack.rabbit.RabbitQueueService

import javax.inject.Singleton

@Singleton
class TestQueueService extends RabbitQueueService {

private ChannelConfig channelConfig = new ChannelConfig(queueName: "test")

@Inject
TestQueueService(RabbitConnectionService rabbitConnectionService) {
super(rabbitConnectionService)
}

@Override
public ChannelConfig getChannelConfig() {
return channelConfig
}

@Override
String getName() {
return "Rabbit TEST queue Service"
}
}

0 comments on commit 662edf0

Please sign in to comment.