tmflib 0.1.37

Interface library for processing TMF payloads
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
//! Product Specification Module
//!

use chrono::Utc;
use regex::Regex;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use super::MOD_PATH;

use crate::common::event::{Event, EventPayload};
use crate::common::tmf_error::TMFError;
use crate::{
    serde_value_to_type, vec_insert, Cardinality, HasDescription, HasId, HasLastUpdate, HasName,
    HasReference, HasValidity, TMFEvent, TimePeriod,
};
use tmflib_derive::{HasDescription, HasId, HasLastUpdate, HasName, HasValidity};

use crate::tmf633::characteristic_specification::CharacteristicSpecification;
use crate::tmf633::service_specification::{ServiceSpecification, ServiceSpecificationRef};

const CLASS_PATH: &str = "productSpecification";
const SPEC_VERS: &str = "1.0";
const CHAR_VALUE_MIN_CARD: Cardinality = 0;
const CHAR_VALUE_MAX_CARD: Cardinality = 1;
/// Verb to tag converted ServiceSpecifications with.
pub const SPEC_CONV_VERB: &str = "Imported";

/// Product Specification Characteristic
#[derive(Clone, Debug, Default, Deserialize, Serialize, HasValidity)]
#[serde(rename_all = "camelCase")]
pub struct ProductSpecificationCharacteristic {
    /// Is this characteristic configurable
    pub configurable: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    extensible: Option<bool>,
    /// Is this characteristic unique
    pub is_unique: bool,
    max_cardinality: Cardinality,
    min_cardinality: Cardinality,
    /// Characteristic Name
    pub name: String,
    /// Regular expression for value
    #[serde(skip_serializing_if = "Option::is_none")]
    regex: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_type: Option<String>,
    /// Validity period for this characteristic
    #[serde(skip_serializing_if = "Option::is_none")]
    pub valid_for: Option<TimePeriod>,
    /// Set of characteristic relationships
    #[serde(skip_serializing_if = "Option::is_none")]
    pub product_spec_char_relationship: Option<Vec<ProductSpecificationCharacteristicRelationship>>,
}

impl ProductSpecificationCharacteristic {
    /// Create a new characteristic
    /// By default, new characteristics are optional (cardinality: min=0 max=1)
    /// # Examples
    /// ```
    /// # use tmflib::tmf620::product_specification::ProductSpecificationCharacteristic;
    /// let ps_char = ProductSpecificationCharacteristic::new(String::from("My Characteristic"));
    /// ```
    pub fn new(name: impl Into<String>) -> ProductSpecificationCharacteristic {
        ProductSpecificationCharacteristic {
            configurable: true,
            max_cardinality: CHAR_VALUE_MAX_CARD,
            min_cardinality: CHAR_VALUE_MIN_CARD,
            name: name.into(),
            valid_for: Some(TimePeriod::default()),
            ..Default::default()
        }
    }

    /// Set configuraable flag
    pub fn configurable(mut self, configurable: bool) -> ProductSpecificationCharacteristic {
        self.configurable = configurable;
        self
    }

    /// Set description of characteristic
    pub fn description(mut self, description: String) -> ProductSpecificationCharacteristic {
        self.description = Some(description.clone());
        self
    }

    /// Set extensible flag
    pub fn extensible(mut self, extensible: bool) -> ProductSpecificationCharacteristic {
        self.extensible = Some(extensible);
        self
    }

    /// Set validity period
    pub fn validity(mut self, validity: Option<TimePeriod>) -> ProductSpecificationCharacteristic {
        self.valid_for = validity;
        self
    }

