|
| 1 | +package io.quarkus.it.jgit; |
| 2 | + |
| 3 | +import static io.restassured.RestAssured.given; |
| 4 | +import static org.assertj.core.api.Assertions.assertThat; |
| 5 | + |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
| 8 | + |
| 9 | +import jakarta.inject.Inject; |
| 10 | + |
| 11 | +import org.eclipse.jgit.api.Git; |
| 12 | +import org.eclipse.jgit.revwalk.RevCommit; |
| 13 | +import org.eclipse.jgit.transport.CredentialsProvider; |
| 14 | +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; |
| 15 | +import org.eclipse.microprofile.config.ConfigProvider; |
| 16 | +import org.junit.jupiter.api.BeforeAll; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.io.TempDir; |
| 19 | + |
| 20 | +import io.gitea.ApiClient; |
| 21 | +import io.gitea.Configuration; |
| 22 | +import io.gitea.api.RepositoryApi; |
| 23 | +import io.gitea.auth.HttpBasicAuth; |
| 24 | +import io.gitea.model.CreateRepoOption; |
| 25 | +import io.quarkus.jgit.runtime.JGitRuntimeConfig; |
| 26 | +import io.quarkus.test.junit.QuarkusTest; |
| 27 | + |
| 28 | +@QuarkusTest |
| 29 | +public class DevServiceTest { |
| 30 | + |
| 31 | + @Inject |
| 32 | + JGitRuntimeConfig config; |
| 33 | + |
| 34 | + @BeforeAll |
| 35 | + public static void setCredentials() { |
| 36 | + CredentialsProvider.setDefault(new UsernamePasswordCredentialsProvider("quarkus", "quarkus")); |
| 37 | + } |
| 38 | + |
| 39 | + @BeforeAll |
| 40 | + public static void createRepository() throws Exception { |
| 41 | + ApiClient defaultClient = Configuration.getDefaultApiClient(); |
| 42 | + String httpUrl = ConfigProvider.getConfig().getValue("quarkus.jgit.devservices.http-url", String.class); |
| 43 | + defaultClient.setBasePath(httpUrl + "/api/v1"); |
| 44 | + |
| 45 | + HttpBasicAuth basicAuth = (HttpBasicAuth) defaultClient.getAuthentication("BasicAuth"); |
| 46 | + basicAuth.setUsername("quarkus"); |
| 47 | + basicAuth.setPassword("quarkus"); |
| 48 | + |
| 49 | + RepositoryApi api = new RepositoryApi(); |
| 50 | + api.createCurrentUserRepo(new CreateRepoOption() |
| 51 | + .autoInit(true) |
| 52 | + ._private(false) |
| 53 | + .name("test-repo") |
| 54 | + .readme("Default")); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void serviceRunning() { |
| 60 | + given() |
| 61 | + .get(config.devservices().httpUrl().orElseThrow()) |
| 62 | + .then() |
| 63 | + .statusCode(200); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void shouldCloneFromDevService(@TempDir Path tempDir) throws Exception { |
| 68 | + try (Git git = Git.cloneRepository().setDirectory(tempDir.toFile()) |
| 69 | + .setURI(config.devservices().httpUrl().get() + "/quarkus/test-repo.git").call()) { |
| 70 | + assertThat(tempDir.resolve("README.md")).isRegularFile(); |
| 71 | + assertThat(git.log().call()).extracting(RevCommit::getFullMessage).map(String::trim).contains("Initial commit"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void shouldPushToDevService(@TempDir Path tempDir) throws Exception { |
| 77 | + try (Git git = Git.cloneRepository().setDirectory(tempDir.resolve("original").toFile()) |
| 78 | + .setURI(config.devservices().httpUrl().get() + "/quarkus/test-repo.git").call()) { |
| 79 | + Path readme = tempDir.resolve("original").resolve("README.md"); |
| 80 | + Files.writeString(readme, "Hello, World!"); |
| 81 | + // Perform commit |
| 82 | + git.commit().setAll(true).setMessage("Update README").setSign(false).call(); |
| 83 | + git.push().call(); |
| 84 | + } |
| 85 | + try (Git git = Git.cloneRepository().setDirectory(tempDir.resolve("updated").toFile()) |
| 86 | + .setURI(config.devservices().httpUrl().get() + "/quarkus/test-repo.git").call()) { |
| 87 | + assertThat(tempDir.resolve("updated").resolve("README.md")).hasContent("Hello, World!"); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments