-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathDataReferenceResolverTest.java
More file actions
421 lines (321 loc) · 15.5 KB
/
DataReferenceResolverTest.java
File metadata and controls
421 lines (321 loc) · 15.5 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* Copyright (C) 2017-2026 HERE Europe B.V.
*
* 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
*
* http://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.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package com.here.xyz.hub.util;
import com.here.xyz.hub.config.DataReferenceConfigClient;
import com.here.xyz.hub.config.SpaceConfigClient;
import com.here.xyz.hub.connectors.models.Space;
import com.here.xyz.models.hub.DataReference;
import io.vertx.core.Future;
import org.apache.logging.log4j.Marker;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class DataReferenceResolverTest {
@Mock
private DataReferenceConfigClient references;
@Mock
private SpaceConfigClient spaces;
@Mock
private Marker marker;
private DataReferenceResolver resolver;
@BeforeEach
void setUp() {
resolver = new DataReferenceResolver(references, spaces);
}
@Test
void loadById_shouldReturnEmpty_whenReferenceDoesNotExist() {
UUID id = UUID.randomUUID();
when(references.load(id)).thenReturn(Future.succeededFuture(Optional.empty()));
Optional<DataReference> result = await(resolver.loadById(marker, id, false));
assertThat(result).isEmpty();
verify(references).load(id);
verifyNoInteractions(spaces);
}
@Test
void loadById_shouldReturnStoredReference_whenOnlyStaleIsTrue() {
UUID id = UUID.randomUUID();
DataReference ref = ref("entity-id-1", 100L).withId(id);
when(references.load(id)).thenReturn(Future.succeededFuture(Optional.of(ref)));
Optional<DataReference> result = await(resolver.loadById(marker, id, true));
assertThat(result).containsSame(ref);
verifyNoInteractions(spaces);
}
@Test
void loadEffectiveById_shouldReturnSameReference_whenAnchorIsMissing() {
UUID id = UUID.randomUUID();
DataReference ref = ref("entity-id-1", 100L).withId(id);
when(references.load(id)).thenReturn(Future.succeededFuture(Optional.of(ref)));
when(spaces.get(marker, "entity-id-1")).thenReturn(Future.succeededFuture(null));
Optional<DataReference> result = await(resolver.loadEffectiveById(marker, id));
assertThat(result).containsSame(ref);
}
@Test
void loadEffectiveById_shouldReturnReplacement_whenReferenceIsStale() {
UUID id = UUID.randomUUID();
Space space = spaceWithCreatedAt(500L);
DataReference stale = ref("entity-id-1", 100L).withId(id);
DataReference candidate1 = ref("entity-id-1", 500L);
DataReference candidate2Newest = ref("entity-id-1", 800L);
when(references.load(id)).thenReturn(Future.succeededFuture(Optional.of(stale)));
when(spaces.get(marker, "entity-id-1")).thenReturn(Future.succeededFuture(space));
when(references.load("entity-id-1", null, null, null, null, null, null))
.thenReturn(Future.succeededFuture(List.of(candidate1, candidate2Newest)));
Optional<DataReference> result = await(resolver.loadEffectiveById(marker, id));
assertThat(result).containsSame(candidate2Newest);
}
@Test
void loadEffectiveById_shouldReturnEmpty_whenReferenceIsStaleAndNoReplacementExists() {
UUID id = UUID.randomUUID();
Space space = spaceWithCreatedAt(500L);
DataReference stale = ref("entity-id-1", 100L).withId(id);
DataReference tooOld = ref("entity-id-1", 400L);
when(references.load(id)).thenReturn(Future.succeededFuture(Optional.of(stale)));
when(spaces.get(marker, "entity-id-1")).thenReturn(Future.succeededFuture(space));
when(references.load("entity-id-1", null, null, null, null, null, null))
.thenReturn(Future.succeededFuture(List.of(tooOld)));
Optional<DataReference> result = await(resolver.loadEffectiveById(marker, id));
assertThat(result).isEmpty();
}
@Test
void loadEffectiveById_shouldReturnReplacementWithSameUniquenessKey_whenReferenceIsStale() {
UUID id = UUID.randomUUID();
Space space = spaceWithCreatedAt(500L);
DataReference stale = ref("entity-id-1", 100L)
.withId(id)
.withEndVersion(10)
.withObjectType("features")
.withContentType("ct")
.withSourceSystem("src")
.withTargetSystem("tgt");
DataReference wrongKeyNewer = ref("entity-id-1", 900L)
.withEndVersion(11)
.withObjectType("features")
.withContentType("ct")
.withSourceSystem("src")
.withTargetSystem("tgt");
DataReference sameKeyReplacement = ref("entity-id-1", 800L)
.withEndVersion(10)
.withObjectType("features")
.withContentType("ct")
.withSourceSystem("src")
.withTargetSystem("tgt");
when(references.load(id)).thenReturn(Future.succeededFuture(Optional.of(stale)));
when(spaces.get(marker, "entity-id-1")).thenReturn(Future.succeededFuture(space));
when(references.load("entity-id-1", null, null, null, null, null, null))
.thenReturn(Future.succeededFuture(List.of(wrongKeyNewer, sameKeyReplacement)));
Optional<DataReference> result = await(resolver.loadEffectiveById(marker, id));
assertThat(result).containsSame(sameKeyReplacement);
}
@Test
void filterForEntity_shouldReturnNewestNonStale_whenOnlyStaleIsFalse() {
String entityId = "entity-id-1";
Space space = spaceWithCreatedAt(150L);
DataReference stale = ref(entityId, 100L);
DataReference latest = ref(entityId, 150L);
DataReference newer = ref(entityId, 180L);
when(spaces.get(marker, entityId)).thenReturn(Future.succeededFuture(space));
List<DataReference> result = await(resolver.filterForEntity(marker, entityId, List.of(stale, latest, newer), false));
assertThat(result).containsExactly(newer);
}
@Test
void filterForEntity_shouldReturnOnlyStale_whenOnlyStaleIsTrue() {
String entityId = "entity-id-1";
Space space = spaceWithCreatedAt(150L);
DataReference stale = ref(entityId, 100L);
DataReference latest = ref(entityId, 150L);
DataReference newer = ref(entityId, 180L);
when(spaces.get(marker, entityId)).thenReturn(Future.succeededFuture(space));
List<DataReference> result = await(resolver.filterForEntity(marker, entityId, List.of(stale, latest, newer), true));
assertThat(result).containsExactly(stale);
}
@Test
void filterForEntity_shouldReturnNewest_whenAnchorIsMissing_andOnlyStaleIsFalse() {
String entityId = "entity-id-1";
DataReference older = ref(entityId, 100L);
DataReference newer = ref(entityId, 200L);
when(spaces.get(marker, entityId)).thenReturn(Future.succeededFuture(null));
List<DataReference> result = await(resolver.filterForEntity(marker, entityId, List.of(older, newer), false));
assertThat(result).containsExactly(newer);
}
@Test
void filterForEntity_shouldReturnEmpty_whenAnchorIsMissing_andOnlyStaleIsTrue() {
String entityId = "entity-id-1";
DataReference r1 = ref(entityId, 100L);
DataReference r2 = ref(entityId, 200L);
when(spaces.get(marker, entityId)).thenReturn(Future.succeededFuture(null));
List<DataReference> result = await(resolver.filterForEntity(marker, entityId, List.of(r1, r2), true));
assertThat(result).isEmpty();
}
@Test
void filterStaleForEntity_shouldReturnOnlyReferencesOlderThanAnchor() {
String entityId = "entity-id-1";
Space space = spaceWithCreatedAt(150L);
DataReference stale = ref(entityId, 100L)
.withStartVersion(0)
.withEndVersion(1)
.withObjectType("features")
.withContentType("ct-a")
.withSourceSystem("src")
.withTargetSystem("tgt");
DataReference atAnchor = ref(entityId, 150L)
.withStartVersion(1)
.withEndVersion(2)
.withObjectType("features")
.withContentType("ct-b")
.withSourceSystem("src")
.withTargetSystem("tgt");
DataReference fresh = ref(entityId, 151L)
.withStartVersion(2)
.withEndVersion(3)
.withObjectType("features")
.withContentType("ct-c")
.withSourceSystem("src")
.withTargetSystem("tgt");
DataReference nullCreatedAt = ref(entityId, null)
.withStartVersion(3)
.withEndVersion(4)
.withObjectType("features")
.withContentType("ct-d")
.withSourceSystem("src")
.withTargetSystem("tgt");
when(spaces.get(marker, entityId)).thenReturn(Future.succeededFuture(space));
List<DataReference> result = await(resolver.filterStaleForEntity(marker, entityId, List.of(stale, atAnchor, fresh, nullCreatedAt)));
assertThat(result).containsExactlyInAnyOrder(stale, nullCreatedAt);
}
@Test
void filterStaleForEntity_shouldUseParentMapAnchor_whenDirectSpaceIsMissing() {
String entityId = "hrn:here:data::olp-here:someMap:layer";
String parent = "hrn:here:data::olp-here:someMap";
Space parentSpace = spaceWithCreatedAt(1000L);
DataReference stale = ref(entityId, 999L);
DataReference atAnchor = ref(entityId, 1000L);
DataReference fresh = ref(entityId, 1001L);
when(spaces.get(marker, entityId)).thenReturn(Future.succeededFuture(null));
when(spaces.get(marker, parent)).thenReturn(Future.succeededFuture(parentSpace));
List<DataReference> result = await(resolver.filterStaleForEntity(marker, entityId, List.of(stale, atAnchor, fresh)));
assertThat(result).containsExactly(stale);
}
@Test
void filterForEntity_shouldNotTrimPlainMapHrnToNamespace_whenDirectSpaceIsMissing() {
String entityId = "hrn:here:data::olp-here:someMap";
String wrongParent = "hrn:here:data::olp-here";
DataReference older = ref(entityId, 100L);
DataReference newer = ref(entityId, 200L);
when(spaces.get(marker, entityId)).thenReturn(Future.succeededFuture(null));
List<DataReference> result = await(resolver.filterForEntity(marker, entityId, List.of(older, newer), false));
assertThat(result).containsExactly(newer);
verify(spaces).get(marker, entityId);
verify(spaces, never()).get(marker, wrongParent);
}
@Test
void filterForEntity_shouldReturnDistinctNewestPerUniquenessKey_whenOnlyStaleIsFalse() {
String entityId = "entity-id-1";
Space space = spaceWithCreatedAt(150L);
DataReference keyAOld = ref(entityId, 180L)
.withEndVersion(10)
.withObjectType("features")
.withContentType("ct-a")
.withSourceSystem("src")
.withTargetSystem("tgt");
DataReference keyANew = ref(entityId, 220L)
.withEndVersion(10)
.withObjectType("features")
.withContentType("ct-a")
.withSourceSystem("src")
.withTargetSystem("tgt");
DataReference keyB = ref(entityId, 200L)
.withEndVersion(11)
.withObjectType("features")
.withContentType("ct-b")
.withSourceSystem("src")
.withTargetSystem("tgt");
when(spaces.get(marker, entityId)).thenReturn(Future.succeededFuture(space));
List<DataReference> result = await(resolver.filterForEntity(marker, entityId, List.of(keyAOld, keyANew, keyB), false));
assertThat(result).containsExactlyInAnyOrder(keyANew, keyB);
}
@Test
void filterForEntity_shouldReturnDistinctNewestPerUniquenessKey_whenAnchorIsMissing_andOnlyStaleIsFalse() {
String entityId = "entity-id-1";
DataReference keyAOld = ref(entityId, 100L)
.withEndVersion(10)
.withObjectType("features")
.withContentType("ct-a")
.withSourceSystem("src")
.withTargetSystem("tgt");
DataReference keyANew = ref(entityId, 200L)
.withEndVersion(10)
.withObjectType("features")
.withContentType("ct-a")
.withSourceSystem("src")
.withTargetSystem("tgt");
DataReference keyB = ref(entityId, 150L)
.withEndVersion(20)
.withObjectType("features")
.withContentType("ct-b")
.withSourceSystem("src")
.withTargetSystem("tgt");
when(spaces.get(marker, entityId)).thenReturn(Future.succeededFuture(null));
List<DataReference> result = await(resolver.filterForEntity(marker, entityId, List.of(keyAOld, keyANew, keyB), false));
assertThat(result).containsExactlyInAnyOrder(keyANew, keyB);
}
@Test
void loadById_shouldReturnEmpty_whenReferenceIsExpired() {
UUID id = UUID.randomUUID();
long expiredKeepUntil = System.currentTimeMillis() - 1000;
DataReference expired = ref("entity-id-1", 100L).withId(id).withKeepUntil(expiredKeepUntil);
when(references.load(id)).thenReturn(Future.succeededFuture(Optional.of(expired)));
Optional<DataReference> result = await(resolver.loadById(marker, id, false));
assertThat(result).isEmpty();
verifyNoInteractions(spaces);
}
@Test
void filterForEntity_shouldExcludeExpiredReferences() {
String entityId = "entity-id-1";
long expiredKeepUntil = System.currentTimeMillis() - 1000;
long futureKeepUntil = System.currentTimeMillis() + 60_000;
DataReference expired = ref(entityId, 100L).withKeepUntil(expiredKeepUntil);
DataReference valid = ref(entityId, 200L).withKeepUntil(futureKeepUntil);
when(spaces.get(marker, entityId)).thenReturn(Future.succeededFuture(null));
List<DataReference> result = await(resolver.filterForEntity(marker, entityId, List.of(expired, valid), false));
assertThat(result).containsExactly(valid);
}
private static DataReference ref(String entityId, Long createdAt) {
DataReference r = new DataReference().withEntityId(entityId);
if (createdAt != null) {
r.withCreatedAt(createdAt);
}
return r;
}
private static Space spaceWithCreatedAt(long createdAt) {
Space s = new Space();
s.setCreatedAt(createdAt);
return s;
}
private static <T> T await(Future<T> f) {
return f.toCompletionStage().toCompletableFuture().join();
}
}