camel-processor 0.70.0

Message processors for rust-camel
Documentation
use camel_api::body::Body;
use camel_api::{CamelError, Exchange, IdentityProcessor, Message};
use camel_processor::JsonSchemaValidateService;
use serde_json::json;
use tower::ServiceExt;

fn schema() -> serde_json::Value {
    json!({
        "type": "object",
        "properties": {
            "name": { "type": "string" },
            "age": { "type": "integer" }
        },
        "required": ["name"]
    })
}

#[tokio::test]
async fn passes_valid_body() {
    let svc = JsonSchemaValidateService::new(IdentityProcessor, &schema()).unwrap();
    let ex = Exchange::new(Message::new(Body::Json(json!({
        "name": "kenny",
        "age": 42
    }))));
    let out = svc.oneshot(ex).await.unwrap();
    assert!(matches!(out.input.body, Body::Json(_)));
}

#[tokio::test]
async fn rejects_missing_required_field() {
    let svc = JsonSchemaValidateService::new(IdentityProcessor, &schema()).unwrap();
    let ex = Exchange::new(Message::new(Body::Json(json!({
        "age": 42
    }))));
    let err = svc.oneshot(ex).await.unwrap_err();
    match err {
        CamelError::ValidationError(msg) => {
            assert!(msg.contains("validation failed"), "msg: {msg}");
        }
        other => panic!("expected ValidationError, got: {other:?}"),
    }
}

#[tokio::test]
async fn rejects_wrong_type() {
    let svc = JsonSchemaValidateService::new(IdentityProcessor, &schema()).unwrap();
    let ex = Exchange::new(Message::new(Body::Json(json!({
        "name": "kenny",
        "age": "not a number"
    }))));
    let err = svc.oneshot(ex).await.unwrap_err();
    assert!(matches!(err, CamelError::ValidationError(_)));
}

#[tokio::test]
async fn non_json_body_skips_validation() {
    // A non-JSON body is the caller's problem (unmarshal handles it); the
    // validator must not fail when the body type is not JSON.
    let svc = JsonSchemaValidateService::new(IdentityProcessor, &schema()).unwrap();
    let ex = Exchange::new(Message::new(Body::Text("plain text".to_string())));
    let out = svc.oneshot(ex).await.unwrap();
    assert!(matches!(out.input.body, Body::Text(_)));
}

#[tokio::test]
async fn invalid_schema_returns_error_at_build() {
    // Not a valid JSON Schema — `validator_for` rejects it.
    let bad = json!({ "type": "not_a_real_type" });
    let err = JsonSchemaValidateService::new(IdentityProcessor, &bad).unwrap_err();
    assert!(matches!(err, CamelError::RouteError(_)));
}

#[tokio::test]
async fn empty_body_skips_validation() {
    // Empty body — no body to validate against; the validator passes through.
    let svc = JsonSchemaValidateService::new(IdentityProcessor, &schema()).unwrap();
    let ex = Exchange::new(Message::new(Body::Empty));
    let out = svc.oneshot(ex).await.unwrap();
    assert!(matches!(out.input.body, Body::Empty));
}

#[tokio::test]
async fn can_be_chained_behind_inner_processor() {
    // Validator wraps an inner processor (IdentityProcessor). After validation
    // passes, the inner service runs and the body passes through unchanged.
    let svc = JsonSchemaValidateService::new(IdentityProcessor, &schema()).unwrap();
    let inner = IdentityProcessor;
    let ex = Exchange::new(Message::new(Body::Json(json!({ "name": "kenny" }))));
    let validated = svc.oneshot(ex).await.unwrap();
    let final_ex = inner.oneshot(validated).await.unwrap();
    assert!(matches!(final_ex.input.body, Body::Json(_)));
}

fn email_schema() -> serde_json::Value {
    json!({
        "type": "object",
        "properties": {
            "email": { "type": "string", "format": "email" }
        }
    })
}

#[tokio::test]
async fn format_email_rejects_malformed_value() {
    let svc = JsonSchemaValidateService::new(IdentityProcessor, &email_schema()).unwrap();
    let ex = Exchange::new(Message::new(Body::Json(json!({
        "email": "not-an-email"
    }))));
    let err = svc.oneshot(ex).await.unwrap_err();
    assert!(matches!(err, CamelError::ValidationError(_)));
}

