forked from halo-dev/halo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
154 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
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,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<Map> 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<byte[]> 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)); | ||
} | ||
} |