1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
#![deny(missing_docs, unused_import_braces, unused_qualifications)]
#![warn(
    missing_debug_implementations,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unsafe_code,
    unstable_features
)]

//! A Hedwig library for Rust. Hedwig is a message bus that works with AWS SNS/SQS and Google Cloud Pubsub, with
//! messages validated using JSON schema. The publisher and consumer are de-coupled and fan-out is supported out of
//! the box.
//!
//! # Examples
//!
//! Publish a new message
//!
//! ```no_run
//! use hedwig::{Hedwig, MajorVersion, MinorVersion, Version};
//! # #[cfg(feature = "google")]
//! # use hedwig::GooglePublisher;
//! # #[cfg(feature = "mock")]
//! # use hedwig::MockPublisher;
//! # use std::path::Path;
//! # use serde::Serialize;
//! # use strum_macros::IntoStaticStr;
//!
//! fn main() -> Result<(), failure::Error> {
//! let schema = r#"
//!     {
//!       "$id": "https://hedwig.standard.ai/schema",
//!       "$schema": "https://json-schema.org/draft-04/schema#",
//!       "description": "Example Schema",
//!       "schemas": {
//!           "user-created": {
//!               "1.*": {
//!                   "description": "A new user was created",
//!                   "type": "object",
//!                   "x-versions": [
//!                       "1.0"
//!                   ],
//!                   "required": [
//!                       "user_id"
//!                   ],
//!                   "properties": {
//!                       "user_id": {
//!                           "$ref": "https://hedwig.standard.ai/schema#/definitions/UserId/1.0"
//!                       }
//!                   }
//!               }
//!           }
//!       },
//!       "definitions": {
//!           "UserId": {
//!               "1.0": {
//!                   "type": "string"
//!               }
//!           }
//!       }
//!     }"#;
//!
//!     # #[derive(Clone, Copy, IntoStaticStr, Hash, PartialEq, Eq)]
//!     # enum MessageType {
//!     #    #[strum(serialize = "user.created")]
//!     #    UserCreated,
//!     # }
//!     #
//!     # #[derive(Serialize)]
//!     # struct UserCreatedData {
//!     #     user_id: String,
//!     # }
//!     #
//!     fn router(t: MessageType, v: MajorVersion) -> Option<&'static str> {
//!         match (t, v) {
//!             (MessageType::UserCreated, MajorVersion(1)) => Some("dev-user-created-v1"),
//!             _ => None,
//!         }
//!     }
//!
//!     // create a publisher instance
//!     # #[cfg(feature = "google")]
//!     # let publisher = GooglePublisher::new(String::from("/home/.google-key.json"), "myproject".into())?;
//!     # #[cfg(feature = "mock")]
//!     # let publisher = MockPublisher::default();
//!     # #[cfg(any(feature = "google", feature="mock"))]
//!
//!     let hedwig = Hedwig::new(
//!         schema,
//!         "myapp",
//!         publisher,
//!         router,
//!     )?;
//!
//!     # #[cfg(any(feature = "google", feature="mock"))]
//!     let message = hedwig.message(
//!         MessageType::UserCreated,
//!         Version(MajorVersion(1), MinorVersion(0)),
//!         UserCreatedData { user_id: "U_123".into() },
//!     )?;
//!
//!     # #[cfg(any(feature = "google", feature="mock"))]
//!     hedwig.publish(message)?;
//!     # Ok(())
//! # }
//! ```

#[cfg(feature = "mock")]
use std::cell::RefCell;
use std::collections::HashMap;
use std::convert::From;
#[cfg(feature = "google")]
use std::default::Default;
use std::fmt;
use std::io;
#[cfg(feature = "google")]
use std::path::Path;
use std::result::Result;
use std::time::{SystemTime, UNIX_EPOCH};

#[cfg(feature = "google")]
use base64;
use failure::Fail;
#[cfg(feature = "google")]
use google_pubsub1::{self as pubsub1, Pubsub};
#[cfg(feature = "google")]
use hyper::{self, net::HttpsConnector, Client};
#[cfg(feature = "google")]
use hyper_rustls;
use serde::Serialize;
use serde_json;
use uuid::Uuid;
use valico::json_schema::{SchemaError, Scope, ValidationState};
#[cfg(feature = "google")]
use yup_oauth2 as oauth2;

/// All errors that may be returned when instantiating a new Hedwig instance
#[allow(missing_docs)]
#[derive(Debug, Fail)]
pub enum HedwigError {
    #[fail(display = "Credentials file couldn't be read")]
    CredentialsIOError(#[cause] io::Error),

    #[fail(display = "Unable to deserialize schema")]
    DeserializationError(#[cause] serde_json::Error),

    #[fail(display = "Schema failed to compile")]
    // can't specify #[cause] since SchemaError doesn't implement Error yet (see https://github.com/rustless/valico/issues/24)
    SchemaCompileError(SchemaError),
}

impl From<SchemaError> for HedwigError {
    fn from(e: SchemaError) -> Self {
        HedwigError::SchemaCompileError(e)
    }
}

/// All errors that may be returned while instantiating a new Message instance
#[allow(missing_docs)]
#[derive(Debug, Fail)]
pub enum MessageError {
    #[fail(display = "Unable to serialize message data")]
    SerializationError(#[cause] serde_json::Error),

    #[fail(display = "Router failed to route message correctly")]
    RouterError(&'static str),

    #[fail(display = "Message has invalid schema declaration: {}", _0)]
    MessageInvalidSchemaError(String),

    #[fail(display = "Message data doesn't validate per the schema")]
    MessageDataValidationError(String),
}

/// All errors that may be returned while publishing a message
#[allow(missing_docs)]
#[derive(Debug, Fail)]
pub enum PublishError {
    #[fail(display = "Unable to serialize message")]
    SerializationError(#[cause] serde_json::Error),

    #[fail(display = "API failure occurred when publishing message")]
    PublishAPIFailure(String),

    #[fail(display = "Invalid from publish API: can't find published message id")]
    InvalidResponseNoMessageId,
}

/// A trait for message publishers. This may be used to implement custom behavior such as publish to \<insert your
/// favorite cloud platform\>.
pub trait Publisher {
    /// Publish a Hedwig message.
    fn publish<D, T>(&self, message: Message<D, T>) -> Result<String, PublishError>
    where
        D: Serialize;
}

/// A publisher that uses Google PubSub. To use this class, add feature `google`.
///
/// # Examples
///
/// ```no_run
/// # #[cfg(feature = "google")]
/// # use hedwig::{Publisher, GooglePublisher};
/// # fn main() -> Result<(), failure::Error> {
/// # #[cfg(feature = "google")]
/// let publisher = GooglePublisher::new(String::from("/home/.google-key.json"), "myproject".into())?;
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "google")]
#[allow(missing_debug_implementations)]
pub struct GooglePublisher {
    client: Pubsub<Client, oauth2::ServiceAccountAccess<Client>>,
    google_cloud_project: String,
}

#[cfg(feature = "google")]
impl GooglePublisher {
    /// Create a new instance of GooglePublisher
    /// For now, this requires explicitly passing in the path to your credentials file, as well as the name of the
    /// project where pubsub infra lives.
    ///
    /// # Arguments
    ///
    /// * google_application_credentials - Path to the google credentials file.
    /// * google_cloud_project - The Google Cloud project where your pubsub resources lie - this /may be/ different
    /// from the credentials.
    pub fn new<P>(
        google_application_credentials: P,
        google_cloud_project: String,
    ) -> Result<GooglePublisher, HedwigError>
    where
        P: AsRef<Path>,
    {
        let client_secret = oauth2::service_account_key_from_file(
            &google_application_credentials
                .as_ref()
                .to_string_lossy()
                .into(),
        )
        .map_err(HedwigError::CredentialsIOError)?;
        let auth_https = HttpsConnector::new(hyper_rustls::TlsClient::new());
        let auth_client = hyper::Client::with_connector(auth_https);

        let access = oauth2::ServiceAccountAccess::new(client_secret, auth_client);

        let https = HttpsConnector::new(hyper_rustls::TlsClient::new());
        let pubsub_client = hyper::Client::with_connector(https);

        let client = Pubsub::new(pubsub_client, access);

        Ok(GooglePublisher {
            client,
            google_cloud_project,
        })
    }
}

#[cfg(feature = "google")]
impl Publisher for GooglePublisher {
    /// Publishes a message on Google Pubsub and returns a pubsub id (usually an integer).
    fn publish<D, T>(&self, message: Message<D, T>) -> Result<String, PublishError>
    where
        D: Serialize,
    {
        // in reality this can't fail since message.data has already been verified serializable in Message::validate
        let raw_message =
            serde_json::to_string(&message).map_err(PublishError::SerializationError)?;

        let pubsub_message = pubsub1::PubsubMessage {
            data: Some(base64::encode(&raw_message)),
            attributes: Some(message.headers()),
            ..Default::default()
        };

        let topic_path = format!(
            "projects/{}/topics/hedwig-{}",
            self.google_cloud_project, message.topic
        );
        let request = pubsub1::PublishRequest {
            messages: Some(vec![pubsub_message]),
        };
        let result = self
            .client
            .projects()
            .topics_publish(request, topic_path.as_ref())
            .doit();

        match result {
            Err(e) => Err(PublishError::PublishAPIFailure(format!(
                "Publish error: {}",
                e
            ))),
            Ok((_, response)) => {
                // find the first item from the returned vector
                response
                    .message_ids
                    .ok_or(PublishError::InvalidResponseNoMessageId)
                    .map(|v| v.into_iter().next())
                    .transpose()
                    .unwrap_or(Err(PublishError::InvalidResponseNoMessageId))
            }
        }
    }
}

/// Type alias for custom headers associated with a message
pub type Headers = HashMap<String, String>;

#[cfg(feature = "mock")]
#[derive(Debug, Default)]
/// A mock publisher that doesn't publish messages, but just stores them in-memory for later verification
/// This is useful primarily in tests. To use this class, add feature `mock`.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "mock")]
/// use hedwig::MockPublisher;
///
/// # #[cfg(feature = "mock")]
/// let publisher = MockPublisher::default();
/// ```
pub struct MockPublisher {
    // `RefCell` for interior mutability
    published_messages: RefCell<HashMap<Uuid, (String, Headers)>>,
}

#[cfg(feature = "mock")]
impl MockPublisher {
    /// Verify that a message was published. This method asserts that the message you expected to be published, was
    /// indeed published
    pub fn assert_message_published<D, T>(&self, message: &Message<D, T>, headers: &Headers)
    where
        D: Serialize,
    {
        let published_messages = self.published_messages.borrow();
        let (published, published_headers) = published_messages
            .get(&message.id)
            .expect("message not found");
        let serialized = serde_json::to_string(&message).unwrap();
        assert_eq!(published, &serialized);
        assert_eq!(published_headers, headers);
    }
}

#[cfg(feature = "mock")]
impl Publisher for MockPublisher {
    fn publish<D, T>(&self, message: Message<D, T>) -> Result<String, PublishError>
    where
        D: Serialize,
    {
        let serialized =
            serde_json::to_string(&message).map_err(PublishError::SerializationError)?;
        self.published_messages
            .borrow_mut()
            .insert(message.id, (serialized, message.headers()));
        Ok(String::new())
    }
}

struct Validator {
    scope: Scope,
    schema_id: url::Url,
}

impl Validator {
    fn new(schema: &str) -> Result<Validator, HedwigError> {
        let master_schema: serde_json::Value =
            serde_json::from_str(schema).map_err(HedwigError::DeserializationError)?;

        let mut scope = Scope::new();
        let schema_id = scope.compile(master_schema, false)?;

        Ok(Validator { scope, schema_id })
    }

    fn validate<D, T>(&self, message: &Message<D, T>) -> Result<ValidationState, MessageError>
    where
        D: Serialize,
    {
        // convert user.created/1.0 -> user.created/1.*
        let msg_schema_ptr = message.schema.trim_end_matches(char::is_numeric).to_owned() + "*";

        let msg_schema_url = match url::Url::parse(msg_schema_ptr.as_str()) {
            Ok(u) => u,
            Err(_) => return Err(MessageError::MessageInvalidSchemaError(msg_schema_ptr)),
        };

        let msg_schema = match self.scope.resolve(&msg_schema_url) {
            None => return Err(MessageError::MessageInvalidSchemaError(msg_schema_ptr)),
            Some(s) => s,
        };

        let msg_data =
            serde_json::to_value(&message.data).map_err(MessageError::SerializationError)?;

        let validation_state = msg_schema.validate(&msg_data);
        if !validation_state.is_strictly_valid() {
            return Err(MessageError::MessageDataValidationError(format!(
                "validation_state: {:#?}",
                validation_state
            )));
        }
        Ok(validation_state)
    }
}

/// Major part component in semver
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize)]
pub struct MajorVersion(pub u8);

impl fmt::Display for MajorVersion {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Minor part component in semver
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize)]
pub struct MinorVersion(pub u8);