    /// Set MIN / MAX cardindiality
    /// Will ignore change if min > max.
    /// # Examples
    /// ```
    /// # use tmflib::tmf620::product_specification::ProductSpecificationCharacteristic;
    /// let ps_char = ProductSpecificationCharacteristic::new(String::from("My Characteristic"))
    ///     .cardinality(0,1);
    /// ```
    pub fn cardinality(
        mut self,
        min: Cardinality,
        max: Cardinality,
    ) -> ProductSpecificationCharacteristic {
        // Quick check to make sure min < max
        if min > max {
            // Not sure if we should just ignore this ?
            return self;
        }
        self.min_cardinality = min;
        self.max_cardinality = max;
        self
    }
}

// Conversion from Service CharacteristicSpecification into Product Spec.
impl From<CharacteristicSpecification> for ProductSpecificationCharacteristic {
    fn from(value: CharacteristicSpecification) -> Self {
        ProductSpecificationCharacteristic {
            name: value.name.unwrap_or_default(),
            min_cardinality: value.min_cardinality.unwrap_or_default(),
            max_cardinality: value.max_cardinality.unwrap_or_default(),
            configurable: value.configurable.unwrap_or_default(),
            is_unique: value.is_unique.unwrap_or_default(),
            description: value.description.clone(),
            valid_for: value.valid_for.clone(),
            ..Default::default()
        }
    }
}

/// Bundled Products
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BundledProductSpecification {
    id: String,
    href: String,
    lifecycle_status: Option<String>,
    name: String,
}

/// Product Specification
#[derive(
    Clone, Debug, Default, Deserialize, HasId, HasLastUpdate, HasDescription, HasName, Serialize,
)]
#[serde(rename_all = "camelCase")]
pub struct ProductSpecification {
    /// Id
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// HREF where object is located
    #[serde(skip_serializing_if = "Option::is_none")]
    pub href: Option<String>,
    /// Brand
    #[serde(skip_serializing_if = "Option::is_none")]
    pub brand: Option<String>,
    /// Description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Is a bundle?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_bundle: Option<bool>,
    /// Timestamp of last change to this payload
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_update: Option<String>,
    /// Status of this specification
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lifecycle_status: Option<String>,
    /// Name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Product Number / Code
    #[serde(skip_serializing_if = "Option::is_none")]
    product_number: Option<String>,
    /// Version of this record
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    /// Set of characteristics for this specification
    #[serde(skip_serializing_if = "Option::is_none")]
    pub product_spec_characteristic: Option<Vec<ProductSpecificationCharacteristic>>,
    /// Bundled specifications
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bundled_product_specification: Option<Vec<BundledProductSpecification>>,
}

impl ProductSpecification {
    /// Create new instance of Product Specification
    pub fn new(name: impl Into<String>) -> ProductSpecification {
        let mut prod_spec = ProductSpecification::create_with_time();
        prod_spec.name = Some(name.into());
        prod_spec.version = Some(SPEC_VERS.to_string());

        prod_spec
    }

    /// Set lifecycle status
    pub fn status(&mut self, status: &str) {
        self.lifecycle_status = Some(status.to_owned());
    }

    /// Add a new Characteristic into the specification
    pub fn with_charateristic(
        mut self,
        characteristic: ProductSpecificationCharacteristic,
    ) -> ProductSpecification {
        vec_insert(&mut self.product_spec_characteristic, characteristic);
        self
    }

    /// Get the class of this object
    pub fn characteristic_by_name(
        &self,
        name: &str,
    ) -> Option<&ProductSpecificationCharacteristic> {
        match self.product_spec_characteristic.as_ref() {
            Some(chars) => chars.iter().find(|c| c.name == name),
            None => None,
        }
    }

    /// Link remote characteristic specification
    pub fn link_characteristic(&mut self, remote: &ProductSpecification, name: impl Into<String>) {
        let name: String = name.into();
        let char_opt = remote.characteristic_by_name(&name);

        if let Some(char_spec) = char_opt {
            let mut new_char = char_spec.clone();
            let char_rel = ProductSpecificationCharacteristicRelationship {
                id: remote.get_id(),
                href: remote.get_href(),
                char_spec_seq: 0,
                name: name.clone(),
                relationship_type: String::from("dependsOn"),
                valid_for: new_char.valid_for.clone(),
            };
            // Insert relationship into placeholder characteristic
            vec_insert(&mut new_char.product_spec_char_relationship, char_rel);
            vec_insert(&mut self.product_spec_characteristic, new_char);
        }
    }
}

