|
| 1 | +use std::convert::TryFrom; |
| 2 | +use std::error::Error; |
| 3 | +use std::fmt; |
| 4 | + |
| 5 | +use domain::validator::message::{Message, State}; |
| 6 | +use domain::{Channel, RepositoryError, ValidatorId}; |
| 7 | + |
| 8 | +use crate::domain::MessageRepository; |
| 9 | + |
| 10 | +pub struct MessagePropagator<S: State> { |
| 11 | + pub message_repository: Box<dyn MessageRepository<S>>, |
| 12 | +} |
| 13 | + |
| 14 | +#[derive(Debug)] |
| 15 | +pub enum PropagationErrorKind { |
| 16 | + Repository(RepositoryError), |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug)] |
| 20 | +pub struct PropagationError { |
| 21 | + kind: PropagationErrorKind, |
| 22 | + message: String, |
| 23 | +} |
| 24 | + |
| 25 | +impl fmt::Display for PropagationError { |
| 26 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 27 | + write!(f, "{}", self.message) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl Error for PropagationError { |
| 32 | + fn source(&self) -> Option<&(dyn Error + 'static)> { |
| 33 | + match &self.kind { |
| 34 | + PropagationErrorKind::Repository(error) => Some(error), |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl From<RepositoryError> for PropagationError { |
| 40 | + fn from(error: RepositoryError) -> Self { |
| 41 | + Self { |
| 42 | + kind: PropagationErrorKind::Repository(error), |
| 43 | + message: "Repository call for propagating the message failed".to_string(), |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl<S: State> MessagePropagator<S> { |
| 49 | + // @TODO: Make sure we have information for logging the results for particular Validator |
| 50 | + pub async fn propagate<'a>( |
| 51 | + &'a self, |
| 52 | + channel: &'a Channel, |
| 53 | + message: Message<S>, |
| 54 | + ) -> Vec<Result<(), PropagationError>> { |
| 55 | + let mut results = Vec::default(); |
| 56 | + |
| 57 | + for validator in channel.spec.validators.into_iter() { |
| 58 | + // @TODO: Remove once we have ValidatorId in ValidatorDesc |
| 59 | + let validator_id = ValidatorId::try_from(validator.id.as_str()).unwrap(); |
| 60 | + let add_result = |
| 61 | + await!(self |
| 62 | + .message_repository |
| 63 | + .add(&channel.id, &validator_id, message.clone())) |
| 64 | + .map_err(Into::into); |
| 65 | + results.push(add_result); |
| 66 | + } |
| 67 | + |
| 68 | + results |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +mod test { |
| 74 | + use std::cell::RefCell; |
| 75 | + |
| 76 | + use futures::future::{ready, FutureExt}; |
| 77 | + |
| 78 | + use domain::channel::fixtures::get_channel; |
| 79 | + use domain::validator::message::fixtures::{get_reject_state, DummyState}; |
| 80 | + use domain::validator::message::{Message, MessageType}; |
| 81 | + use domain::{ChannelId, ValidatorId}; |
| 82 | + use domain::{RepositoryError, RepositoryFuture}; |
| 83 | + |
| 84 | + use crate::application::MessagePropagator; |
| 85 | + use crate::domain::MessageRepository; |
| 86 | + |
| 87 | + struct MockMessageRepository<I> |
| 88 | + where |
| 89 | + I: Iterator<Item = Result<(), RepositoryError>>, |
| 90 | + { |
| 91 | + add_results: RefCell<I>, |
| 92 | + } |
| 93 | + |
| 94 | + impl<I> MessageRepository<DummyState> for MockMessageRepository<I> |
| 95 | + where |
| 96 | + I: Iterator<Item = Result<(), RepositoryError>>, |
| 97 | + { |
| 98 | + fn add( |
| 99 | + &self, |
| 100 | + _channel: &ChannelId, |
| 101 | + _validator: &ValidatorId, |
| 102 | + _message: Message<DummyState>, |
| 103 | + ) -> RepositoryFuture<()> { |
| 104 | + let result = self |
| 105 | + .add_results |
| 106 | + .borrow_mut() |
| 107 | + .next() |
| 108 | + .expect("Whoops, you called add() more than the provided results"); |
| 109 | + ready(result).boxed() |
| 110 | + } |
| 111 | + |
| 112 | + fn latest( |
| 113 | + &self, |
| 114 | + _channel: &ChannelId, |
| 115 | + _from: &ValidatorId, |
| 116 | + _types: Option<&[&MessageType]>, |
| 117 | + ) -> RepositoryFuture<Option<Message<DummyState>>> { |
| 118 | + unimplemented!("No need for latest in this Mock") |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn propagates_and_returns_vector_of_results() { |
| 124 | + futures::executor::block_on(async { |
| 125 | + let add_error = RepositoryError::User; |
| 126 | + |
| 127 | + let iterator = vec![Ok(()), Err(add_error)].into_iter(); |
| 128 | + let message_repository = MockMessageRepository { |
| 129 | + add_results: RefCell::new(iterator), |
| 130 | + }; |
| 131 | + let propagator = MessagePropagator { |
| 132 | + message_repository: Box::new(message_repository), |
| 133 | + }; |
| 134 | + |
| 135 | + let message = get_reject_state(None); |
| 136 | + let channel = get_channel("id", &None, None); |
| 137 | + |
| 138 | + let result = await!(propagator.propagate(&channel, Message::RejectState(message))); |
| 139 | + |
| 140 | + assert_eq!(2, result.len()); |
| 141 | + assert!(result[0].is_ok()); |
| 142 | + match &result[1] { |
| 143 | + Ok(_) => panic!("It should be an error"), |
| 144 | + Err(error) => assert_eq!( |
| 145 | + "Repository call for propagating the message failed", |
| 146 | + error.message |
| 147 | + ), |
| 148 | + } |
| 149 | + }) |
| 150 | + } |
| 151 | +} |
0 commit comments