Skip to content

Commit

Permalink
Restore ContentIntegrationTest GHCommit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Sep 15, 2024
1 parent 048b84b commit d7c8675
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 44 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/kohsuke/github/GHCommit.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public ShortInfo() {
// Empty constructor required for Jackson binding
};

/**
* Instantiates a new short info.
*
* @param commit
* the commit
*/
ShortInfo(GitCommit commit) {
// Inherited copy constructor, used for bridge method from {@link GitCommit},
// which is used in {@link GHContentUpdateResponse}) to {@link GHCommit}.
super(commit);
}

/**
* Gets the parent SHA 1 s.
*
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/kohsuke/github/GitCommit.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ public GitCommit() {
// empty constructor for Jackson binding
};

/**
* Instantiates a new git commit.
*
* @param commit
* the commit
*/
GitCommit(GitCommit commit) {
// copy constructor used to cast to GitCommit.ShortInfo and from there
// to GHCommit, for testing purposes
this.owner = commit.getOwner();
this.sha = commit.getSha();
this.node_id = commit.getNodeId();
this.url = commit.getUrl();
this.html_url = commit.getHtmlUrl();
this.author = commit.getAuthor();
this.committer = commit.getCommitter();
this.message = commit.getMessage();
this.verification = commit.getVerification();
this.tree = commit.getTree();
this.parents = commit.getParents();
}

/**
* Gets owner.
*
Expand Down Expand Up @@ -247,4 +269,13 @@ GitCommit wrapUp(GHRepository owner) {
return this;
}

/**
* For test purposes only.
*
* @return Equivalent GHCommit
*/
GHCommit toGHCommit() {
return new GHCommit(new GHCommit.ShortInfo(this));
}

}
71 changes: 27 additions & 44 deletions src/test/java/org/kohsuke/github/GHContentIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.List;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThrows;

// TODO: Auto-generated Javadoc
/**
Expand Down Expand Up @@ -242,17 +242,15 @@ int checkCreatedCommits(GitCommit gitCommit, GHCommit ghCommit, int expectedRequ
assertThat(gitCommit.getAuthoredDate(), equalTo(GitHubClient.parseDate("2021-06-28T20:37:49Z")));
assertThat(gitCommit.getCommitDate(), equalTo(GitHubClient.parseDate("2021-06-28T20:37:49Z")));

// Changed this to assert null since bridge methods are missing.
assertThat(ghCommit, nullValue());
// assertThat(ghCommit.getCommitShortInfo().getMessage(), equalTo("Creating a file for integration tests."));
// assertThat("Message already resolved", mockGitHub.getRequestCount(), equalTo(expectedRequestCount));
// Assert.assertThrows(GHException.class, () -> ghCommit.getCommitShortInfo().getCommentCount());
assertThat(ghCommit.getCommitShortInfo().getMessage(), equalTo("Creating a file for integration tests."));
assertThat("Message already resolved", mockGitHub.getRequestCount(), equalTo(expectedRequestCount));
assertThrows(GHException.class, () -> ghCommit.getCommitShortInfo().getCommentCount());

// ghCommit.populate();
// assertThat("Populate GHCommit", mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));
ghCommit.populate();
assertThat("Populate GHCommit", mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));

// expectedRequestCount = checkCommitUserInfo(gitCommit, ghCommit, expectedRequestCount);
// assertThat("Resolved GHUser for GHCommit", mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));
expectedRequestCount = checkCommitUserInfo(gitCommit, ghCommit, expectedRequestCount);
assertThat("Resolved GHUser for GHCommit", mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));

expectedRequestCount = checkCommitTree(gitCommit, ghCommit, expectedRequestCount);

Expand All @@ -271,13 +269,7 @@ int checkCreatedCommits(GitCommit gitCommit, GHCommit ghCommit, int expectedRequ
* the exception
*/
GHCommit getGHCommit(GHContentUpdateResponse resp) throws Exception {
for (Method method : resp.getClass().getMethods()) {
if (method.getName().equals("getCommit") && method.getReturnType().equals(GHCommit.class)) {
return (GHCommit) method.invoke(resp);
}
}
System.out.println("Unable to find bridge method");
return null;
return resp.getCommit().toGHCommit();
}

/**
Expand All @@ -303,16 +295,14 @@ int checkUpdatedContentResponseCommits(GitCommit gitCommit, GHCommit ghCommit, i
assertThat(gitCommit.getAuthoredDate(), equalTo(GitHubClient.parseDate("2021-06-28T20:37:51Z")));
assertThat(gitCommit.getCommitDate(), equalTo(GitHubClient.parseDate("2021-06-28T20:37:51Z")));

// Changed this to assert null since bridge methods are missing.
assertThat(ghCommit, nullValue());
// assertThat(ghCommit.getCommitShortInfo().getMessage(), equalTo("Updated file for integration tests."));
// assertThat("Message already resolved", mockGitHub.getRequestCount(), equalTo(expectedRequestCount));
assertThat(ghCommit.getCommitShortInfo().getMessage(), equalTo("Updated file for integration tests."));
assertThat("Message already resolved", mockGitHub.getRequestCount(), equalTo(expectedRequestCount));

// ghCommit.populate();
// assertThat("Populate GHCommit", mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));
ghCommit.populate();
assertThat("Populate GHCommit", mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));

// expectedRequestCount = checkCommitUserInfo(gitCommit, ghCommit, expectedRequestCount);
// assertThat("GHUser already resolved", mockGitHub.getRequestCount(), equalTo(expectedRequestCount));
expectedRequestCount = checkCommitUserInfo(gitCommit, ghCommit, expectedRequestCount);
assertThat("GHUser already resolved", mockGitHub.getRequestCount(), equalTo(expectedRequestCount));

expectedRequestCount = checkCommitTree(gitCommit, ghCommit, expectedRequestCount);

Expand Down Expand Up @@ -342,11 +332,9 @@ int checkBasicCommitInfo(GitCommit gitCommit, GHCommit ghCommit, int expectedReq
equalTo("https://github.com/hub4j-test-org/GHContentIntegrationTest/commit/" + gitCommit.getSHA1()));
assertThat(gitCommit.getVerification(), notNullValue());

// Changed this to assert null since bridge methods are missing.
assertThat(ghCommit, nullValue());
// assertThat(ghCommit.getSHA1(), notNullValue());
// assertThat(ghCommit.getUrl().toString(),
// endsWith("/repos/hub4j-test-org/GHContentIntegrationTest/git/commits/" + ghCommit.getSHA1()));
assertThat(ghCommit.getSHA1(), notNullValue());
assertThat(ghCommit.getUrl().toString(),
endsWith("/repos/hub4j-test-org/GHContentIntegrationTest/git/commits/" + ghCommit.getSHA1()));

return expectedRequestCount;
}
Expand Down Expand Up @@ -402,16 +390,13 @@ int checkCommitTree(GitCommit gitCommit, GHCommit ghCommit, int expectedRequestC
endsWith("/repos/hub4j-test-org/GHContentIntegrationTest/git/trees/" + gitCommit.getTree().getSha()));
assertThat("GHTree already resolved", mockGitHub.getRequestCount(), equalTo(expectedRequestCount));

// Changed this to assert null since bridge methods are missing.
assertThat(ghCommit, nullValue());
// assertThat(ghCommit.getTree().getSha(), notNullValue());
// assertThat("GHCommit has to resolve GHTree", mockGitHub.getRequestCount(), equalTo(expectedRequestCount +=
// 1));
// assertThat(ghCommit.getTree().getUrl().toString(),
// endsWith("/repos/hub4j-test-org/GHContentIntegrationTest/git/trees/" + ghCommit.getTree().getSha()));
// assertThat("GHCommit resolving GHTree is not cached",
// mockGitHub.getRequestCount(),
// equalTo(expectedRequestCount += 2));
assertThat(ghCommit.getTree().getSha(), notNullValue());
assertThat("GHCommit has to resolve GHTree", mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));
assertThat(ghCommit.getTree().getUrl().toString(),
endsWith("/repos/hub4j-test-org/GHContentIntegrationTest/git/trees/" + ghCommit.getTree().getSha()));
assertThat("GHCommit resolving GHTree is not cached",
mockGitHub.getRequestCount(),
equalTo(expectedRequestCount += 2));

return expectedRequestCount;
}
Expand All @@ -432,10 +417,8 @@ int checkCommitTree(GitCommit gitCommit, GHCommit ghCommit, int expectedRequestC
int checkCommitParents(GitCommit gitCommit, GHCommit ghCommit, int expectedRequestCount) throws IOException {
assertThat(gitCommit.getParentSHA1s().size(), is(greaterThan(0)));
assertThat(gitCommit.getParentSHA1s().get(0), notNullValue());
// Changed this to assert null since bridge methods are missing.
assertThat(ghCommit, nullValue());
// assertThat(ghCommit.getParentSHA1s().size(), is(greaterThan(0)));
// assertThat(ghCommit.getParentSHA1s().get(0), notNullValue());
assertThat(ghCommit.getParentSHA1s().size(), is(greaterThan(0)));
assertThat(ghCommit.getParentSHA1s().get(0), notNullValue());

return expectedRequestCount;
}
Expand Down

0 comments on commit d7c8675

Please sign in to comment.