Skip to content

Commit 7a8a8bd

Browse files
authored
Merge pull request #52 from simplelocalize/truncate-file-on-write
Truncate files before writing
2 parents 6545db0 + 7635af9 commit 7a8a8bd

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

junit/truncate/file.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sample

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>io.simplelocalize</groupId>
66
<artifactId>simplelocalize-cli</artifactId>
7-
<version>2.0.1</version>
7+
<version>2.0.2</version>
88
<packaging>jar</packaging>
99
<name>simplelocalize-cli</name>
1010
<description>Official SimpleLocalize Command Line Interface</description>

src/main/java/io/simplelocalize/cli/Version.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class Version
44
{
55

6-
public static final String NUMBER = "2.0.1";
6+
public static final String NUMBER = "2.0.2";
77

88
private Version()
99
{

src/main/java/io/simplelocalize/cli/client/SimpleLocalizeClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.net.http.HttpResponse;
1919
import java.nio.file.Files;
2020
import java.nio.file.Path;
21+
import java.nio.file.StandardOpenOption;
2122
import java.time.Duration;
2223
import java.util.Collection;
2324
import java.util.List;
@@ -111,7 +112,7 @@ public void downloadFile(DownloadableFile downloadableFile, String downloadPathT
111112
Files.createDirectories(parentDirectory);
112113
}
113114
log.info(" 🌍 Downloading {}", savePath);
114-
httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofFile(savePath));
115+
httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofFile(savePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING));
115116
} catch (IOException e)
116117
{
117118
log.error(" 😝 Download failed: {}", savePath, e);

src/test/java/io/simplelocalize/cli/client/SimpleLocalizeClientTest.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import static io.simplelocalize.cli.client.dto.DownloadRequest.DownloadRequestBuilder.aDownloadRequest;
2323
import static io.simplelocalize.cli.client.dto.UploadRequest.UploadFileRequestBuilder.anUploadFileRequest;
24+
import static org.assertj.core.api.Assertions.assertThat;
2425
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
2526
import static org.mockserver.model.HttpRequest.request;
2627
import static org.mockserver.model.HttpResponse.response;
@@ -251,12 +252,42 @@ void shouldDownloadS3File() throws Exception
251252
DownloadableFile downloadableFile = new DownloadableFile();
252253
downloadableFile.setUrl(MOCK_SERVER_BASE_URL + "/s3/file");
253254
downloadableFile.setNamespace("common");
254-
String downloadPath = "file.json";
255+
String downloadPath = "./junit/download-test/file.json";
255256

256257
//when
257258
client.downloadFile(downloadableFile, downloadPath);
258259

259260
//then
261+
assertThat(Path.of(downloadPath)).isRegularFile();
262+
}
263+
264+
@Test
265+
void shouldDownloadAndTruncateBeforeWriting() throws Exception
266+
{
267+
//given
268+
SimpleLocalizeClient client = new SimpleLocalizeClient(MOCK_SERVER_BASE_URL, "96a7b6ca75c79d4af4dfd5db2946fdd4");
269+
mockServer.when(request()
270+
.withMethod("GET")
271+
.withPath("/s3/file"),
272+
Times.exactly(1))
273+
.respond(
274+
response()
275+
.withStatusCode(200)
276+
.withContentType(MediaType.APPLICATION_JSON_UTF_8)
277+
.withBody("sample".getBytes(StandardCharsets.UTF_8))
278+
.withDelay(TimeUnit.MILLISECONDS, 200)
279+
);
280+
281+
DownloadableFile downloadableFile = new DownloadableFile();
282+
downloadableFile.setUrl(MOCK_SERVER_BASE_URL + "/s3/file");
283+
downloadableFile.setNamespace("common");
284+
String downloadPath = "./junit/truncate/file.json";
285+
286+
//when
287+
client.downloadFile(downloadableFile, downloadPath);
288+
289+
//then
290+
assertThat(Path.of(downloadPath)).hasContent("sample").isRegularFile();
260291
}
261292

262293
@Test
@@ -280,6 +311,6 @@ void shouldValidateQuality() throws Exception
280311
int validateGate = client.validateGate();
281312

282313
//then
283-
Assertions.assertThat(validateGate).isEqualTo(200);
314+
assertThat(validateGate).isEqualTo(200);
284315
}
285316
}

0 commit comments

Comments
 (0)