|
11 | 11 | import org.junit.jupiter.api.Test; |
12 | 12 | import org.junit.jupiter.api.io.TempDir; |
13 | 13 |
|
| 14 | +import java.io.ByteArrayInputStream; |
14 | 15 | import java.io.IOException; |
15 | 16 | import java.nio.file.Files; |
16 | 17 | import java.nio.file.Path; |
17 | 18 | import java.nio.file.StandardOpenOption; |
18 | 19 | import java.nio.file.StandardWatchEventKinds; |
19 | 20 | import java.nio.file.WatchService; |
20 | 21 | import java.time.Duration; |
| 22 | +import java.time.Instant; |
| 23 | +import java.util.Properties; |
21 | 24 | import java.util.concurrent.CompletableFuture; |
22 | 25 | import java.util.concurrent.ConcurrentHashMap; |
23 | 26 | import java.util.concurrent.ConcurrentMap; |
|
29 | 32 | import static org.mockito.ArgumentMatchers.eq; |
30 | 33 | import static org.mockito.Mockito.doAnswer; |
31 | 34 | import static org.mockito.Mockito.mock; |
| 35 | +import static org.mockito.Mockito.never; |
| 36 | +import static org.mockito.Mockito.verify; |
32 | 37 |
|
33 | 38 | public class RealUseTokenTest { |
34 | 39 |
|
@@ -75,6 +80,21 @@ public void testFileCreation() { |
75 | 80 | Assertions.assertTrue(Files.notExists(filePath)); |
76 | 81 | } |
77 | 82 |
|
| 83 | + @Test |
| 84 | + @DisplayName("The properties file contains required keys with valid content") |
| 85 | + public void testFileContent() throws IOException { |
| 86 | + var filePath = tmpDir.resolve("inUse.file"); |
| 87 | + try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, tokenPersistor, StandardOpenOption.CREATE_NEW, encWrapper)) { |
| 88 | + Awaitility.await().atLeast(FILE_OPERATION_DELAY).atMost(FILE_OPERATION_MAX).until(() -> Files.exists(filePath)); |
| 89 | + |
| 90 | + var props = new Properties(); |
| 91 | + var rawProps = Files.readAllBytes(filePath); |
| 92 | + props.load(new ByteArrayInputStream(rawProps)); |
| 93 | + Assertions.assertEquals("test3000", props.getProperty(UseToken.OWNER_KEY)); |
| 94 | + Assertions.assertDoesNotThrow(() -> Instant.parse(props.getProperty(UseToken.LASTUPDATED_KEY))); |
| 95 | + } |
| 96 | + } |
| 97 | + |
78 | 98 | @Test |
79 | 99 | @DisplayName("After X seconds of token creation, a file is updated") |
80 | 100 | public void testFileSteal() throws IOException { |
@@ -198,4 +218,40 @@ public void testMoveToClosed() throws IOException { |
198 | 218 | Assertions.assertNull(useTokens.get(targetPath)); |
199 | 219 | } |
200 | 220 | } |
| 221 | + |
| 222 | + @Test |
| 223 | + @DisplayName("After token persisting, refreshing a token modifies content") |
| 224 | + public void testFileRefresh() throws IOException { |
| 225 | + var filePath = tmpDir.resolve("inUse.file"); |
| 226 | + |
| 227 | + try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, tokenPersistor, StandardOpenOption.CREATE_NEW, encWrapper)) { |
| 228 | + Awaitility.await().atLeast(FILE_OPERATION_DELAY).atMost(FILE_OPERATION_MAX).until(() -> Files.exists(filePath)); |
| 229 | + |
| 230 | + var props = new Properties(); |
| 231 | + var rawProps = Files.readAllBytes(filePath); |
| 232 | + props.load(new ByteArrayInputStream(rawProps)); |
| 233 | + var oldLastUpdated = Instant.parse(props.getProperty(UseToken.LASTUPDATED_KEY)); |
| 234 | + |
| 235 | + token.refresh(); |
| 236 | + |
| 237 | + var props2 = new Properties(); |
| 238 | + rawProps = Files.readAllBytes(filePath); |
| 239 | + props2.load(new ByteArrayInputStream(rawProps)); |
| 240 | + |
| 241 | + Assertions.assertEquals("test3000", props2.getProperty(UseToken.OWNER_KEY)); |
| 242 | + var newLastUpdated = Instant.parse(props2.getProperty(UseToken.LASTUPDATED_KEY)); |
| 243 | + Assertions.assertTrue(newLastUpdated.isAfter(oldLastUpdated)); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + @DisplayName("Before token persisting, refreshing a token does nothing") |
| 249 | + public void testFileRefreshSkip() throws IOException { |
| 250 | + var filePath = tmpDir.resolve("inUse.file"); |
| 251 | + |
| 252 | + try (var token = new RealUseToken(filePath, "test3000", cryptor, useTokens, tokenPersistor, StandardOpenOption.CREATE_NEW, encWrapper)) { |
| 253 | + token.refresh(); |
| 254 | + verify(encWrapper, never()).wrapWithEncryption(any(), eq(cryptor)); |
| 255 | + } |
| 256 | + } |
201 | 257 | } |
0 commit comments