/** Test that the broker starts up and sets SERVICE_UP correctly. */
 @Test(groups = {"Integration", "WIP"})
 public void canStartupAndShutdown() throws Exception {
   rabbit = app.createAndManageChild(EntitySpec.create(RabbitBroker.class));
   rabbit.start(ImmutableList.of(testLocation));
   EntityTestUtils.assertAttributeEqualsEventually(rabbit, Startable.SERVICE_UP, true);
   rabbit.stop();
   assertFalse(rabbit.getAttribute(Startable.SERVICE_UP));
 }
  /** Test that an AMQP client can connect to and use the broker. */
  @Test(groups = {"Integration", "WIP"})
  public void testClientConnection() throws Exception {
    rabbit = app.createAndManageChild(EntitySpec.create(RabbitBroker.class));
    rabbit.start(ImmutableList.of(testLocation));
    EntityTestUtils.assertAttributeEqualsEventually(rabbit, Startable.SERVICE_UP, true);

    byte[] content = "MessageBody".getBytes(Charsets.UTF_8);
    String queue = "queueName";
    Channel producer = null;
    Channel consumer = null;
    try {
      producer = getAmqpChannel(rabbit);
      consumer = getAmqpChannel(rabbit);

      producer.queueDeclare(queue, true, false, false, ImmutableMap.<String, Object>of());
      producer.queueBind(queue, AmqpExchange.DIRECT, queue);
      producer.basicPublish(AmqpExchange.DIRECT, queue, null, content);

      QueueingConsumer queueConsumer = new QueueingConsumer(consumer);
      consumer.basicConsume(queue, true, queueConsumer);

      QueueingConsumer.Delivery delivery =
          queueConsumer.nextDelivery(60 * 1000l); // one minute timeout
      assertEquals(delivery.getBody(), content);
    } finally {
      closeSafely(producer, 10 * 1000);
      closeSafely(consumer, 10 * 1000);
    }
  }
 private Channel getAmqpChannel(RabbitBroker rabbit) throws Exception {
   String uri = rabbit.getAttribute(MessageBroker.BROKER_URL);
   log.warn("connecting to rabbit {}", uri);
   ConnectionFactory factory = new ConnectionFactory();
   factory.setUri(uri);
   Connection conn = factory.newConnection();
   Channel channel = conn.createChannel();
   return channel;
 }