Skip to content

Commit

Permalink
Merge pull request #46 from avast/ArgumentsFix
Browse files Browse the repository at this point in the history
Fix: declare/bind arguments were not loading correctly from HOCON
  • Loading branch information
jendakol authored Feb 17, 2020
2 parents da12332 + bfb1fbc commit 93b0856
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions core/src/test/scala/com/avast/clients/rabbitmq/LiveTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ class LiveTest extends TestBase with ScalaFutures {
_ <- rabbitConnection.bindQueue("bindQueue")
} yield ()).unsafeRunSync()

assertResult(Map("x-max-length" -> 10000))(testHelper.queue.getArguments(queueName2))

assertResult(0)(testHelper.queue.getMessagesCount(queueName1))
assertResult(0)(testHelper.queue.getMessagesCount(queueName2))

Expand Down
29 changes: 28 additions & 1 deletion core/src/test/scala/com/avast/clients/rabbitmq/TestHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package com.avast.clients.rabbitmq
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

import io.circe.Decoder
import io.circe.generic.auto._
import io.circe.parser._
import scalaj.http.Http

import scala.util.Success

//noinspection ScalaStyle
class TestHelper(host: String, port: Int) {

Expand Down Expand Up @@ -46,6 +49,21 @@ class TestHelper(host: String, port: Int) {
}
}

def getArguments(queueName: String): Map[String, Any] = {
val encoded = URLEncoder.encode(queueName, StandardCharsets.UTF_8.toString)

val resp = Http(s"$RootUri/queues/%2f/$encoded").auth("guest", "guest").asString.body

decode[QueueProperties](resp) match {
case Right(p) =>
p.arguments.getOrElse {
Console.err.println(s"Could not extract arguments for $queueName!")
Map.empty
}
case r => throw new IllegalStateException(s"Wrong response $r")
}
}

def delete(queueName: String, ifEmpty: Boolean = false, ifUnused: Boolean = false): Unit = {
println(s"Deleting queue: $queueName")
val encoded = URLEncoder.encode(queueName, StandardCharsets.UTF_8.toString)
Expand All @@ -63,7 +81,16 @@ class TestHelper(host: String, port: Int) {
}
}

private case class QueueProperties(messages: Int, message_stats: Option[MessagesStats])
private implicit val anyDecoder: Decoder[Any] = Decoder.decodeJson.emapTry { json =>
// we are in test, it's enough to support just Int and String here
if (json.isNumber) {
json.as[Int].toTry
} else if (json.isString) {
json.as[String].toTry
} else Success(null)
}

private case class QueueProperties(messages: Int, message_stats: Option[MessagesStats], arguments: Option[Map[String, Any]])
private case class MessagesStats(publish: Int, ack: Option[Int])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ class PureconfigImplicits(implicit namingConvention: NamingConvention = CamelCas
implicit val autoBindQueueConfigReader: ConfigReader[AutoBindQueueConfig] = deriveReader
implicit val autoBindExchangeConfigReader: ConfigReader[AutoBindExchangeConfig] = deriveReader
implicit val producerPropertiesConfigReader: ConfigReader[ProducerPropertiesConfig] = deriveReader
implicit val declareArgumentsConfigReader: ConfigReader[DeclareArgumentsConfig] = deriveReader
implicit val bindArgumentsConfigReader: ConfigReader[BindArgumentsConfig] = deriveReader

implicit val logLevelReader: ConfigReader[Level] = ConfigReader.stringConfigReader.map(Level.valueOf)
implicit val recoveryDelayHandlerReader: ConfigReader[RecoveryDelayHandler] = RecoveryDelayHandlerReader
Expand Down Expand Up @@ -119,6 +117,9 @@ class PureconfigImplicits(implicit namingConvention: NamingConvention = CamelCas
cur.asObjectCursor.map(_.value.asScala.toMap.mapValues(_.unwrapped()))
}

implicit val declareArgumentsConfigReader: ConfigReader[DeclareArgumentsConfig] = mapStringAnyReader.map(DeclareArgumentsConfig)
implicit val bindArgumentsConfigReader: ConfigReader[BindArgumentsConfig] = mapStringAnyReader.map(BindArgumentsConfig)

private def withType[A](cur: ConfigCursor)(f: (Config, String) => Result[A]): Result[A] = {
cur.asObjectCursor.right.map(_.value.toConfig).flatMap { config =>
val `type` = config.getString("type")
Expand Down

0 comments on commit 93b0856

Please sign in to comment.