impl fmt::Display for MinorVersion {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// A semver version without patch part
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Version(pub MajorVersion, pub MinorVersion);

impl Serialize for Version {
    fn serialize<S>(
        &self,
        serializer: S,
    ) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(format!("{}.{}", self.0, self.1).as_ref())
    }
}

/// `MessageRouter` is a function that can route messages of a given type and version to a Hedwig topic.
///
/// # Examples
/// ```
/// # use serde::Serialize;
/// # use strum_macros::IntoStaticStr;
/// use hedwig::{MajorVersion, MessageRouter};
///
/// # #[derive(Clone, Copy, IntoStaticStr, Hash, PartialEq, Eq)]
/// # enum MessageType {
/// #    #[strum(serialize = "user.created")]
/// #    UserCreated,
/// # }
/// #
/// let r: MessageRouter<MessageType> = |t, v| match (t, v) {
///     (MessageType::UserCreated, MajorVersion(1)) => Some("user-created-v1"),
///     _ => None,
/// };
/// ```
pub type MessageRouter<T> = fn(T, MajorVersion) -> Option<&'static str>;

/// Central instance to access all Hedwig related resources
#[allow(missing_debug_implementations)]
pub struct Hedwig<T, P> {
    validator: Validator,
    publisher_name: String,
    message_router: MessageRouter<T>,
    publisher: P,
}

