-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfactory.rs
More file actions
358 lines (322 loc) · 12.6 KB
/
Copy pathfactory.rs
File metadata and controls
358 lines (322 loc) · 12.6 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//! Command Handler Factory
//!
//! Provides centralized creation of command handlers with consistent
//! dependency injection and configuration management.
//!
//! ## Purpose
//!
//! Previously, each presentation command handler manually created application
//! command handlers with explicit dependency setup:
//!
//! ```rust,ignore
//! // Duplicate code in every handler:
//! let command_handler = CreateCommandHandler::new(
//! ctx.repository().clone(),
//! ctx.clock().clone()
//! );
//! ```
//!
//! `CommandHandlerFactory` eliminates this duplication by providing a single place to:
//! - Create application layer command handlers consistently
//! - Manage shared configuration (lock timeout)
//! - Support testing with custom factory configuration
//!
//! ## Benefits
//!
//! - **Consistency**: All command handlers created with same configuration
//! - **Maintainability**: Changes to handler creation logic in one place
//! - **Testability**: Easy to inject test configuration via `new_for_testing()`
//! - **Simplicity**: Presentation handlers focus on workflow, not setup
//!
//! ## Usage Example
//!
//! ```rust
//! use std::path::Path;
//! use std::sync::{Arc, Mutex};
//! use torrust_tracker_deployer_lib::presentation::commands::factory::CommandHandlerFactory;
//! use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
//!
//! fn handle_command(working_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
//! // Create factory with default configuration
//! let factory = CommandHandlerFactory::new();
//! let output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)));
//!
//! // Create command context
//! let context = factory.create_context(working_dir.to_path_buf(), output);
//!
//! // Create command handler
//! let handler = factory.create_create_handler(&context);
//!
//! // Use handler...
//! Ok(())
//! }
//! ```
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use crate::application::command_handlers::{CreateCommandHandler, DestroyCommandHandler};
use crate::infrastructure::persistence::repository_factory::RepositoryFactory;
use crate::presentation::user_output::UserOutput;
use super::constants::DEFAULT_LOCK_TIMEOUT;
use super::context::CommandContext;
/// Factory for creating command handlers with consistent configuration
///
/// This factory centralizes the creation of application layer command handlers,
/// ensuring consistent dependency injection and configuration across all commands.
///
/// The factory uses `RepositoryFactory` to configure repository lock timeouts,
/// and delegates context creation to `CommandContext` for managing output,
/// repository, and clock dependencies.
///
/// # Examples
///
/// ```rust
/// use std::path::PathBuf;
/// use std::sync::{Arc, Mutex};
/// use torrust_tracker_deployer_lib::presentation::commands::factory::CommandHandlerFactory;
/// use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
///
/// let factory = CommandHandlerFactory::new();
/// let output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)));
/// let context = factory.create_context(PathBuf::from("."), output);
/// let handler = factory.create_create_handler(&context);
/// ```
pub struct CommandHandlerFactory {
/// Repository factory for creating environment repositories
repository_factory: RepositoryFactory,
}
impl CommandHandlerFactory {
/// Create a new factory with default configuration
///
/// This constructor initializes the factory with production defaults:
/// - Repository lock timeout from `DEFAULT_LOCK_TIMEOUT`
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::commands::factory::CommandHandlerFactory;
///
/// let factory = CommandHandlerFactory::new();
/// ```
#[must_use]
pub fn new() -> Self {
let repository_factory = RepositoryFactory::new(DEFAULT_LOCK_TIMEOUT);
Self { repository_factory }
}
/// Create a command context for the given working directory
///
/// This method creates a `CommandContext` with all shared dependencies:
/// - Repository configured with the factory's settings
/// - System clock
/// - User output with default verbosity
///
/// # Arguments
///
/// * `working_dir` - Root directory for environment data storage
///
/// # Returns
///
/// A `CommandContext` ready for use with command handlers
///
/// # Examples
///
/// ```rust
/// use std::path::PathBuf;
/// use std::sync::{Arc, Mutex};
/// use torrust_tracker_deployer_lib::presentation::commands::factory::CommandHandlerFactory;
/// use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
///
/// let factory = CommandHandlerFactory::new();
/// let user_output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)));
/// let context = factory.create_context(PathBuf::from("./data"), user_output);
/// ```
#[must_use]
pub fn create_context(
&self,
working_dir: PathBuf,
user_output: Arc<Mutex<UserOutput>>,
) -> CommandContext {
CommandContext::new_with_factory(&self.repository_factory, working_dir, user_output)
}
/// Create a create command handler
///
/// This method creates a `CreateCommandHandler` with dependencies from the context.
///
/// # Arguments
///
/// * `context` - Command context containing repository and clock
///
/// # Returns
///
/// A `CreateCommandHandler` ready to execute create operations
///
/// # Examples
///
/// ```rust
/// use std::path::PathBuf;
/// use std::sync::{Arc, Mutex};
/// use torrust_tracker_deployer_lib::presentation::commands::factory::CommandHandlerFactory;
/// use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
///
/// let factory = CommandHandlerFactory::new();
/// let output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)));
/// let context = factory.create_context(PathBuf::from("."), output);
/// let handler = factory.create_create_handler(&context);
/// ```
#[must_use]
pub fn create_create_handler(&self, context: &CommandContext) -> CreateCommandHandler {
CreateCommandHandler::new(context.repository().clone(), context.clock().clone())
}
/// Create a destroy command handler
///
/// This method creates a `DestroyCommandHandler` with dependencies from the context.
///
/// # Arguments
///
/// * `context` - Command context containing repository and clock
///
/// # Returns
///
/// A `DestroyCommandHandler` ready to execute destroy operations
///
/// # Examples
///
/// ```rust
/// use std::path::PathBuf;
/// use std::sync::{Arc, Mutex};
/// use torrust_tracker_deployer_lib::presentation::commands::factory::CommandHandlerFactory;
/// use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
///
/// let factory = CommandHandlerFactory::new();
/// let output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)));
/// let context = factory.create_context(PathBuf::from("."), output);
/// let handler = factory.create_destroy_handler(&context);
/// ```
#[must_use]
pub fn create_destroy_handler(&self, context: &CommandContext) -> DestroyCommandHandler {
DestroyCommandHandler::new(context.repository().clone(), context.clock().clone())
}
}
impl Default for CommandHandlerFactory {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
impl CommandHandlerFactory {
/// Create a factory for testing with custom repository factory
///
/// This constructor allows tests to inject custom configuration, such as
/// different lock timeouts for testing timeout scenarios.
///
/// # Arguments
///
/// * `repository_factory` - Custom repository factory for testing
///
/// # Examples
///
/// ```rust
/// use std::time::Duration;
/// use torrust_tracker_deployer_lib::presentation::commands::factory::CommandHandlerFactory;
/// use torrust_tracker_deployer_lib::infrastructure::persistence::repository_factory::RepositoryFactory;
///
/// // Create factory with custom lock timeout for testing
/// let repository_factory = RepositoryFactory::new(Duration::from_millis(100));
/// let factory = CommandHandlerFactory::new_for_testing(repository_factory);
/// ```
#[must_use]
pub fn new_for_testing(repository_factory: RepositoryFactory) -> Self {
Self { repository_factory }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::presentation::user_output::test_support::TestUserOutput;
use crate::presentation::user_output::VerbosityLevel;
use tempfile::TempDir;
/// Test helper to create a test setup with factory, temp directory, and user output
///
/// Returns a tuple of (`CommandHandlerFactory`, `TempDir`, `PathBuf`, `Arc<Mutex<UserOutput>>`)
/// The `TempDir` must be kept alive for the duration of the test.
fn create_test_setup() -> (
CommandHandlerFactory,
TempDir,
PathBuf,
Arc<Mutex<UserOutput>>,
) {
let factory = CommandHandlerFactory::new();
let temp_dir = TempDir::new().unwrap();
let working_dir = temp_dir.path().to_path_buf();
let user_output = TestUserOutput::wrapped(VerbosityLevel::Normal);
(factory, temp_dir, working_dir, user_output)
}
#[test]
fn it_should_create_factory_with_default_configuration() {
let factory = CommandHandlerFactory::new();
// Verify factory is created (basic structure test)
// The internal repository_factory is private, so we just verify
// the factory can be created
let _ = factory;
}
#[test]
fn it_should_create_factory_via_default_trait() {
let factory = CommandHandlerFactory::default();
// Verify default trait works
let _ = factory;
}
#[test]
fn it_should_create_context_with_factory() {
let (factory, _temp_dir, working_dir, user_output) = create_test_setup();
let context = factory.create_context(working_dir, user_output);
// Verify context is created with dependencies
let _ = context.repository();
let _ = context.clock();
}
#[test]
fn it_should_create_create_handler() {
let (factory, _temp_dir, working_dir, user_output) = create_test_setup();
let context = factory.create_context(working_dir, user_output);
let _handler = factory.create_create_handler(&context);
// Verify handler is created (basic structure test)
}
#[test]
fn it_should_create_destroy_handler() {
let (factory, _temp_dir, working_dir, user_output) = create_test_setup();
let context = factory.create_context(working_dir, user_output);
let _handler = factory.create_destroy_handler(&context);
// Verify handler is created (basic structure test)
}
#[test]
fn it_should_create_multiple_contexts_from_same_factory() {
let (factory, _temp_dir, working_dir, _user_output) = create_test_setup();
// Should be able to create multiple contexts
let context1 = factory.create_context(
working_dir.clone(),
TestUserOutput::wrapped(VerbosityLevel::Normal),
);
let context2 =
factory.create_context(working_dir, TestUserOutput::wrapped(VerbosityLevel::Normal));
// Both contexts should be functional
let _ = context1.repository();
let _ = context2.repository();
}
#[test]
fn it_should_create_multiple_handlers_from_same_context() {
let (factory, _temp_dir, working_dir, user_output) = create_test_setup();
let context = factory.create_context(working_dir, user_output);
// Should be able to create multiple handlers from same context
let _create_handler = factory.create_create_handler(&context);
let _destroy_handler = factory.create_destroy_handler(&context);
// Both handlers should be functional
}
#[test]
fn it_should_create_factory_for_testing() {
use std::time::Duration;
let repository_factory = RepositoryFactory::new(Duration::from_millis(100));
let factory = CommandHandlerFactory::new_for_testing(repository_factory);
let (_factory, _temp_dir, working_dir, user_output) = create_test_setup();
// Should be able to create context with custom factory
let context = factory.create_context(working_dir, user_output);
let _ = context.repository();
}
}