redis-enterprise 0.8.7

Redis Enterprise REST API client library
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
//! Mock server wrapper for testing Redis Enterprise API clients
//!
//! # Example
//!
//! ```ignore
//! use redis_enterprise::testing::MockEnterpriseServer;
//! use redis_enterprise::testing::fixtures::DatabaseFixture;
//! use redis_enterprise::testing::responses;
//!
//! #[tokio::test]
//! async fn test_my_app() {
//!     let server = MockEnterpriseServer::start().await;
//!
//!     // Set up mock responses
//!     server.mock_databases_list(vec![
//!         DatabaseFixture::new(1, "cache").build(),
//!         DatabaseFixture::new(2, "sessions").build(),
//!     ]).await;
//!
//!     // Create a client pointing to the mock
//!     let client = server.client();
//!
//!     // Test your application code
//!     let dbs = client.databases().list().await.unwrap();
//!     assert_eq!(dbs.len(), 2);
//! }
//! ```

use crate::EnterpriseClient;
use serde_json::Value;
use wiremock::matchers::{method, path, path_regex};
use wiremock::{Mock, MockServer, ResponseTemplate};

/// A wrapper around wiremock's MockServer configured for Redis Enterprise API testing
pub struct MockEnterpriseServer {
    server: MockServer,
}

impl MockEnterpriseServer {
    /// Start a new mock server
    pub async fn start() -> Self {
        Self {
            server: MockServer::start().await,
        }
    }

    /// Get the base URI of the mock server
    pub fn uri(&self) -> String {
        self.server.uri()
    }

    /// Create an EnterpriseClient configured to use this mock server
    pub fn client(&self) -> EnterpriseClient {
        self.client_builder()
            .build()
            .expect("Failed to build test client")
    }

    /// Get a pre-configured client builder for this mock server
    ///
    /// Use this when you need to customize the client configuration,
    /// for example to test CA certificate handling or custom timeouts.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let server = MockEnterpriseServer::start().await;
    ///
    /// // Customize the client
    /// let client = server.client_builder()
    ///     .timeout(std::time::Duration::from_secs(5))
    ///     .user_agent("my-app/1.0")
    ///     .build()
    ///     .unwrap();
    /// ```
    pub fn client_builder(&self) -> crate::EnterpriseClientBuilder {
        EnterpriseClient::builder()
            .base_url(self.uri())
            .username("test@example.com")
            .password("password")
            .insecure(true)
    }

    /// Get a reference to the underlying MockServer for custom mocking
    pub fn inner(&self) -> &MockServer {
        &self.server
    }

    // Database mocks

    /// Mock GET /v1/bdbs to return a list of databases
    pub async fn mock_databases_list(&self, databases: Vec<Value>) {
        Mock::given(method("GET"))
            .and(path("/v1/bdbs"))
            .respond_with(ResponseTemplate::new(200).set_body_json(databases))
            .mount(&self.server)
            .await;
    }

    /// Mock GET /v1/bdbs/{uid} to return a specific database
    pub async fn mock_database_get(&self, uid: u32, database: Value) {
        Mock::given(method("GET"))
            .and(path(format!("/v1/bdbs/{}", uid)))
            .respond_with(ResponseTemplate::new(200).set_body_json(database))
            .mount(&self.server)
            .await;
    }

    /// Mock POST /v1/bdbs to create a database
    pub async fn mock_database_create(&self, response: Value) {
        Mock::given(method("POST"))
            .and(path("/v1/bdbs"))
            .respond_with(ResponseTemplate::new(201).set_body_json(response))
            .mount(&self.server)
            .await;
    }

    /// Mock DELETE /v1/bdbs/{uid}
    pub async fn mock_database_delete(&self, uid: u32) {
        Mock::given(method("DELETE"))
            .and(path(format!("/v1/bdbs/{}", uid)))
            .respond_with(ResponseTemplate::new(204))
            .mount(&self.server)
            .await;
    }

    // Node mocks

    /// Mock GET /v1/nodes to return a list of nodes
    pub async fn mock_nodes_list(&self, nodes: Vec<Value>) {
        Mock::given(method("GET"))
            .and(path("/v1/nodes"))
            .respond_with(ResponseTemplate::new(200).set_body_json(nodes))
            .mount(&self.server)
            .await;
    }

    /// Mock GET /v1/nodes/{uid} to return a specific node
    pub async fn mock_node_get(&self, uid: u32, node: Value) {
        Mock::given(method("GET"))
            .and(path(format!("/v1/nodes/{}", uid)))
            .respond_with(ResponseTemplate::new(200).set_body_json(node))
            .mount(&self.server)
            .await;
    }

