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
//! # interledger-test-helpers
//!
//! Helper structs and functions for use when testing other Interledger services.

use bytes::Bytes;
use futures::future::{result, FutureResult};
use interledger_ildcp::IldcpAccount;
use interledger_packet::{Fulfill, Reject};
use interledger_service::*;
use parking_lot::Mutex;
use std::{marker::PhantomData, sync::Arc};

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct TestAccount {
    id: u64,
    ilp_address: Bytes,
    asset_scale: u8,
    asset_code: String,
}

impl TestAccount {
    pub fn new(id: u64, ilp_address: &[u8], asset_code: &str, asset_scale: u8) -> Self {
        TestAccount {
            id,
            ilp_address: Bytes::from(ilp_address),
            asset_code: asset_code.to_string(),
            asset_scale,
        }
    }

    pub fn default() -> Self {
        TestAccount {
            id: 0,
            ilp_address: Bytes::from("example.account"),
            asset_code: "XYZ".to_string(),
            asset_scale: 9,
        }
    }
}

impl Account for TestAccount {
    type AccountId = u64;

    fn id(&self) -> u64 {
        self.id
    }
}

impl IldcpAccount for TestAccount {
    fn asset_code(&self) -> String {
        self.asset_code.clone()
    }

    fn asset_scale(&self) -> u8 {
        self.asset_scale
    }

    fn client_address(&self) -> Bytes {
        self.ilp_address.clone()
    }
}

#[derive(Clone)]
pub struct TestIncomingService<A: Account> {
    response: Result<Fulfill, Reject>,
    incoming_requests: Arc<Mutex<Vec<IncomingRequest<A>>>>,
    account_type: PhantomData<A>,
}

impl<A> TestIncomingService<A>
where
    A: Account,
{
    pub fn fulfill(fulfill: Fulfill) -> Self {
        TestIncomingService {
            response: Ok(fulfill),
            incoming_requests: Arc::new(Mutex::new(Vec::new())),
            account_type: PhantomData,
        }
    }

    pub fn reject(reject: Reject) -> Self {
        TestIncomingService {
            response: Err(reject),
            incoming_requests: Arc::new(Mutex::new(Vec::new())),
            account_type: PhantomData,
        }
    }

    pub fn get_incoming_requests(&self) -> Vec<IncomingRequest<A>> {
        self.incoming_requests.lock().clone()
    }
}

impl<A> IncomingService<A> for TestIncomingService<A>
where
    A: Account,
{
    type Future = FutureResult<Fulfill, Reject>;

    fn handle_request(&mut self, request: IncomingRequest<A>) -> Self::Future {
        self.incoming_requests.lock().push(request);
        result(self.response.clone())
    }
}

#[derive(Clone)]
pub struct TestOutgoingService<A: Account> {
    response: Result<Fulfill, Reject>,
    outgoing_requests: Arc<Mutex<Vec<OutgoingRequest<A>>>>,
    account_type: PhantomData<A>,
}

impl<A> TestOutgoingService<A>
where
    A: Account,
{
    pub fn fulfill(fulfill: Fulfill) -> Self {
        TestOutgoingService {
            response: Ok(fulfill),
            outgoing_requests: Arc::new(Mutex::new(Vec::new())),
            account_type: PhantomData,
        }
    }

    pub fn reject(reject: Reject) -> Self {
        TestOutgoingService {
            response: Err(reject),
            outgoing_requests: Arc::new(Mutex::new(Vec::new())),
            account_type: PhantomData,
        }
    }

    pub fn get_outgoing_requests(&self) -> Vec<OutgoingRequest<A>> {
        self.outgoing_requests.lock().clone()
    }
}

impl<A> OutgoingService<A> for TestOutgoingService<A>
where
    A: Account,
{
    type Future = FutureResult<Fulfill, Reject>;

    fn send_request(&mut self, request: OutgoingRequest<A>) -> Self::Future {
        self.outgoing_requests.lock().push(request);
        result(self.response.clone())
    }
}