1
+ use clap:: { App , SubCommand } ;
2
+ use masq_lib:: constants:: NODE_NOT_RUNNING_ERROR ;
3
+ use masq_lib:: messages:: { UiCollectNeighborhoodInfoRequest , UiCollectNeighborhoodInfoResponse } ;
4
+ use masq_lib:: short_writeln;
5
+ use crate :: command_context:: CommandContext ;
6
+ use crate :: commands:: commands_common:: { Command , CommandError , STANDARD_COMMAND_TIMEOUT_MILLIS , transaction} ;
7
+ use crate :: commands:: commands_common:: CommandError :: Payload ;
8
+
9
+
10
+ #[ derive( Debug ) ]
11
+
12
+ pub struct NeighborhoodInfoCommand { }
13
+
14
+ const NEIGHBORHOOD_INFO_SUBCOMMAND_ABOUT : & str =
15
+ "Example about for Neighborhood Info Command." ;
16
+
17
+ pub fn neighborhood_info_subcommand ( ) -> App < ' static , ' static > {
18
+ SubCommand :: with_name ( "neighborhood-info" ) . about ( NEIGHBORHOOD_INFO_SUBCOMMAND_ABOUT )
19
+ }
20
+
21
+ impl Command for NeighborhoodInfoCommand {
22
+ fn execute ( & self , context : & mut dyn CommandContext ) -> Result < ( ) , CommandError > {
23
+ let input = UiCollectNeighborhoodInfoRequest { } ;
24
+ let output: Result < UiCollectNeighborhoodInfoResponse , CommandError > =
25
+ transaction ( input, context, STANDARD_COMMAND_TIMEOUT_MILLIS ) ;
26
+
27
+ match output {
28
+ Ok ( response) => {
29
+ short_writeln ! ( context. stdout( ) , "NeighborhoodInfo Command msg -- TODO {:?}" , response) ;
30
+ Ok ( ( ) )
31
+ }
32
+
33
+ Err ( Payload ( code, message) ) if code == NODE_NOT_RUNNING_ERROR => {
34
+ short_writeln ! (
35
+ context. stderr( ) ,
36
+ "MASQNode is not running; therefore neighborhood information cannot be displayed."
37
+ ) ;
38
+ Err ( Payload ( code, message) )
39
+ }
40
+ Err ( e) => {
41
+ short_writeln ! ( context. stderr( ) , "Neighborhood information retrieval failed: {:?}" , e) ;
42
+ Err ( e)
43
+ }
44
+ }
45
+ }
46
+ }
47
+
48
+ impl Default for NeighborhoodInfoCommand {
49
+ fn default ( ) -> Self {
50
+ Self :: new ( )
51
+ }
52
+ }
53
+
54
+ impl NeighborhoodInfoCommand {
55
+ pub fn new ( ) -> Self {
56
+ Self { }
57
+ }
58
+ }
59
+
60
+
61
+ #[ cfg( test) ]
62
+ mod tests {
63
+ use std:: collections:: HashMap ;
64
+ use std:: sync:: { Arc , Mutex } ;
65
+ use masq_lib:: constants:: NODE_NOT_RUNNING_ERROR ;
66
+ use masq_lib:: messages:: { NodeInfo , ToMessageBody , UiCollectNeighborhoodInfoRequest , UiCollectNeighborhoodInfoResponse } ;
67
+ use crate :: command_context:: ContextError ;
68
+ use crate :: command_context:: ContextError :: ConnectionDropped ;
69
+ use crate :: command_factory:: { CommandFactory , CommandFactoryReal } ;
70
+ use crate :: commands:: commands_common:: { Command , CommandError , STANDARD_COMMAND_TIMEOUT_MILLIS } ;
71
+ use crate :: commands:: commands_common:: CommandError :: ConnectionProblem ;
72
+ use crate :: commands:: neighborhood_info_command:: { NEIGHBORHOOD_INFO_SUBCOMMAND_ABOUT , NeighborhoodInfoCommand } ;
73
+ use crate :: test_utils:: mocks:: CommandContextMock ;
74
+
75
+ #[ test]
76
+ fn constants_have_correct_values ( ) {
77
+ assert_eq ! (
78
+ NEIGHBORHOOD_INFO_SUBCOMMAND_ABOUT ,
79
+ "Example about for Neighborhood Info Command."
80
+ ) ;
81
+ }
82
+
83
+ #[ test]
84
+ fn testing_command_factory ( ) {
85
+ let factory = CommandFactoryReal :: new ( ) ;
86
+ let expect_result = HashMap :: from ( [
87
+ (
88
+ "public_key_1" . to_string ( ) ,
89
+ NodeInfo {
90
+ version : 252 ,
91
+ country_code : "UK" . to_string ( ) ,
92
+ exit_service : true ,
93
+ unreachable_hosts : vec ! [ "facebook.com" . to_string( ) , "x.com" . to_string( ) ] ,
94
+ } ,
95
+ ) ,
96
+ (
97
+ "public_key_2" . to_string ( ) ,
98
+ NodeInfo {
99
+ version : 5 ,
100
+ country_code : "CZ" . to_string ( ) ,
101
+ exit_service : false ,
102
+ unreachable_hosts : vec ! [ "facebook.com" . to_string( ) , "x.com" . to_string( ) ] ,
103
+ } ,
104
+ ) ,
105
+ ] ) ;
106
+ let mut context = CommandContextMock :: new ( ) . transact_result ( Ok ( UiCollectNeighborhoodInfoResponse {
107
+ neighborhood_database : expect_result,
108
+ } . tmb ( 0 ) ) ) ;
109
+ let subject = factory. make ( & [ "neighborhood-info" . to_string ( ) ] ) . unwrap ( ) ;
110
+
111
+ let result = subject. execute ( & mut context) ;
112
+
113
+ assert_eq ! ( result, Ok ( ( ) ) ) ;
114
+ }
115
+
116
+ #[ test]
117
+ fn doesnt_work_if_node_is_not_running ( ) {
118
+ let mut context = CommandContextMock :: new ( ) . transact_result ( Err (
119
+ ContextError :: PayloadError ( NODE_NOT_RUNNING_ERROR , "irrelevant" . to_string ( ) ) ,
120
+ ) ) ;
121
+ let stdout_arc = context. stdout_arc ( ) ;
122
+ let stderr_arc = context. stderr_arc ( ) ;
123
+ let subject = NeighborhoodInfoCommand :: new ( ) ;
124
+
125
+ let result = subject. execute ( & mut context) ;
126
+
127
+ assert_eq ! (
128
+ result,
129
+ Err ( CommandError :: Payload (
130
+ NODE_NOT_RUNNING_ERROR ,
131
+ "irrelevant" . to_string( )
132
+ ) )
133
+ ) ;
134
+ assert_eq ! (
135
+ stderr_arc. lock( ) . unwrap( ) . get_string( ) ,
136
+ "MASQNode is not running; therefore neighborhood information cannot be displayed.\n "
137
+ ) ;
138
+ assert_eq ! ( stdout_arc. lock( ) . unwrap( ) . get_string( ) , String :: new( ) ) ;
139
+ }
140
+
141
+
142
+ #[ test]
143
+ fn descriptor_command_bad_path ( ) {
144
+ let transact_params_arc = Arc :: new ( Mutex :: new ( vec ! [ ] ) ) ;
145
+ let mut context = CommandContextMock :: new ( )
146
+ . transact_params ( & transact_params_arc)
147
+ . transact_result ( Err ( ConnectionDropped ( "Booga" . to_string ( ) ) ) ) ;
148
+ let stdout_arc = context. stdout_arc ( ) ;
149
+ let stderr_arc = context. stderr_arc ( ) ;
150
+ let subject = NeighborhoodInfoCommand :: new ( ) ;
151
+
152
+ let result = subject. execute ( & mut context) ;
153
+
154
+ assert_eq ! ( result, Err ( ConnectionProblem ( "Booga" . to_string( ) ) ) ) ;
155
+ let transact_params = transact_params_arc. lock ( ) . unwrap ( ) ;
156
+ assert_eq ! (
157
+ * transact_params,
158
+ vec![ (
159
+ UiCollectNeighborhoodInfoRequest { } . tmb( 0 ) ,
160
+ STANDARD_COMMAND_TIMEOUT_MILLIS
161
+ ) ]
162
+ ) ;
163
+ assert_eq ! ( stdout_arc. lock( ) . unwrap( ) . get_string( ) , String :: new( ) ) ;
164
+ assert_eq ! (
165
+ stderr_arc. lock( ) . unwrap( ) . get_string( ) ,
166
+ "Neighborhood information retrieval failed: ConnectionProblem(\" Booga\" )\n "
167
+ ) ;
168
+ }
169
+
170
+ }
0 commit comments