-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathEntitiesTests.java
More file actions
177 lines (145 loc) · 7.52 KB
/
EntitiesTests.java
File metadata and controls
177 lines (145 loc) · 7.52 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright Cedar Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cedarpolicy;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.cedarpolicy.value.*;
import com.cedarpolicy.model.entity.Entity;
import com.cedarpolicy.model.entity.Entities;
import static com.cedarpolicy.CedarJson.objectWriter;
import org.skyscreamer.jsonassert.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class EntitiesTests {
private static final String TEST_RESOURCES_DIR = "src/test/resources/";
@Test
public void givenValidEntitySetConstructorConstructs() {
Entity alice = new Entity(EntityUID.parse("User::\"Alice\"").get());
Set<EntityUID> parentAlice = new HashSet<>();
parentAlice.add(alice.getEUID());
PrimString stringAttr = new PrimString("stringAttrValue");
HashMap<String, Value> attrs = new HashMap<>();
attrs.put("stringAttr", stringAttr);
Entity aliceChild = new Entity(EntityUID.parse("User::\"Alice_child\"").get(), attrs, parentAlice);
Set<Entity> entitySet = new HashSet<>();
entitySet.add(aliceChild);
entitySet.add(alice);
Entities entities = new Entities(entitySet);
assertEquals(entitySet, entities.getEntities());
}
@Test
public void givenValidJSONStringParseReturns() throws JsonProcessingException, JSONException {
String validEntitiesJson = """
[
{"uid":{"type":"Photo","id":"pic02"},"parents":[{"type":"PhotoParent","id":"picParent"}],
"attrs":{"dummyIP": {"__extn":{"fn":"ip","arg":"199.168.1.130"}}}},
{"uid":{"type":"Photo","id":"pic01"},"parents":[{"type":"Photo","id":"pic02"}],"attrs":{}}
]
""";
String expectedRepresentation = "{\"entities\":[" + "{\"uid\":{\"type\":\"Photo\",\"id\":\"pic02\"},"
+ "\"attrs\":{\"dummyIP\":{\"__extn\":{\"fn\":\"ip\",\"arg\":\"199.168.1.130\"}}},"
+ "\"parents\":[{\"type\":\"PhotoParent\",\"id\":\"picParent\"}]," + "\"tags\":{}},"
+ "{\"uid\":{\"type\":\"Photo\",\"id\":\"pic01\"}," + "\"attrs\":{},"
+ "\"parents\":[{\"type\":\"Photo\",\"id\":\"pic02\"}]," + "\"tags\":{}}]}";
Entities entities = Entities.parse(validEntitiesJson);
String actualRepresentation = objectWriter().writeValueAsString(entities);
JSONAssert.assertEquals(expectedRepresentation, actualRepresentation, JSONCompareMode.NON_EXTENSIBLE);
validEntitiesJson = """
[
{"uid":{"type":"Photo","id":"pic01"},"parents":[],"attrs":{}},
{"uid":{"type":"Photo","id":"pic02"},"parents":[],"attrs":{}}
]
""";
expectedRepresentation = "{\"entities\":[" + "{\"uid\":{\"type\":\"Photo\",\"id\":\"pic01\"}," + "\"attrs\":{},"
+ "\"parents\":[]," + "\"tags\":{}}," + "{\"uid\":{\"type\":\"Photo\",\"id\":\"pic02\"},"
+ "\"attrs\":{}," + "\"parents\":[]," + "\"tags\":{}}]}";
entities = Entities.parse(validEntitiesJson);
actualRepresentation = objectWriter().writeValueAsString(entities);
JSONAssert.assertEquals(expectedRepresentation, actualRepresentation, JSONCompareMode.NON_EXTENSIBLE);
}
@Test
public void givenInvalidJSONStringParseThrows() throws JsonProcessingException, JSONException {
String invalidEntityJson = """
[{"uid":{"type":"Photo","id":"pic01"}},
{"uid":{"type":"Photo","id":"pic02"},"parents":[],"attrs":{}}]
""";
assertThrows(JsonProcessingException.class, () -> {
Entities.parse(invalidEntityJson);
});
String invalidEntityJson2 = """
[{"uid":{"type":"Photo","id":"pic02"}, "parents":[{"parent_id":"Alice"}]},
{"uid":{"type":"Photo","id":"pic01"},"parents":[],"attrs":{}}]
""";
assertThrows(JsonProcessingException.class, () -> {
Entities.parse(invalidEntityJson2);
});
}
@Test
public void givenValidJSONFileParseReturns() throws JsonProcessingException, IOException, JSONException {
Entities entities = Entities.parse(Path.of(TEST_RESOURCES_DIR + "valid_entities.json"));
String actualRepresentation = objectWriter().writeValueAsString(entities);
String expectedRepresentation = "{\"entities\":[" + "{\"uid\":{\"type\":\"Photo\",\"id\":\"pic02\"},"
+ "\"attrs\":{\"dummyIP\":{\"__extn\":{\"fn\":\"ip\",\"arg\":\"199.168.1.130\"}}},"
+ "\"parents\":[{\"type\":\"PhotoParent\",\"id\":\"picParent\"}]," + "\"tags\":{}},"
+ "{\"uid\":{\"type\":\"Photo\",\"id\":\"pic01\"}," + "\"attrs\":{},"
+ "\"parents\":[{\"type\":\"Photo\",\"id\":\"pic02\"}]," + "\"tags\":{}}]}";
JSONAssert.assertEquals(expectedRepresentation, actualRepresentation, JSONCompareMode.NON_EXTENSIBLE);
}
@Test
public void givenExplicitEntityParentSyntaxParseReturns() throws JsonProcessingException, IOException {
String validEntitiesJson = """
[
{"uid":{"type":"Photo","id":"pic01"},
"parents":[{"__entity":{"type":"Photo","id":"pic02"}}],
"attrs":{}},
{"uid":{"type":"Photo","id":"pic02"},"parents":[],"attrs":{}}
]
""";
EntityUID childEUID = EntityUID.parse("Photo::\"pic01\"").get();
EntityUID parentEUID = EntityUID.parse("Photo::\"pic02\"").get();
Entities entitiesFromString = Entities.parse(validEntitiesJson);
Entity childFromString = entitiesFromString.getEntities().stream()
.filter(entity -> entity.getEUID().equals(childEUID))
.findFirst()
.orElseThrow();
assertEquals(Set.of(parentEUID), childFromString.getParents());
Path tempFile = Files.createTempFile("entities-explicit-parent", ".json");
try {
Files.writeString(tempFile, validEntitiesJson);
Entities entitiesFromFile = Entities.parse(tempFile);
Entity childFromFile = entitiesFromFile.getEntities().stream()
.filter(entity -> entity.getEUID().equals(childEUID))
.findFirst()
.orElseThrow();
assertEquals(Set.of(parentEUID), childFromFile.getParents());
} finally {
Files.deleteIfExists(tempFile);
}
}
@Test
public void givenInvalidJSONFileParseThrows() throws JsonProcessingException, IOException {
assertThrows(JsonProcessingException.class, () -> {
Entities.parse(Path.of(TEST_RESOURCES_DIR + "invalid_entities.json"));
});
}
}