Skip to content

Commit

Permalink
Merge pull request #213 from rruckley/TMF633-ServiceCatalog-210
Browse files Browse the repository at this point in the history
feat: ServiceCatalog module
  • Loading branch information
rruckley authored Jan 8, 2025
2 parents 9b03e3a + d78708a commit f62cf8d
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/tmf633/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
const MOD_PATH : &str = "serviceCatalogManagement/v4";

pub mod service_catalog;
pub mod service_category;
pub mod service_specification;
pub mod service_candidate;
Expand Down
114 changes: 114 additions & 0 deletions src/tmf633/service_catalog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//! Service Category Module

use serde::{Deserialize, Serialize};

use crate::{
HasId,
HasLastUpdate,
HasName,
HasDescription,
HasValidity,
TimeStamp,
TimePeriod,
LIB_PATH,
Uri,
vec_insert,
};
use crate::common::related_party::RelatedParty;
use super::service_category::ServiceCategoryRef;
use tmflib_derive::{HasId, HasLastUpdate, HasDescription, HasName, HasValidity};

use super::MOD_PATH;
const CLASS_PATH : &str = "serviceCategory";
const CAT_STATUS_NEW : &str = "new";
const CAT_VERS_NEW : &str = "1.0";

/// Service Catalog
#[derive(Clone, Debug, Default, Deserialize, HasId, HasName, HasDescription, HasLastUpdate, HasValidity, Serialize)]
pub struct ServiceCatalog {
description: Option<String>,
href: Option<Uri>,
id: Option<String>,
last_update: Option<TimeStamp>,
lifecycle_status: Option<String>,
name: Option<String>,
valid_for: Option<TimePeriod>,
version: Option<String>,
// References
related_party: Option<Vec<RelatedParty>>,
category: Option<Vec<ServiceCategoryRef>>,
// META
/// Base Type this type is derived from if creating sub-classes
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "@baseType")]
pub base_type : Option<String>,
/// Schema Definition of the sub-class (if required)
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "@schemaLocation")]
pub schema_location: Option<Uri>,
/// Name for this Type when sub-classing
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "@type")]
pub r#type : Option<String>,
}

impl ServiceCatalog {
/// Create a new Service Catalog instance
pub fn new(name : impl Into<String>) -> ServiceCatalog {
ServiceCatalog {
name : Some(name.into()),
lifecycle_status: Some(CAT_STATUS_NEW.into()),
version: Some(CAT_VERS_NEW.into()),
..ServiceCatalog::create_with_time()
}
}

/// Add a category to this Service Candidate by passing in a Category reference
pub fn category(mut self, category : ServiceCategoryRef) -> ServiceCatalog {
vec_insert(&mut self.category, category);
self
}
}

#[cfg(test)]
mod test {
use crate::tmf633::service_category::ServiceCategory;

use super::*;

const CAT_NAME : &str = "CAT_NAME";
const CAT_DESC : &str = "CAT_DESC";
const CATEGORY_NAME : &str = "A_CATEGORY";

#[test]
fn test_servicecatalog_create() {
let catalog = ServiceCatalog::new(CAT_NAME);

assert_eq!(catalog.get_name(),CAT_NAME.to_string());
assert_eq!(catalog.version.is_some(),true);
assert_eq!(catalog.version.unwrap(),CAT_VERS_NEW.to_string());
assert_eq!(catalog.lifecycle_status.is_some(),true);
assert_eq!(catalog.lifecycle_status.unwrap(),CAT_STATUS_NEW.to_string());
}

#[test]
fn test_servicecatalog_create_with_category() {
let category = ServiceCategory::new(CATEGORY_NAME);

let catalog = ServiceCatalog::new(CAT_NAME)
.category(ServiceCategoryRef::from(category));

assert_eq!(catalog.category.is_some(),true);
assert_eq!(catalog.category.unwrap().len(),1);
}

#[test]
fn test_servicecatalog_create_with_desc() {
let catalog = ServiceCatalog::new(CAT_NAME)
.description(CAT_DESC);

assert_eq!(catalog.description.is_some(),true);
assert_eq!(catalog.description.unwrap(),CAT_DESC.to_string());
}
}
47 changes: 45 additions & 2 deletions src/tmf633/service_category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ use crate::{
TimePeriod,
LIB_PATH,
Uri,
vec_insert,
};
use tmflib_derive::{HasId, HasLastUpdate, HasDescription, HasName, HasValidity};

use super::{service_candidate::ServiceCandidate, MOD_PATH};
use super::{service_candidate::{ServiceCandidate, ServiceCandidateRef}, MOD_PATH};
const CLASS_PATH : &str = "serviceCategory";
const CAT_STATUS_NEW : &str = "new";
const CAT_VERS_NEW : &str = "1.0";
Expand Down Expand Up @@ -85,7 +86,7 @@ pub struct ServiceCategory {
#[serde(skip_serializing_if = "Option::is_none")]
category : Option<Vec<ServiceCategoryRef>>,
#[serde(skip_serializing_if = "Option::is_none")]
service_candidate: Option<Vec<ServiceCandidate>>,
service_candidate: Option<Vec<ServiceCandidateRef>>,
}

impl ServiceCategory {
Expand All @@ -98,6 +99,18 @@ impl ServiceCategory {
..ServiceCategory::create_with_time()
}
}

/// Add a child category to this category
pub fn child_category(mut self, category: ServiceCategoryRef) -> ServiceCategory {
vec_insert(&mut self.category, category);
self
}

/// Add a ServiceCandidate to this category
pub fn candidate(mut self, candidate: ServiceCandidateRef) -> ServiceCategory {
vec_insert(&mut self.service_candidate,candidate);
self
}
}

#[cfg(test)]
Expand All @@ -106,6 +119,8 @@ mod test {
use super::*;

const CAT_NAME : &str = "CAT_NAME";
const CHILD_CAT : &str = "CHILD_CAT";
const CANDIDATE_NAME : &str = "CANDIDATE_NAME";

#[test]
fn test_servicecategory_create() {
Expand All @@ -123,4 +138,32 @@ mod test {
// name in ref should match input name of cat
assert_eq!(CAT_NAME.to_string(),cat_ref.name);
}

#[test]
fn test_category_addchild() {
let child_cat = ServiceCategory::new(CHILD_CAT);
let parent_cat = ServiceCategory::new(CAT_NAME)
.child_category(ServiceCategoryRef::from(child_cat));

// Vec should have been created
assert_eq!(parent_cat.category.is_some(),true);
// Vec should only have a single entry
assert_eq!(parent_cat.category.unwrap().len(),1);
}

#[test]
fn test_category_addcandidate() {
let mut candidate = ServiceCandidate::default();
// Need to generate an id here eles the conversion to Ref will panic
candidate.generate_id();
candidate.set_name(CANDIDATE_NAME);

let category = ServiceCategory::new(CAT_NAME)
.candidate(ServiceCandidateRef::from(candidate));

// Vec should have been created
assert_eq!(category.service_candidate.is_some(),true);
// Vec should only have a single entry
assert_eq!(category.service_candidate.unwrap().len(),1);
}
}

0 comments on commit f62cf8d

Please sign in to comment.