Skip to main content

kftray_commons/models/
http_logs_config_model.rs

1use serde::{
2    Deserialize,
3    Serialize,
4};
5
6#[derive(Clone, Deserialize, PartialEq, Serialize, Debug)]
7pub struct HttpLogsConfig {
8    pub config_id: i64,
9    #[serde(default)]
10    pub enabled: bool,
11    #[serde(default = "default_max_file_size")]
12    pub max_file_size: u64,
13    #[serde(default = "default_retention_days")]
14    pub retention_days: u64,
15    #[serde(default = "default_auto_cleanup")]
16    pub auto_cleanup: bool,
17}
18
19impl Default for HttpLogsConfig {
20    fn default() -> Self {
21        HttpLogsConfig {
22            config_id: 0,
23            enabled: false,
24            max_file_size: default_max_file_size(),
25            retention_days: default_retention_days(),
26            auto_cleanup: default_auto_cleanup(),
27        }
28    }
29}
30
31impl HttpLogsConfig {
32    pub fn new(config_id: i64) -> Self {
33        HttpLogsConfig {
34            config_id,
35            ..Default::default()
36        }
37    }
38}
39
40fn default_max_file_size() -> u64 {
41    10 * 1024 * 1024 // 10MB
42}
43
44fn default_retention_days() -> u64 {
45    7 // 7 days
46}
47
48fn default_auto_cleanup() -> bool {
49    true
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_http_logs_config_default() {
58        let config = HttpLogsConfig::default();
59
60        assert_eq!(config.config_id, 0);
61        assert!(!config.enabled);
62        assert_eq!(config.max_file_size, 10 * 1024 * 1024);
63        assert_eq!(config.retention_days, 7);
64        assert!(config.auto_cleanup);
65    }
66
67    #[test]
68    fn test_http_logs_config_new() {
69        let config = HttpLogsConfig::new(123);
70
71        assert_eq!(config.config_id, 123);
72        assert!(!config.enabled);
73        assert_eq!(config.max_file_size, 10 * 1024 * 1024);
74        assert_eq!(config.retention_days, 7);
75        assert!(config.auto_cleanup);
76    }
77
78    #[test]
79    fn test_http_logs_config_serde() {
80        let config = HttpLogsConfig {
81            config_id: 456,
82            enabled: true,
83            max_file_size: 5 * 1024 * 1024,
84            retention_days: 14,
85            auto_cleanup: false,
86        };
87
88        let json = serde_json::to_string(&config).unwrap();
89        let deserialized: HttpLogsConfig = serde_json::from_str(&json).unwrap();
90
91        assert_eq!(config, deserialized);
92    }
93
94    #[test]
95    fn test_http_logs_config_partial_json() {
96        let json = r#"{"config_id": 789, "enabled": true}"#;
97        let config: HttpLogsConfig = serde_json::from_str(json).unwrap();
98
99        assert_eq!(config.config_id, 789);
100        assert!(config.enabled);
101        assert_eq!(config.max_file_size, 10 * 1024 * 1024);
102        assert_eq!(config.retention_days, 7);
103        assert!(config.auto_cleanup);
104    }
105}