Skip to content

Commit 614c557

Browse files
committed
Fix code coverage and GHRepositoryTraffic test
1 parent d456e60 commit 614c557

24 files changed

Lines changed: 979 additions & 853 deletions

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
<exclude>org.kohsuke.github.GHRelease</exclude>
210210

211211
<!-- TODO: These still need test coverage -->
212-
<exclude>org.kohsuke.github.GitHub.GHApiInfo</exclude>
212+
<exclude>org.kohsuke.github.GitHubClient.GHApiInfo</exclude>
213213
<exclude>org.kohsuke.github.GHBranchProtection.RequiredSignatures</exclude>
214214
<exclude>org.kohsuke.github.GHBranchProtectionBuilder.Restrictions</exclude>
215215
<exclude>org.kohsuke.github.GHBranchProtection.Restrictions</exclude>

src/test/java/org/kohsuke/github/RepositoryTrafficTest.java

Lines changed: 48 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,17 @@
11
package org.kohsuke.github;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import org.apache.commons.io.IOUtils;
53
import org.junit.Assert;
6-
import org.junit.Ignore;
74
import org.junit.Test;
85
import org.kohsuke.github.GHRepositoryTraffic.DailyInfo;
9-
import org.mockito.Mockito;
106

117
import java.io.IOException;
12-
import java.io.InputStream;
13-
import java.net.HttpURLConnection;
14-
import java.net.URL;
15-
import java.text.SimpleDateFormat;
168
import java.util.Arrays;
17-
import java.util.HashMap;
189
import java.util.Iterator;
1910
import java.util.List;
20-
import java.util.TimeZone;
2111

2212
public class RepositoryTrafficTest extends AbstractGitHubWireMockTest {
2313
final private String repositoryName = "github-api";
2414

25-
@Override
26-
protected GitHubBuilder getGitHubBuilder() {
27-
return new GitHubBuilder().withPassword(GITHUB_API_TEST_ORG, null);
28-
}
29-
3015
@SuppressWarnings("unchecked")
3116
private <T extends GHRepositoryTraffic> void checkResponse(T expected, T actual) {
3217
Assert.assertEquals(expected.getCount(), actual.getCount());
@@ -50,100 +35,66 @@ private <T extends GHRepositoryTraffic> void checkResponse(T expected, T actual)
5035
}
5136
}
5237

