libits-client 3.1.0

library to connect on an ITS MQTT server
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*
 * Software Name : libits-client
 * SPDX-FileCopyrightText: Copyright (c) Orange SA
 * SPDX-License-Identifier: MIT
 *
 * This software is distributed under the MIT license,
 * see the "LICENSE.txt" file for more details or https://opensource.org/license/MIT/
 *
 * Authors: see CONTRIBUTORS.md
 */

use crate::client::configuration::configuration_error::ConfigurationError;
use ini::{Ini, Properties};
use std::any::type_name;

use crate::client::configuration::configuration_error::ConfigurationError::{
    FieldNotFound, MissingMandatoryField, MissingMandatorySection, NoCustomSettings, TypeError,
};

use std::str::FromStr;

#[cfg(feature = "telemetry")]
use crate::client::configuration::telemetry_configuration::{
    TELEMETRY_SECTION, TelemetryConfiguration,
};

#[cfg(feature = "mobility")]
use crate::client::configuration::mobility_configuration::{
    MOBILITY_SECTION, MobilityConfiguration,
};

#[cfg(feature = "geo_routing")]
use crate::client::configuration::geo_configuration::{GEO_SECTION, GeoConfiguration};
use crate::client::configuration::mqtt_configuration::MqttConfiguration;

pub(crate) mod bootstrap_configuration;
pub mod configuration_error;
#[cfg(feature = "geo_routing")]
pub(crate) mod geo_configuration;
#[cfg(feature = "mobility")]
pub(crate) mod mobility_configuration;
pub mod mqtt_configuration;
#[cfg(feature = "telemetry")]
pub(crate) mod telemetry_configuration;

const MQTT_SECTION: &str = "mqtt";

#[derive(Clone, Debug, Default)]
pub struct Configuration {
    pub mqtt: MqttConfiguration,
    #[cfg(feature = "geo_routing")]
    pub geo: GeoConfiguration,
    #[cfg(feature = "telemetry")]
    pub telemetry: TelemetryConfiguration,
    #[cfg(feature = "mobility")]
    pub mobility: MobilityConfiguration,
    pub custom_settings: Option<Ini>,
}

impl Configuration {
    pub fn set_mqtt_credentials(&mut self, username: &str, password: &str) {
        self.mqtt.mqtt_options.set_credentials(username, password);
    }

    pub fn get<T: FromStr>(
        &self,
        section: Option<&'static str>,
        key: &'static str,
    ) -> Result<T, ConfigurationError> {
        if let Some(custom_settings) = &self.custom_settings {
            match get_optional(section, key, custom_settings) {
                Ok(result) => match result {
                    Some(value) => Ok(value),
                    _ => Err(FieldNotFound(key)),
                },
                Err(e) => Err(e),
            }
        } else {
            Err(NoCustomSettings)
        }
    }

    pub fn set<T: Into<String>>(&mut self, section: Option<&str>, key: &str, value: T) {
        let custom_settings = self.custom_settings.get_or_insert_with(Ini::default);
        custom_settings.with_section(section).set(key, value);
    }

    /// Get a list of values from configuration, separated by commas
    pub fn get_list<T: FromStr>(
        &self,
        section: Option<&'static str>,
        key: &'static str,
    ) -> Result<Vec<T>, ConfigurationError> {
        if let Some(custom_settings) = &self.custom_settings {
            match get_optional_list(section, key, custom_settings) {
                Ok(result) => match result {
                    Some(values) => Ok(values),
                    None => Ok(Vec::new()), // Return empty vec if not found
                },
                Err(e) => Err(e),
            }
        } else {
            Err(NoCustomSettings)
        }
    }
}

pub(crate) fn get_optional<T: FromStr>(
    section: Option<&'static str>,
    field: &'static str,
    ini_config: &Ini,
) -> Result<Option<T>, ConfigurationError> {
    let properties = if let Some(properties) = ini_config.section(section) {
        properties
    } else {
        ini_config.general_section()
    };
    get_optional_from_properties(field, properties)
}

pub fn get_optional_from_properties<T: FromStr>(
    field: &'static str,
    properties: &Properties,
) -> Result<Option<T>, ConfigurationError> {
    if let Some(value) = properties.get(field) {
        match T::from_str(value) {
            Ok(value) => Ok(Some(value)),
            Err(_) => Err(TypeError(field, type_name::<T>())),
        }
    } else {
        Ok(None)
    }
}

