Skip to content

Commit fc6585d

Browse files
committed
refactor: move key to constant
1 parent a031926 commit fc6585d

File tree

1 file changed

+32
-41
lines changed

1 file changed

+32
-41
lines changed

commons/src/test/java/io/aiven/kafka/tieredstorage/commons/storage/filesystem/FileSystemStorageTest.java

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
class FileSystemStorageTest {
3232

33+
static final String TOPIC_PARTITION_SEGMENT_KEY = "topic/partition/log";
34+
3335
@TempDir
3436
Path root;
3537

@@ -57,59 +59,61 @@ void testRootCannotBeNonWritableDirectory() throws IOException {
5759
void testUploadANewFile() throws IOException {
5860
final FileSystemStorage storage = new FileSystemStorage(root, false);
5961
final String content = "content";
60-
final String key = "key";
61-
storage.upload(new ByteArrayInputStream(content.getBytes()), key);
62+
storage.upload(new ByteArrayInputStream(content.getBytes()), TOPIC_PARTITION_SEGMENT_KEY);
6263

63-
assertThat(content).isEqualTo(Files.readString(root.resolve(key)));
64+
assertThat(content).isEqualTo(Files.readString(root.resolve(TOPIC_PARTITION_SEGMENT_KEY)));
6465
}
6566

6667
@Test
6768
void testUploadFailsWhenFileExists() throws IOException {
68-
final String key = "key";
69-
final Path previous = root.resolve(key);
69+
final Path previous = root.resolve(TOPIC_PARTITION_SEGMENT_KEY);
70+
Files.createDirectories(previous.getParent());
7071
Files.writeString(previous, "previous");
7172
final FileSystemStorage storage = new FileSystemStorage(root, false);
7273
final String content = "content";
7374

74-
assertThatThrownBy(() -> storage.upload(new ByteArrayInputStream(content.getBytes()), key))
75+
assertThatThrownBy(() -> storage.upload(new ByteArrayInputStream(content.getBytes()),
76+
TOPIC_PARTITION_SEGMENT_KEY))
7577
.isInstanceOf(IOException.class)
7678
.hasMessage("File " + previous + " already exists");
7779
}
7880

7981
@Test
8082
void testUploadWithOverridesWhenFileExists() throws IOException {
81-
final String key = "key";
82-
final Path previous = root.resolve(key);
83+
final Path previous = root.resolve(TOPIC_PARTITION_SEGMENT_KEY);
84+
Files.createDirectories(previous.getParent());
8385
Files.writeString(previous, "previous");
8486
final FileSystemStorage storage = new FileSystemStorage(root, true);
8587
final String content = "content";
86-
storage.upload(new ByteArrayInputStream(content.getBytes()), key);
87-
assertThat(content).isEqualTo(Files.readString(root.resolve(key)));
88+
storage.upload(new ByteArrayInputStream(content.getBytes()), TOPIC_PARTITION_SEGMENT_KEY);
89+
assertThat(content).isEqualTo(Files.readString(root.resolve(TOPIC_PARTITION_SEGMENT_KEY)));
8890
}
8991

9092
@Test
9193
void testFetchAll() throws IOException {
92-
final String key = "key";
9394
final String content = "content";
94-
Files.writeString(root.resolve(key), content);
95+
final Path keyPath = root.resolve(TOPIC_PARTITION_SEGMENT_KEY);
96+
Files.createDirectories(keyPath.getParent());
97+
Files.writeString(keyPath, content);
9598
final FileSystemStorage storage = new FileSystemStorage(root, true);
9699

97-
try (final InputStream fetch = storage.fetch(key)) {
100+
try (final InputStream fetch = storage.fetch(TOPIC_PARTITION_SEGMENT_KEY)) {
98101
assertThat(content.getBytes()).isEqualTo(fetch.readAllBytes());
99102
}
100103
}
101104

102105
@Test
103106
void testFetchWithOffsetRange() throws IOException {
104-
final String key = "key";
105107
final String content = "AABBBBAA";
106108
final int from = 2;
107109
final int to = 6;
108110
final String range = content.substring(from, to + 1); // exclusive
109-
Files.writeString(root.resolve(key), content);
111+
final Path keyPath = root.resolve(TOPIC_PARTITION_SEGMENT_KEY);
112+
Files.createDirectories(keyPath.getParent());
113+
Files.writeString(keyPath, content);
110114
final FileSystemStorage storage = new FileSystemStorage(root, true);
111115

112-
try (final InputStream fetch = storage.fetch(key, from, to)) {
116+
try (final InputStream fetch = storage.fetch(TOPIC_PARTITION_SEGMENT_KEY, from, to)) {
113117
assertThat(range.getBytes()).isEqualTo(fetch.readAllBytes());
114118
}
115119
}
@@ -118,51 +122,38 @@ void testFetchWithOffsetRange() throws IOException {
118122
void testFetchWithFailsWithWrongOffsets() {
119123
final FileSystemStorage storage = new FileSystemStorage(root, true);
120124

121-
assertThatThrownBy(() -> storage.fetch("key", -1, 1))
125+
assertThatThrownBy(() -> storage.fetch(TOPIC_PARTITION_SEGMENT_KEY, -1, 1))
122126
.isInstanceOf(IllegalArgumentException.class)
123127
.hasMessage("from cannot be negative, -1 given");
124-
assertThatThrownBy(() -> storage.fetch("key", 2, 1))
128+
assertThatThrownBy(() -> storage.fetch(TOPIC_PARTITION_SEGMENT_KEY, 2, 1))
125129
.isInstanceOf(IllegalArgumentException.class)
126130
.hasMessage("from cannot be more than to, from=2, to=1 given");
127131
}
128132

129133
@Test
130134
void testFetchWithOffsetRangeLargerThanFileSize() throws IOException {
131-
final String key = "key";
132135
final String content = "content";
133-
Files.writeString(root.resolve(key), content);
136+
final Path keyPath = root.resolve(TOPIC_PARTITION_SEGMENT_KEY);
137+
Files.createDirectories(keyPath.getParent());
138+
Files.writeString(keyPath, content);
134139
final FileSystemStorage storage = new FileSystemStorage(root, true);
135140

136-
assertThatThrownBy(() -> storage.fetch(key, 0, content.length() + 1))
141+
assertThatThrownBy(() -> storage.fetch(TOPIC_PARTITION_SEGMENT_KEY, 0, content.length() + 1))
137142
.isInstanceOf(IllegalArgumentException.class)
138143
.hasMessage("to cannot be bigger than file size, to=8, file size=7 given");
139144
}
140145

141146
@Test
142147
void testDelete() throws IOException {
143-
final String key = "key";
144-
final Path keyPath = root.resolve(key);
148+
final Path keyPath = root.resolve(TOPIC_PARTITION_SEGMENT_KEY);
149+
Files.createDirectories(keyPath.getParent());
145150
Files.writeString(keyPath, "test");
146151
final FileSystemStorage storage = new FileSystemStorage(root, false);
147-
storage.delete(key);
148-
149-
assertThat(Files.exists(keyPath)).isFalse();
150-
assertThat(Files.exists(root)).isTrue();
151-
}
152+
storage.delete(TOPIC_PARTITION_SEGMENT_KEY);
152153

153-
@Test
154-
void testDeleteRemoveParentEmptyDir() throws IOException {
155-
final String parent = "parent";
156-
final String key = "key";
157-
final Path parentPath = root.resolve(parent);
158-
Files.createDirectories(parentPath);
159-
final Path keyPath = parentPath.resolve(key);
160-
Files.writeString(keyPath, "test");
161-
final FileSystemStorage storage = new FileSystemStorage(root, false);
162-
storage.delete(parent + "/" + key);
163-
164-
assertThat(Files.exists(keyPath)).isFalse();
165-
assertThat(Files.exists(parentPath)).isFalse();
154+
assertThat(Files.exists(keyPath)).isFalse(); // segment key
155+
assertThat(Files.exists(keyPath.getParent())).isFalse(); // partition dir
156+
assertThat(Files.exists(keyPath.getParent().getParent())).isFalse(); // topic dir
166157
assertThat(Files.exists(root)).isTrue();
167158
}
168159

0 commit comments

Comments
 (0)