impl<T, P> Hedwig<T, P>
where
    P: Publisher,
{
    /// Creates a new Hedwig instance. Application credentials cannot be auto-discovered in Google Cloud environment
    /// unlike the Python library.
    ///
    /// # Arguments
    ///
    /// * schema: The JSON schema content. It's up to the caller to read the schema from a file.
    /// * publisher name: Name of the publisher service. This will be part of the metadata in the message.
    /// * publisher - An implementation of Publisher
    /// * message_router - A function that can route messages to topics
    pub fn new(
        schema: &str,
        publisher_name: &str,
        publisher: P,
        message_router: MessageRouter<T>,
    ) -> Result<Hedwig<T, P>, HedwigError> {
        Ok(Hedwig {
            validator: Validator::new(schema)?,
            publisher_name: String::from(publisher_name),
            message_router,
            publisher,
        })
    }

    /// Creates a new message with given data type, schema version and data object.
    ///
    /// # Arguments
    ///
    /// * data_type -  An Enum instance with static str representation
    /// * data_schema_version - Version of the data object
    /// * data - The message data - must be serializable
    pub fn message<D>(
        &self,
        data_type: T,
        data_schema_version: Version,
        data: D,
    ) -> Result<Message<D, T>, MessageError>
    where
        D: Serialize,
        T: Copy + Into<&'static str>,
    {
        Message::new(self, data_type, data_schema_version, data)
    }

    /// Publish a message using the configured publisher. Returns the publish id if successful. The publish id depends
    /// on your publisher.
    ///
    /// # Arguments
    ///
    /// * `message` - the message to publish
    pub fn publish<D>(&self, message: Message<D, T>) -> Result<String, PublishError>
    where
        D: Serialize,
    {
        self.publisher.publish(message)
    }
}

/// Additional metadata associated with a message
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Metadata {
    /// The timestamp when message was created in the publishing service
    pub timestamp: u128,

    /// Name of the publishing service
    pub publisher: String,

    /// Custom headers. This may be used to track request_id, for example.
    pub headers: Headers,
}

const FORMAT_VERSION_V1: Version = Version(MajorVersion(1), MinorVersion(0));

/// Message represents an instance of a message on the message bus.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Message<D, T> {
    /// Message identifier
    pub id: Uuid,

    /// Metadata associated with the message
    pub metadata: Metadata,

    /// Message schema, e.g. `https://hedwig.standard.ai/schemas#/schemas/user.created/1.0`
    pub schema: String,

    /// Associated message data
    pub data: D,

    /// Format version for the message container
    pub format_version: Version,

    /// Type of data represented by this message
    #[serde(skip)]
    pub data_type: T,

    /// Schema version of the data object. This should follow semver.
    #[serde(skip)]
    pub data_schema_version: Version,

    #[serde(skip)]
    topic: String,
}