/// Product Specification Reference
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ProductSpecificationRef {
    /// Id
    pub id: String,
    /// HREF where object is located
    pub href: String,
    /// Description
    pub name: Option<String>,
    /// Version of this record
    pub version: Option<String>,
}

impl From<ProductSpecification> for ProductSpecificationRef {
    fn from(ps: ProductSpecification) -> ProductSpecificationRef {
        ProductSpecificationRef {
            id: ps.get_id(),
            href: ps.get_href(),
            name: ps.name.clone(),
            version: ps.version.clone(),
        }
    }
}

impl HasReference for ProductSpecification {
    type RefType = ProductSpecificationRef;
    fn as_ref(&self) -> Option<Self::RefType> {
        Some(ProductSpecificationRef::from(self.clone()))
    }
}

impl From<&ServiceSpecificationRef> for ProductSpecificationRef {
    fn from(value: &ServiceSpecificationRef) -> Self {
        // we cannot simply copy across the href but we can reuse the id
        let mut ps = ProductSpecification {
            id: Some(value.id.clone()),
            name: Some(value.name.clone()),
            ..Default::default()
        };
        ps.generate_href();

        ProductSpecificationRef::from(ps)
    }
}

// Convert a service specification into a peroduct specification
// used as part of the import process.
impl From<&ServiceSpecification> for ProductSpecification {
    fn from(value: &ServiceSpecification) -> Self {
        let mut ps = ProductSpecification::new(value.get_name());
        // get_description() is a method on the ServiceSpecification that always returns a string
        ps.description = Some(format!("{} [{}]", value.get_description(), SPEC_CONV_VERB));
        if value.description.is_some() {
            ps.description = Some(format!("{} [{}]", value.get_description(), SPEC_CONV_VERB));
        }
        ps.is_bundle = value.is_bundle;
        if value.last_update.is_some() {
            ps.set_last_update(value.last_update.as_ref().unwrap());
        }
        if value.spec_characteristics.is_some() {
            // We have characteristics that require conversion
            let mut out: Vec<ProductSpecificationCharacteristic> = Vec::new();
            value
                .spec_characteristics
                .as_ref()
                .unwrap()
                .iter()
                .for_each(|cs| {
                    let psc = ProductSpecificationCharacteristic::from(cs.clone());
                    out.push(psc);
                });
            ps.product_spec_characteristic = Some(out);
        }
        if value.version.is_some() {
            // If source has a version defined take that
            ps.version.clone_from(&value.version);
        }
        ps.lifecycle_status.clone_from(&value.lifecycle_status);
        ps
    }
}

// Events
/// Product Specification Event Container
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ProductSpecificationEvent {
    product_specification: ProductSpecification,
}

impl TMFEvent<ProductSpecificationEvent> for ProductSpecification {
    fn event(&self) -> ProductSpecificationEvent {
        ProductSpecificationEvent {
            product_specification: self.clone(),
        }
    }
}

impl EventPayload<ProductSpecificationEvent> for ProductSpecification {
    type Subject = ProductSpecification;
    type EventType = ProductSpecificationEventType;
    fn to_event(
        &self,
        event_type: Self::EventType,
    ) -> Event<ProductSpecificationEvent, Self::EventType> {
        let now = Utc::now();
        let event_time = chrono::DateTime::from_timestamp(now.timestamp(), 0).unwrap();
        let desc = format!("{:?} for {}", event_type, self.get_name());
        Event {
            description: Some(desc),
            domain: Some(ProductSpecification::get_class()),
            event_id: Uuid::new_v4().to_string(),
            href: self.href.clone(),
            id: self.id.clone(),
            title: self.name.clone(),
            event_time: event_time.to_string(),
            event_type,
            event: self.event(),
            ..Event::default()
        }
    }
}

