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
mod block;
mod message;
mod node;
mod utxo;
mod vote;

use crate::jcli::command::rest::V0Command;
use assert_cmd::assert::OutputAssertExt;
use block::Block;
use jormungandr_lib::interfaces::{AccountState, LeadershipLog, SettingsDto, StakePoolStats};
use jortestkit::prelude::ProcessOutput;
use message::Message;
use node::Node;
use utxo::UtxO;
use vote::Vote;
pub struct RestV0 {
    v0_command: V0Command,
}

impl RestV0 {
    pub fn new(v0_command: V0Command) -> Self {
        Self { v0_command }
    }

    pub fn utxo(self) -> UtxO {
        UtxO::new(self.v0_command.utxo())
    }

    pub fn node(self) -> Node {
        Node::new(self.v0_command.node())
    }

    pub fn message(self) -> Message {
        Message::new(self.v0_command.message())
    }

    pub fn block(self) -> Block {
        Block::new(self.v0_command.block())
    }

    pub fn vote(self) -> Vote {
        Vote::new(self.v0_command.vote())
    }

    pub fn settings<S: Into<String>>(self, host: S) -> SettingsDto {
        let content = self
            .v0_command
            .settings(host)
            .build()
            .assert()
            .success()
            .get_output()
            .as_lossy_string();
        serde_yaml::from_str(&content).expect("Failed to parse settings")
    }

    pub fn stake_pools<S: Into<String>>(self, host: S) -> Vec<String> {
        let content = self
            .v0_command
            .stake_pools(host)
            .build()
            .assert()
            .success()
            .get_output()
            .as_lossy_string();
        serde_yaml::from_str(&content).expect("Failed to parse stake poools collection")
    }

    pub fn stake_pool<S: Into<String>, P: Into<String>>(
        self,
        stake_pool_id: S,
        host: P,
    ) -> StakePoolStats {
        let content = self
            .v0_command
            .stake_pool(stake_pool_id, host)
            .build()
            .assert()
            .success()
            .get_output()
            .as_lossy_string();
        serde_yaml::from_str(&content).expect("Failed to parse stak pool stats")
    }

    pub fn leadership_log<S: Into<String>>(self, host: S) -> Vec<LeadershipLog> {
        let content = self
            .v0_command
            .leadership_log(host)
            .build()
            .assert()
            .success()
            .get_output()
            .as_lossy_string();
        println!("Output: {:?}", content);
        serde_yaml::from_str(&content).unwrap()
    }

    pub fn tip<S: Into<String>>(self, host: S) -> String {
        self.v0_command
            .tip(host)
            .build()
            .assert()
            .success()
            .get_output()
            .as_single_line()
    }

    pub fn tip_expect_fail<S: Into<String>>(self, host: S, expected_msg: &str) {
        self.v0_command
            .tip(host)
            .build()
            .assert()
            .failure()
            .stderr(predicates::str::contains(expected_msg));
    }

    pub fn account_stats<S: Into<String>, P: Into<String>>(
        self,
        address: S,
        host: P,
    ) -> AccountState {
        let content = self
            .v0_command
            .account_stats(address, host)
            .build()
            .assert()
            .success()
            .get_output()
            .as_lossy_string();
        serde_yaml::from_str(&content).unwrap()
    }

    pub fn shutdown<S: Into<String>>(self, host: S) {
        self.v0_command.shutdown(host).build().assert().success();
    }
}