Skip to content

Commit

Permalink
Add convenience methods to see how many policies and templates a poli…
Browse files Browse the repository at this point in the history
…cy set has

cedar-policy#1179
  • Loading branch information
juanvgarcia committed Sep 6, 2024
1 parent 12cbc25 commit e7ab4ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
12 changes: 12 additions & 0 deletions cedar-policy/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,18 @@ impl PolicySet {
self.ast.is_empty()
}

/// Returns the number of `Template`s in the `PolicySet`.
///
/// This will include both static and template-linked policies.
pub fn num_of_policies(&self) -> usize {
self.policies.len()
}

/// Returns the number of `Template`s in the `PolicySet`.
pub fn num_of_templates(&self) -> usize {
self.templates.len()
}

/// Attempt to link a template and add the new template-linked policy to the policy set.
/// If link fails, the `PolicySet` is not modified.
/// Failure can happen for three reasons
Expand Down
14 changes: 7 additions & 7 deletions cedar-policy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,8 +720,8 @@ mod policy_set_tests {
)
.expect("Link failure");

assert_eq!(pset.templates().count(), 1);
assert_eq!(pset.policies().count(), 2);
assert_eq!(pset.num_of_templates(), 1);
assert_eq!(pset.num_of_policies(), 2);
assert_eq!(pset.policies().filter(|p| p.is_static()).count(), 1);

assert_eq!(
Expand Down Expand Up @@ -4211,7 +4211,7 @@ mod policy_set_est_tests {
let pset2 = PolicySet::from_json_value(json).unwrap();

// There should be 2 policies, one static and two links
assert_eq!(pset2.policies().count(), 3);
assert_eq!(pset2.num_of_policies(), 3);
let static_policy = pset2.policy(&PolicyId::new("policy")).unwrap();
assert!(static_policy.is_static());

Expand Down Expand Up @@ -4297,8 +4297,8 @@ mod policy_set_est_tests {
});

let policyset = PolicySet::from_json_value(value).unwrap();
assert_eq!(policyset.templates().count(), 0);
assert_eq!(policyset.policies().count(), 1);
assert_eq!(policyset.num_of_templates(), 0);
assert_eq!(policyset.num_of_policies(), 1);
assert!(policyset.policy(&PolicyId::new("policy1")).is_some());
}

Expand Down Expand Up @@ -4370,8 +4370,8 @@ mod policy_set_est_tests {
});

let policyset = PolicySet::from_json_value(value).unwrap();
assert_eq!(policyset.policies().count(), 2);
assert_eq!(policyset.templates().count(), 1);
assert_eq!(policyset.num_of_policies(), 2);
assert_eq!(policyset.num_of_templates(), 1);
assert!(policyset.template(&PolicyId::new("template")).is_some());
let link = policyset.policy(&PolicyId::new("link")).unwrap();
assert_eq!(link.template_id(), Some(&PolicyId::new("template")));
Expand Down

0 comments on commit e7ab4ba

Please sign in to comment.