/// Product Specification Event Type
#[derive(Clone, Default, Debug)]
pub enum ProductSpecificationEventType {
    /// Product Specification Created
    #[default]
    ProductSpecificationCreateEvent,
    /// Product Specification Deleted
    ProductSpecificationDeleteEvent,
}

/// Product Specification Characteristic Value
/// # Detalis
/// This object contains values used by a specification characteristic.
/// # Example
/// If the Product Offering is "Internet", then the Specification might be "Bandwidht" and the Value might be "100Mb"
#[derive(Clone, Debug, Default, Deserialize, Serialize, HasValidity)]
#[serde(rename_all = "camelCase")]
pub struct ProductSpecificationCharacteristicValue {
    /// Description of Characteristic Value
    pub is_default: bool,
    /// Value Range Interval
    #[serde(skip_serializing_if = "Option::is_none")]
    pub range_interval: Option<String>,
    /// Characteristic Value Regular Expression
    #[serde(skip_serializing_if = "Option::is_none")]
    pub regex: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    unit_of_measure: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_from: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_to: Option<String>,
    /// Type of Characteristic Value
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    valid_for: Option<TimePeriod>,
    value: serde_json::Value,
}

impl ProductSpecificationCharacteristicValue {
    /// Create a new Product Specification Characteristic Value with a value
    /// # Description
    /// Creates a new Characterisitic Value with the provided value.
    /// Other fields can be set by directly updating the structure.
    /// This bypasses regular experssion checks
    /// # Example
    /// ```
    /// # use tmflib::tmf620::product_specification::ProductSpecificationCharacteristicValue;
    /// let pscv = ProductSpecificationCharacteristicValue::new();
    /// ```
    pub fn new() -> ProductSpecificationCharacteristicValue {
        ProductSpecificationCharacteristicValue {
            is_default: false,
            ..Default::default()
        }
    }

    /// Set the regular expression for this characteristic value
    /// # Example
    /// ```
    /// # use tmflib::tmf620::product_specification::ProductSpecificationCharacteristicValue;
    ///
    /// let pscv = ProductSpecificationCharacteristicValue::new()
    ///     .regex(String::from("[0-9]+(Mb|Gb)"));
    /// ```
    pub fn regex(
        mut self,
        regex: String,
    ) -> Result<ProductSpecificationCharacteristicValue, TMFError> {
        // For now we only wish to test if we can parse the regex string
        let _re = Regex::new(&regex)?;
        self.regex = Some(regex);
        Ok(self)
    }

    /// Set the value for this characteristic value
    /// # Example
    /// ```
    /// # use tmflib::tmf620::product_specification::ProductSpecificationCharacteristicValue;
    /// # use serde_json::json;
    /// let pscv = ProductSpecificationCharacteristicValue::new()
    ///     .regex(String::from("[0-9]+(Mb|Gb)")).unwrap()
    ///     .value("100Mb".into()).unwrap();
    /// ```
    pub fn value(
        mut self,
        value: serde_json::Value,
    ) -> Result<ProductSpecificationCharacteristicValue, TMFError> {
        self.value_type = Some(serde_value_to_type(&value).to_string());
        match self.regex {
            Some(ref re_str) => {
                let re = Regex::new(re_str)?;
                let val_str = value.to_string();
                if !re.is_match(&val_str) {
                    return Err(TMFError::GenericError(format!(
                        "Value {} does not match regex {}",
                        val_str, re_str
                    )));
                }
                self.value = value;
            }
            // If no regex, then just set the value
            None => self.value = value,
        }
        Ok(self)
    }

