|
7 | 7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
8 | 8 | import static org.junit.jupiter.api.Assertions.fail;
|
9 | 9 |
|
| 10 | +import java.io.BufferedReader; |
10 | 11 | import java.io.BufferedWriter;
|
11 | 12 | import java.io.File;
|
12 | 13 | import java.io.FileWriter;
|
13 | 14 | import java.io.IOException;
|
14 | 15 | import java.io.InputStream;
|
| 16 | +import java.io.InputStreamReader; |
15 | 17 | import java.io.StringWriter;
|
16 | 18 | import java.nio.charset.StandardCharsets;
|
17 | 19 | import java.sql.Connection;
|
18 | 20 | import java.sql.ResultSet;
|
19 | 21 | import java.sql.SQLException;
|
20 | 22 | import java.sql.Statement;
|
21 | 23 | import java.util.Properties;
|
| 24 | +import java.util.UUID; |
| 25 | +import java.util.stream.Collectors; |
22 | 26 | import net.snowflake.client.annotations.DontRunOnGithubActions;
|
23 | 27 | import net.snowflake.client.category.TestTags;
|
24 | 28 | import org.apache.commons.io.IOUtils;
|
@@ -237,4 +241,72 @@ public void testSpecialCharactersInFileName() throws SQLException, IOException {
|
237 | 241 | }
|
238 | 242 | }
|
239 | 243 | }
|
| 244 | + |
| 245 | + @Test |
| 246 | + public void shouldDownloadStreamInDeterministicWay() throws Exception { |
| 247 | + try (Connection conn = getConnection(); |
| 248 | + Statement stat = conn.createStatement()) { |
| 249 | + String randomStage = "test" + UUID.randomUUID().toString().replaceAll("-", ""); |
| 250 | + try { |
| 251 | + stat.execute("CREATE OR REPLACE STAGE " + randomStage); |
| 252 | + String randomDir = UUID.randomUUID().toString(); |
| 253 | + String sourceFilePathWithoutExtension = getClass().getResource("/test_file").getPath(); |
| 254 | + String sourceFilePathWithExtension = getClass().getResource("/test_file.csv").getPath(); |
| 255 | + String stageDest = String.format("@%s/%s", randomStage, randomDir); |
| 256 | + putFile(stat, sourceFilePathWithExtension, stageDest, false); |
| 257 | + putFile(stat, sourceFilePathWithoutExtension, stageDest, false); |
| 258 | + putFile(stat, sourceFilePathWithExtension, stageDest, true); |
| 259 | + putFile(stat, sourceFilePathWithoutExtension, stageDest, true); |
| 260 | + expectsFilesOnStage(stat, stageDest, 4); |
| 261 | + String stageName = "@" + randomStage; |
| 262 | + downloadStreamExpectingContent( |
| 263 | + conn, stageName, randomDir + "/test_file.gz", true, "I am a file without extension"); |
| 264 | + downloadStreamExpectingContent( |
| 265 | + conn, stageName, randomDir + "/test_file.csv.gz", true, "I am a file with extension"); |
| 266 | + downloadStreamExpectingContent( |
| 267 | + conn, stageName, randomDir + "/test_file", false, "I am a file without extension"); |
| 268 | + downloadStreamExpectingContent( |
| 269 | + conn, stageName, randomDir + "/test_file.csv", false, "I am a file with extension"); |
| 270 | + } finally { |
| 271 | + stat.execute("DROP STAGE IF EXISTS " + randomStage); |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + private static void downloadStreamExpectingContent( |
| 277 | + Connection conn, |
| 278 | + String stageName, |
| 279 | + String fileName, |
| 280 | + boolean decompress, |
| 281 | + String expectedFileContent) |
| 282 | + throws IOException, SQLException { |
| 283 | + try (InputStream inputStream = |
| 284 | + conn.unwrap(SnowflakeConnectionV1.class) |
| 285 | + .downloadStream(stageName, fileName, decompress); |
| 286 | + InputStreamReader isr = new InputStreamReader(inputStream); |
| 287 | + BufferedReader br = new BufferedReader(isr)) { |
| 288 | + String content = br.lines().collect(Collectors.joining("\n")); |
| 289 | + assertEquals(expectedFileContent, content); |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + private static void expectsFilesOnStage(Statement stat, String stageDest, int expectCount) |
| 294 | + throws SQLException { |
| 295 | + int filesInStageDir = 0; |
| 296 | + try (ResultSet rs = stat.executeQuery("LIST " + stageDest)) { |
| 297 | + while (rs.next()) { |
| 298 | + ++filesInStageDir; |
| 299 | + } |
| 300 | + } |
| 301 | + assertEquals(expectCount, filesInStageDir); |
| 302 | + } |
| 303 | + |
| 304 | + private static boolean putFile( |
| 305 | + Statement stat, String localFileName, String stageDest, boolean autoCompress) |
| 306 | + throws SQLException { |
| 307 | + return stat.execute( |
| 308 | + String.format( |
| 309 | + "PUT file://%s %s AUTO_COMPRESS=%s", |
| 310 | + localFileName, stageDest, String.valueOf(autoCompress).toUpperCase())); |
| 311 | + } |
240 | 312 | }
|
0 commit comments