mockforge-federation 0.3.110

Multi-workspace federation for MockForge - compose multiple mock workspaces into federated virtual systems
Documentation
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! # `MockForge` Federation
//!
//! Multi-workspace federation for `MockForge`.
//!
//! This crate enables composing multiple mock workspaces into a single federated
//! "virtual system" for large organizations with microservices architectures.
//!
//! ## Overview
//!
//! Federation allows you to:
//!
//! - Define service boundaries and map services to workspaces
//! - Compose multiple workspaces into one federated virtual system
//! - Run system-wide scenarios that span multiple services
//! - Control reality level per service independently
//!
//! ## Example Federation
//!
//! ```yaml
//! federation:
//!   name: "e-commerce-platform"
//!   services:
//!     - name: "auth"
//!       workspace_id: "workspace-auth-123"
//!       base_path: "/auth"
//!       reality_level: "real"  # Use real upstream
//!
//!     - name: "payments"
//!       workspace_id: "workspace-payments-456"
//!       base_path: "/payments"
//!       reality_level: "mock_v3"
//!
//!     - name: "inventory"
//!       workspace_id: "workspace-inventory-789"
//!       base_path: "/inventory"
//!       reality_level: "blended"  # Mix of mock and real
//!
//!     - name: "shipping"
//!       workspace_id: "workspace-shipping-012"
//!       base_path: "/shipping"
//!       reality_level: "chaos_driven"  # Chaos testing mode
//! ```
//!
//! ## Features
//!
//! - **Service Registry**: Define services and their workspace mappings
//! - **Federation Router**: Route requests to appropriate workspace based on service
//! - **Virtual System Manager**: Compose workspaces into unified system
//! - **Per-Service Reality Level**: Control reality level independently per service
//! - **System-Wide Scenarios**: Define scenarios that span multiple services

pub mod database;
pub mod federation;
pub mod router;
pub mod service;

