Skip to content

Commit

Permalink
Merge pull request #6 from stefandanaita/tests-exchanges
Browse files Browse the repository at this point in the history
Test the exchanges endpoints. Add tests for NotFound errors
  • Loading branch information
stefandanaita authored Apr 26, 2024
2 parents 5d641a3 + 180ef05 commit 2d20349
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 31 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ serde_json = "1"
thiserror = "1"
base64 = "0.22"
tokio = { version = "1.37", features = ["macros"]}

[dev-dependencies]
uuid = { version = "1", features = ["v4"] }
52 changes: 24 additions & 28 deletions src/api/_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,43 @@ where
let status = response.status();

if status.is_success() {
return match response.json::<T>().await {
match response.json::<T>().await {
Ok(response) => Ok(response),
Err(e) => Err(RabbitMqClientError::ParsingError(e)),
};
}

let text = response
.text()
.await
.map_err(RabbitMqClientError::ParsingError)?;

if status.eq(&StatusCode::UNAUTHORIZED) {
return Err(RabbitMqClientError::Unauthorized);
}
}
} else {
let text = response
.text()
.await
.map_err(RabbitMqClientError::ParsingError)?;

if status.eq(&StatusCode::NOT_FOUND) {
return Err(RabbitMqClientError::NotFound(text));
Err(map_error(status, text))
}

Err(RabbitMqClientError::ApiError(RabbitMqApiError {
code: status,
text,
}))
}

pub async fn handle_empty_response(response: Response) -> Result<(), RabbitMqClientError> {
let status = response.status();

if status.is_success() {
return Ok(());
Ok(())
} else {
let text = response
.text()
.await
.map_err(RabbitMqClientError::ParsingError)?;

Err(map_error(status, text))
}
}

fn map_error(status: StatusCode, text: String) -> RabbitMqClientError {
if status.eq(&StatusCode::UNAUTHORIZED) {
return Err(RabbitMqClientError::Unauthorized);
return RabbitMqClientError::Unauthorized;
}

Err(RabbitMqClientError::ApiError(RabbitMqApiError {
code: status,
text: response
.text()
.await
.map_err(RabbitMqClientError::ParsingError)?,
}))
if status.eq(&StatusCode::NOT_FOUND) {
return RabbitMqClientError::NotFound(text);
}

RabbitMqClientError::ApiError(RabbitMqApiError { code: status, text })
}
6 changes: 3 additions & 3 deletions src/api/exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ pub struct RabbitMqExchangeMessageStats {
pub struct RabbitMqExchangeRequest {
#[serde(rename = "type")]
pub kind: String,
pub auto_delete: Option<bool>,
pub durable: Option<bool>,
pub internal: Option<bool>,
pub auto_delete: bool,
pub durable: bool,
pub internal: bool,
}

#[derive(Debug, Serialize)]
Expand Down
24 changes: 24 additions & 0 deletions tests/all/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use rabbitmq_management_client::api::vhost::{RabbitMqVhost, RabbitMqVhostRequest};
use rabbitmq_management_client::config::RabbitMqConfiguration;
use rabbitmq_management_client::errors::RabbitMqClientError;
use rabbitmq_management_client::{RabbitMqClient, RabbitMqClientBuilder};
use uuid::Uuid;

const RABBITMQ_API_URL: &str = "http://localhost:15672";
const RABBITMQ_USERNAME: &str = "guest";
Expand All @@ -21,6 +24,27 @@ impl TestContext {

Self { rabbitmq: rmq }
}

pub async fn create_random_vhost(&self) -> Result<RabbitMqVhost, RabbitMqClientError> {
let id = Uuid::new_v4().to_string();

self.rabbitmq
.apis
.vhosts
.create_vhost(RabbitMqVhostRequest {
name: id.clone(),
description: Some(format!("{} testing vhost", id.clone())),
tags: vec![],
tracing: false,
})
.await?;

self.rabbitmq.apis.vhosts.get_vhost(id).await
}

pub async fn delete_vhost(&self, name: String) -> Result<(), RabbitMqClientError> {
self.rabbitmq.apis.vhosts.delete_vhost(name).await
}
}