    // Cluster mocks

    /// Mock GET /v1/cluster to return cluster info
    pub async fn mock_cluster_info(&self, cluster: Value) {
        Mock::given(method("GET"))
            .and(path("/v1/cluster"))
            .respond_with(ResponseTemplate::new(200).set_body_json(cluster))
            .mount(&self.server)
            .await;
    }

    /// Mock GET /v1/cluster/stats/last to return cluster stats
    pub async fn mock_cluster_stats(&self, stats: Value) {
        Mock::given(method("GET"))
            .and(path("/v1/cluster/stats/last"))
            .respond_with(ResponseTemplate::new(200).set_body_json(stats))
            .mount(&self.server)
            .await;
    }

    /// Mock GET /v1/license to return license info
    pub async fn mock_license(&self, license: Value) {
        Mock::given(method("GET"))
            .and(path("/v1/license"))
            .respond_with(ResponseTemplate::new(200).set_body_json(license))
            .mount(&self.server)
            .await;
    }

    // User mocks

    /// Mock GET /v1/users to return a list of users
    pub async fn mock_users_list(&self, users: Vec<Value>) {
        Mock::given(method("GET"))
            .and(path("/v1/users"))
            .respond_with(ResponseTemplate::new(200).set_body_json(users))
            .mount(&self.server)
            .await;
    }

    /// Mock GET /v1/users/{uid} to return a specific user
    pub async fn mock_user_get(&self, uid: u32, user: Value) {
        Mock::given(method("GET"))
            .and(path(format!("/v1/users/{}", uid)))
            .respond_with(ResponseTemplate::new(200).set_body_json(user))
            .mount(&self.server)
            .await;
    }

    // Alert mocks

    /// Mock GET /v1/alerts to return a list of alerts
    pub async fn mock_alerts_list(&self, alerts: Vec<Value>) {
        Mock::given(method("GET"))
            .and(path("/v1/alerts"))
            .respond_with(ResponseTemplate::new(200).set_body_json(alerts))
            .mount(&self.server)
            .await;
    }

    /// Mock GET /v1/alerts/{uid} to return a specific alert
    pub async fn mock_alert_get(&self, uid: &str, alert: Value) {
        Mock::given(method("GET"))
            .and(path(format!("/v1/alerts/{}", uid)))
            .respond_with(ResponseTemplate::new(200).set_body_json(alert))
            .mount(&self.server)
            .await;
    }

    /// Mock GET /v1/bdbs/{bdb_uid}/alerts to return alerts for a database
    pub async fn mock_database_alerts(&self, bdb_uid: u32, alerts: Vec<Value>) {
        Mock::given(method("GET"))
            .and(path(format!("/v1/bdbs/{}/alerts", bdb_uid)))
            .respond_with(ResponseTemplate::new(200).set_body_json(alerts))
            .mount(&self.server)
            .await;
    }

    /// Mock GET /v1/nodes/{node_uid}/alerts to return alerts for a node
    pub async fn mock_node_alerts(&self, node_uid: u32, alerts: Vec<Value>) {
        Mock::given(method("GET"))
            .and(path(format!("/v1/nodes/{}/alerts", node_uid)))
            .respond_with(ResponseTemplate::new(200).set_body_json(alerts))
            .mount(&self.server)
            .await;
    }

    /// Mock GET /v1/cluster/alerts to return cluster alerts
    pub async fn mock_cluster_alerts(&self, alerts: Vec<Value>) {
        Mock::given(method("GET"))
            .and(path("/v1/cluster/alerts"))
            .respond_with(ResponseTemplate::new(200).set_body_json(alerts))
            .mount(&self.server)
            .await;
    }

    // Error mocks

    /// Mock any GET request to a path pattern to return 404
    pub async fn mock_not_found(&self, path_pattern: &str) {
        Mock::given(method("GET"))
            .and(path_regex(path_pattern))
            .respond_with(super::responses::not_found("Resource not found"))
            .mount(&self.server)
            .await;
    }

    /// Mock any request to return 401 Unauthorized
    pub async fn mock_unauthorized(&self) {
        Mock::given(method("GET"))
            .respond_with(super::responses::unauthorized())
            .mount(&self.server)
            .await;
    }

    /// Mock any request to a path to return 500 Server Error
    pub async fn mock_server_error(&self, path_str: &str, message: &str) {
        Mock::given(method("GET"))
            .and(path(path_str))
            .respond_with(super::responses::server_error(message))
            .mount(&self.server)
            .await;
    }

    // Custom mock support

