forked from cedar-policy/cedar-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolicyFormatterTests.java
More file actions
68 lines (51 loc) · 2.45 KB
/
PolicyFormatterTests.java
File metadata and controls
68 lines (51 loc) · 2.45 KB
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
package com.cedarpolicy;
import com.cedarpolicy.formatter.PolicyFormatter;
import com.cedarpolicy.model.exception.InternalException;
import com.cedarpolicy.model.formatter.Config;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class PolicyFormatterTests {
private static final String TEST_RESOURCES_DIR = "src/test/resources/";
@Test
public void testPoliciesStrToPretty() throws Exception {
String unformattedCedarPolicy = Files.readString(
Path.of(TEST_RESOURCES_DIR + "unformatted_policy.cedar"));
String formattedCedarPolicy = Files.readString(
Path.of(TEST_RESOURCES_DIR + "formatted_policy.cedar"));
assertEquals(formattedCedarPolicy, PolicyFormatter.policiesStrToPretty(unformattedCedarPolicy));
}
@Test
public void testPoliciesStrToPrettyMalformedCedarPolicy() throws Exception {
String malformedCedarPolicy = Files.readString(
Path.of(TEST_RESOURCES_DIR + "malformed_policy_set.cedar"));
assertThrows(InternalException.class,
() -> PolicyFormatter.policiesStrToPretty(malformedCedarPolicy));
}
@Test
public void testPoliciesStrToPrettyNullSafety() {
assertThrows(NullPointerException.class, () -> PolicyFormatter.policiesStrToPretty(null));
}
@Test
public void testPoliciesStrToPrettyWithConfigNullSafety() throws Exception {
String cedarPolicy = Files.readString(Path.of(TEST_RESOURCES_DIR + "formatted_policy.cedar"));
assertThrows(NullPointerException.class,
() -> PolicyFormatter.policiesStrToPrettyWithConfig(null, null));
assertThrows(NullPointerException.class,
() -> PolicyFormatter.policiesStrToPrettyWithConfig(cedarPolicy, null));
assertThrows(NullPointerException.class,
() -> PolicyFormatter.policiesStrToPrettyWithConfig(null, new Config(120, 4)));
}
@Test
public void testPoliciesStrToPrettyWithConfig() throws Exception {
String unformattedCedarPolicy = Files.readString(
Path.of(TEST_RESOURCES_DIR + "unformatted_policy.cedar"));
String formattedCedarPolicyWithCustomConfig = Files.readString(
Path.of(TEST_RESOURCES_DIR + "formatted_policy_custom_config.cedar"));
// Random change
assertEquals(formattedCedarPolicyWithCustomConfig,
PolicyFormatter.policiesStrToPrettyWithConfig(unformattedCedarPolicy, new Config(120, 4)));
}
}