Skip to content

Commit

Permalink
Merge pull request #1190 from bitwiseman/test/contentcommit
Browse files Browse the repository at this point in the history
Add tests for GHContentUpdatedResponse
  • Loading branch information
bitwiseman committed Jun 28, 2021
2 parents 77c50b2 + 82e3b5b commit b3f4c6b
Show file tree
Hide file tree
Showing 89 changed files with 3,521 additions and 638 deletions.
17 changes: 13 additions & 4 deletions src/main/java/org/kohsuke/github/GHCommit.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,15 @@ public String getSHA1() {
return sha;
}

/**
* Gets url.
*
* @return API URL of this object.
*/
public URL getUrl() {
return GitHubClient.parseURL(url);
}

/**
* List of files changed/added/removed in this commit.
*
Expand Down Expand Up @@ -392,6 +401,7 @@ public int size() {
* on error
*/
public List<GHCommit> getParents() throws IOException {
populate();
List<GHCommit> r = new ArrayList<GHCommit>();
for (String sha1 : getParentSHA1s())
r.add(owner.getCommit(sha1));
Expand All @@ -406,6 +416,7 @@ public List<GHCommit> getParents() throws IOException {
* the io exception
*/
public GHUser getAuthor() throws IOException {
populate();
return resolveUser(author);
}

Expand All @@ -428,6 +439,7 @@ public Date getAuthoredDate() throws IOException {
* the io exception
*/
public GHUser getCommitter() throws IOException {
populate();
return resolveUser(committer);
}

Expand Down Expand Up @@ -485,10 +497,7 @@ public PagedIterable<GHBranch> listBranchesWhereHead() throws IOException {
* @return {@link PagedIterable} with all the commit comments in this repository.
*/
public PagedIterable<GHCommitComment> listComments() {
return owner.root.createRequest()
.withUrlPath(
String.format("/repos/%s/%s/commits/%s/comments", owner.getOwnerName(), owner.getName(), sha))
.toIterable(GHCommitComment[].class, item -> item.wrap(owner));
return owner.listCommitComments(sha);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,16 @@ public void testCommit() throws Exception {
assertThat(commit.getFiles().size(), equalTo(1));
assertThat(commit.getHtmlUrl().toString(),
equalTo("https://github.com/jenkinsci/jenkins/commit/08c1c9970af4d609ae754fbe803e06186e3206f7"));
assertThat(commit.getLinesAdded(), equalTo(40));
assertThat(commit.getLinesChanged(), equalTo(48));
assertThat(commit.getLinesDeleted(), equalTo(8));
assertThat(commit.getParentSHA1s().size(), equalTo(1));
assertThat(commit.getAuthoredDate(), equalTo(GitHubClient.parseDate("2012-04-24T00:16:52Z")));
assertThat(commit.getCommitDate(), equalTo(GitHubClient.parseDate("2012-04-24T00:16:52Z")));
assertThat(commit.getCommitShortInfo().getCommentCount(), equalTo(0));
assertThat(commit.getCommitShortInfo().getAuthoredDate(), equalTo(commit.getAuthoredDate()));
assertThat(commit.getCommitShortInfo().getCommitDate(), equalTo(commit.getCommitDate()));
assertThat(commit.getCommitShortInfo().getMessage(), equalTo("creating an RC branch"));

File f = commit.getFiles().get(0);
assertThat(f.getLinesChanged(), equalTo(48));
Expand Down Expand Up @@ -543,6 +553,8 @@ public void testCreateCommitComment() throws Exception {
GHCommit commit = gitHub.getUser("kohsuke")
.getRepository("sandbox-ant")
.getCommit("8ae38db0ea5837313ab5f39d43a6f73de3bd9000");

assertThat(commit.getCommitShortInfo().getCommentCount(), equalTo(30));
GHCommitComment c = commit.createComment("[testing](http://kohsuse.org/)");
try {
assertThat(c.getPath(), nullValue());
Expand All @@ -554,6 +566,13 @@ public void testCreateCommitComment() throws Exception {

c.update("updated text");
assertThat(c.getBody(), equalTo("updated text"));

commit = gitHub.getUser("kohsuke")
.getRepository("sandbox-ant")
.getCommit("8ae38db0ea5837313ab5f39d43a6f73de3bd9000");

assertThat(commit.getCommitShortInfo().getCommentCount(), equalTo(31));

} finally {
c.delete();
}
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/kohsuke/github/CommitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ public void testQueryCommits() throws Exception {
.list()
.toList();

assertThat(commits.get(0).getSHA1(), equalTo("1cccddb22e305397151b2b7b87b4b47d74ca337b"));
assertThat(commits.size(), equalTo(29));

GHCommit commit = commits.get(0);
assertThat(commit.getSHA1(), equalTo("1cccddb22e305397151b2b7b87b4b47d74ca337b"));

commits = gitHub.getUser("jenkinsci")
.getRepository("jenkins")
.queryCommits()
Expand Down
84 changes: 82 additions & 2 deletions src/test/java/org/kohsuke/github/GHContentIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.equalTo;

/**
* Integration test for {@link GHContent}.
Expand Down Expand Up @@ -89,14 +90,52 @@ public void testCRUDContent() throws Exception {
GHContentUpdateResponse created = repo.createContent("this is an awesome file I created\n",
"Creating a file for integration tests.",
createdFilename);
int expectedRequestCount = mockGitHub.getRequestCount();
GHContent createdContent = created.getContent();

assertThat(mockGitHub.getRequestCount(), equalTo(expectedRequestCount));
assertThat(created.getCommit(), notNullValue());
assertThat(created.getContent(), notNullValue());
assertThat(createdContent.getContent(), notNullValue());

assertThat(createdContent.getPath(), equalTo(createdFilename));
assertThat(mockGitHub.getRequestCount(), equalTo(expectedRequestCount));

assertThat(createdContent.getContent(), notNullValue());
assertThat(createdContent.getContent(), equalTo("this is an awesome file I created\n"));

;
assertThat(mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));

assertThat(created.getCommit().getSHA1(), notNullValue());
assertThat(mockGitHub.getRequestCount(), equalTo(expectedRequestCount));
assertThat(created.getCommit().getUrl().toString(),
endsWith(
"/repos/hub4j-test-org/GHContentIntegrationTest/git/commits/" + created.getCommit().getSHA1()));

assertThat(mockGitHub.getRequestCount(), equalTo(expectedRequestCount));

assertThat(created.getCommit().getCommitShortInfo().getMessage(),
equalTo("Creating a file for integration tests."));

assertThat(mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));

assertThat(created.getCommit().getAuthor().getName(), equalTo("Liam Newman"));
assertThat(created.getCommit().getAuthor().getEmail(), equalTo("bitwiseman@gmail.com"));
assertThat(created.getCommit().getCommitter().getName(), equalTo("Liam Newman"));
assertThat(created.getCommit().getCommitter().getEmail(), equalTo("bitwiseman@gmail.com"));

assertThat("Resolving GHUser", mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));

assertThat(created.getCommit().getTree().getSha(), notNullValue());

assertThat("Resolving GHTree", mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));

assertThat(created.getCommit().getTree().getUrl().toString(),
endsWith("/repos/hub4j-test-org/GHContentIntegrationTest/git/trees/"
+ created.getCommit().getTree().getSha()));

assertThat("Resolving GHTree is not cached", mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 2));

GHContent content = repo.getFileContent(createdFilename);
assertThat(content, is(notNullValue()));
assertThat(content.getSha(), equalTo(createdContent.getSha()));
Expand All @@ -113,15 +152,56 @@ public void testCRUDContent() throws Exception {

GHContentUpdateResponse updatedContentResponse = createdContent.update("this is some new content\n",
"Updated file for integration tests.");

expectedRequestCount = mockGitHub.getRequestCount();

GHContent updatedContent = updatedContentResponse.getContent();

assertThat(mockGitHub.getRequestCount(), equalTo(expectedRequestCount));

assertThat(updatedContentResponse.getCommit(), notNullValue());
assertThat(updatedContentResponse.getContent(), notNullValue());

assertThat(mockGitHub.getRequestCount(), equalTo(expectedRequestCount));

// due to what appears to be a cache propagation delay, this test is too flaky
assertThat(new BufferedReader(new InputStreamReader(updatedContent.read())).readLine(),
equalTo("this is some new content"));
assertThat(updatedContent.getContent(), equalTo("this is some new content\n"));

assertThat(mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));

assertThat(updatedContentResponse.getCommit().getSHA1(), notNullValue());
assertThat(updatedContentResponse.getCommit().getUrl().toString(),
endsWith("/repos/hub4j-test-org/GHContentIntegrationTest/git/commits/"
+ updatedContentResponse.getCommit().getSHA1()));

assertThat(mockGitHub.getRequestCount(), equalTo(expectedRequestCount));

assertThat(updatedContentResponse.getCommit().getCommitShortInfo().getMessage(),
equalTo("Updated file for integration tests."));

assertThat(mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));

assertThat(updatedContentResponse.getCommit().getAuthor().getName(), equalTo("Liam Newman"));
assertThat(updatedContentResponse.getCommit().getAuthor().getEmail(), equalTo("bitwiseman@gmail.com"));
assertThat(updatedContentResponse.getCommit().getCommitter().getName(), equalTo("Liam Newman"));
assertThat(updatedContentResponse.getCommit().getCommitter().getEmail(), equalTo("bitwiseman@gmail.com"));

assertThat("Resolving GHUser - was already resolved",
mockGitHub.getRequestCount(),
equalTo(expectedRequestCount));

assertThat(updatedContentResponse.getCommit().getTree().getSha(), notNullValue());

assertThat("Resolving GHTree", mockGitHub.getRequestCount(), equalTo(expectedRequestCount += 1));

assertThat(updatedContentResponse.getCommit().getTree().getUrl().toString(),
endsWith("/repos/hub4j-test-org/GHContentIntegrationTest/git/trees/"
+ updatedContentResponse.getCommit().getTree().getSha()));

assertThat("Resolving GHTree is not cached", mockGitHub.getRequestCount(), equalTo(expectedRequestCount + 2));

GHContentUpdateResponse deleteResponse = updatedContent.delete("Enough of this foolishness!");

assertThat(deleteResponse.getCommit(), notNullValue());
Expand All @@ -133,7 +213,7 @@ public void testCRUDContent() throws Exception {
} catch (GHFileNotFoundException e) {
assertThat(e.getMessage(),
endsWith(
"/repos/hub4j-test-org/GHContentIntegrationTest/contents/test+directory%20%2350/test%20file-to+create-%231.txt {\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/repos/contents/#get-contents\"}"));
"/repos/hub4j-test-org/GHContentIntegrationTest/contents/test+directory%20%2350/test%20file-to+create-%231.txt {\"message\":\"Not Found\",\"documentation_url\":\"https://docs.github.com/rest/reference/repos#get-repository-content\"}"));
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/kohsuke/github/GHRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ public void listCommitCommentsNoComments() throws IOException {
.toList();

assertThat("Commit has no comments", commitComments.isEmpty());

commitComments = getRepository().getCommit("c413fc1e3057332b93850ea48202627d29a37de5").listComments().toList();

assertThat("Commit has no comments", commitComments.isEmpty());
}

@Test
Expand All @@ -423,6 +427,14 @@ public void listCommitCommentsSomeComments() throws IOException {
assertThat("Comment text found",
commitComments.stream().map(GHCommitComment::getBody).collect(Collectors.toList()),
containsInAnyOrder("comment 1", "comment 2"));

commitComments = getRepository().getCommit("499d91f9f846b0087b2a20cf3648b49dc9c2eeef").listComments().toList();

assertThat("Two comments present", commitComments.size(), equalTo(2));
assertThat("Comment text found",
commitComments.stream().map(GHCommitComment::getBody).collect(Collectors.toList()),
containsInAnyOrder("comment 1", "comment 2"));

}

@Test // Issue #261
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"forks": 2,
"open_issues": 0,
"watchers": 2,
"default_branch": "main",
"default_branch": "master",
"permissions": {
"admin": false,
"push": false,
Expand Down Expand Up @@ -189,7 +189,7 @@
"forks": 4,
"open_issues": 0,
"watchers": 4,
"default_branch": "main"
"default_branch": "master"
},
"source": {
"id": 3231216,
Expand Down Expand Up @@ -283,7 +283,7 @@
"forks": 4,
"open_issues": 0,
"watchers": 4,
"default_branch": "main"
"default_branch": "master"
},
"network_count": 4,
"subscribers_count": 0
Expand Down
Loading

0 comments on commit b3f4c6b

Please sign in to comment.