Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
feat: fix amqp basic get + add scenario basic get amqp
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrouand authored and KarimGl committed Feb 16, 2023
1 parent ba9bb37 commit 85a8c7a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fun ChutneyStepBuilder.AmqpBasicGetAction(
strategy: Strategy? = null
) {
implementation = ChutneyStepImpl(
type = "amqp-basic-consume",
type = "amqp-basic-get",
target = target,
inputs = listOf(
"queue-name" to queueName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,23 @@ import com.chutneytesting.kotlin.dsl.*

const val RABBITMQ_EXCHANGE = "my.exchange"
const val RABBITMQ_QUEUE = "my.queue"
val amqp_scenario = Scenario(title = "Films library") {

val amqp_scenario = Scenario(title = "Publish - consume") {

val title = "Castle in the Sky"
val director = "Hayao Miyazaki"
val category = "fantasy"

When("I publish my favorite film") {
AmqpBasicPublishAction(
target = "RABBITMQ_TARGET",
exchangeName = RABBITMQ_EXCHANGE,
routingKey = "children.fiction",
headers = mapOf(
"season" to "1",
),
properties = mapOf(
"content_type" to "application/json",
),
payload = """
{
"title": "Castle in the Sky",
"director": "Hayao Miyazaki",
"rating": 78,
"category": "fiction"
}
""".trimIndent(),
)
publishFilm(title, director, category)
}

Then("I consume available films from my queue") {
AmqpBasicConsumeAction(
target = "RABBITMQ_TARGET",
queueName = RABBITMQ_QUEUE,
nbMessages = 1,
selector = "\$..[?(\$.headers.season=='1')]",
selector = "\$..[?(\$.headers.category=='$category')]",
timeout = "5 sec",
ack = true
)
Expand All @@ -42,11 +29,59 @@ val amqp_scenario = Scenario(title = "Films library") {
And("I check that I got my favorite film") {
AssertAction(
asserts = listOf(
"#headers.get(0).get('season').equals('1')".elEval(),
"#jsonPath(#payload, '\$.title').equals('Castle in the Sky')".elEval(),
"#jsonPath(#payload, '\$.director').equals('Hayao Miyazaki')".elEval(),
"#headers.get(0).get('category').equals('$category')".elEval(),
"#jsonPath(#payload, '\$.title').equals('$title')".elEval(),
"#jsonPath(#payload, '\$.director').equals('$director')".elEval(),
)
)
}

}

val amqp_scenario_2 = Scenario(title = "Publish - get") {

val title = "Demon slayer"
val director = "Koyoharu Gotôge"
val category = "dark fantasy"

When("I publish my favorite film") {
publishFilm(title, director, category)
}

Then("I get 1 film from my queue") {
AmqpBasicGetAction(
target = "RABBITMQ_TARGET",
queueName = RABBITMQ_QUEUE
)
}

And("I check that I got my favorite film") {
AssertAction(
asserts = listOf(
"#headers.get('category').equals('$category')".elEval(),
"#jsonPath(#payload, '\$.title').equals('$title')".elEval(),
"#jsonPath(#payload, '\$.director').equals('$director')".elEval(),
)
)
}
}

private fun ChutneyStepBuilder.publishFilm(title: String, director: String, category: String) {
AmqpBasicPublishAction(
target = "RABBITMQ_TARGET",
exchangeName = RABBITMQ_EXCHANGE,
routingKey = "children.film",
headers = mapOf(
"category" to category,
),
properties = mapOf(
"content_type" to "application/json",
),
payload = """
{
"title": "$title",
"director": "$director"
}
""".trimIndent(),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.chutneytesting.example.amqp
import com.chutneytesting.example.scenario.RABBITMQ_EXCHANGE
import com.chutneytesting.example.scenario.RABBITMQ_QUEUE
import com.chutneytesting.example.scenario.amqp_scenario
import com.chutneytesting.example.scenario.amqp_scenario_2
import com.chutneytesting.kotlin.dsl.ChutneyEnvironment
import com.chutneytesting.kotlin.dsl.Environment
import com.chutneytesting.kotlin.launcher.Launcher
import org.junit.jupiter.api.BeforeEach
Expand All @@ -18,6 +20,7 @@ class AmqpScenarioTest {

private var rabbitAddress: String = ""
private var rabbitPort: Int = 0
private var environment: ChutneyEnvironment = ChutneyEnvironment("default value")

@Container
val rabbitmqContainer = RabbitMQContainer(DockerImageName.parse("rabbitmq:3-management"))
Expand All @@ -26,22 +29,27 @@ class AmqpScenarioTest {
Collections.emptyMap()
)
.withQueue(RABBITMQ_QUEUE)
.withBinding(RABBITMQ_EXCHANGE, RABBITMQ_QUEUE, Collections.emptyMap(), "children.*", "queue");
.withBinding(RABBITMQ_EXCHANGE, RABBITMQ_QUEUE, Collections.emptyMap(), "children.*", "queue")

@BeforeEach
fun setUp() {
rabbitAddress = rabbitmqContainer.host
rabbitPort = rabbitmqContainer.firstMappedPort
}

@Test
fun `publish & consume amqp message`() {
val environment = Environment(name = "local", description = "local environment") {
environment = Environment(name = "local", description = "local environment") {
Target {
Name("RABBITMQ_TARGET")
Url("amqp://$rabbitAddress:$rabbitPort")
}
}
}

@Test
fun `publish & consume amqp message`() {
Launcher().run(amqp_scenario, environment)
}

@Test
fun `publish & get amqp message`() {
Launcher().run(amqp_scenario_2, environment)
}
}

0 comments on commit 85a8c7a

Please sign in to comment.