    /// Mount a custom mock on the server
    pub async fn mount(&self, mock: Mock) {
        mock.mount(&self.server).await;
    }

    /// Mount a custom response template at a specific path
    pub async fn mock_path(&self, http_method: &str, path_str: &str, response: ResponseTemplate) {
        Mock::given(method(http_method))
            .and(path(path_str))
            .respond_with(response)
            .mount(&self.server)
            .await;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testing::fixtures::{AlertFixture, ClusterFixture, DatabaseFixture, NodeFixture};

    #[tokio::test]
    async fn test_mock_server_starts() {
        let server = MockEnterpriseServer::start().await;
        assert!(server.uri().starts_with("http://"));
    }

    #[tokio::test]
    async fn test_mock_databases_list() {
        let server = MockEnterpriseServer::start().await;
        server
            .mock_databases_list(vec![
                DatabaseFixture::new(1, "test-db").build(),
                DatabaseFixture::new(2, "other-db").build(),
            ])
            .await;

        let client = server.client();
        let dbs = client.databases().list().await.unwrap();
        assert_eq!(dbs.len(), 2);
        assert_eq!(dbs[0].name, "test-db");
        assert_eq!(dbs[1].name, "other-db");
    }

    #[tokio::test]
    async fn test_mock_database_get() {
        let server = MockEnterpriseServer::start().await;
        server
            .mock_database_get(
                1,
                DatabaseFixture::new(1, "my-cache")
                    .memory_size(2 * 1024 * 1024 * 1024)
                    .build(),
            )
            .await;

        let client = server.client();
        let db = client.databases().get(1).await.unwrap();
        assert_eq!(db.name, "my-cache");
        assert_eq!(db.memory_size, Some(2 * 1024 * 1024 * 1024));
    }

    #[tokio::test]
    async fn test_mock_nodes_list() {
        let server = MockEnterpriseServer::start().await;
        server
            .mock_nodes_list(vec![
                NodeFixture::new(1, "10.0.0.1").build(),
                NodeFixture::new(2, "10.0.0.2").cores(8).build(),
            ])
            .await;

        let client = server.client();
        let nodes = client.nodes().list().await.unwrap();
        assert_eq!(nodes.len(), 2);
    }

    #[tokio::test]
    async fn test_mock_cluster_info() {
        let server = MockEnterpriseServer::start().await;
        server
            .mock_cluster_info(
                ClusterFixture::new("test-cluster")
                    .nodes(vec![1, 2, 3])
                    .build(),
            )
            .await;

        let client = server.client();
        let info = client.cluster().info().await.unwrap();
        assert_eq!(info.name, "test-cluster");
    }

    #[tokio::test]
    async fn test_custom_mock() {
        use wiremock::ResponseTemplate;
        use wiremock::matchers::{method, path};

        let server = MockEnterpriseServer::start().await;

        // Use the inner MockServer for custom mocking
        Mock::given(method("GET"))
            .and(path("/v1/custom"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "custom": "response"
            })))
            .mount(server.inner())
            .await;
    }

    #[tokio::test]
    async fn test_mock_alerts_list_with_handler() {
        let server = MockEnterpriseServer::start().await;

        server
            .mock_alerts_list(vec![
                AlertFixture::new("alert-1", "bdb_size")
                    .severity("WARNING")
                    .entity_type("bdb")
                    .entity_uid("1")
                    .build(),
                AlertFixture::new("alert-2", "node_memory")
                    .severity("CRITICAL")
                    .entity_type("node")
                    .entity_uid("2")
                    .build(),
            ])
            .await;

        let client = server.client();
        let alerts = client.alerts().list().await.unwrap();

        assert_eq!(alerts.len(), 2);
        assert_eq!(alerts[0].uid, "alert-1");
        assert_eq!(alerts[0].name, "bdb_size");
        assert_eq!(alerts[0].severity, "WARNING");
        assert_eq!(alerts[1].uid, "alert-2");
        assert_eq!(alerts[1].name, "node_memory");
        assert_eq!(alerts[1].severity, "CRITICAL");
    }

    #[tokio::test]
    async fn test_mock_database_alerts_with_handler() {
        let server = MockEnterpriseServer::start().await;

        server
            .mock_database_alerts(
                1,
                vec![
                    AlertFixture::new("alert-db-1", "bdb_high_latency")
                        .severity("WARNING")
                        .description("Latency above threshold")
                        .build(),
                ],
            )
            .await;

        let client = server.client();
        let alerts = client.alerts().list_by_database(1).await.unwrap();

        assert_eq!(alerts.len(), 1);
        assert_eq!(alerts[0].name, "bdb_high_latency");
    }
}