impl<D, T> Message<D, T> {
    fn new<P>(
        hedwig: &Hedwig<T, P>,
        data_type: T,
        data_schema_version: Version,
        data: D,
    ) -> Result<Self, MessageError>
    where
        D: Serialize,
        T: Copy + Into<&'static str>,
    {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("Time went backwards");

        let topic = (hedwig.message_router)(data_type, data_schema_version.0)
            .ok_or(MessageError::RouterError("Topic not found"))?
            .to_owned();

        let message = Message {
            id: Uuid::new_v4(),
            metadata: Metadata {
                headers: Headers::new(),
                publisher: hedwig.publisher_name.clone(),
                timestamp: timestamp.as_millis(),
            },
            schema: format!(
                "{}#/schemas/{}/{}.{}",
                hedwig.validator.schema_id,
                data_type.into(),
                data_schema_version.0,
                data_schema_version.1,
            ),
            data,
            format_version: FORMAT_VERSION_V1,
            data_type,
            data_schema_version,
            topic,
        };
        hedwig.validator.validate(&message)?;
        Ok(message)
    }

    /// Add custom headers to the message. This may be used to track `request_id`, for example.
    pub fn with_headers(&mut self, headers: Headers) -> &mut Self {
        self.metadata.headers = headers;
        self
    }

    /// Add custom id to the message. If not provided, a new uuid v4 is used.
    pub fn with_id(&mut self, id: Uuid) -> &mut Self {
        self.id = id;
        self
    }

    #[cfg(any(feature = "google", feature = "mock"))]
    fn headers(&self) -> Headers {
        self.metadata.headers.clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use assert_matches::assert_matches;
    use strum_macros::IntoStaticStr;

    #[derive(Clone, Copy, Debug, IntoStaticStr, Hash, PartialEq, Eq)]
    enum MessageType {
        #[strum(serialize = "user.created")]
        UserCreated,

        #[strum(serialize = "invalid.schema")]
        InvalidSchema,

        #[strum(serialize = "invalid.route")]
        InvalidRoute,
    }

    #[derive(Clone, Debug, Serialize, PartialEq)]
    struct UserCreatedData {
        user_id: String,
    }

    const VERSION_1_0: Version = Version(MajorVersion(1), MinorVersion(0));

    const SCHEMA: &str = r#"
{
  "$id": "https://hedwig.standard.ai/schema",
  "$schema": "https://json-schema.org/draft-04/schema#",
  "description": "Example Schema",
  "schemas": {
      "user.created": {
          "1.*": {
              "description": "A new user was created",
              "type": "object",
              "x-versions": [
                  "1.0"
              ],
              "required": [
                  "user_id"
              ],
              "properties": {
                  "user_id": {
                      "$ref": "https://hedwig.standard.ai/schema#/definitions/UserId/1.0"
                  }
              }
          }
      },
      "invalid.route": {
          "1.*": {}
      }
  },
  "definitions": {
      "UserId": {
          "1.0": {
              "type": "string"
          }
      }
  }
}"#;

    fn router(t: MessageType, v: MajorVersion) -> Option<&'static str> {
        match (t, v) {
            (MessageType::UserCreated, MajorVersion(1)) => Some("dev-user-created-v1"),
            (MessageType::InvalidSchema, MajorVersion(1)) => Some("invalid-schema"),
            _ => None,
        }
    }

    #[cfg(feature = "mock")]
    fn mock_hedwig() -> Hedwig<MessageType, MockPublisher> {
        Hedwig::new(SCHEMA, "myapp", MockPublisher::default(), router).unwrap()
    }

    #[test]
    #[cfg(feature = "mock")]
    fn message_constructor() {
        let hedwig = mock_hedwig();
        let data = UserCreatedData {
            user_id: "U_123".into(),
        };
        let message = hedwig
            .message(MessageType::UserCreated, VERSION_1_0, data.clone())
            .unwrap();
        assert_eq!(Headers::new(), message.metadata.headers);
        assert_eq!(hedwig.publisher_name, message.metadata.publisher);
        assert_eq!(data, message.data);
        assert_eq!(MessageType::UserCreated, message.data_type);
        assert_eq!(VERSION_1_0, message.data_schema_version);
        assert_eq!(
            "https://hedwig.standard.ai/schema#/schemas/user.created/1.0",
            message.schema
        );
        assert_eq!(FORMAT_VERSION_V1, message.format_version);
    }