fn test_config() -> RabbitMqConfiguration {
Expand Down
127 changes: 127 additions & 0 deletions tests/all/exchanges.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::context::TestContext;
use rabbitmq_management_client::api::exchange::RabbitMqExchangeRequest;
use rabbitmq_management_client::errors::RabbitMqClientError;

#[tokio::test]
async fn can_list_exchanges() {
Expand All @@ -14,3 +16,128 @@ async fn can_list_exchanges() {

assert!(!exchanges.is_empty());
}

#[tokio::test]
async fn can_crud_exchange() {
let ctx = TestContext::new();

let vhost = ctx
.create_random_vhost()
.await
.expect("failed to create vhost");

// Create the exchange
ctx.rabbitmq
.apis
.exchanges
.create_exchange(
vhost.name.clone(),
"test-exchange".to_string(),
RabbitMqExchangeRequest {
kind: "direct".to_string(),
auto_delete: true,
durable: false,
internal: false,
},
)
.await
.expect("failed to create exchange");

// Get the exchange
let exchange = ctx
.rabbitmq
.apis
.exchanges
.get_exchange(vhost.name.clone(), "test-exchange".to_string())
.await
.expect("failed to get the exchange back");

assert_eq!(exchange.kind, "direct");
assert!(exchange.auto_delete);
assert!(!exchange.durable);
assert!(!exchange.internal);

// Delete the exchange
ctx.rabbitmq
.apis
.exchanges
.delete_exchange(vhost.name.clone(), "test-exchange".to_string())
.await
.expect("failed to delete the exchange");

ctx.delete_vhost(vhost.name)
.await
.expect("failed to delete vhost");
}

#[tokio::test]
async fn cannot_create_if_exchange_exists() {
let ctx = TestContext::new();

let vhost = ctx
.create_random_vhost()
.await
.expect("failed to create vhost");

// Create the exchange
ctx.rabbitmq
.apis
.exchanges
.create_exchange(
vhost.name.clone(),
"test-exchange".to_string(),
RabbitMqExchangeRequest {
kind: "direct".to_string(),
auto_delete: true,
durable: false,
internal: false,
},
)
.await
.expect("failed to create exchange");

// Recreate the exchange
let result = ctx
.rabbitmq
.apis
.exchanges
.create_exchange(
vhost.name.clone(),
"test-exchange".to_string(),
RabbitMqExchangeRequest {
kind: "direct".to_string(),
auto_delete: true,
durable: false,
internal: false,
},
)
.await;
assert!(matches!(result, Err(RabbitMqClientError::AlreadyExists(_))));

ctx.delete_vhost(vhost.name)
.await
.expect("failed to delete vhost");
}

#[tokio::test]
async fn returns_not_found() {
let ctx = TestContext::new();

let vhost = ctx
.create_random_vhost()
.await
.expect("failed to create vhost");

let result = ctx
.rabbitmq
.apis
.exchanges
.delete_exchange(vhost.name.clone(), "doesnotexist".to_string())
.await;

assert!(matches!(result, Err(RabbitMqClientError::NotFound(_))));

ctx.delete_vhost(vhost.name)
.await
.expect("failed to delete vhost");
}
14 changes: 14 additions & 0 deletions tests/all/vhosts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,17 @@ async fn cannot_create_if_vhosts_exists() {

assert!(matches!(result, Err(RabbitMqClientError::AlreadyExists(_))));
}

#[tokio::test]
async fn returns_not_found() {
let ctx = TestContext::new();

let result = ctx
.rabbitmq
.apis
.vhosts
.delete_vhost("doesnotexist".to_string())
.await;

assert!(matches!(result, Err(RabbitMqClientError::NotFound(_))));
}

0 comments on commit 2d20349

Please sign in to comment.