-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SNOW-1708304: Download stream from git repository
- Loading branch information
1 parent
f425350
commit 2bdf3fd
Showing
3 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/test/java/net/snowflake/client/jdbc/GitRepositoryDownloadLatestIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package net.snowflake.client.jdbc; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.List; | ||
import net.snowflake.client.category.TestCategoryOthers; | ||
import org.apache.commons.io.IOUtils; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
@Category(TestCategoryOthers.class) | ||
public class GitRepositoryDownloadLatestIT extends BaseJDBCWithSharedConnectionIT { | ||
@Test | ||
public void shouldDownloadFileAndStreamFromGitRepository() throws Exception { | ||
prepareJdbcRepoInSnowflake(); | ||
|
||
String stageName = | ||
String.format("@%s.%s.JDBC", connection.getCatalog(), connection.getSchema()); | ||
String fileName = ".pre-commit-config.yaml"; | ||
String filePathInGitRepo = "branches/master/" + fileName; | ||
|
||
List<String> fetchedFileContent = getContentFromFile(stageName, filePathInGitRepo, fileName); | ||
|
||
List<String> fetchedStreamContent = getContentFromStream(stageName, filePathInGitRepo); | ||
|
||
assertFalse("File content cannot be empty", fetchedFileContent.isEmpty()); | ||
assertFalse("Stream content cannot be empty", fetchedStreamContent.isEmpty()); | ||
assertEquals(fetchedFileContent, fetchedStreamContent); | ||
} | ||
|
||
private static void prepareJdbcRepoInSnowflake() throws SQLException { | ||
try (Statement statement = connection.createStatement()) { | ||
statement.execute( | ||
"CREATE OR REPLACE API INTEGRATION gh_integration\n" | ||
+ " API_PROVIDER = git_https_api\n" | ||
+ " API_ALLOWED_PREFIXES = ('https://github.com/snowflakedb/snowflake-jdbc.git')\n" | ||
+ " ENABLED = TRUE;"); | ||
statement.execute( | ||
"CREATE OR REPLACE GIT REPOSITORY jdbc\n" | ||
+ "ORIGIN = 'https://github.com/snowflakedb/snowflake-jdbc.git'\n" | ||
+ "API_INTEGRATION = gh_integration;"); | ||
} | ||
} | ||
|
||
private static List<String> getContentFromFile( | ||
String stageName, String filePathInGitRepo, String fileName) | ||
throws IOException, SQLException { | ||
Path tempDir = Files.createTempDirectory("git"); | ||
String stagePath = stageName + "/" + filePathInGitRepo; | ||
Path downloadedFile = tempDir.resolve(fileName); | ||
String command = String.format("GET '%s' '%s'", stagePath, tempDir.toUri()); | ||
|
||
try (Statement statement = connection.createStatement(); | ||
ResultSet rs = statement.executeQuery(command); ) { | ||
// then | ||
assertTrue("has result", rs.next()); | ||
return Files.readAllLines(downloadedFile); | ||
} finally { | ||
Files.delete(downloadedFile); | ||
Files.delete(tempDir); | ||
} | ||
} | ||
|
||
private static List<String> getContentFromStream(String stageName, String filePathInGitRepo) | ||
throws SQLException, IOException { | ||
SnowflakeConnection unwrap = connection.unwrap(SnowflakeConnection.class); | ||
try (InputStream inputStream = unwrap.downloadStream(stageName, filePathInGitRepo, false)) { | ||
return IOUtils.readLines(inputStream, StandardCharsets.UTF_8); | ||
} | ||
} | ||
} |