Skip to content

Commit

Permalink
Add Correlations (#2)
Browse files Browse the repository at this point in the history
# Description

## Changes

1. Implements Correlations API in library
2. Implements Correlations API in binary
3. Adds Correlations tests
  • Loading branch information
stefanluth committed Nov 21, 2023
2 parents 76da27b + 5545b84 commit fab8922
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Coverage of the available endpoints is currently rather limited.
| ------------------- | :-----: | :-: |
| AnonymousSessions |||
| ApplicationInfo |||
| Correlations | | |
| Correlations | | |
| Cronjobs |||
| DataObjectInstances |||
| EmptyActivities |||
Expand Down
12 changes: 10 additions & 2 deletions src/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ pub enum Client {
ApplicationInfo {
cmd: subcommands::application_info::ApplicationInfoCommands,
},
/// Trigger events.
// Get Correlations.
Correlation {
#[clap(subcommand)]
cmd: subcommands::correlation::CorrelationCommands,
},
/// Trigger Events.
Event,
/// Get all flow node instances.
/// Get all Flow Node Instances.
FlowNodeInstance,
/// Handle Process Definitions.
ProcessDefinition {
Expand All @@ -48,6 +53,9 @@ pub async fn register_commands(cli: Cli) {
Client::ApplicationInfo { cmd } => {
subcommands::application_info::register_commands(client_factory, cmd).await
}
Client::Correlation { cmd } => {
subcommands::correlation::register_commands(client_factory, cmd).await
}
Client::Event => {
println!("Event");
}
Expand Down
30 changes: 30 additions & 0 deletions src/cli/subcommands/correlation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use clap::Subcommand;
use serde::Deserialize;

use crate::clients::client_factory::ClientFactory;

#[derive(Clone, Debug, Deserialize, Subcommand)]
#[serde(rename_all = "kebab-case")]
pub enum CorrelationCommands {
/// Gets all Correlations.
GetAll,
/// Gets a single Correlation by ID.
GetById {
/// The ID of the Correlation to retrieve.
id: String,
},
}

pub async fn register_commands(client_factory: ClientFactory, cmd: CorrelationCommands) {
let client = client_factory.create_correlation_client();
match cmd {
CorrelationCommands::GetAll => match client.get_correlations().await {
Ok(correlations) => println!("{:#?}", correlations),
Err(e) => eprintln!("Error getting correlation: {:#?}", e),
},
CorrelationCommands::GetById { id } => match client.get_correlation_by_id(&id).await {
Ok(correlation) => println!("{:#?}", correlation),
Err(e) => eprintln!("Error getting correlation: {:#?}", e),
},
}
}
1 change: 1 addition & 0 deletions src/cli/subcommands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod application_info;
pub mod correlation;
pub mod process_definition;
2 changes: 1 addition & 1 deletion src/cli/subcommands/process_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::clients::{
pub enum ProcessDefinitionCommands {
/// Gets all Process Definitions.
GetAll,
/// Gets a single Process Definition.
/// Gets a single Process Definition by ID.
GetById {
/// The ID of the Process Definition to retrieve.
id: String,
Expand Down
7 changes: 6 additions & 1 deletion src/clients/client_factory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
api::api_client::ApiClient, application_info::application_info_client::ApplicationInfoClient,
event::event_client::EventClient,
correlation::correlation_client::CorrelationClient, event::event_client::EventClient,
process_definition::process_definition_client::ProcessDefinitionClient,
process_model::process_model_client::ProcessModelClient,
};
Expand Down Expand Up @@ -49,6 +49,11 @@ impl ClientFactory {
ApplicationInfoClient::new(self.api_client.clone())
}

/// Creates a new instance of the CorrelationClient.
pub fn create_correlation_client(&self) -> CorrelationClient {
CorrelationClient::new(self.api_client.clone())
}

/// Creates a new instance of the ProcessDefinitionClient.
pub fn create_process_definition_client(&self) -> ProcessDefinitionClient {
ProcessDefinitionClient::new(self.api_client.clone())
Expand Down
17 changes: 17 additions & 0 deletions src/clients/correlation/correlation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CorrelationList {
pub correlations: Vec<Correlation>,
pub total_count: u32,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Correlation {
#[serde(rename = "correlationId")]
pub id: String,
pub metadata: serde_json::Value,
// TODO: pub process_instances: Vec<ProcessInstance>,
}
62 changes: 62 additions & 0 deletions src/clients/correlation/correlation_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::clients::{api::api_client::ApiClient, error::EngineError};

use super::correlation::{Correlation, CorrelationList};

const CORRELATIONS_ENDPOINT: &str = "/correlations";

/// A client for retrieving correlations from the ProcessCube® Engine.
#[derive(Clone)]
pub struct CorrelationClient {
api_client: ApiClient,
pub correlation_url: String,
}

impl CorrelationClient {
/// Creates a new instance of the CorrelationClient.
///
/// # Arguments
/// * `api_client` - The ApiClient to use for communication with the ProcessCube® Engine.
///
/// # Example
/// ```
/// use processcube_engine_client::clients::{api::api_client::ApiClient, correlation::correlation_client::CorrelationClient, error::EngineError};
/// const DUMMY_TOKEN: &str = "Bearer ZHVtbXlfdG9rZW4=";
/// const ENGINE_URL: &str = "http://localhost:10560";
/// // Be sure to have a running ProcessCube® Engine at the given URL
///
/// #[tokio::main]
/// async fn main() -> Result<(), EngineError> {
/// let api_client = ApiClient::new(ENGINE_URL, DUMMY_TOKEN);
/// let correlation_client = CorrelationClient::new(api_client);
/// // Get all correlations from the ProcessCube® Engine
/// let correlations = correlation_client.get_correlations().await?;
/// println!("Correlations: {:#?}", correlations);
/// Ok(())
/// }
/// ```
pub fn new(api_client: ApiClient) -> CorrelationClient {
let correlation_url = format!(
"{}{}{}",
api_client.get_engine_url(),
api_client.get_engine_api_endpoint(),
CORRELATIONS_ENDPOINT
);
CorrelationClient {
api_client,
correlation_url,
}
}

/// Returns all correlations from the ProcessCube® Engine.
pub async fn get_correlations(&self) -> Result<CorrelationList, EngineError> {
self.api_client
.get::<CorrelationList>(&self.correlation_url)
.await
}

/// Returns a correlation with the given id from the ProcessCube® Engine.
pub async fn get_correlation_by_id(&self, id: &str) -> Result<Correlation, EngineError> {
let url = format!("{}/{}", self.correlation_url, id);
self.api_client.get::<Correlation>(&url).await
}
}
2 changes: 2 additions & 0 deletions src/clients/correlation/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod correlation;
pub mod correlation_client;
1 change: 1 addition & 0 deletions src/clients/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod api;
pub mod application_info;
pub mod client_factory;
pub mod correlation;
pub mod error;
pub mod event;
pub mod flow_node_instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ fn create_application_info_client() {
let _client = client_factory.create_application_info_client();
}

#[test]
fn create_correlation_client() {
let client_factory = ClientFactory::new(ENGINE_URL, DUMMY_TOKEN);
let _client = client_factory.create_correlation_client();
}

#[test]
fn create_event_client() {
let client_factory = ClientFactory::new(ENGINE_URL, DUMMY_TOKEN);
Expand Down
2 changes: 1 addition & 1 deletion tests/client_factory_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod get;
mod create;
31 changes: 31 additions & 0 deletions tests/correlation_client_tests/get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use processcube_engine_client::clients::client_factory::ClientFactory;

const DUMMY_TOKEN: &str = "Bearer ZHVtbXlfdG9rZW4=";
const ENGINE_URL: &str = "http://localhost:10560";

// Happy cases

#[tokio::test]
async fn get_correlations() {
let client_factory = ClientFactory::new(ENGINE_URL, DUMMY_TOKEN);
let client = client_factory.create_correlation_client();
let result = client.get_correlations().await;
assert!(result.is_ok());
}

// TODO: Add happy case test for get_correlation_by_id once ProcessInstance is implemented

// Error cases

#[tokio::test]
async fn get_correlation_by_id_not_found() {
let client_factory = ClientFactory::new(ENGINE_URL, DUMMY_TOKEN);
let client = client_factory.create_correlation_client();
let result = client.get_correlation_by_id("dummy").await;

assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error.code, 404);
assert_eq!(error.error_type, "NotFoundError");
assert_eq!(error.message, "Correlation with ID `dummy` not found.");
}
1 change: 1 addition & 0 deletions tests/correlation_client_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod get;
1 change: 1 addition & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod application_info_client_tests;
mod client_factory_tests;
mod correlation_client_tests;
mod fixtures;
mod process_definition_client_tests;

0 comments on commit fab8922

Please sign in to comment.