pub(crate) fn get_optional_list<T: FromStr>(
    section: Option<&'static str>,
    field: &'static str,
    ini_config: &Ini,
) -> Result<Option<Vec<T>>, ConfigurationError> {
    let properties = if let Some(properties) = ini_config.section(section) {
        properties
    } else {
        ini_config.general_section()
    };
    get_optional_list_from_properties(field, properties)
}

pub fn get_optional_list_from_properties<T: FromStr>(
    field: &'static str,
    properties: &Properties,
) -> Result<Option<Vec<T>>, ConfigurationError> {
    if let Some(value) = properties.get(field) {
        let cleaned_value = value.trim();

        // Handle JSON array format [item1, item2, ...]
        let items_str = if cleaned_value.starts_with('[') && cleaned_value.ends_with(']') {
            &cleaned_value[1..cleaned_value.len() - 1]
        } else {
            cleaned_value
        };

        let parsed_values: Result<Vec<T>, _> = items_str
            .split(',')
            .map(|s| s.trim())
            .map(|s| s.trim_matches('"')) // Remove quotes if present
            .filter(|s| !s.is_empty())
            .map(|item| T::from_str(item))
            .collect();

        match parsed_values {
            Ok(values) => Ok(Some(values)),
            Err(_) => Err(TypeError(field, type_name::<T>())),
        }
    } else {
        Ok(None)
    }
}

pub fn get_mandatory<T: FromStr>(
    section: Option<&'static str>,
    field: &'static str,
    ini_config: &Ini,
) -> Result<T, ConfigurationError> {
    let properties = if let Some(properties) = ini_config.section(section) {
        properties
    } else {
        ini_config.general_section()
    };
    get_mandatory_from_properties(field, properties)
}

pub(crate) fn get_mandatory_from_properties<T: FromStr>(
    field: &'static str,
    properties: &Properties,
) -> Result<T, ConfigurationError> {
    match properties.get(field) {
        Some(value) => match T::from_str(value) {
            Ok(value) => Ok(value),
            Err(_e) => Err(TypeError(field, type_name::<T>())),
        },
        None => Err(MissingMandatoryField(field)),
    }
}

pub(crate) fn pick_mandatory_section(
    section: &'static str,
    ini_config: &mut Ini,
) -> Result<Properties, ConfigurationError> {
    match ini_config.delete(Some(section)) {
        Some(properties) => Ok(properties),
        None => Err(MissingMandatorySection(section)),
    }
}

impl TryFrom<Ini> for Configuration {
    type Error = ConfigurationError;