    #[test]
    #[cfg(feature = "mock")]
    fn message_set_headers() {
        let mut custom_headers = Headers::new();
        let request_id = Uuid::new_v4().to_string();
        custom_headers.insert("request_id".to_owned(), request_id.clone());
        let hedwig = mock_hedwig();
        let mut message = hedwig
            .message(
                MessageType::UserCreated,
                VERSION_1_0,
                UserCreatedData {
                    user_id: "U_123".into(),
                },
            )
            .unwrap();
        message.with_headers(custom_headers);
        assert_eq!(
            request_id,
            message
                .metadata
                .headers
                .get(&"request_id".to_owned())
                .unwrap()
                .as_str()
        );
    }

    #[test]
    #[cfg(feature = "mock")]
    fn message_with_id() {
        let id = uuid::Uuid::new_v4();
        let hedwig = mock_hedwig();
        let mut message = hedwig
            .message(
                MessageType::UserCreated,
                VERSION_1_0,
                UserCreatedData {
                    user_id: "U_123".into(),
                },
            )
            .unwrap();
        message.with_id(id);
        assert_eq!(id, message.id);
    }

    #[test]
    #[cfg(feature = "mock")]
    fn publish() {
        let hedwig = mock_hedwig();
        let mut custom_headers = Headers::new();
        let request_id = Uuid::new_v4().to_string();
        custom_headers.insert("request_id".to_owned(), request_id);
        let mut message = hedwig
            .message(
                MessageType::UserCreated,
                VERSION_1_0,
                UserCreatedData {
                    user_id: "U_123".into(),
                },
            )
            .unwrap();
        message.with_headers(custom_headers.clone());
        hedwig.publish(message.clone()).unwrap();
        hedwig
            .publisher
            .assert_message_published(&message, &custom_headers);
    }

    #[test]
    #[cfg(feature = "google")]
    fn google_publisher_credentials_error() {
        let r = GooglePublisher::new("path does not exist", "myproject".into());
        assert_matches!(r.err(), Some(HedwigError::CredentialsIOError(_)));
    }

    #[test]
    fn validator_deserialization_error() {
        let r = Validator::new("bad json");
        assert_matches!(r.err(), Some(HedwigError::DeserializationError(_)));
    }

    #[test]
    fn validator_schema_compile_error() {
        const BAD_SCHEMA: &str = r#"
{
  "$schema": "https://json-schema.org/draft-04/schema#",
  "definitions": {
      "UserId": {
          "1.0": {
              "type": "bad value"
          }
      }
  }
}"#;

        let r = Validator::new(BAD_SCHEMA);
        assert_matches!(r.err(), Some(HedwigError::SchemaCompileError(_)));
    }

    #[test]
    #[cfg(feature = "mock")]
    fn message_serialization_error() {
        let hedwig = mock_hedwig();
        #[derive(Serialize)]
        struct BadUserCreatedData {
            user_ids: HashMap<Vec<i32>, String>,
        };
        let mut user_ids = HashMap::new();
        user_ids.insert(vec![32, 64], "U_123".to_owned());
        let data = BadUserCreatedData { user_ids };
        let r = hedwig.message(MessageType::UserCreated, VERSION_1_0, data);
        assert_matches!(r.err(), Some(MessageError::SerializationError(_)));
    }

    #[test]
    #[cfg(feature = "mock")]
    fn message_router_error() {
        let hedwig = mock_hedwig();
        let r = hedwig.message(MessageType::InvalidRoute, VERSION_1_0, ());
        assert_matches!(r.err(), Some(MessageError::RouterError(_)));
    }

    #[test]
    #[cfg(feature = "mock")]
    fn message_invalid_schema_error() {
        let hedwig = mock_hedwig();
        let r = hedwig.message(MessageType::InvalidSchema, VERSION_1_0, ());
        assert_matches!(r.err(), Some(MessageError::MessageInvalidSchemaError(_)));
    }

    #[test]
    #[cfg(feature = "mock")]
    fn message_data_validation_eror() {
        let hedwig = mock_hedwig();
        #[derive(Serialize)]
        struct BadUserCreatedData {
            user_ids: Vec<i32>,
        };
        let data = BadUserCreatedData { user_ids: vec![1] };
        let r = hedwig.message(MessageType::UserCreated, VERSION_1_0, data);
        assert_matches!(r.err(), Some(MessageError::MessageDataValidationError(_)));
    }
}