interledger_test_helpers/
lib.rs1use bytes::Bytes;
6use futures::future::{result, FutureResult};
7use interledger_ildcp::IldcpAccount;
8use interledger_packet::{Fulfill, Reject};
9use interledger_service::*;
10use parking_lot::Mutex;
11use std::{marker::PhantomData, sync::Arc};
12
13#[derive(Debug, Eq, PartialEq, Clone)]
14pub struct TestAccount {
15 id: u64,
16 ilp_address: Bytes,
17 asset_scale: u8,
18 asset_code: String,
19}
20
21impl TestAccount {
22 pub fn new(id: u64, ilp_address: &[u8], asset_code: &str, asset_scale: u8) -> Self {
23 TestAccount {
24 id,
25 ilp_address: Bytes::from(ilp_address),
26 asset_code: asset_code.to_string(),
27 asset_scale,
28 }
29 }
30
31 pub fn default() -> Self {
32 TestAccount {
33 id: 0,
34 ilp_address: Bytes::from("example.account"),
35 asset_code: "XYZ".to_string(),
36 asset_scale: 9,
37 }
38 }
39}
40
41impl Account for TestAccount {
42 type AccountId = u64;
43
44 fn id(&self) -> u64 {
45 self.id
46 }
47}
48
49impl IldcpAccount for TestAccount {
50 fn asset_code(&self) -> String {
51 self.asset_code.clone()
52 }
53
54 fn asset_scale(&self) -> u8 {
55 self.asset_scale
56 }
57
58 fn client_address(&self) -> Bytes {
59 self.ilp_address.clone()
60 }
61}
62
63#[derive(Clone)]
64pub struct TestIncomingService<A: Account> {
65 response: Result<Fulfill, Reject>,
66 incoming_requests: Arc<Mutex<Vec<IncomingRequest<A>>>>,
67 account_type: PhantomData<A>,
68}
69
70impl<A> TestIncomingService<A>
71where
72 A: Account,
73{
74 pub fn fulfill(fulfill: Fulfill) -> Self {
75 TestIncomingService {
76 response: Ok(fulfill),
77 incoming_requests: Arc::new(Mutex::new(Vec::new())),
78 account_type: PhantomData,
79 }
80 }
81
82 pub fn reject(reject: Reject) -> Self {
83 TestIncomingService {
84 response: Err(reject),
85 incoming_requests: Arc::new(Mutex::new(Vec::new())),
86 account_type: PhantomData,
87 }
88 }
89
90 pub fn get_incoming_requests(&self) -> Vec<IncomingRequest<A>> {
91 self.incoming_requests.lock().clone()
92 }
93}
94
95impl<A> IncomingService<A> for TestIncomingService<A>
96where
97 A: Account,
98{
99 type Future = FutureResult<Fulfill, Reject>;
100
101 fn handle_request(&mut self, request: IncomingRequest<A>) -> Self::Future {
102 self.incoming_requests.lock().push(request);
103 result(self.response.clone())
104 }
105}
106
107#[derive(Clone)]
108pub struct TestOutgoingService<A: Account> {
109 response: Result<Fulfill, Reject>,
110 outgoing_requests: Arc<Mutex<Vec<OutgoingRequest<A>>>>,
111 account_type: PhantomData<A>,
112}
113
114impl<A> TestOutgoingService<A>
115where
116 A: Account,
117{
118 pub fn fulfill(fulfill: Fulfill) -> Self {
119 TestOutgoingService {
120 response: Ok(fulfill),
121 outgoing_requests: Arc::new(Mutex::new(Vec::new())),
122 account_type: PhantomData,
123 }
124 }
125
126 pub fn reject(reject: Reject) -> Self {
127 TestOutgoingService {
128 response: Err(reject),
129 outgoing_requests: Arc::new(Mutex::new(Vec::new())),
130 account_type: PhantomData,
131 }
132 }
133
134 pub fn get_outgoing_requests(&self) -> Vec<OutgoingRequest<A>> {
135 self.outgoing_requests.lock().clone()
136 }
137}
138
139impl<A> OutgoingService<A> for TestOutgoingService<A>
140where
141 A: Account,
142{
143 type Future = FutureResult<Fulfill, Reject>;
144
145 fn send_request(&mut self, request: OutgoingRequest<A>) -> Self::Future {
146 self.outgoing_requests.lock().push(request);
147 result(self.response.clone())
148 }
149}