|
| 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