53-
private <T extends GHRepositoryTraffic> void testTraffic(T expectedResult) throws IOException {
54-
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
55-
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
56-
ObjectMapper mapper = new ObjectMapper().setDateFormat(dateFormat);
57-
String mockedResponse = mapper.writeValueAsString(expectedResult);
58-
59-
GitHub gitHubSpy = Mockito.spy(gitHub);
60-
GHRepository repo = Mockito.spy(gitHubSpy.getOrganization(GITHUB_API_TEST_ORG).getRepository(repositoryName));
61-
Mockito.doReturn(GITHUB_API_TEST_ORG).when(repo).getOwnerName();
62-
63-
// accessing traffic info requires push access to the repo
64-
// since we don't have that, let the mocking begin...
65-
66-
HttpConnector connectorSpy = Mockito.spy(gitHubSpy.getConnector());
67-
Mockito.doReturn(connectorSpy).when(gitHubSpy).getConnector();
68-
69-
// also known as the "uc" in the Requester class
70-
HttpURLConnection mockHttpURLConnection = Mockito.mock(HttpURLConnection.class);
71-
72-
// needed for Requester.setRequestMethod
73-
Mockito.doReturn("GET").when(mockHttpURLConnection).getRequestMethod();
74-
75-
// this covers calls on "uc" in Requester.setupConnection and Requester.buildRequest
76-
String tailApiUrl = "/repos/" + GITHUB_API_TEST_ORG + "/" + repositoryName + "/traffic/"
77-
+ ((expectedResult instanceof GHRepositoryViewTraffic) ? "views" : "clones");
78-
URL trafficURL = GitHubRequest.getApiURL(gitHub.getClient().getApiUrl(), tailApiUrl);
79-
Mockito.doReturn(mockHttpURLConnection).when(connectorSpy).connect(Mockito.eq(trafficURL));
80-
81-
// make Requester.parse work
82-
Mockito.doReturn(200).when(mockHttpURLConnection).getResponseCode();
83-
// Mocking failing here due to refactoriing.
84-
// Mockito.doReturn("OK").when(mockHttpURLConnection).getResponseMessage();
85-
Mockito.doReturn(new HashMap<String, List<String>>()).when(mockHttpURLConnection).getHeaderFields();
86-
87-
InputStream stubInputStream = IOUtils.toInputStream(mockedResponse, "UTF-8");
88-
Mockito.doReturn(stubInputStream).when(mockHttpURLConnection).getInputStream();
89-
90-
if (expectedResult instanceof GHRepositoryViewTraffic) {
91-
GHRepositoryViewTraffic views = repo.getViewTraffic();
92-
checkResponse(expectedResult, views);
93-
} else if (expectedResult instanceof GHRepositoryCloneTraffic) {
94-
GHRepositoryCloneTraffic clones = repo.getCloneTraffic();
95-
checkResponse(expectedResult, clones);
96-
}
38+
private static GHRepository getRepository(GitHub gitHub) throws IOException {
39+
return gitHub.getOrganization("github-api").getRepository("github-api");
9740
}
9841

99-
@Ignore("Refactoring broke mocking")
10042
@Test
10143
public void testGetViews() throws IOException {
102-
GHRepositoryViewTraffic expectedResult = new GHRepositoryViewTraffic(21523359,
103-
65534,
104-
Arrays.asList(new GHRepositoryViewTraffic.DailyInfo("2016-10-10T00:00:00Z", 3, 2),
105-
new GHRepositoryViewTraffic.DailyInfo("2016-10-11T00:00:00Z", 9, 4),
106-
new GHRepositoryViewTraffic.DailyInfo("2016-10-12T00:00:00Z", 27, 8),
107-
new GHRepositoryViewTraffic.DailyInfo("2016-10-13T00:00:00Z", 81, 16),
108-
new GHRepositoryViewTraffic.DailyInfo("2016-10-14T00:00:00Z", 243, 32),
109-
new GHRepositoryViewTraffic.DailyInfo("2016-10-15T00:00:00Z", 729, 64),
110-
new GHRepositoryViewTraffic.DailyInfo("2016-10-16T00:00:00Z", 2187, 128),
111-
new GHRepositoryViewTraffic.DailyInfo("2016-10-17T00:00:00Z", 6561, 256),
112-
new GHRepositoryViewTraffic.DailyInfo("2016-10-18T00:00:00Z", 19683, 512),
113-
new GHRepositoryViewTraffic.DailyInfo("2016-10-19T00:00:00Z", 59049, 1024),
114-
new GHRepositoryViewTraffic.DailyInfo("2016-10-20T00:00:00Z", 177147, 2048),
115-
new GHRepositoryViewTraffic.DailyInfo("2016-10-21T00:00:00Z", 531441, 4096),
116-
new GHRepositoryViewTraffic.DailyInfo("2016-10-22T00:00:00Z", 1594323, 8192),
117-
new GHRepositoryViewTraffic.DailyInfo("2016-10-23T00:00:00Z", 4782969, 16384),
118-
new GHRepositoryViewTraffic.DailyInfo("2016-10-24T00:00:00Z", 14348907, 32768)));
119-
testTraffic(expectedResult);
44+
// Would change all the time
45+
snapshotNotAllowed();
46+
47+
GHRepository repository = getRepository(gitHub);
48+
GHRepositoryViewTraffic views = repository.getViewTraffic();
49+
50+
GHRepositoryViewTraffic expectedResult = new GHRepositoryViewTraffic(3533,
51+
616,
52+
Arrays.asList(new GHRepositoryViewTraffic.DailyInfo("2020-02-08T00:00:00Z", 101, 31),
53+
new GHRepositoryViewTraffic.DailyInfo("2020-02-09T00:00:00Z", 92, 22),
54+
new GHRepositoryViewTraffic.DailyInfo("2020-02-10T00:00:00Z", 317, 84),
55+
new GHRepositoryViewTraffic.DailyInfo("2020-02-11T00:00:00Z", 365, 90),
56+
new GHRepositoryViewTraffic.DailyInfo("2020-02-12T00:00:00Z", 428, 78),
57+
new GHRepositoryViewTraffic.DailyInfo("2020-02-13T00:00:00Z", 334, 52),
58+
new GHRepositoryViewTraffic.DailyInfo("2020-02-14T00:00:00Z", 138, 44),
59+
new GHRepositoryViewTraffic.DailyInfo("2020-02-15T00:00:00Z", 76, 13),
60+
new GHRepositoryViewTraffic.DailyInfo("2020-02-16T00:00:00Z", 99, 27),
61+
new GHRepositoryViewTraffic.DailyInfo("2020-02-17T00:00:00Z", 367, 65),
62+
new GHRepositoryViewTraffic.DailyInfo("2020-02-18T00:00:00Z", 411, 76),
63+
new GHRepositoryViewTraffic.DailyInfo("2020-02-19T00:00:00Z", 140, 61),
64+
new GHRepositoryViewTraffic.DailyInfo("2020-02-20T00:00:00Z", 259, 55),
65+
new GHRepositoryViewTraffic.DailyInfo("2020-02-21T00:00:00Z", 406, 66)));
66+
checkResponse(expectedResult, views);
12067
}
12168

122-
@Ignore("Refactoring broke mocking")
12369
@Test
12470
public void testGetClones() throws IOException {
125-
GHRepositoryCloneTraffic expectedResult = new GHRepositoryCloneTraffic(1500,
126-
455,
127-
Arrays.asList(new GHRepositoryCloneTraffic.DailyInfo("2016-10-10T00:00:00Z", 10, 3),
128-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-11T00:00:00Z", 20, 6),
129-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-12T00:00:00Z", 30, 5),
130-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-13T00:00:00Z", 40, 7),
131-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-14T00:00:00Z", 50, 11),
132-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-15T00:00:00Z", 60, 12),
133-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-16T00:00:00Z", 70, 19),
134-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-17T00:00:00Z", 170, 111),
135-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-18T00:00:00Z", 180, 70),
136-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-19T00:00:00Z", 190, 10),
137-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-20T00:00:00Z", 200, 18),
138-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-21T00:00:00Z", 210, 8),
139-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-22T00:00:00Z", 220, 168),
140-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-23T00:00:00Z", 5, 2),
141-
new GHRepositoryCloneTraffic.DailyInfo("2016-10-24T00:00:00Z", 45, 5)));
142-
testTraffic(expectedResult);
71+
// Would change all the time
72+
snapshotNotAllowed();
73+
74+
GHRepository repository = getRepository(gitHub);
75+
GHRepositoryCloneTraffic clones = repository.getCloneTraffic();
76+
77+
GHRepositoryCloneTraffic expectedResult = new GHRepositoryCloneTraffic(128,
78+
25,
79+
Arrays.asList(new GHRepositoryCloneTraffic.DailyInfo("2020-02-08T00:00:00Z", 6, 3),
80+
new GHRepositoryCloneTraffic.DailyInfo("2020-02-10T00:00:00Z", 6, 4),
81+
new GHRepositoryCloneTraffic.DailyInfo("2020-02-11T00:00:00Z", 2, 2),
82+
new GHRepositoryCloneTraffic.DailyInfo("2020-02-12T00:00:00Z", 1, 1),
83+
new GHRepositoryCloneTraffic.DailyInfo("2020-02-13T00:00:00Z", 1, 1),
84+
new GHRepositoryCloneTraffic.DailyInfo("2020-02-14T00:00:00Z", 2, 2),
85+
new GHRepositoryCloneTraffic.DailyInfo("2020-02-15T00:00:00Z", 2, 2),
86+
new GHRepositoryCloneTraffic.DailyInfo("2020-02-16T00:00:00Z", 2, 2),
87+
new GHRepositoryCloneTraffic.DailyInfo("2020-02-17T00:00:00Z", 3, 3),
88+
new GHRepositoryCloneTraffic.DailyInfo("2020-02-18T00:00:00Z", 1, 1),
89+
new GHRepositoryCloneTraffic.DailyInfo("2020-02-20T00:00:00Z", 25, 2),
90+
new GHRepositoryCloneTraffic.DailyInfo("2020-02-21T00:00:00Z", 77, 6)));
91+
checkResponse(expectedResult, clones);
14392
}
14493

14594
@Test
14695
public void testGetTrafficStatsAccessFailureDueToInsufficientPermissions() throws IOException {
96+
// Snapshot taken without permissions
97+
snapshotNotAllowed();
14798
String errorMsg = "Exception should be thrown, since we don't have permission to access repo traffic info.";
14899

149100
GHRepository repo = gitHub.getOrganization(GITHUB_API_TEST_ORG).getRepository(repositoryName);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"login": "github-api",
3+
"id": 54909825,
4+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjU0OTA5ODI1",
5+
"url": "https://api.github.com/orgs/github-api",
6+
"repos_url": "https://api.github.com/orgs/github-api/repos",
7+
"events_url": "https://api.github.com/orgs/github-api/events",
8+
"hooks_url": "https://api.github.com/orgs/github-api/hooks",
9+
"issues_url": "https://api.github.com/orgs/github-api/issues",
10+
"members_url": "https://api.github.com/orgs/github-api/members{/member}",
11+
"public_members_url": "https://api.github.com/orgs/github-api/public_members{/member}",
12+
"avatar_url": "https://avatars3.githubusercontent.com/u/54909825?v=4",
13+
"description": null,
14+
"is_verified": false,
15+
"has_organization_projects": true,
16+
"has_repository_projects": true,
17+
"public_repos": 1,
18+
"public_gists": 0,
19+
"followers": 0,
20+
"following": 0,
21+
"html_url": "https://github.com/github-api",
22+
"created_at": "2019-09-04T18:12:34Z",
23+
"updated_at": "2019-09-04T18:12:34Z",
24+
"type": "Organization",
25+
"total_private_repos": 0,
26+
"owned_private_repos": 0,
27+
"private_gists": 0,
28+
"disk_usage": 19498,
29+
"collaborators": 0,
30+
"billing_email": "bitwiseman@gmail.com",
31+
"default_repository_permission": "read",
32+
"members_can_create_repositories": true,
33+
"two_factor_requirement_enabled": false,
34+
"plan": {
35+
"name": "free",
36+
"space": 976562499,
37+
"private_repos": 0,
38+
"filled_seats": 2,
39+
"seats": 0
40+
}
41+
}

src/test/resources/org/kohsuke/github/RepositoryTrafficTest/wiremock/testGetClones/__files/orgs_github-api-test-org-c83bc734-162b-4cf3-bd90-e7d01adcbba4.json

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)