From 08ba24d330dbdb3ad8216de2c3cbb3567731a112 Mon Sep 17 00:00:00 2001 From: johnniang Date: Tue, 21 May 2019 23:45:39 +0800 Subject: [PATCH] Add git and github test --- gradle/wrapper/gradle-wrapper.properties | 2 +- src/test/java/run/halo/app/utils/GitTest.java | 62 +++++++++++++ .../java/run/halo/app/utils/GithubTest.java | 91 +++++++++++++++++++ 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/test/java/run/halo/app/utils/GitTest.java create mode 100644 src/test/java/run/halo/app/utils/GithubTest.java diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index eac4a2431..56c603f58 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip diff --git a/src/test/java/run/halo/app/utils/GitTest.java b/src/test/java/run/halo/app/utils/GitTest.java new file mode 100644 index 000000000..af62fc5c2 --- /dev/null +++ b/src/test/java/run/halo/app/utils/GitTest.java @@ -0,0 +1,62 @@ +package run.halo.app.utils; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.Status; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.errors.RepositoryNotFoundException; +import org.junit.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +/** + * Git test. + * + * @author johnniang + * @date 19-5-21 + */ +public class GitTest { + + private final Path tempPath; + + public GitTest() throws IOException { + tempPath = Files.createTempDirectory("git-test"); + } + + @Test(expected = RepositoryNotFoundException.class) + public void openTest() throws IOException { + Git.open(tempPath.toFile()); + } + + @Test + public void initTest() throws GitAPIException { + Git.init().setDirectory(tempPath.toFile()).call(); + } + + @Test + public void statusTest() throws GitAPIException { + Git git = Git.init().setDirectory(tempPath.toFile()).call(); + git.status().call(); + } + + @Test + public void cloneTest() throws GitAPIException { + cloneRepository(); + } + + @Test + public void pullTest() throws GitAPIException { + Git git = cloneRepository(); + git.pull().call(); + git.clean().call(); + git.close(); + } + + private Git cloneRepository() throws GitAPIException { + return Git.cloneRepository() + .setURI("https://github.com/halo-dev/halo-theme-pinghsu.git") + .setDirectory(tempPath.toFile()) + .call(); + } +} diff --git a/src/test/java/run/halo/app/utils/GithubTest.java b/src/test/java/run/halo/app/utils/GithubTest.java new file mode 100644 index 000000000..4305ac26a --- /dev/null +++ b/src/test/java/run/halo/app/utils/GithubTest.java @@ -0,0 +1,91 @@ +package run.halo.app.utils; + +import org.junit.Assert; +import org.junit.Test; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; +import run.halo.app.exception.BadRequestException; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.util.List; +import java.util.Map; + +/** + * Github api test. + * + * @author johnniang + * @date 19-5-21 + */ +public class GithubTest { + + private final Path tempPath; + + private final static String API_URL = "https://api.github.com/repos/halo-dev/halo-admin/releases/latest"; + + private final static String HALO_ADMIN_REGEX = "halo-admin-\\d+\\.\\d+(\\.\\d+)?(-\\S*)?\\.zip"; + + private final RestTemplate restTemplate; + + public GithubTest() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException { + tempPath = Files.createTempDirectory("git-test"); + this.restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(HttpClientUtils.createHttpsClient(5000))); + } + + @Test + public void getLatestReleaseTest() throws Throwable { + ResponseEntity responseEntity = restTemplate.getForEntity(API_URL, Map.class); + System.out.println("Reponse: " + responseEntity); + Object assetsObject = responseEntity.getBody().get("assets"); + System.out.println("Assets class: " + assetsObject.getClass()); + System.out.println("Assets: " + assetsObject); + if (assetsObject instanceof List) { + List assets = (List) assetsObject; + Map assetMap = (Map) assets.stream().filter(aAsset -> { + if (!(aAsset instanceof Map)) { + return false; + } + Map aAssetMap = (Map) aAsset; + Object name = aAssetMap.getOrDefault("name", ""); + return name.toString().matches(HALO_ADMIN_REGEX); + }) + .findFirst() + .orElseThrow(() -> new BadRequestException("Halo admin has no assets available")); + + Object name = assetMap.getOrDefault("name", ""); + Object browserDownloadUrl = assetMap.getOrDefault("browser_download_url", ""); + // Download the assets + ResponseEntity downloadResponseEntity = restTemplate.getForEntity(browserDownloadUrl.toString(), byte[].class); + System.out.println("Download response entity status: " + downloadResponseEntity.getStatusCode()); + + Path downloadedPath = Files.write(tempPath.resolve(name.toString()), downloadResponseEntity.getBody()); + + System.out.println("Downloaded path: " + downloadedPath.toString()); + } + } + + @Test + public void nameMatchTEst() { + String name = "halo-admin-1.0.0-beta.1.zip"; + + Assert.assertTrue(name.matches(HALO_ADMIN_REGEX)); + + name = "halo-admin-1.0.zip"; + Assert.assertTrue(name.matches(HALO_ADMIN_REGEX)); + + name = "halo-admin-1.0.0.zip"; + Assert.assertTrue(name.matches(HALO_ADMIN_REGEX)); + + name = "halo-admin-v1.0.0-beta.zip"; + Assert.assertFalse(name.matches(HALO_ADMIN_REGEX)); + + name = "halo-admin.zip"; + Assert.assertFalse(name.matches(HALO_ADMIN_REGEX)); + } +}