#[tokio::test]
async fn format_email_accepts_valid_value() {
    let svc = JsonSchemaValidateService::new(IdentityProcessor, &email_schema()).unwrap();
    let ex = Exchange::new(Message::new(Body::Json(json!({
        "email": "user@example.com"
    }))));
    let out = svc.oneshot(ex).await.unwrap();
    assert!(matches!(out.input.body, Body::Json(_)));
}

#[tokio::test]
async fn pattern_enforcement_unchanged() {
    let pat = json!({ "type": "string", "pattern": "^[a-z]+$" });
    let svc = JsonSchemaValidateService::new(IdentityProcessor, &pat).unwrap();

    let ex = Exchange::new(Message::new(Body::Json(json!("ABC"))));
    let err = svc.clone().oneshot(ex).await.unwrap_err();
    assert!(matches!(err, CamelError::ValidationError(_)));

    let ex = Exchange::new(Message::new(Body::Json(json!("abc"))));
    assert!(svc.oneshot(ex).await.is_ok());
}

#[tokio::test]
async fn unknown_format_is_annotation() {
    let s = json!({ "type": "string", "format": "unknown-format-x" });
    let svc = JsonSchemaValidateService::new(IdentityProcessor, &s).unwrap();
    let ex = Exchange::new(Message::new(Body::Json(json!("anything"))));
    assert!(svc.oneshot(ex).await.is_ok());
}

#[tokio::test]
async fn draft07_schema_enforces_format() {
    let s = json!({
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "object",
        "properties": {
            "email": { "type": "string", "format": "email" }
        }
    });
    let svc = JsonSchemaValidateService::new(IdentityProcessor, &s).unwrap();
    let ex = Exchange::new(Message::new(Body::Json(json!({
        "email": "not-an-email"
    }))));
    let err = svc.oneshot(ex).await.unwrap_err();
    assert!(matches!(err, CamelError::ValidationError(_)));
}

#[tokio::test]
async fn request_schema_internal_ref_works() {
    let s = json!({
        "$defs": { "obj": { "type": "object", "required": ["a"] } },
        "$ref": "#/$defs/obj"
    });
    let svc = JsonSchemaValidateService::new(IdentityProcessor, &s).unwrap();

    let ex = Exchange::new(Message::new(Body::Json(json!({ "a": 1 }))));
    assert!(svc.clone().oneshot(ex).await.is_ok());

    let ex = Exchange::new(Message::new(Body::Json(json!({}))));
    let err = svc.oneshot(ex).await.unwrap_err();
    assert!(matches!(err, CamelError::ValidationError(_)));
}

#[test]
fn request_schema_external_ref_rejected_at_build() {
    let s = json!({ "$ref": "defs.json#/$defs/x" });
    let err = JsonSchemaValidateService::new(IdentityProcessor, &s).unwrap_err();
    let msg = err.to_string();
    assert!(msg.contains("defs.json"), "msg: {msg}");
    assert!(msg.contains("rejected"), "msg: {msg}");
}

#[test]
fn request_schema_remote_ref_error_preserves_literal() {
    let s = json!({ "$ref": "https://example.com/s.json" });
    let err = JsonSchemaValidateService::new(IdentityProcessor, &s).unwrap_err();
    let msg = err.to_string();
    assert!(
        msg.contains(
            "remote $ref 'https://example.com/s.json' rejected: schemas resolve from local files only (ADR-0100); vendor the file"
        ),
        "msg: {msg}"
    );
}

#[tokio::test]
async fn schema_without_dollar_schema_enforces_format_2020_12() {
    let s = json!({
        "type": "object",
        "properties": {
            "email": { "type": "string", "format": "email" }
        }
    });
    let svc = JsonSchemaValidateService::new(IdentityProcessor, &s).unwrap();
    let ex = Exchange::new(Message::new(Body::Json(json!({
        "email": "not-an-email"
    }))));
    let err = svc.oneshot(ex).await.unwrap_err();
    assert!(matches!(err, CamelError::ValidationError(_)));
}