pub use database::FederationDatabase;
pub use federation::{Federation, FederationConfig, FederationService};
pub use router::{FederationRouter, RoutingResult};
pub use service::{ServiceBoundary, ServiceRealityLevel};

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::sync::Arc;
    use uuid::Uuid;

    // Integration tests to ensure the public API works as expected

    #[test]
    fn test_service_reality_level_public_api() {
        // Test that ServiceRealityLevel enum is accessible and usable
        let level = ServiceRealityLevel::Real;
        assert_eq!(level.as_str(), "real");

        let parsed = ServiceRealityLevel::from_str("mock_v3");
        assert_eq!(parsed, Some(ServiceRealityLevel::MockV3));
    }

    #[test]
    fn test_service_boundary_public_api() {
        // Test that ServiceBoundary can be created and used
        let workspace_id = Uuid::new_v4();
        let service = ServiceBoundary::new(
            "test-service".to_string(),
            workspace_id,
            "/api".to_string(),
            ServiceRealityLevel::Blended,
        );

        assert_eq!(service.name, "test-service");
        assert_eq!(service.workspace_id, workspace_id);
        assert_eq!(service.base_path, "/api");
        assert_eq!(service.reality_level, ServiceRealityLevel::Blended);
        assert!(service.matches_path("/api/users"));
        assert!(!service.matches_path("/other"));
    }

    #[test]
    fn test_federation_config_public_api() {
        // Test that FederationConfig can be created and serialized
        let config = FederationConfig {
            name: "test-fed".to_string(),
            description: "Test federation".to_string(),
            services: vec![FederationService {
                name: "service1".to_string(),
                workspace_id: Uuid::new_v4().to_string(),
                base_path: "/service1".to_string(),
                reality_level: "real".to_string(),
                config: HashMap::new(),
                dependencies: Vec::new(),
            }],
        };

        assert_eq!(config.name, "test-fed");
        assert_eq!(config.services.len(), 1);

        // Test serialization
        let json = serde_json::to_string(&config).unwrap();
        assert!(json.contains("test-fed"));
    }

    #[test]
    fn test_federation_public_api() {
        // Test that Federation can be created from config
        let org_id = Uuid::new_v4();
        let config = FederationConfig {
            name: "my-federation".to_string(),
            description: "My test federation".to_string(),
            services: vec![FederationService {
                name: "auth".to_string(),
                workspace_id: Uuid::new_v4().to_string(),
                base_path: "/auth".to_string(),
                reality_level: "real".to_string(),
                config: HashMap::new(),
                dependencies: Vec::new(),
            }],
        };

        let federation = Federation::from_config(org_id, config).unwrap();

        assert_eq!(federation.name, "my-federation");
        assert_eq!(federation.org_id, org_id);
        assert_eq!(federation.services.len(), 1);
        assert_eq!(federation.services[0].name, "auth");
    }

    #[test]
    fn test_federation_router_public_api() {
        // Test that FederationRouter can route requests
        let org_id = Uuid::new_v4();
        let workspace_id = Uuid::new_v4();

        let config = FederationConfig {
            name: "router-test".to_string(),
            description: String::new(),
            services: vec![
                FederationService {
                    name: "auth".to_string(),
                    workspace_id: workspace_id.to_string(),
                    base_path: "/auth".to_string(),
                    reality_level: "real".to_string(),
                    config: HashMap::new(),
                    dependencies: Vec::new(),
                },
                FederationService {
                    name: "payments".to_string(),
                    workspace_id: Uuid::new_v4().to_string(),
                    base_path: "/payments".to_string(),
                    reality_level: "mock_v3".to_string(),
                    config: HashMap::new(),
                    dependencies: Vec::new(),
                },
            ],
        };

        let federation = Federation::from_config(org_id, config).unwrap();
        let router = FederationRouter::new(Arc::new(federation));

        // Test routing
        let result = router.route("/auth/login").unwrap();
        assert_eq!(result.service.name, "auth");
        assert_eq!(result.workspace_id, workspace_id);
        assert_eq!(result.service_path, "/login");

        // Test no match
        assert!(router.route("/unknown").is_none());
    }

    #[test]
    fn test_routing_result_public_api() {
        // Test that RoutingResult is accessible and usable
        let workspace_id = Uuid::new_v4();
        let service = Arc::new(ServiceBoundary::new(
            "test".to_string(),
            workspace_id,
            "/test".to_string(),
            ServiceRealityLevel::Real,
        ));

        let result = RoutingResult {
            workspace_id,
            service,
            service_path: "/path".to_string(),
        };

        assert_eq!(result.workspace_id, workspace_id);
        assert_eq!(result.service.name, "test");
        assert_eq!(result.service_path, "/path");

        // Test that it can be cloned
        let cloned = result.clone();
        assert_eq!(cloned.workspace_id, result.workspace_id);
    }

    #[test]
    fn test_federation_service_with_config() {
        // Test FederationService with complex config
        let mut config = HashMap::new();
        config.insert("timeout".to_string(), serde_json::json!(3000));
        config.insert("retries".to_string(), serde_json::json!(5));

        let service = FederationService {
            name: "api".to_string(),
            workspace_id: Uuid::new_v4().to_string(),
            base_path: "/api".to_string(),
            reality_level: "blended".to_string(),
            config: config.clone(),
            dependencies: vec!["auth".to_string(), "db".to_string()],
        };

        assert_eq!(service.config.len(), 2);
        assert_eq!(service.dependencies.len(), 2);

        // Test serialization/deserialization
        let json = serde_json::to_string(&service).unwrap();
        let deserialized: FederationService = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.name, service.name);
        assert_eq!(deserialized.config.len(), service.config.len());
    }

    #[test]
    fn test_all_reality_levels_exposed() {
        // Ensure all reality levels are accessible through public API
        // Verify all reality levels are constructible
        assert_eq!(ServiceRealityLevel::Real.as_str(), "real");
        assert_eq!(ServiceRealityLevel::MockV3.as_str(), "mock_v3");
        assert_eq!(ServiceRealityLevel::Blended.as_str(), "blended");
        assert_eq!(ServiceRealityLevel::ChaosDriven.as_str(), "chaos_driven");

        // Test conversion
        assert_eq!(ServiceRealityLevel::Real.as_str(), "real");
        assert_eq!(ServiceRealityLevel::MockV3.as_str(), "mock_v3");
        assert_eq!(ServiceRealityLevel::Blended.as_str(), "blended");
        assert_eq!(ServiceRealityLevel::ChaosDriven.as_str(), "chaos_driven");
    }

    #[test]
    fn test_federation_mutation_methods() {
        // Test that Federation mutation methods work
        let org_id = Uuid::new_v4();
        let config = FederationConfig {
            name: "mutable-fed".to_string(),
            description: String::new(),
            services: vec![FederationService {
                name: "initial".to_string(),
                workspace_id: Uuid::new_v4().to_string(),
                base_path: "/initial".to_string(),
                reality_level: "real".to_string(),
                config: HashMap::new(),
                dependencies: Vec::new(),
            }],
        };

        let mut federation = Federation::from_config(org_id, config).unwrap();
        assert_eq!(federation.services.len(), 1);

        // Add a service
        federation.add_service(ServiceBoundary::new(
            "added".to_string(),
            Uuid::new_v4(),
            "/added".to_string(),
            ServiceRealityLevel::MockV3,
        ));
        assert_eq!(federation.services.len(), 2);

        // Remove a service
        let removed = federation.remove_service("initial");
        assert!(removed);
        assert_eq!(federation.services.len(), 1);
        assert_eq!(federation.services[0].name, "added");
    }

    #[test]
    fn test_service_path_extraction() {
        // Test that service path extraction works through public API
        let service = ServiceBoundary::new(
            "api".to_string(),
            Uuid::new_v4(),
            "/api/v1".to_string(),
            ServiceRealityLevel::Real,
        );

        assert_eq!(service.extract_service_path("/api/v1/users"), Some("/users".to_string()));
        assert_eq!(service.extract_service_path("/api/v1"), Some("/".to_string()));
        assert_eq!(service.extract_service_path("/other"), None);
    }

    #[test]
    fn test_federation_service_lookup() {
        // Test finding services in federation
        let org_id = Uuid::new_v4();
        let config = FederationConfig {
            name: "lookup-test".to_string(),
            description: String::new(),
            services: vec![
                FederationService {
                    name: "service-a".to_string(),
                    workspace_id: Uuid::new_v4().to_string(),
                    base_path: "/a".to_string(),
                    reality_level: "real".to_string(),
                    config: HashMap::new(),
                    dependencies: Vec::new(),
                },
                FederationService {
                    name: "service-b".to_string(),
                    workspace_id: Uuid::new_v4().to_string(),
                    base_path: "/b".to_string(),
                    reality_level: "mock_v3".to_string(),
                    config: HashMap::new(),
                    dependencies: Vec::new(),
                },
            ],
        };

        let federation = Federation::from_config(org_id, config).unwrap();

        // Get by name
        let service_a = federation.get_service("service-a");
        assert!(service_a.is_some());
        assert_eq!(service_a.unwrap().base_path, "/a");

        // Find by path
        let service_b = federation.find_service_by_path("/b/endpoint");
        assert!(service_b.is_some());
        assert_eq!(service_b.unwrap().name, "service-b");
    }

    #[test]
    fn test_complete_workflow() {
        // Integration test of a complete workflow
        let org_id = Uuid::new_v4();

        // 1. Create configuration
        let config = FederationConfig {
            name: "e-commerce".to_string(),
            description: "E-commerce platform federation".to_string(),
            services: vec![
                FederationService {
                    name: "auth".to_string(),
                    workspace_id: Uuid::new_v4().to_string(),
                    base_path: "/auth".to_string(),
                    reality_level: "real".to_string(),
                    config: HashMap::new(),
                    dependencies: Vec::new(),
                },
                FederationService {
                    name: "catalog".to_string(),
                    workspace_id: Uuid::new_v4().to_string(),
                    base_path: "/catalog".to_string(),
                    reality_level: "mock_v3".to_string(),
                    config: HashMap::new(),
                    dependencies: vec!["auth".to_string()],
                },
            ],
        };

        // 2. Create federation from config
        let federation = Federation::from_config(org_id, config).unwrap();
        assert_eq!(federation.services.len(), 2);

        // 3. Create router
        let router = FederationRouter::new(Arc::new(federation));

        // 4. Route requests
        let auth_route = router.route("/auth/login").unwrap();
        assert_eq!(auth_route.service.name, "auth");
        assert_eq!(auth_route.service.reality_level, ServiceRealityLevel::Real);

        let catalog_route = router.route("/catalog/products/123").unwrap();
        assert_eq!(catalog_route.service.name, "catalog");
        assert_eq!(catalog_route.service_path, "/products/123");
        assert_eq!(catalog_route.service.reality_level, ServiceRealityLevel::MockV3);

        // 5. Verify service dependencies
        let catalog_service = router.services().iter().find(|s| s.name == "catalog").unwrap();
        assert_eq!(catalog_service.dependencies, vec!["auth".to_string()]);
    }

    #[test]
    fn test_yaml_config_roundtrip() {
        // Test that config can be serialized/deserialized with YAML
        let config = FederationConfig {
            name: "yaml-test".to_string(),
            description: "Testing YAML".to_string(),
            services: vec![FederationService {
                name: "test".to_string(),
                workspace_id: Uuid::new_v4().to_string(),
                base_path: "/test".to_string(),
                reality_level: "real".to_string(),
                config: HashMap::new(),
                dependencies: Vec::new(),
            }],
        };

        let yaml = serde_yaml::to_string(&config).unwrap();
        let deserialized: FederationConfig = serde_yaml::from_str(&yaml).unwrap();

        assert_eq!(deserialized.name, config.name);
        assert_eq!(deserialized.description, config.description);
        assert_eq!(deserialized.services.len(), config.services.len());
    }

    #[test]
    fn test_json_config_roundtrip() {
        // Test that config can be serialized/deserialized with JSON
        let mut service_config = HashMap::new();
        service_config.insert("key".to_string(), serde_json::json!("value"));

        let config = FederationConfig {
            name: "json-test".to_string(),
            description: "Testing JSON".to_string(),
            services: vec![FederationService {
                name: "test".to_string(),
                workspace_id: Uuid::new_v4().to_string(),
                base_path: "/test".to_string(),
                reality_level: "blended".to_string(),
                config: service_config,
                dependencies: vec!["dep1".to_string()],
            }],
        };

        let json = serde_json::to_string(&config).unwrap();
        let deserialized: FederationConfig = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.name, config.name);
        assert_eq!(deserialized.services[0].config.len(), 1);
        assert_eq!(deserialized.services[0].dependencies.len(), 1);
    }
}