30
30
31
31
class FileSystemStorageTest {
32
32
33
+ static final String TOPIC_PARTITION_SEGMENT_KEY = "topic/partition/log" ;
34
+
33
35
@ TempDir
34
36
Path root ;
35
37
@@ -57,59 +59,61 @@ void testRootCannotBeNonWritableDirectory() throws IOException {
57
59
void testUploadANewFile () throws IOException {
58
60
final FileSystemStorage storage = new FileSystemStorage (root , false );
59
61
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 );
62
63
63
- assertThat (content ).isEqualTo (Files .readString (root .resolve (key )));
64
+ assertThat (content ).isEqualTo (Files .readString (root .resolve (TOPIC_PARTITION_SEGMENT_KEY )));
64
65
}
65
66
66
67
@ Test
67
68
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 () );
70
71
Files .writeString (previous , "previous" );
71
72
final FileSystemStorage storage = new FileSystemStorage (root , false );
72
73
final String content = "content" ;
73
74
74
- assertThatThrownBy (() -> storage .upload (new ByteArrayInputStream (content .getBytes ()), key ))
75
+ assertThatThrownBy (() -> storage .upload (new ByteArrayInputStream (content .getBytes ()),
76
+ TOPIC_PARTITION_SEGMENT_KEY ))
75
77
.isInstanceOf (IOException .class )
76
78
.hasMessage ("File " + previous + " already exists" );
77
79
}
78
80
79
81
@ Test
80
82
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 () );
83
85
Files .writeString (previous , "previous" );
84
86
final FileSystemStorage storage = new FileSystemStorage (root , true );
85
87
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 )));
88
90
}
89
91
90
92
@ Test
91
93
void testFetchAll () throws IOException {
92
- final String key = "key" ;
93
94
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 );
95
98
final FileSystemStorage storage = new FileSystemStorage (root , true );
96
99
97
- try (final InputStream fetch = storage .fetch (key )) {
100
+ try (final InputStream fetch = storage .fetch (TOPIC_PARTITION_SEGMENT_KEY )) {
98
101
assertThat (content .getBytes ()).isEqualTo (fetch .readAllBytes ());
99
102
}
100
103
}
101
104
102
105
@ Test
103
106
void testFetchWithOffsetRange () throws IOException {
104
- final String key = "key" ;
105
107
final String content = "AABBBBAA" ;
106
108
final int from = 2 ;
107
109
final int to = 6 ;
108
110
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 );
110
114
final FileSystemStorage storage = new FileSystemStorage (root , true );
111
115
112
- try (final InputStream fetch = storage .fetch (key , from , to )) {
116
+ try (final InputStream fetch = storage .fetch (TOPIC_PARTITION_SEGMENT_KEY , from , to )) {
113
117
assertThat (range .getBytes ()).isEqualTo (fetch .readAllBytes ());
114
118
}
115
119
}
@@ -118,51 +122,38 @@ void testFetchWithOffsetRange() throws IOException {
118
122
void testFetchWithFailsWithWrongOffsets () {
119
123
final FileSystemStorage storage = new FileSystemStorage (root , true );
120
124
121
- assertThatThrownBy (() -> storage .fetch ("key" , -1 , 1 ))
125
+ assertThatThrownBy (() -> storage .fetch (TOPIC_PARTITION_SEGMENT_KEY , -1 , 1 ))
122
126
.isInstanceOf (IllegalArgumentException .class )
123
127
.hasMessage ("from cannot be negative, -1 given" );
124
- assertThatThrownBy (() -> storage .fetch ("key" , 2 , 1 ))
128
+ assertThatThrownBy (() -> storage .fetch (TOPIC_PARTITION_SEGMENT_KEY , 2 , 1 ))
125
129
.isInstanceOf (IllegalArgumentException .class )
126
130
.hasMessage ("from cannot be more than to, from=2, to=1 given" );
127
131
}
128
132
129
133
@ Test
130
134
void testFetchWithOffsetRangeLargerThanFileSize () throws IOException {
131
- final String key = "key" ;
132
135
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 );
134
139
final FileSystemStorage storage = new FileSystemStorage (root , true );
135
140
136
- assertThatThrownBy (() -> storage .fetch (key , 0 , content .length () + 1 ))
141
+ assertThatThrownBy (() -> storage .fetch (TOPIC_PARTITION_SEGMENT_KEY , 0 , content .length () + 1 ))
137
142
.isInstanceOf (IllegalArgumentException .class )
138
143
.hasMessage ("to cannot be bigger than file size, to=8, file size=7 given" );
139
144
}
140
145
141
146
@ Test
142
147
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 () );
145
150
Files .writeString (keyPath , "test" );
146
151
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 );
152
153
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
166
157
assertThat (Files .exists (root )).isTrue ();
167
158
}
168
159
0 commit comments