-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCachingCodaClientTest.java
More file actions
119 lines (97 loc) · 3.87 KB
/
CachingCodaClientTest.java
File metadata and controls
119 lines (97 loc) · 3.87 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
/*
* Copyright (c) 2022, iland Internet Solutions, Corp
*
* This software is licensed under the Terms and Conditions contained within the
* "LICENSE.txt" file that accompanied this software. Any inquiries concerning
* the scope or enforceability of the license should be addressed to:
*
* iland Internet Solutions, Corp
* 1235 North Loop West, Suite 800
* Houston, TX 77008
* USA
*
* http://www.iland.com
*/
package com.iland.coda.footprint;
import static com.iland.coda.footprint.TestValues.TEST_DESCRIPTION;
import static com.iland.coda.footprint.TestValues.TEST_LABEL;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import com.google.common.base.Stopwatch;
import net.codacloud.model.RegistrationEditRequest;
import net.codacloud.model.RegistrationLight;
import org.junit.jupiter.api.Test;
class CachingCodaClientTest {
@Test
void testThatRegistrationsAreCached() throws Throwable {
final CodaClient client = Clients.cachingCodaClient.login();
testThatResultWasCached(client::listRegistrations);
}
@Test
void testThatCreateAndDeleteRegistrationInvalidatesCache()
throws Throwable {
final CachingCodaClient client =
(CachingCodaClient) Clients.cachingCodaClient.login();
final Runnable assertAccountCacheWasInvalidated =
() -> assertTrue(client.getAccountCache().isEmpty(),
"cached collection must be empty after being invalidated");
final RegistrationLight registration =
client.createRegistration(TEST_LABEL, TEST_DESCRIPTION);
assertAccountCacheWasInvalidated.run();
boolean containsRegistration = client.listRegistrations().stream()
.anyMatch(r -> Objects.equals(r.getId(), registration.getId()));
try {
assertTrue(containsRegistration,
"new registration must be present in cached collection");
} finally {
client.deleteRegistration(registration);
assertAccountCacheWasInvalidated.run();
}
containsRegistration =
client.listRegistrations().stream().map(RegistrationLight::getId)
.anyMatch(id -> Objects.equals(id, registration.getId()));
assertFalse(containsRegistration,
"deleted registration must not be present in cached collection");
}
@Test
void testThatAccountsAreCached() throws Throwable {
final CodaClient client = Clients.cachingCodaClient.login();
testThatResultWasCached(() -> client.listAccounts(null));
}
@Test
void testThatScannersAreCached() throws Throwable {
final CodaClient client = Clients.cachingCodaClient.login();
testThatResultWasCached(() -> client.getScanners(null));
}
private static <R> void testThatResultWasCached(
final Callable<Collection<R>> callable) throws Exception {
final Collection<R> collection = callable.call();
assertFalse(collection.isEmpty(),
"cached collection must not be empty");
final Stopwatch stopwatch = Stopwatch.createStarted();
callable.call();
final long elapsedMillis = stopwatch.elapsed(TimeUnit.MILLISECONDS);
assertEquals(0, elapsedMillis,
"cached collection must return instantly");
}
@Test
void testRegistrationEditReplacesCachedRegistration() throws Throwable {
final CodaClient client = Clients.cachingCodaClient.login();
final RegistrationLight registration =
client.findOrCreateRegistration(TEST_LABEL, TEST_DESCRIPTION);
final String description = "foo";
client.updateRegistration(registration.getId(),
new RegistrationEditRequest().description(description).manageType(
RegistrationEditRequest.ManageTypeEnum.FULLY_MANAGED));
final Optional<RegistrationLight> optionalRegistration =
client.getRegistrationForLabel(TEST_LABEL);
assertTrue(optionalRegistration.isPresent(),
"test registration must be present");
assertEquals(description, optionalRegistration.get().getDescription(),
"cached registration description must match new description");
}
}