-
Notifications
You must be signed in to change notification settings - Fork 215
/
PolicyTrieTest.java
executable file
·228 lines (192 loc) · 11 KB
/
PolicyTrieTest.java
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.policies.model.enforcers.trie;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Optional;
import org.assertj.core.api.Assertions;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.policies.model.EffectedPermissions;
import org.eclipse.ditto.policies.model.Label;
import org.eclipse.ditto.policies.model.Permissions;
import org.eclipse.ditto.policies.model.PoliciesModelFactory;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.Resource;
import org.eclipse.ditto.policies.model.ResourceKey;
import org.eclipse.ditto.policies.model.Resources;
import org.eclipse.ditto.policies.model.SubjectId;
import org.eclipse.ditto.policies.model.SubjectIssuer;
import org.eclipse.ditto.policies.model.enforcers.TestConstants;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Unit test for {@link org.eclipse.ditto.policies.model.enforcers.trie.PolicyTrie}.
*/
public final class PolicyTrieTest {
private static String subjectId;
private static String anotherSubjectId;
private static ResourceKey fooResourceKey;
private static ResourceKey barResourceKey;
private static Policy policy;
private PolicyTrie underTest = null;
@BeforeClass
public static void initTestConstants() {
subjectId = TestConstants.Policy.SUBJECT_ID.toString();
final SubjectId johnTitor = PoliciesModelFactory.newSubjectId(SubjectIssuer.GOOGLE, "JohnTitor");
anotherSubjectId = johnTitor.toString();
final Label fooLabel = TestConstants.Policy.LABEL;
final Label barLabel = Label.of(fooLabel + "BAR");
final JsonPointer fooResourcePath = TestConstants.Policy.RESOURCE_PATH;
final JsonPointer barResourcePath = fooResourcePath.addLeaf(JsonFactory.newKey("Bar"));
fooResourceKey = ResourceKey.newInstance(TestConstants.Policy.RESOURCE_TYPE, fooResourcePath);
barResourceKey = ResourceKey.newInstance(TestConstants.Policy.RESOURCE_TYPE, barResourcePath);
policy = Policy.newBuilder(TestConstants.Policy.POLICY_ID)
.set(PoliciesModelFactory.newPolicyEntry(PoliciesModelFactory.newLabel(fooLabel),
PoliciesModelFactory.newSubjects(
PoliciesModelFactory.newSubject(TestConstants.Policy.SUBJECT_ID)),
Resources.newInstance(
Resource.newInstance(fooResourceKey,
EffectedPermissions.newInstance(Collections.singleton("WRITE"),
Collections.emptySet())),
Resource.newInstance(barResourceKey,
EffectedPermissions.newInstance(Collections.singleton("READ"),
Collections.emptySet())))))
.set(PoliciesModelFactory.newPolicyEntry(PoliciesModelFactory.newLabel(barLabel.toString()),
PoliciesModelFactory.newSubjects(
PoliciesModelFactory.newSubject(johnTitor)),
Resources.newInstance(
Resource.newInstance(
ResourceKey.newInstance(TestConstants.Policy.RESOURCE_TYPE, fooResourcePath),
EffectedPermissions.newInstance(Collections.singleton("READ"),
Collections.singleton("WRITE"))),
Resource.newInstance(barResourceKey,
EffectedPermissions.newInstance(Collections.emptySet(),
Collections.singleton("READ"))))))
.build();
}
@Before
public void initTestVariables() {
underTest = PolicyTrie.fromPolicy(policy);
}
@SuppressWarnings("ConstantConditions")
@Test
public void tryToCreateInstanceFromNullPolicy() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> PolicyTrie.fromPolicy(null))
.withMessage("The %s must not be null!", "policy to interpret")
.withNoCause();
}
@Test
public void getJsonKeyIteratorReturnsExpected() {
final JsonPointer barResourcePath = barResourceKey.getResourcePath();
final Collection<JsonKey> expectedJsonKeys = new ArrayList<>(1 + barResourcePath.getLevelCount());
expectedJsonKeys.add(JsonFactory.newKey(barResourceKey.getResourceType()));
barResourcePath.forEach(expectedJsonKeys::add);
final Iterator<JsonKey> jsonKeyIterator = PolicyTrie.getJsonKeyIterator(barResourceKey);
assertThat(jsonKeyIterator).toIterable().containsExactlyElementsOf(expectedJsonKeys);
}
@Test
public void getGrandRevokeIndexOnRootReturnsEmptyIndex() {
final GrantRevokeIndex expectedGrantRevokeIndex = new GrantRevokeIndex();
final GrantRevokeIndex grantRevokeIndex = underTest.getGrantRevokeIndex();
assertThat(grantRevokeIndex).isEqualTo(expectedGrantRevokeIndex);
}
@Test
public void policyTrieHasChildThing() {
final JsonKey child = JsonFactory.newKey("thing");
final boolean hasChild = underTest.hasChild(child);
assertThat(hasChild).as("Has child '%s'", child).isTrue();
}
@Test
public void seekToLeastAncestorOfBarResourcePath() {
final JsonPointer barResourcePath = barResourceKey.getResourcePath();
final JsonKey expectedChild = barResourcePath.getLeaf().orElse(null);
final PermissionSubjectsMap grantMap = new PermissionSubjectsMap();
grantMap.addTotalRelationOfWeightZero(Collections.singleton("READ"), Collections.singleton(anotherSubjectId));
grantMap.addTotalRelationOfWeightZero(Collections.singleton("WRITE"), Collections.singleton(subjectId));
final PermissionSubjectsMap revokeMap = new PermissionSubjectsMap();
revokeMap.addTotalRelationOfWeightZero(Collections.singleton("WRITE"), Collections.singleton(anotherSubjectId));
final GrantRevokeIndex expectedGrantRevokeIndex = new GrantRevokeIndex(grantMap, revokeMap);
final PolicyTrie leastAncestor = underTest.seekToLeastAncestor(PolicyTrie.getJsonKeyIterator(fooResourceKey));
final boolean hasChild = leastAncestor.hasChild(expectedChild);
final GrantRevokeIndex grantRevokeIndex = leastAncestor.getGrantRevokeIndex();
assertThat(hasChild).as("Has child '%s'", expectedChild).isTrue();
assertThat(grantRevokeIndex).isEqualTo(expectedGrantRevokeIndex);
}
@Test
public void seekToExactNodeReturnsExpected() {
final PermissionSubjectsMap grantMap = new PermissionSubjectsMap();
grantMap.addTotalRelationOfWeightZero(Collections.singleton("READ"), Collections.singleton(subjectId));
final PermissionSubjectsMap revokeMap = new PermissionSubjectsMap();
revokeMap.addTotalRelationOfWeightZero(Collections.singleton("READ"), Collections.singleton(anotherSubjectId));
final GrantRevokeIndex expectedGrantRevokeIndex = new GrantRevokeIndex(grantMap, revokeMap);
final Optional<PolicyTrie> exactNodeOptional =
underTest.seekToExactNode(PolicyTrie.getJsonKeyIterator(barResourceKey));
Assertions.assertThat(exactNodeOptional).isPresent();
assertThat(getGrantRevokeIndex(exactNodeOptional)).isEqualTo(expectedGrantRevokeIndex);
}
@Test
public void getTransitiveClosureReturnsExpected() {
// ARRANGE
final PermissionSubjectsMap fooNodeGrantMap = new PermissionSubjectsMap();
fooNodeGrantMap.put("READ", Collections.singletonMap(anotherSubjectId, 0));
fooNodeGrantMap.put("WRITE", Collections.singletonMap(subjectId, 0));
final PermissionSubjectsMap fooNodeRevokeMap = new PermissionSubjectsMap();
fooNodeRevokeMap.put("WRITE", Collections.singletonMap(anotherSubjectId, 0));
final GrantRevokeIndex expectedFooNodeGrantRevokeIndex =
new GrantRevokeIndex(fooNodeGrantMap, fooNodeRevokeMap);
final PermissionSubjectsMap barNodeGrantMap = new PermissionSubjectsMap();
barNodeGrantMap.put("READ", Collections.singletonMap(subjectId, 0));
barNodeGrantMap.put("WRITE", Collections.singletonMap(subjectId, -1));
final PermissionSubjectsMap barNodeRevokeMap = new PermissionSubjectsMap();
barNodeRevokeMap.put("READ", Collections.singletonMap(anotherSubjectId, 0));
barNodeRevokeMap.put("WRITE", Collections.singletonMap(anotherSubjectId, -1));
final GrantRevokeIndex expectedBarNodeGrantRevokeIndex =
new GrantRevokeIndex(barNodeGrantMap, barNodeRevokeMap);
// ACT
final PolicyTrie transitiveClosure = underTest.getTransitiveClosure();
final Optional<PolicyTrie> fooNodeOptional =
transitiveClosure.seekToExactNode(PolicyTrie.getJsonKeyIterator(fooResourceKey));
final Optional<PolicyTrie> barNodeOptional =
transitiveClosure.seekToExactNode(PolicyTrie.getJsonKeyIterator(barResourceKey));
// ASSERT
Assertions.assertThat(fooNodeOptional).as("Has exact Node for '%s'", fooResourceKey).isPresent();
Assertions.assertThat(barNodeOptional).as("Has exact Node for '%s'", barResourceKey).isPresent();
assertThat(getGrantRevokeIndex(fooNodeOptional))
.as("GrantRevokeIndex of foo node is expected")
.isEqualTo(expectedFooNodeGrantRevokeIndex);
assertThat(getGrantRevokeIndex(barNodeOptional))
.as("GrantRevokeIndex of bar node is expected")
.isEqualTo(expectedBarNodeGrantRevokeIndex);
}
@Test
public void buildJsonViewOfNullValue() {
final JsonObject createdJsonView =
underTest.buildJsonView(JsonFactory.nullObject(), new HashSet<>(), Permissions.none());
final JsonObject expectedJsonView = JsonFactory.nullObject();
assertThat(createdJsonView).isEqualTo(expectedJsonView);
}
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private static GrantRevokeIndex getGrantRevokeIndex(final Optional<PolicyTrie> policyTrieOptional) {
return policyTrieOptional.map(PolicyTrie::getGrantRevokeIndex).orElse(null);
}
}