Skip to content

Commit bb0ead9

Browse files
committed
Fixed wiremock#2388 - empty getPath() returned from new FileStore implementation passed to transformers
1 parent 91b058b commit bb0ead9

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-3
lines changed

src/main/java/com/github/tomakehurst/wiremock/store/files/BlobStoreFileSource.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public FileSource child(String subDirectoryName) {
5454

5555
@Override
5656
public String getPath() {
57+
if (blobStore instanceof PathBased) {
58+
return ((PathBased) blobStore).getPath();
59+
}
60+
5761
return "";
5862
}
5963

src/main/java/com/github/tomakehurst/wiremock/store/files/FileSourceBlobStore.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.wiremock.annotations.Beta;
2525

2626
@Beta(justification = "Externalized State API: https://github.com/wiremock/wiremock/issues/2144")
27-
public class FileSourceBlobStore implements BlobStore {
27+
public class FileSourceBlobStore implements BlobStore, PathBased {
2828

2929
private final FileSource fileSource;
3030

@@ -73,4 +73,13 @@ public void remove(String key) {
7373
public void clear() {
7474
fileSource.listFilesRecursively().forEach(file -> fileSource.deleteFile(file.getPath()));
7575
}
76+
77+
public FileSource getFileSource() {
78+
return fileSource;
79+
}
80+
81+
@Override
82+
public String getPath() {
83+
return fileSource.getPath();
84+
}
7685
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2023 Thomas Akehurst
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.tomakehurst.wiremock.store.files;
17+
18+
import org.wiremock.annotations.Beta;
19+
20+
@Beta(justification = "Externalized State API: https://github.com/wiremock/wiremock/issues/2144")
21+
public interface PathBased {
22+
23+
String getPath();
24+
}

src/test/java/com/github/tomakehurst/wiremock/store/files/BlobStoreFileSourceTest.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@
2020
import static org.hamcrest.MatcherAssert.assertThat;
2121
import static org.hamcrest.Matchers.is;
2222

23+
import com.github.tomakehurst.wiremock.common.FileSource;
24+
import com.github.tomakehurst.wiremock.common.InputStreamSource;
2325
import com.github.tomakehurst.wiremock.common.TextFile;
2426
import com.github.tomakehurst.wiremock.store.BlobStore;
27+
import java.io.InputStream;
2528
import java.nio.file.Files;
2629
import java.nio.file.Path;
2730
import java.util.List;
31+
import java.util.Optional;
32+
import java.util.stream.Stream;
2833
import org.junit.jupiter.api.BeforeEach;
2934
import org.junit.jupiter.api.Test;
3035
import org.junit.jupiter.api.io.TempDir;
@@ -130,7 +135,45 @@ void delete_all_files(@TempDir Path tempDir) {
130135
}
131136

132137
@Test
133-
void text_file_path() {
134-
// fileSource.getPath()
138+
void returns_file_path_when_backed_by_file_source() {
139+
assertThat(fileSource.getPath(), is(ROOT_PATH));
140+
}
141+
142+
@Test
143+
void returns_empty_string_when_not_backed_by_file_source() {
144+
FileSource otherFileSource = new BlobStoreFileSource(new TestInMemoryBlobStore());
145+
assertThat(otherFileSource.getPath(), is(""));
146+
}
147+
148+
static class TestInMemoryBlobStore implements BlobStore {
149+
150+
@Override
151+
public Optional<InputStream> getStream(String key) {
152+
return Optional.empty();
153+
}
154+
155+
@Override
156+
public InputStreamSource getStreamSource(String key) {
157+
return null;
158+
}
159+
160+
@Override
161+
public Stream<String> getAllKeys() {
162+
return null;
163+
}
164+
165+
@Override
166+
public Optional<byte[]> get(String key) {
167+
return Optional.empty();
168+
}
169+
170+
@Override
171+
public void put(String key, byte[] content) {}
172+
173+
@Override
174+
public void remove(String key) {}
175+
176+
@Override
177+
public void clear() {}
135178
}
136179
}

0 commit comments

Comments
 (0)