-
Notifications
You must be signed in to change notification settings - Fork 215
/
EnforcedThingMapperTest.java
103 lines (94 loc) · 4.49 KB
/
EnforcedThingMapperTest.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
/*
* Copyright (c) 2019 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.thingsearch.service.persistence.write.mapping;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.ditto.policies.model.PoliciesResourceType.THING;
import org.bson.BsonDocument;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.policies.api.Permission;
import org.eclipse.ditto.policies.model.PoliciesModelFactory;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.SubjectType;
import org.junit.Test;
public final class EnforcedThingMapperTest {
@Test
public void testV2Thing() {
final JsonObject thing = JsonFactory.newObject("""
{
"thingId": "hello:world",
"_namespace": "hello",
"_revision": 1024,
"_modified": "2019-01-02T03:04:05.006Z",
"policyId": "hello:world",
"features": {
"hi": {
"definition": [
"earth:v0:1",
"mars:v0:2"
],
"properties": {
"there": true
}
}
},
"attributes": {
"hello": "world"
}
}""");
final var policy =
PoliciesModelFactory.newPolicyBuilder(PolicyId.of("policy", "id"))
.forLabel("grant-root")
.setSubject("g:0", SubjectType.GENERATED)
.setGrantedPermissions(THING, "/", Permission.READ)
.forLabel("grant-d")
.setSubject("g:1", SubjectType.GENERATED)
.setGrantedPermissions(THING, "/features/hi/properties/there", Permission.READ)
.build();
final long policyRevision = 56L;
final JsonObject expectedJson = JsonFactory.newObject("""
{
"_id": "hello:world",
"_revision": 1024,
"_namespace": "hello",
"gr":["g:1","g:0"],
"policyId": "hello:world",
"__policyRev": 56,
"s": {
"thingId": "hello:world",
"_namespace": "hello",
"_revision": 1024,
"_modified": "2019-01-02T03:04:05.006Z",
"policyId": "hello:world",
"features": { "hi": { "definition": [ "earth:v0:1", "mars:v0:2" ], "properties": { "there": true } } },
"attributes": { "hello": "world" }
},
"d": [
{ "k": "/thingId", "v": "hello:world", "g": [ "g:0" ], "r": [] },
{ "k": "/_namespace", "v": "hello", "g": [ "g:0" ], "r": [] },
{ "k": "/_revision", "v": 1024, "g": [ "g:0" ], "r": [] },
{ "k": "/_modified", "v": "2019-01-02T03:04:05.006Z", "g": [ "g:0" ], "r": [] },
{ "k": "/policyId", "v": "hello:world", "g": [ "g:0" ], "r": [] },
{ "k": "/features/hi/definition", "v": "earth:v0:1", "g": [ "g:0" ], "r": [] },
{ "k": "/features/*/definition", "v": "earth:v0:1", "g": [ "g:0" ], "r": [] },
{ "k": "/features/hi/definition", "v": "mars:v0:2", "g": [ "g:0" ], "r": [] },
{ "k": "/features/*/definition", "v": "mars:v0:2", "g": [ "g:0" ], "r": [] },
{ "k": "/features/hi/properties/there", "v": true, "g": [ "g:1", "g:0" ], "r": [] },
{ "k": "/features/*/properties/there", "v": true, "g": [ "g:1", "g:0" ], "r": [] },
{ "k": "/attributes/hello", "v": "world", "g": [ "g:0" ], "r": [] }
]
}""");
final BsonDocument result = EnforcedThingMapper.mapThing(thing, policy, policyRevision);
assertThat(JsonFactory.newObject(result.toJson())).isEqualTo(expectedJson);
}
}