Skip to content

Commit 6dcbace

Browse files
committed
Added getViews method to GHRepository.
getViews implements https://developer.github.com/v3/repos/traffic/#views
1 parent f2fe8ea commit 6dcbace

3 files changed

Lines changed: 170 additions & 0 deletions

File tree

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,12 @@ public GHNotificationStream listNotifications() {
14811481
return new GHNotificationStream(root,getApiTailUrl("/notifications"));
14821482
}
14831483

1484+
/**
1485+
* <a href="https://developer.github.com/v3/repos/traffic/#views">https://developer.github.com/v3/repos/traffic/#views</a>
1486+
*/
1487+
public GHRepositoryViews getViews() throws IOException{
1488+
return root.retrieve().to(getApiTailUrl("/traffic/views"), GHRepositoryViews.class);
1489+
}
14841490

14851491
@Override
14861492
public int hashCode() {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.kohsuke.github;
2+
3+
import java.util.List;
4+
5+
public class GHRepositoryViews{
6+
private Integer count;
7+
private Integer uniques;
8+
private List<DayViews> views;
9+
10+
public GHRepositoryViews() {
11+
}
12+
13+
public GHRepositoryViews(Integer count, Integer uniques, List<DayViews> views) {
14+
this.count = count;
15+
this.uniques = uniques;
16+
this.views = views;
17+
}
18+
19+
public Integer getCount() {
20+
return count;
21+
}
22+
23+
public Integer getUniques() {
24+
return uniques;
25+
}
26+
27+
public List<DayViews> getViews() {
28+
return views;
29+
}
30+
31+
public static class DayViews {
32+
private String timestamp;
33+
private Integer count;
34+
private Integer uniques;
35+
36+
public String getTimestamp() {
37+
return timestamp;
38+
}
39+
40+
public Integer getCount() {
41+
return count;
42+
}
43+
44+
public Integer getUniques() {
45+
return uniques;
46+
}
47+
48+
public DayViews() {
49+
}
50+
51+
public DayViews(String timestamp, Integer count, Integer uniques) {
52+
this.timestamp = timestamp;
53+
this.count = count;
54+
this.uniques = uniques;
55+
}
56+
}
57+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package org.kohsuke.github;
2+
3+
import org.apache.commons.io.IOUtils;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.mockito.Mockito;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.net.HttpURLConnection;
11+
import java.net.URL;
12+
import java.util.Arrays;
13+
import java.util.Iterator;
14+
15+
public class RepositoryTrafficMockTest {
16+
final private String login = "kohsuke", repositoryName = "github-api";
17+
private void checkResponse(GHRepositoryViews expected, GHRepositoryViews actual){
18+
Assert.assertEquals(expected.getCount(), actual.getCount());
19+
Assert.assertEquals(expected.getUniques(), actual.getUniques());
20+
Assert.assertEquals(expected.getViews().size(), actual.getViews().size());
21+
Iterator<GHRepositoryViews.DayViews> expectedIt = expected.getViews().iterator();
22+
Iterator<GHRepositoryViews.DayViews> actualIt = actual.getViews().iterator();
23+
while(expectedIt.hasNext() && actualIt.hasNext()) {
24+
GHRepositoryViews.DayViews expectedDayViews = expectedIt.next();
25+
GHRepositoryViews.DayViews actualDayViews = actualIt.next();
26+
Assert.assertEquals(expectedDayViews.getCount(), actualDayViews.getCount());
27+
Assert.assertEquals(expectedDayViews.getUniques(), actualDayViews.getUniques());
28+
Assert.assertEquals(expectedDayViews.getTimestamp(), actualDayViews.getTimestamp());
29+
}
30+
}
31+
32+
@Test
33+
public void getViews() throws IOException{
34+
// example taken from the docs https://developer.github.com/v3/repos/traffic/#views
35+
GHRepositoryViews expectedResult = new GHRepositoryViews(
36+
14850,
37+
3782,
38+
Arrays.asList(
39+
new GHRepositoryViews.DayViews("2016-10-10T00:00:00Z", 440,143),
40+
new GHRepositoryViews.DayViews("2016-10-11T00:00:00Z", 1308,414),
41+
new GHRepositoryViews.DayViews("2016-10-12T00:00:00Z", 1486,452),
42+
new GHRepositoryViews.DayViews("2016-10-13T00:00:00Z", 1170,401),
43+
new GHRepositoryViews.DayViews("2016-10-14T00:00:00Z", 868,266),
44+
new GHRepositoryViews.DayViews("2016-10-15T00:00:00Z", 495,157),
45+
new GHRepositoryViews.DayViews("2016-10-16T00:00:00Z", 524,175),
46+
new GHRepositoryViews.DayViews("2016-10-17T00:00:00Z", 1263,412),
47+
new GHRepositoryViews.DayViews("2016-10-18T00:00:00Z", 1402,417),
48+
new GHRepositoryViews.DayViews("2016-10-19T00:00:00Z", 1394,424),
49+
new GHRepositoryViews.DayViews("2016-10-20T00:00:00Z", 1492,448),
50+
new GHRepositoryViews.DayViews("2016-10-21T00:00:00Z", 1153,332),
51+
new GHRepositoryViews.DayViews("2016-10-22T00:00:00Z", 566,168),
52+
new GHRepositoryViews.DayViews("2016-10-23T00:00:00Z", 675,184),
53+
new GHRepositoryViews.DayViews("2016-10-24T00:00:00Z", 614,237)
54+
)
55+
);
56+
String mockedGHRepositoryViewsResponse = GitHub.MAPPER.writeValueAsString(expectedResult);
57+
58+
GitHub gitHub = GitHub.connect(login, null);
59+
GitHub gitHubSpy = Mockito.spy(gitHub);
60+
GHRepository repo = gitHubSpy.getUser(login).getRepository(repositoryName);
61+
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+
70+
// also known as the "uc" in the Requester class
71+
HttpURLConnection mockHttpURLConnection = Mockito.mock(HttpURLConnection.class);
72+
73+
74+
// needed for Requester.setRequestMethod
75+
Mockito.doReturn("GET").when(mockHttpURLConnection).getRequestMethod();
76+
77+
78+
// this covers calls on "uc" in Requester.setupConnection and Requester.buildRequest
79+
URL trafficURL = new URL("https://api.github.com/repos/"+login+"/"+repositoryName+"/traffic/views");
80+
Mockito.doReturn(mockHttpURLConnection).when(connectorSpy).connect(Mockito.eq(trafficURL));
81+
82+
83+
// make Requester.parse work
84+
Mockito.doReturn(200).when(mockHttpURLConnection).getResponseCode();
85+
Mockito.doReturn("OK").when(mockHttpURLConnection).getResponseMessage();
86+
InputStream stubInputStream = IOUtils.toInputStream(mockedGHRepositoryViewsResponse, "UTF-8");
87+
Mockito.doReturn(stubInputStream).when(mockHttpURLConnection).getInputStream();
88+
89+
90+
GHRepositoryViews views = repo.getViews();
91+
92+
93+
checkResponse(expectedResult, views);
94+
}
95+
96+
@Test
97+
public void getViewsAccessFailureDueToInsufficientPermissions() throws IOException {
98+
GitHub gitHub = GitHub.connect(login, null);
99+
GHRepository repo = gitHub.getUser(login).getRepository(repositoryName);
100+
try {
101+
repo.getViews();
102+
Assert.fail("Exception should be thrown, since we don't have permission to access repo's traffic info.");
103+
}
104+
catch (HttpException ex){
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)