    /// Validate a value against the regex (if set) and return an updated
    /// ProductSpecificationCharacteristicValue with the value set.
    /// # Example
    /// ```
    /// # use tmflib::tmf620::product_specification::ProductSpecificationCharacteristicValue;
    /// # use serde_json::json;
    /// let pscv = ProductSpecificationCharacteristicValue::new()
    ///     .regex(String::from("[0-9]+(Mb|Gb)")).unwrap()
    ///    .validate("200Mb".into()).unwrap();
    /// ```
    pub fn validate(
        mut self,
        value: serde_json::Value,
    ) -> Result<ProductSpecificationCharacteristicValue, TMFError> {
        // If we have a regex, then validate the value against it.
        if let Some(re_str) = &self.regex {
            let re = Regex::new(re_str)?;
            let val_str = value.to_string();
            if !re.is_match(&val_str) {
                return Err(TMFError::GenericError(format!(
                    "Value {} does not match regex {}",
                    val_str, re_str
                )));
            }
        }
        self.value = value;
        Ok(self)
    }
}

/// Product Specification Characteristic Value Use
#[derive(Clone, Debug, Deserialize, Serialize, HasValidity)]
#[serde(rename_all = "camelCase")]
pub struct ProductSpecificationCharacteristicValueUse {
    /// Description of Characteristic Value Use
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    max_cardinality: u16,
    min_cardinality: u16,
    name: String,
    value_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    valid_for: Option<TimePeriod>,
    #[serde(skip_serializing_if = "Option::is_none")]
    product_spec_characteristic_value: Option<Vec<ProductSpecificationCharacteristicValue>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    product_specification: Option<ProductSpecificationRef>,
}

impl ProductSpecificationCharacteristicValueUse {
    /// Create a new instance of ProductSpecificationCharacteristicValueUse
    pub fn new(name: impl Into<String>) -> ProductSpecificationCharacteristicValueUse {
        ProductSpecificationCharacteristicValueUse {
            description: None,
            max_cardinality: CHAR_VALUE_MAX_CARD,
            min_cardinality: CHAR_VALUE_MIN_CARD,
            name: name.into(),
            value_type: String::from("String"),
            valid_for: None,
            product_spec_characteristic_value: None,
            product_specification: None,
        }
    }

    /// Add a specificatoin into the ProductSpecificationCharacteristicValueUse
    pub fn with_spec(&mut self, specification: ProductSpecification) {
        self.product_specification = Some(ProductSpecificationRef::from(specification));
    }
}

/// Product Specification Characteristic Relationship
#[derive(Clone, Debug, Deserialize, Serialize, HasValidity)]
pub struct ProductSpecificationCharacteristicRelationship {
    /// Id
    pub id: String,
    /// HREF where object is located
    pub href: String,
    /// Sequence number of the related characteristic
    pub char_spec_seq: u32,
    /// Name of the related characteristic
    pub name: String,
    /// Type of relationship
    pub relationship_type: String,
    /// Validity period for this relationship
    #[serde(skip_serializing_if = "Option::is_none")]
    pub valid_for: Option<TimePeriod>,
}

#[cfg(test)]
mod test {

    use crate::tmf633::characteristic_specification::CharacteristicSpecification;

    use super::*;
    const SPEC_NAME: &str = "MySpecification";
    const VALUE_NAME: &str = "MyCharValueUse";
    const DESC: &str = "A Description";
    const SERVICE_SPEC: &str = "ServiceSpecification";
    const SPEC_STATUS: &str = "SpecificationStatus";

    #[test]
    fn test_char_value_use_new() {
        let value_use = ProductSpecificationCharacteristicValueUse::new(VALUE_NAME);

        assert_eq!(value_use.name, VALUE_NAME.to_string());
    }

    #[test]
    fn test_char_value_use_new_card() {
        let value_use = ProductSpecificationCharacteristicValueUse::new(VALUE_NAME);

        assert_eq!(value_use.min_cardinality, CHAR_VALUE_MIN_CARD);
        assert_eq!(value_use.max_cardinality, CHAR_VALUE_MAX_CARD);
    }

