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
use crate::ingredient::{Ingredient, Fill};
use crate::source::Source;
use crate::brewery::Brewery;

use std::sync::{Arc, RwLock};

/// Data Structure that holds the recipe to brew tea (ETL data).
pub struct Pot {
    recipe:  Arc<RwLock<Vec<Box<dyn Ingredient + Send + Sync>>>>,
    sources: Vec<Box<dyn Source>>,
}

impl Pot {
    ///
    /// Initializes Pot with an empty recipe and empty sources.
    pub fn new() -> Pot {
        Pot { recipe: Arc::new(RwLock::new(Vec::new())), sources: Vec::new() }
    }

    ///
    /// Adds Ingredient to recipe held by the Pot.
    ///
    /// # Arguments
    ///
    /// * `ingredient` - the ingredient to add to the recipe
    pub fn add_ingredient(&self, ingredient: Box<dyn Ingredient + Send + Sync>) {
        let mut recipe = self.recipe.write().unwrap();
        recipe.push(ingredient);
    }

    ///
    /// Adds Source to sources held by the Pot.
    ///
    /// # Arguments
    ///
    /// * `source` - the source to add to the sources Array
    pub fn add_source(&mut self, source: Box<dyn Source>) {
        &self.sources.push(source);
    }

    /// 
    /// Returns the sources held by the Pot.
    pub fn get_sources(&self) -> &Vec<Box<dyn Source>> {
        &self.sources
    }

    /// 
    /// Returns the recipe held by the Pot.
    pub fn get_recipe(&self) -> Arc<RwLock<Vec<Box<dyn Ingredient + Send + Sync>>>> {
        Arc::clone(&self.recipe)
    }

    ///
    /// Iterates over sources to pull in data and send jobs to the Brewery for processing.
    ///
    /// # Arguments
    ///
    /// * `brewery` - Brewery struct holding the receiver and Brewer Array to process Tea
    pub fn brew(&self, brewery: &Brewery) {
        println!("Brewing Tea...");
        for source in self.get_sources() {
            source.print();
            let fill = source.as_any().downcast_ref::<Fill>().unwrap();
            fill.collect(brewery, self.get_recipe());
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Pot;
    use super::super::ingredient::{Fill, Steep, Pour, Argument};
    use super::super::tea::Tea;
    use std::any::Any;

    #[derive(Debug, PartialEq, Default)]
    struct TestTea {
        x: i32,
    }

    impl Tea for TestTea {
        fn as_any(&self) -> &dyn Any {
            self
        }
    }

    #[derive(Default)]
    struct TestArgs {
        pub val: i32
    }

    impl Argument for TestArgs {
        fn as_any(&self) -> &dyn Any {
            self
        }
    }

    #[test]
    fn create_empty_pot() {
        let new_pot = Pot::new();
        assert_eq!(new_pot.get_recipe().read().unwrap().len(), 0);
    }

    #[test]
    fn create_pot_with_source() {
        let mut new_pot = Pot::new();
        new_pot.add_source(Box::new(Fill{
            name: String::from("fake_tea"),
            source: String::from("hardcoded"),
            computation: Box::new(|_args, _brewery, _recipe| {
                Box::new(TestTea::default()) as Box<dyn Tea + Send>;
            }),
            params: None,
        }));
        assert_eq!(new_pot.get_sources().len(), 1);
        assert_eq!(new_pot.get_sources()[0].get_name(), "fake_tea");
        assert_eq!(new_pot.get_sources()[0].get_source(), "hardcoded");
    }

    #[test]
    fn create_pot_with_recipe() {
        let new_pot = Pot::new();
        new_pot.add_ingredient(Box::new(Steep{
            name: String::from("steep1"),
            computation: Box::new(|_tea, _args| {
                vec![Box::new(TestTea::default()) as Box<dyn Tea + Send>]
            }),
            params: None,
        }));
        new_pot.add_ingredient(Box::new(Pour{
            name: String::from("pour1"),
            computation: Box::new(|_tea, _args| {
                vec![Box::new(TestTea::default()) as Box<dyn Tea + Send>]
            }),
            params: None,
        }));
        assert_eq!(new_pot.get_recipe().read().unwrap().len(), 2);
        assert_eq!(new_pot.get_recipe().read().unwrap()[0].get_name(), "steep1");
        assert_eq!(new_pot.get_recipe().read().unwrap()[1].get_name(), "pour1");
    }

    #[test]
    fn create_pot_with_recipe_and_optional_params() {
        let new_pot = Pot::new();
        new_pot.add_ingredient(Box::new(Steep{
            name: String::from("steep1"),
            computation: Box::new(|_tea, _args| {
                vec![Box::new(TestTea::default()) as Box<dyn Tea + Send>]
            }),
            params: Some(Box::new(TestArgs::default())),
        }));
        new_pot.add_ingredient(Box::new(Pour{
            name: String::from("pour1"),
            computation: Box::new(|_tea, _args| {
                vec![Box::new(TestTea::default()) as Box<dyn Tea + Send>]
            }),
            params: None,
        }));
        assert_eq!(new_pot.get_recipe().read().unwrap().len(), 2);
        assert_eq!(new_pot.get_recipe().read().unwrap()[0].get_name(), "steep1");
        assert_eq!(new_pot.get_recipe().read().unwrap()[1].get_name(), "pour1");
    }

    #[test]
    fn create_pot_with_source_and_recipe() {
        let mut new_pot = Pot::new();
        new_pot.add_source(Box::new(Fill{
            name: String::from("fake_tea"),
            source: String::from("hardcoded"),
            computation: Box::new(|_args, _brewery, _recipe| {
                Box::new(TestTea::default()) as Box<dyn Tea + Send>;
            }),
            params: None,
        }));
        new_pot.add_ingredient(Box::new(Steep{
            name: String::from("steep1"),
            computation: Box::new(|_tea, _args| {
                vec![Box::new(TestTea::default()) as Box<dyn Tea + Send>]
            }),
            params: None,
        }));
        new_pot.add_ingredient(Box::new(Pour{
            name: String::from("pour1"),
            computation: Box::new(|_tea, _args| {
                vec![Box::new(TestTea::default()) as Box<dyn Tea + Send>]
            }),
            params: None,
        }));
        assert_eq!(new_pot.get_sources().len(), 1);
        assert_eq!(new_pot.get_recipe().read().unwrap().len(), 2);
        assert_eq!(new_pot.get_sources()[0].get_name(), "fake_tea");
        assert_eq!(new_pot.get_sources()[0].get_source(), "hardcoded");
        assert_eq!(new_pot.get_recipe().read().unwrap()[0].get_name(), "steep1");
        assert_eq!(new_pot.get_recipe().read().unwrap()[1].get_name(), "pour1");
    }

    //TODO: Readd test after returning Result
    //#[test]
    //fn brew_recipe() {
    //    let mut new_pot = Pot::new();
    //    new_pot.add(Box::new(Fill));
    //    new_pot.add(Box::new(Steep));
    //    new_pot.add(Box::new(Pour));
    //    assert_eq!(new_pot.brew(), 3);
    //}
}