2323
2424import java .io .IOException ;
2525import java .nio .file .Path ;
26+ import com .fasterxml .jackson .core .JsonProcessingException ;
2627
2728import static org .junit .jupiter .api .Assertions .assertThrows ;
2829import static org .junit .jupiter .api .Assertions .assertNotNull ;
2930import static org .junit .jupiter .api .Assertions .assertEquals ;
31+ import static com .cedarpolicy .TestUtil .buildValidPolicySet ;
32+ import static com .cedarpolicy .TestUtil .buildInvalidPolicySet ;
3033
3134public class PolicySetTests {
3235 private static final String TEST_RESOURCES_DIR = "src/test/resources/" ;
3336
3437 @ Test
3538 public void parsePoliciesTests () throws InternalException , IOException {
36- PolicySet policySet = PolicySet .parsePolicies (Path .of (TEST_RESOURCES_DIR + "policies.cedar" ));
37- for (Policy p : policySet .policies ) {
39+ PolicySet policySet =
40+ PolicySet .parsePolicies (Path .of (TEST_RESOURCES_DIR + "policies.cedar" ));
41+ for (Policy p : policySet .policies ) {
3842 assertNotNull (p .policySrc );
3943 }
4044 // Make sure the policy IDs are unique as Policies are made
@@ -46,13 +50,14 @@ public void parsePoliciesTests() throws InternalException, IOException {
4650 @ Test
4751 public void parsePoliciesStringTests () throws InternalException {
4852 PolicySet policySet = PolicySet .parsePolicies ("permit(principal, action, resource);" );
49- PolicySet policySet2 = PolicySet .parsePolicies ("permit(principal, action, resource) when { principal has x && principal.x == 5};" );
50- for (Policy p : policySet .policies ) {
53+ PolicySet policySet2 = PolicySet .parsePolicies (
54+ "permit(principal, action, resource) when { principal has x && principal.x == 5};" );
55+ for (Policy p : policySet .policies ) {
5156 assertNotNull (p .policySrc );
5257 }
5358 assertEquals (1 , policySet .policies .size ());
5459 assertEquals (0 , policySet .templates .size ());
55- for (Policy p : policySet2 .policies ) {
60+ for (Policy p : policySet2 .policies ) {
5661 assertNotNull (p .policySrc );
5762 }
5863 assertEquals (1 , policySet2 .policies .size ());
@@ -61,13 +66,14 @@ public void parsePoliciesStringTests() throws InternalException {
6166
6267 @ Test
6368 public void parseTemplatesTests () throws InternalException , IOException {
64- PolicySet policySet = PolicySet .parsePolicies (Path .of (TEST_RESOURCES_DIR + "template.cedar" ));
65- for (Policy p : policySet .policies ) {
69+ PolicySet policySet =
70+ PolicySet .parsePolicies (Path .of (TEST_RESOURCES_DIR + "template.cedar" ));
71+ for (Policy p : policySet .policies ) {
6672 assertNotNull (p .policySrc );
6773 }
6874 assertEquals (2 , policySet .policies .size ());
6975
70- for (Policy p : policySet .templates ) {
76+ for (Policy p : policySet .templates ) {
7177 assertNotNull (p .policySrc );
7278 }
7379 assertEquals (1 , policySet .templates .size ());
@@ -96,8 +102,33 @@ public void getNumTests() throws InternalException, IOException {
96102 assertEquals (0 , emptyPolicySet .getNumTemplates ());
97103
98104 // Non-empty policy set
99- PolicySet policySet = PolicySet .parsePolicies (Path .of (TEST_RESOURCES_DIR + "template.cedar" ));
105+ PolicySet policySet =
106+ PolicySet .parsePolicies (Path .of (TEST_RESOURCES_DIR + "template.cedar" ));
100107 assertEquals (2 , policySet .getNumPolicies ());
101108 assertEquals (1 , policySet .getNumTemplates ());
102109 }
110+
111+ @ Test
112+ public void policySetToJsonTests ()
113+ throws JsonProcessingException , IOException , InternalException {
114+ // Tests valid PolicySet
115+ PolicySet validPolicySet = buildValidPolicySet ();
116+ String validJson =
117+ "{\" templates\" :{\" t0\" :{\" effect\" :\" permit\" ,\" principal\" :{\" op\" :\" ==\" ,\" slot\" :\" ?principal\" },"
118+ + "\" action\" :{\" op\" :\" ==\" ,\" entity\" :{\" type\" :\" Action\" ,\" id\" :\" View_Photo\" }},"
119+ + "\" resource\" :{\" op\" :\" in\" ,\" entity\" :{\" type\" :\" Album\" ,\" id\" :\" Vacation\" }},\" conditions\" :[]}},"
120+ + "\" staticPolicies\" :{\" p1\" :{\" effect\" :\" permit\" ,\" principal\" :{\" op\" :\" ==\" ,"
121+ + "\" entity\" :{\" type\" :\" User\" ,\" id\" :\" Bob\" }},"
122+ + "\" action\" :{\" op\" :\" ==\" ,\" entity\" :{\" type\" :\" Action\" ,\" id\" :\" View_Photo\" }},"
123+ + "\" resource\" :{\" op\" :\" in\" ,\" entity\" :{\" type\" :\" Album\" ,\" id\" :\" Vacation\" }},\" conditions\" :[]}},"
124+ + "\" templateLinks\" :[{\" templateId\" :\" t0\" ,\" newId\" :\" tl0\" ,\" values\" :{\" ?principal\" :"
125+ + "{\" __entity\" :{\" type\" :\" User\" ,\" id\" :\" Alice\" }}}}]}" ;
126+ assertEquals (validJson , validPolicySet .toJson ());
127+
128+ // Tests invalid PolicySet
129+ PolicySet invalidPolicySet = buildInvalidPolicySet ();
130+ assertThrows (InternalException .class , () -> {
131+ invalidPolicySet .toJson ();
132+ });
133+ }
103134}
0 commit comments