    #[test]
    fn test_char_value_use_new_value() {
        let value_use = ProductSpecificationCharacteristicValueUse::new(VALUE_NAME);

        assert_eq!(value_use.value_type, String::from("String"));
    }

    #[test]
    fn test_spec_new() {
        let spec = ProductSpecification::new(SPEC_NAME);

        assert_eq!(spec.name, Some(SPEC_NAME.to_string()));
    }
    #[test]
    fn test_spec_new_vers() {
        let spec = ProductSpecification::new(SPEC_NAME);

        assert_eq!(spec.version, Some(SPEC_VERS.to_string()));
    }

    #[test]
    fn test_spec_char_configurable() {
        let spec_char = ProductSpecificationCharacteristic::new(SPEC_NAME).configurable(true);

        assert_eq!(spec_char.configurable, true);
    }

    #[test]
    fn test_spec_char_description() {
        let spec_char =
            ProductSpecificationCharacteristic::new(SPEC_NAME).description(DESC.to_string());

        assert_eq!(spec_char.description.unwrap(), DESC.to_string());
    }

    #[test]
    fn test_spec_extensible() {
        let spec_char = ProductSpecificationCharacteristic::new(SPEC_NAME).extensible(true);

        assert_eq!(spec_char.extensible, Some(true));
    }

    #[test]
    fn test_spec_cardinality() {
        let spec_char = ProductSpecificationCharacteristic::new(SPEC_NAME).cardinality(1, 2);

        assert_eq!(spec_char.min_cardinality, 1);
        assert_eq!(spec_char.max_cardinality, 2);
    }

    #[test]
    fn test_spec_cardinality_invalid() {
        let spec_char = ProductSpecificationCharacteristic::new(SPEC_NAME).cardinality(10, 2);

        // Show in valid setting wont' update anything.
        assert_eq!(spec_char.min_cardinality, CHAR_VALUE_MIN_CARD);
        assert_eq!(spec_char.max_cardinality, CHAR_VALUE_MAX_CARD);
    }

    #[test]
    fn test_spec_from_service_spec() {
        let service_spec = CharacteristicSpecification::new(SERVICE_SPEC);

        let spec = ProductSpecificationCharacteristic::from(service_spec.clone());

        assert_eq!(service_spec.name.unwrap(), spec.name);
        assert_eq!(
            service_spec.min_cardinality.unwrap_or_default(),
            spec.min_cardinality
        );
        assert_eq!(
            service_spec.max_cardinality.unwrap_or_default(),
            spec.max_cardinality
        );
        assert_eq!(service_spec.description, spec.description);
    }

    #[test]
    fn test_spec_status() {
        let mut spec = ProductSpecification::new(SPEC_NAME);

        spec.status(SPEC_STATUS);

        assert_eq!(spec.lifecycle_status.is_some(), true);
        assert_eq!(spec.lifecycle_status.unwrap(), SPEC_STATUS.to_string());
    }

    #[test]
    fn test_spec_with_char() {
        let spec_char1 = ProductSpecificationCharacteristic::new(SPEC_NAME).cardinality(1, 2);
        let spec_char2 = ProductSpecificationCharacteristic::new(SPEC_NAME).cardinality(3, 4);
        let spec = ProductSpecification::new(SPEC_NAME)
            .with_charateristic(spec_char1)
            .with_charateristic(spec_char2);

        assert_eq!(spec.product_spec_characteristic.is_some(), true);
        assert_eq!(spec.product_spec_characteristic.unwrap().len(), 2);
    }

    // #[test]
    // fn test_prodspeccharval_new() {
    //     let pscv = ProductSpecificationCharacteristicValue::new("Value".into());

    //     assert_eq!(pscv.value,"Value".into());
    // }

