TestCommitsApi.java 14.10 KiB
package org.gitlab4j.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Comment;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.CommitAction;
import org.gitlab4j.api.models.CommitAction.Action;
import org.gitlab4j.api.models.CommitRef;
import org.gitlab4j.api.models.Diff;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.RepositoryFile;
import org.gitlab4j.api.utils.ISO8601;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runners.MethodSorters;
/**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
 * TEST_NAMESPACE
 * TEST_PROJECT_NAME
 * TEST_HOST_URL
 * TEST_PRIVATE_TOKEN
 * If any of the above are NULL, all tests in this class will be skipped.
@Category(IntegrationTest.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestCommitsApi extends AbstractIntegrationTest {
    private static final String TEST_CREATE_COMMIT_FILEPATH = "gitlab4j-create-commit-test-file.txt";
    private static GitLabApi gitLabApi;
    private static Project testProject;
    public TestCommitsApi() {
        super();
    @BeforeClass
    public static void setup() {
        // Must setup the connection to the GitLab test server and get the test Project instance
        gitLabApi = baseTestSetup();
        testProject = getTestProject();
        if (!gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_PROJECT_SUBDIRECTORY_PATH, "master").isPresent()) {
            try {
                RepositoryFile repoFile = new RepositoryFile();
                repoFile.setFilePath(TEST_PROJECT_SUBDIRECTORY_PATH);
                repoFile.setContent("This is a test project used to test GitLab4J-API.");
                gitLabApi.getRepositoryFileApi().createFile(testProject, repoFile, "master", "Initial commit.");
            } catch (GitLabApiException ignore) {}
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
} } @Before public void beforeMethod() { assumeTrue(gitLabApi != null); } @Test public void testDiff() throws GitLabApiException { assertNotNull(testProject); List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject.getId()); assertNotNull(commits); assertTrue(commits.size() > 0); List<Diff> diffs = gitLabApi.getCommitsApi().getDiff(testProject.getId(), commits.get(0).getId()); assertNotNull(diffs); assertTrue(diffs.size() > 0); diffs = gitLabApi.getCommitsApi().getDiff(TEST_NAMESPACE + "/" + TEST_PROJECT_NAME, commits.get(0).getId()); assertNotNull(diffs); assertTrue(diffs.size() > 0); } @Test public void testComments() throws GitLabApiException, ParseException { assertNotNull(testProject); List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject.getId(), null, ISO8601.toDate("2000-01-01"), new Date()); assertNotNull(commits); assertTrue(commits.size() > 0); String note = "This is a note."; Comment addedComment = gitLabApi.getCommitsApi().addComment(testProject.getId(), commits.get(0).getId(), note); assertNotNull(addedComment); assertEquals(note, addedComment.getNote()); List<Comment> comments = gitLabApi.getCommitsApi().getComments(testProject.getId(), commits.get(0).getId()); assertNotNull(comments); assertTrue(comments.size() > 0); } @Test public void testCommitsSince() throws GitLabApiException, ParseException { assertNotNull(testProject); Date since = ISO8601.toDate("2000-01-01"); List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject.getId(), null, new Date(), null); assertNotNull(commits); assertTrue(commits.isEmpty()); commits = gitLabApi.getCommitsApi().getCommits(testProject.getId(), null, since, new Date()); assertNotNull(commits); assertTrue(commits.size() > 0); Pager<Commit> pager = gitLabApi.getCommitsApi().getCommits(testProject.getId(), null, new Date(), null, 10); assertNotNull(pager); assertTrue(pager.getTotalItems() == 0); pager = gitLabApi.getCommitsApi().getCommits(testProject.getId(), null, since, null, 10); assertNotNull(pager); assertTrue(pager.getTotalItems() > 0); } @Test public void testCommitRefs() throws GitLabApiException {
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
assertNotNull(testProject); List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject.getId()); assertNotNull(commits); assertTrue(commits.size() > 0); List<CommitRef> commitRefs = gitLabApi.getCommitsApi().getCommitRefs(testProject.getId(), commits.get(0).getId()); assertNotNull(commitRefs); assertTrue(commitRefs.size() > 0); } @Test public void testCommitsSinceWithPath() throws GitLabApiException, ParseException { assertNotNull(testProject); List<Commit> commits = gitLabApi.getCommitsApi().getCommits( testProject.getId(), null, ISO8601.toDate("2000-01-01"), new Date(), TEST_PROJECT_SUBDIRECTORY_PATH); assertNotNull(commits); assertTrue(commits.size() > 0); } @Test public void testCommitsByPath() throws GitLabApiException { assertNotNull(testProject); CommitsApi commitsApi = gitLabApi.getCommitsApi(); List<Commit> commits = commitsApi.getCommits(testProject, "master", null); assertNotNull(commits); assertTrue(commits.size() > 1); commitsApi = gitLabApi.getCommitsApi(); commits = commitsApi.getCommits(testProject, "master", "README.md"); assertNotNull(commits); assertTrue(commits.size() > 1); commits = commitsApi.getCommits(testProject, "master", TEST_PROJECT_SUBDIRECTORY_PATH); assertNotNull(commits); assertTrue(commits.size() > 0); } @Test public void testCommitsByPathNotFound() throws GitLabApiException { try { List<Commit> commits = gitLabApi.getCommitsApi().getCommits( testProject, "master", "this-file-does-not-exist.an-extension"); assertTrue(commits == null || commits.isEmpty()); } catch (GitLabApiException gle) { assertEquals(Response.Status.NOT_FOUND, gle.getHttpStatus()); } } @Test public void testCreateCommit() throws GitLabApiException { // Make sure the file to create does not exist. if (gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_CREATE_COMMIT_FILEPATH, "master").isPresent()) { gitLabApi.getRepositoryFileApi().deleteFile(testProject, TEST_CREATE_COMMIT_FILEPATH, "master", "Deleted test file"); } // Arrange CommitAction commitAction = new CommitAction() .withAction(Action.CREATE) .withContent("This is the original data in the file") .withFilePath(TEST_CREATE_COMMIT_FILEPATH); // Act Commit commit = gitLabApi.getCommitsApi().createCommit(
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
testProject, "master", "Testing createCommit() create action", null, null, null, Arrays.asList(commitAction)); // Assert assertNotNull(commit); // Arrange commitAction = new CommitAction() .withAction(Action.DELETE) .withFilePath(TEST_CREATE_COMMIT_FILEPATH); // Act commit = gitLabApi.getCommitsApi().createCommit( testProject, "master", "Testing createCommit() delete action", null, null, null, Arrays.asList(commitAction)); // Assert assertNotNull(commit); Optional<RepositoryFile> repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_CREATE_COMMIT_FILEPATH, "master"); assertFalse(repoFile.isPresent()); } @Test public void testCreateCommitFromFile() throws GitLabApiException { // Make sure the file to create does not exist. if (gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_CREATE_COMMIT_FILEPATH, "master").isPresent()) { try { gitLabApi.getRepositoryFileApi().deleteFile(testProject, TEST_CREATE_COMMIT_FILEPATH, "master", "Deleted test file"); } catch (GitLabApiException ignore) {} } // Arrange CommitAction commitAction = new CommitAction() .withAction(Action.CREATE) .withContent("This is the original data in the file") .withFilePath(TEST_CREATE_COMMIT_FILEPATH); // Act Commit commit = gitLabApi.getCommitsApi().createCommit( testProject, "master", "Testing createCommit() create action", null, null, null, Arrays.asList(commitAction)); // Assert assertNotNull(commit); // Arrange commitAction = new CommitAction() .withAction(Action.DELETE) .withFilePath(TEST_CREATE_COMMIT_FILEPATH); // Act commit = gitLabApi.getCommitsApi().createCommit( testProject, "master", "Testing createCommit() delete action", null, null, null, Arrays.asList(commitAction)); // Assert assertNotNull(commit); Optional<RepositoryFile> repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_CREATE_COMMIT_FILEPATH, "master"); assertFalse(repoFile.isPresent()); } @Test public void testCreateCommitCreateWithNoContent() throws GitLabApiException { // Arrange CommitAction commitAction = new CommitAction() .withAction(Action.CREATE) .withContent(null) .withFilePath(TEST_CREATE_COMMIT_FILEPATH + ".bk"); // Act - expecting exception
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
try { gitLabApi.getCommitsApi().createCommit(testProject, "master", "Testing createCommit() create action", null, null, null, Arrays.asList(commitAction)); fail("Commit should have been rejected due to no content."); } catch (GitLabApiException ignore) { } } @Test public void testRevertCommit() throws GitLabApiException { // Make sure the file to create does not exist. String filePath = TEST_CREATE_COMMIT_FILEPATH + ".test"; if (gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master").isPresent()) { gitLabApi.getRepositoryFileApi().deleteFile(testProject, filePath, "master", "Deleted test file"); } // Arrange CommitAction commitAction = new CommitAction() .withAction(Action.CREATE) .withContent("This is the original data in the file") .withFilePath(filePath); // Act Commit commit = gitLabApi.getCommitsApi().createCommit( testProject, "master", "Testing createCommit() create action", null, null, null, commitAction); // Assert assertNotNull(commit); Optional<RepositoryFile> repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master"); assertTrue(repoFile.isPresent()); // Act Commit revertedCommit = gitLabApi.getCommitsApi().revertCommit(testProject, commit.getId(), "master"); // Assert assertNotEquals(commit.getId(), revertedCommit.getId()); repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master"); assertFalse(repoFile.isPresent()); } @Test public void testCherryPickCommit() throws GitLabApiException { // Make sure the branch to cherry pick does not exist if(gitLabApi.getRepositoryApi().getOptionalBranch(testProject, "cherry-pick-branch").isPresent()) { gitLabApi.getRepositoryApi().deleteBranch(testProject, "cherry-pick-branch"); } // Make sure the file to create does not exist. String filePath = TEST_CREATE_COMMIT_FILEPATH + ".test"; if (gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master").isPresent()) { gitLabApi.getRepositoryFileApi().deleteFile(testProject, filePath, "master", "Deleted test file"); } // Act Branch branch = gitLabApi.getRepositoryApi().createBranch(testProject, "cherry-pick-branch", "master"); // Assert assertNotNull(branch); Optional<RepositoryFile> repoFileBranch = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, branch.getName()); assertFalse(repoFileBranch.isPresent()); // Arrange CommitAction commitAction = new CommitAction() .withAction(Action.CREATE) .withContent("This is the original data in the file") .withFilePath(filePath); // Act
351352353354355356357358359360361362363364365366367
Commit commit = gitLabApi.getCommitsApi().createCommit( testProject, "master", "Testing createCommit() create action", null, null, null, commitAction); // Assert assertNotNull(commit); Optional<RepositoryFile> repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master"); assertTrue(repoFile.isPresent()); // Act Commit cherryPickedCommit = gitLabApi.getCommitsApi().cherryPickCommit(testProject, commit.getId(), "cherry-pick-branch"); // Assert assertNotNull(cherryPickedCommit); Optional<RepositoryFile> repoFileBranchCherryPicked = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, branch.getName()); assertTrue(repoFileBranchCherryPicked.isPresent()); } }