    fn try_from(ini_config: Ini) -> Result<Self, Self::Error> {
        let mut ini_config = ini_config;

        Ok(Configuration {
            mqtt: MqttConfiguration::try_from(&pick_mandatory_section(
                MQTT_SECTION,
                &mut ini_config,
            )?)?,
            #[cfg(feature = "geo_routing")]
            geo: GeoConfiguration::try_from(&pick_mandatory_section(
                GEO_SECTION,
                &mut ini_config,
            )?)?,
            #[cfg(feature = "telemetry")]
            telemetry: TelemetryConfiguration::try_from(&pick_mandatory_section(
                TELEMETRY_SECTION,
                &mut ini_config,
            )?)?,
            #[cfg(feature = "mobility")]
            mobility: MobilityConfiguration::try_from(&pick_mandatory_section(
                MOBILITY_SECTION,
                &mut ini_config,
            )?)?,
            custom_settings: Some(ini_config),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::configuration::{
        Configuration, get_mandatory, get_optional, pick_mandatory_section,
    };
    use ini::Ini;

    const EXHAUSTIVE_CUSTOM_INI_CONFIG: &str = r#"
no_section = noitceson

[mqtt]
host = localhost
port = 1883
use_tls = false
use_websocket = false
client_id = com_myapplication
username = username
password = password

[geo]
prefix = sandbox
suffix = v2x

[mobility]
source_uuid = com_myapplication-1
station_id = 1
use_responsibility = true
thread_count = 4

[telemetry]
host = otlp.domain.com
port = 5418
use_tls = false
path = /custom/v1/traces
max_batch_size = 10
username = username
password = password

[custom]
test = success
"#;

    const MINIMAL_FEATURELESS_CONFIGURATION: &str = r#"
[mqtt]
host = localhost
port = 1883
use_tls = false
use_websocket = false
client_id = com_myapplication
"#;

    #[cfg(feature = "mobility")]
    const MINIMAL_MOBILITY_CONFIGURATION: &str = r#"
[mqtt]
host = localhost
port = 1883
use_tls = false
use_websocket = false
client_id = com_myapplication

[mobility]
source_uuid = com_myapplication-1
station_id = 1
use_responsibility = false
thread_count = 4
"#;

    #[cfg(feature = "geo_routing")]
    const MINIMAL_GEO_ROUTING_CONFIGURATION: &str = r#"
[mqtt]
host = localhost
port=1883
use_tls = false
use_websocket = false
client_id= com_myapplication

[mobility]
source_uuid = com_myapplication-1
station_id = 1
use_responsibility = false
thread_count = 4

[geo]
prefix = sandbox
suffix = v2x
"#;

    #[cfg(feature = "telemetry")]
    const MINIMAL_TELEMETRY_CONFIGURATION: &str = r#"
[mqtt]
host = localhost
port = 1883
use_tls = false
use_websocket = false
client_id = com_myapplication

[telemetry]
host = otlp.domain.com
port = 5418
use_tls = false
"#;

    #[test]
    fn custom_settings() {
        let ini =
            Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).expect("Ini creation should not fail");

        let configuration = Configuration::try_from(ini).expect("Minimal config should not fail");
        let no_section = configuration
            .get::<String>(None, "no_section")
            .expect("Failed to get field with no section");
        let custom_test = configuration
            .get::<String>(Some("custom"), "test")
            .expect("Failed to get field under custom section");

        assert_eq!(no_section, "noitceson");
        assert_eq!(custom_test, "success");
    }

    #[test]
    fn set_custom_setting() {
        let ini =
            Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).expect("Ini creation should not fail");
        let mut configuration =
            Configuration::try_from(ini).expect("Minimal config should not fail");

        configuration.set(Some("my_section"), "cool_key", "cool_value");
        configuration.set(None, "no_section", "updated");
        let no_section = configuration
            .get::<String>(None, "no_section")
            .expect("Failed to get field with no section");
        let cool_value = configuration
            .get::<String>(Some("my_section"), "cool_key")
            .expect("Failed to get field under custom section");

        assert_eq!(no_section, "updated");
        assert_eq!(cool_value, "cool_value");
    }

    #[test]
    fn pick_section() {
        let mut ini =
            Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).expect("Ini creation should not fail");

        let s = pick_mandatory_section("mqtt", &mut ini);

        assert!(s.is_ok());
        assert!(ini.section(Some("mqtt")).is_none());
    }

    #[test]
    fn not_set_optional_returns_none() {
        let ini =
            Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).expect("Ini creation should not fail");

        let ok_none = get_optional::<String>(None, "pmloikjuyh", &ini);

        let none = ok_none.expect("Not set field should return Ok(None)");
        assert!(none.is_none());
    }

    #[test]
    fn optional_no_section_is_ok_some() {
        let ini =
            Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).expect("Ini creation should not fail");

        let ok_some = get_optional::<String>(None, "no_section", &ini);

        let some = ok_some.expect("Optional field must return Ok(Some(T)): found Err(_)");
        let value = some.expect("Optional field must return Ok(Some(T)): found OK(None)");
        assert_eq!(value, "noitceson");
    }

    #[test]
    fn optional_from_section_is_ok_some() {
        let ini =
            Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).expect("Ini creation should not fail");

        let ok_some = get_optional::<String>(Some("custom"), "test", &ini);

        let some = ok_some.expect("Optional field must return Ok(Some(T)): found Err(_)");
        let value = some.expect("Optional field must return Ok(Some(T)): found OK(None)");
        assert_eq!(value, "success");
    }

    #[test]
    fn optional_wrong_type_is_err() {
        let ini =
            Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).expect("Ini creation should not fail");

        let err = get_optional::<u16>(Some("custom"), "test", &ini);

        assert!(err.is_err());
    }

    #[test]
    #[cfg_attr(any(feature = "telemetry", feature = "mobility"), should_panic)]
    fn minimal_featureless_configuration() {
        let ini = Ini::load_from_str(MINIMAL_FEATURELESS_CONFIGURATION)
            .expect("Ini creation should not fail");

        let _ = Configuration::try_from(ini)
            .expect("Failed to create Configuration with minimal mandatory sections and fields");
    }

    #[test]
    #[cfg(feature = "telemetry")]
    #[cfg_attr(feature = "mobility", should_panic)]
    fn minimal_telemetry_configuration() {
        let ini = Ini::load_from_str(MINIMAL_TELEMETRY_CONFIGURATION)
            .expect("Ini creation should not fail");

        Configuration::try_from(ini)
            .expect("Failed to create Configuration with minimal mandatory sections and fields");
    }

    #[test]
    #[cfg(feature = "mobility")]
    #[cfg_attr(any(feature = "telemetry", feature = "geo_routing"), should_panic)]
    fn minimal_mobility_configuration() {
        let ini = Ini::load_from_str(MINIMAL_MOBILITY_CONFIGURATION)
            .expect("Ini creation should not fail");

        Configuration::try_from(ini)
            .expect("Failed to create Configuration with minimal mandatory sections and fields");
    }

    #[test]
    #[cfg(feature = "geo_routing")]
    #[cfg_attr(feature = "telemetry", should_panic)]
    fn minimal_geo_routing_configuration() {
        let ini = Ini::load_from_str(MINIMAL_GEO_ROUTING_CONFIGURATION)
            .expect("Ini creation should not fail");

        Configuration::try_from(ini)
            .expect("Failed to create Configuration with minimal mandatory sections and fields");
    }

    #[test]
    fn mandatory_no_section_is_ok() {
        let ini =
            Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).expect("Ini creation should not fail");

        let result = get_mandatory::<String>(None, "no_section", &ini);

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "noitceson");
    }

    #[test]
    fn mandatory_from_section_is_ok() {
        let ini =
            Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).expect("Ini creation should not fail");

        let result = get_mandatory::<String>(Some("custom"), "test", &ini);

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "success");
    }

    #[test]
    fn mandatory_missing_field_is_err() {
        let ini =
            Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).expect("Ini creation should not fail");

        let result = get_mandatory::<String>(None, "non_existent", &ini);

        assert!(result.is_err());
    }

    #[test]
    fn mandatory_wrong_type_is_err() {
        let ini =
            Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).expect("Ini creation should not fail");

        let result = get_mandatory::<u16>(Some("custom"), "test", &ini);

        assert!(result.is_err());
    }

    #[test]
    fn optional_missing_section_returns_none() {
        let ini =
            Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).expect("Ini creation should not fail");

        let result = get_optional::<String>(Some("non_existent_section"), "field", &ini);

        assert!(result.is_ok());
        assert!(result.unwrap().is_none());
    }

    #[test]
    fn get_mandatory_ok() {
        let ini = Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).unwrap();
        let value = get_mandatory::<String>(Some("custom"), "test", &ini).unwrap();
        assert_eq!(value, "success");
    }

    #[test]
    fn get_mandatory_missing_section() {
        let ini = Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).unwrap();
        let result = get_mandatory::<String>(Some("missing"), "test", &ini);
        assert!(result.is_err());
    }

    #[test]
    fn get_mandatory_missing_field() {
        let ini = Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).unwrap();
        let result = get_mandatory::<String>(Some("custom"), "missing", &ini);
        assert!(result.is_err());
    }

    #[test]
    fn get_mandatory_type_error() {
        let ini = Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).unwrap();
        let result = get_mandatory::<u16>(Some("custom"), "test", &ini);
        assert!(result.is_err());
    }

    #[test]
    fn get_optional_ok() {
        let ini = Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).unwrap();
        let value = get_optional::<String>(Some("custom"), "test", &ini)
            .unwrap()
            .unwrap();
        assert_eq!(value, "success");
    }

    #[test]
    fn get_optional_missing_section() {
        let ini = Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).unwrap();
        let result = get_optional::<String>(Some("missing"), "test", &ini).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn get_optional_missing_field() {
        let ini = Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).unwrap();
        let result = get_optional::<String>(Some("custom"), "missing", &ini).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn get_optional_type_error() {
        let ini = Ini::load_from_str(EXHAUSTIVE_CUSTOM_INI_CONFIG).unwrap();
        let result = get_optional::<u16>(Some("custom"), "test", &ini);
        assert!(result.is_err());
    }

    #[test]
    fn get_list_with_comma_separated_values() {
        let mut configuration = Configuration::default();
        configuration.set(Some("test"), "list_field", "item1,item2,item3");
        let result = configuration
            .get_list::<String>(Some("test"), "list_field")
            .unwrap();
        assert_eq!(result, vec!["item1", "item2", "item3"]);
    }

    #[test]
    fn get_list_no_custom_settings_error() {
        let configuration = Configuration {
            custom_settings: None,
            ..Default::default()
        };
        let result = configuration.get_list::<String>(Some("test"), "list_field");
        assert!(matches!(result, Err(NoCustomSettings)));
    }

    #[test]
    fn get_list_with_spaces_and_trimming() {
        let mut configuration = Configuration::default();
        configuration.set(Some("test"), "list_field", " item1 , item2 , item3 ");
        let result = configuration
            .get_list::<String>(Some("test"), "list_field")
            .unwrap();
        assert_eq!(result, vec!["item1", "item2", "item3"]);
    }

    #[test]
    fn get_list_with_empty_items_filtered() {
        let mut configuration = Configuration::default();
        configuration.set(Some("test"), "list_field", "item1,,item3,");
        let result = configuration
            .get_list::<String>(Some("test"), "list_field")
            .unwrap();
        assert_eq!(result, vec!["item1", "item3"]);
    }

    #[test]
    fn get_list_type_conversion_error() {
        let mut configuration = Configuration::default();
        configuration.set(Some("test"), "list_field", "not_a_number,123");
        let result = configuration.get_list::<u32>(Some("test"), "list_field");
        assert!(result.is_err());
    }

    // Tests for get_optional_list functionality
    #[test]
    fn get_optional_list_ok() {
        let mut properties = Properties::new();
        properties.insert("test_list", "a,b,c".to_string());
        let result = get_optional_list_from_properties::<String>("test_list", &properties).unwrap();
        assert_eq!(
            result,
            Some(vec!["a".to_string(), "b".to_string(), "c".to_string()])
        );
    }

    #[test]
    fn get_optional_list_missing_field() {
        let properties = Properties::new();
        let result = get_optional_list_from_properties::<String>("missing", &properties).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn get_optional_list_type_error() {
        let mut properties = Properties::new();
        properties.insert("test_list", "not_a_number,123".to_string());
        let result = get_optional_list_from_properties::<u32>("test_list", &properties);
        assert!(result.is_err());
    }

    // Tests for get_optional_list functionality with Ini
    #[test]
    fn get_optional_list_from_ini_ok() {
        let ini_str = r#"
        [test_section]
        list_field = item1,item2,item3
        "#;
        let ini = Ini::load_from_str(ini_str).unwrap();
        let result = get_optional_list::<String>(Some("test_section"), "list_field", &ini).unwrap();
        assert_eq!(
            result,
            Some(vec![
                "item1".to_string(),
                "item2".to_string(),
                "item3".to_string()
            ])
        );
    }

    #[test]
    fn get_optional_list_from_ini_missing_section() {
        let ini_str = r#"
        [other_section]
        list_field = item1,item2,item3
        "#;
        let ini = Ini::load_from_str(ini_str).unwrap();
        let result =
            get_optional_list::<String>(Some("missing_section"), "list_field", &ini).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn get_optional_list_from_general_section() {
        let ini_str = r#"
        list_field = item1,item2,item3
        [other_section]
        other = value
        "#;
        let ini = Ini::load_from_str(ini_str).unwrap();
        let result = get_optional_list::<String>(None, "list_field", &ini).unwrap();
        assert_eq!(
            result,
            Some(vec![
                "item1".to_string(),
                "item2".to_string(),
                "item3".to_string()
            ])
        );
    }

    // Test for set_mqtt_credentials
    #[test]
    fn set_mqtt_credentials_test() {
        let mut configuration = Configuration::default();
        configuration.set_mqtt_credentials("testuser", "testpass");
        assert_eq!(
            configuration.mqtt.mqtt_options.credentials(),
            Some(("testuser".to_string(), "testpass".to_string()))
        );
    }
}