    #[test]
    fn test_prodspec_asref() {
        let ps = ProductSpecification::new(SPEC_NAME);
        let ps_ref = ps.as_ref().unwrap();

        assert_eq!(ps_ref.id, ps.get_id());
        assert_eq!(ps_ref.href, ps.get_href());
        assert_eq!(ps_ref.name, ps.name);
        assert_eq!(ps_ref.version, ps.version);
    }

    #[test]
    fn test_prodspecvalue_regex() {
        let pscv = ProductSpecificationCharacteristicValue::new()
            .regex(String::from("[0-9]+(Mb|Gb)"))
            .unwrap()
            .value("100Mb".into())
            .unwrap();

        assert_eq!(pscv.regex.is_some(), true);
        assert_eq!(pscv.regex.unwrap(), String::from("[0-9]+(Mb|Gb)"));
        assert_eq!(pscv.value, serde_json::Value::String("100Mb".to_string()));
    }

    #[test]
    fn test_prodspecvalue_regex_invalid() {
        let pscv = ProductSpecificationCharacteristicValue::new()
            .regex(String::from("[0-9]+(Mb|Gb)"))
            .unwrap()
            .value("Invalid".into());
        assert!(pscv.is_err());
    }

    #[test]
    fn test_with_charspec() {
        let spec_char1 = ProductSpecificationCharacteristic::new(SPEC_NAME).cardinality(1, 2);
        let spec_char2 = ProductSpecificationCharacteristic::new(SPEC_NAME).cardinality(3, 4);
        let spec = ProductSpecification::new(SPEC_NAME)
            .with_charateristic(spec_char1)
            .with_charateristic(spec_char2);

        assert_eq!(spec.product_spec_characteristic.is_some(), true);
        assert_eq!(spec.product_spec_characteristic.unwrap().len(), 2);
    }

    #[test]
    fn test_charspec_by_name() {
        let spec_char1 = ProductSpecificationCharacteristic::new("Char1").cardinality(1, 2);
        let spec_char2 = ProductSpecificationCharacteristic::new("Char2").cardinality(3, 4);
        let spec = ProductSpecification::new(SPEC_NAME)
            .with_charateristic(spec_char1)
            .with_charateristic(spec_char2);

        let c1 = spec.characteristic_by_name("Char1");
        assert!(c1.is_some());
        assert_eq!(c1.unwrap().name, "Char1".to_string());

        let c2 = spec.characteristic_by_name("Char2");
        assert!(c2.is_some());
        assert_eq!(c2.unwrap().name, "Char2".to_string());

        let c3 = spec.characteristic_by_name("Char3");
        assert!(c3.is_none());
    }

    #[test]
    fn test_link_characteristic() {
        let spec_char1 = ProductSpecificationCharacteristic::new("Char1").cardinality(1, 2);
        let mut spec1 = ProductSpecification::new("Spec1").with_charateristic(spec_char1);
        let spec_char2 = ProductSpecificationCharacteristic::new("Char2").cardinality(3, 4);
        let spec2 = ProductSpecification::new("Spec2").with_charateristic(spec_char2);

        spec1.link_characteristic(&spec2, "Char2");

        assert!(spec1.product_spec_characteristic.is_some());
        let chars = spec1.product_spec_characteristic.unwrap();
        assert_eq!(chars.len(), 2);
        let linked_char = chars.iter().find(|c| c.name == "Char2".to_string());
        assert!(linked_char.is_some());
        let rels = &linked_char.unwrap().product_spec_char_relationship;
        assert!(rels.is_some());
        let rels = rels.as_ref().unwrap();
        assert_eq!(rels.len(), 1);
        let rel = &rels[0];
        assert_eq!(rel.name, "Char2".to_string());
        assert_eq!(rel.relationship_type, "dependsOn".to_string());
        assert_eq!(rel.id, spec2.get_id());
        assert_eq!(rel.href, spec2.get_href());
    }
}