use asl::asl::execution::{
Execution, ExecutionStatus, StateExecutionHandler, StateExecutionOutput,
};
use asl::asl::state_machine::StateMachine;
use asl::asl::types::execution::EmptyContext;
use rstest::rstest;
use serde_json::{json, Value};
use similar_asserts::assert_eq;
struct TaskHandler {}
impl StateExecutionHandler for TaskHandler {
type TaskExecutionError = String;
fn execute_task(
&self,
resource: &str,
input: &Value,
credentials: Option<&Value>,
) -> Result<Option<Value>, Self::TaskExecutionError> {
let result = match resource {
"SayHello" => "Hello",
"SayWorld" => "World",
_ => Err("Unknown resource!")?,
};
println!("{}", result);
Ok(None) }
}
#[rstest]
fn execute_hello_world() {
let definition = r#"{
"Comment": "A simple minimal example of the States language",
"StartAt": "State: Hello",
"States": {
"State: Hello": {
"Type": "Task",
"Resource": "SayHello",
"Next": "State: World"
},
"State: World": {
"Type": "Task",
"Resource": "SayWorld",
"End": true
}
}
}"#;
let state_machine = StateMachine::parse(definition).unwrap();
let input = Value::Null;
let execution: Execution<TaskHandler, EmptyContext> =
state_machine.start(&input, TaskHandler {}, EmptyContext {});
let steps: Vec<StateExecutionOutput> = execution.collect();
assert_eq!(
steps[0],
StateExecutionOutput {
status: ExecutionStatus::Executing,
state_name: Some("State: Hello".to_string()),
result: Some(json!({})), }
);
assert_eq!(
steps[1],
StateExecutionOutput {
status: ExecutionStatus::FinishedWithSuccess(Some(json!({}))),
state_name: Some("State: World".to_string()),
result: Some(json!({})),
}
);
}