|
6 | 6 | import org.junit.jupiter.api.Test; |
7 | 7 |
|
8 | 8 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
9 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
10 | 10 | import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
11 | 12 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 13 | +import static org.junit.jupiter.api.Assertions.fail; |
12 | 14 |
|
13 | 15 | public class PolicyTests { |
14 | 16 | @Test |
@@ -99,4 +101,34 @@ public void validateTemplateLinkedPolicyFailsWhenExpected() { |
99 | 101 | Policy.validateTemplateLinkedPolicy(p3, principal, resource); |
100 | 102 | }); |
101 | 103 | } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void staticPolicyToJsonTests() throws InternalException { |
| 107 | + assertThrows(NullPointerException.class, () -> { |
| 108 | + Policy p = new Policy(null, null); |
| 109 | + p.toJson(); |
| 110 | + }); |
| 111 | + assertThrows(InternalException.class, () -> { |
| 112 | + Policy p = new Policy("permit();", null); |
| 113 | + p.toJson(); |
| 114 | + }); |
| 115 | + |
| 116 | + Policy p = Policy.parseStaticPolicy("permit(principal, action, resource);"); |
| 117 | + String actualJson = p.toJson(); |
| 118 | + String expectedJson = "{\"effect\":\"permit\",\"principal\":{\"op\":\"All\"},\"action\":{\"op\":\"All\"}," |
| 119 | + + "\"resource\":{\"op\":\"All\"},\"conditions\":[]}"; |
| 120 | + assertEquals(expectedJson, actualJson); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void policyTemplateToJsonFailureTests() throws InternalException { |
| 125 | + try { |
| 126 | + String tbody = "permit(principal == ?principal, action, resource in ?resource);"; |
| 127 | + Policy template = Policy.parsePolicyTemplate(tbody); |
| 128 | + template.toJson(); |
| 129 | + fail("Expected InternalException"); |
| 130 | + } catch (InternalException e) { |
| 131 | + assertTrue(e.getMessage().contains("expected a static policy, got a template containing the slot ?resource")); |
| 132 | + } |
| 133 | + } |
102 | 134 | } |
0 commit comments