|
| 1 | +package org.kohsuke.github; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import org.apache.commons.io.IOUtils; |
| 5 | +import org.junit.Assert; |
| 6 | +import org.junit.Test; |
| 7 | +import org.kohsuke.github.GHRepositoryCloneTraffic.DayInfo; |
| 8 | +import org.kohsuke.github.GHRepositoryViewTraffic.Daily; |
| 9 | +import org.mockito.Mockito; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.net.HttpURLConnection; |
| 14 | +import java.net.URL; |
| 15 | +import java.text.SimpleDateFormat; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.Iterator; |
| 18 | +import java.util.List; |
| 19 | +import java.util.TimeZone; |
| 20 | + |
| 21 | +public class RepositoryTrafficTest { |
| 22 | + final private String login = "kohsuke", repositoryName = "github-api"; |
| 23 | + |
| 24 | + @SuppressWarnings("unchecked") |
| 25 | + private <T extends GHRepositoryTrafficInfo> void checkResponse(T expected, T actual){ |
| 26 | + Assert.assertEquals(expected.getCount(), actual.getCount()); |
| 27 | + Assert.assertEquals(expected.getUniques(), actual.getUniques()); |
| 28 | + |
| 29 | + List<? extends GHRepositoryTrafficInfo.DayInfo> expectedList = expected.getDailyInfo(); |
| 30 | + List<? extends GHRepositoryTrafficInfo.DayInfo> actualList = actual.getDailyInfo(); |
| 31 | + Iterator<? extends GHRepositoryTrafficInfo.DayInfo> expectedIt; |
| 32 | + Iterator<? extends GHRepositoryTrafficInfo.DayInfo> actualIt; |
| 33 | + |
| 34 | + Assert.assertEquals(expectedList.size(), actualList.size()); |
| 35 | + expectedIt = expectedList.iterator(); |
| 36 | + actualIt = actualList.iterator(); |
| 37 | + |
| 38 | + while(expectedIt.hasNext() && actualIt.hasNext()) { |
| 39 | + GHRepositoryTrafficInfo.DayInfo expectedDayInfo = expectedIt.next(); |
| 40 | + GHRepositoryTrafficInfo.DayInfo actualDayInfo = actualIt.next(); |
| 41 | + Assert.assertEquals(expectedDayInfo.getCount(), actualDayInfo.getCount()); |
| 42 | + Assert.assertEquals(expectedDayInfo.getUniques(), actualDayInfo.getUniques()); |
| 43 | + Assert.assertEquals(expectedDayInfo.getTimestamp(), actualDayInfo.getTimestamp()); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private <T extends GHRepositoryTrafficInfo> void testTraffic(T expectedResult) throws IOException{ |
| 48 | + SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); |
| 49 | + dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); |
| 50 | + ObjectMapper mapper = new ObjectMapper().setDateFormat(dateFormat); |
| 51 | + String mockedResponse = mapper.writeValueAsString(expectedResult); |
| 52 | + |
| 53 | + |
| 54 | + GitHub gitHub = GitHub.connect(login, null); |
| 55 | + GitHub gitHubSpy = Mockito.spy(gitHub); |
| 56 | + GHRepository repo = gitHubSpy.getUser(login).getRepository(repositoryName); |
| 57 | + |
| 58 | + |
| 59 | + // accessing traffic info requires push access to the repo |
| 60 | + // since we don't have that, let the mocking begin... |
| 61 | + |
| 62 | + HttpConnector connectorSpy = Mockito.spy(gitHubSpy.getConnector()); |
| 63 | + Mockito.doReturn(connectorSpy).when(gitHubSpy).getConnector(); |
| 64 | + |
| 65 | + |
| 66 | + // also known as the "uc" in the Requester class |
| 67 | + HttpURLConnection mockHttpURLConnection = Mockito.mock(HttpURLConnection.class); |
| 68 | + |
| 69 | + |
| 70 | + // needed for Requester.setRequestMethod |
| 71 | + Mockito.doReturn("GET").when(mockHttpURLConnection).getRequestMethod(); |
| 72 | + |
| 73 | + |
| 74 | + // this covers calls on "uc" in Requester.setupConnection and Requester.buildRequest |
| 75 | + URL trafficURL = new URL( |
| 76 | + "https://api.github.com/repos/"+login+"/"+repositoryName+"/traffic/" + |
| 77 | + ((expectedResult instanceof GHRepositoryViewTraffic) ? "views" : "clones") |
| 78 | + ); |
| 79 | + Mockito.doReturn(mockHttpURLConnection).when(connectorSpy).connect(Mockito.eq(trafficURL)); |
| 80 | + |
| 81 | + |
| 82 | + // make Requester.parse work |
| 83 | + Mockito.doReturn(200).when(mockHttpURLConnection).getResponseCode(); |
| 84 | + Mockito.doReturn("OK").when(mockHttpURLConnection).getResponseMessage(); |
| 85 | + InputStream stubInputStream = IOUtils.toInputStream(mockedResponse, "UTF-8"); |
| 86 | + Mockito.doReturn(stubInputStream).when(mockHttpURLConnection).getInputStream(); |
| 87 | + |
| 88 | + if(expectedResult instanceof GHRepositoryViewTraffic){ |
| 89 | + GHRepositoryViewTraffic views = repo.getViewTraffic(); |
| 90 | + checkResponse(expectedResult, views); |
| 91 | + } |
| 92 | + else if(expectedResult instanceof GHRepositoryCloneTraffic) { |
| 93 | + GHRepositoryCloneTraffic clones = repo.getCloneTraffic(); |
| 94 | + checkResponse(expectedResult, clones); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testGetViews() throws IOException{ |
| 100 | + GHRepositoryViewTraffic expectedResult = new GHRepositoryViewTraffic( |
| 101 | + 21523359, |
| 102 | + 65534, |
| 103 | + Arrays.asList( |
| 104 | + new Daily("2016-10-10T00:00:00Z", 3, 2), |
| 105 | + new Daily("2016-10-11T00:00:00Z", 9, 4), |
| 106 | + new Daily("2016-10-12T00:00:00Z", 27, 8), |
| 107 | + new Daily("2016-10-13T00:00:00Z", 81, 16), |
| 108 | + new Daily("2016-10-14T00:00:00Z", 243, 32), |
| 109 | + new Daily("2016-10-15T00:00:00Z", 729, 64), |
| 110 | + new Daily("2016-10-16T00:00:00Z", 2187, 128), |
| 111 | + new Daily("2016-10-17T00:00:00Z", 6561, 256), |
| 112 | + new Daily("2016-10-18T00:00:00Z", 19683, 512), |
| 113 | + new Daily("2016-10-19T00:00:00Z", 59049, 1024), |
| 114 | + new Daily("2016-10-20T00:00:00Z", 177147, 2048), |
| 115 | + new Daily("2016-10-21T00:00:00Z", 531441, 4096), |
| 116 | + new Daily("2016-10-22T00:00:00Z", 1594323, 8192), |
| 117 | + new Daily("2016-10-23T00:00:00Z", 4782969, 16384), |
| 118 | + new Daily("2016-10-24T00:00:00Z", 14348907, 32768) |
| 119 | + ) |
| 120 | + ); |
| 121 | + testTraffic(expectedResult); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testGetClones() throws IOException{ |
| 126 | + GHRepositoryCloneTraffic expectedResult = new GHRepositoryCloneTraffic( |
| 127 | + 1500, |
| 128 | + 455, |
| 129 | + Arrays.asList( |
| 130 | + new DayInfo("2016-10-10T00:00:00Z", 10,3), |
| 131 | + new DayInfo("2016-10-11T00:00:00Z", 20,6), |
| 132 | + new DayInfo("2016-10-12T00:00:00Z", 30,5), |
| 133 | + new DayInfo("2016-10-13T00:00:00Z", 40,7), |
| 134 | + new DayInfo("2016-10-14T00:00:00Z", 50,11), |
| 135 | + new DayInfo("2016-10-15T00:00:00Z", 60,12), |
| 136 | + new DayInfo("2016-10-16T00:00:00Z", 70,19), |
| 137 | + new DayInfo("2016-10-17T00:00:00Z", 170,111), |
| 138 | + new DayInfo("2016-10-18T00:00:00Z", 180,70), |
| 139 | + new DayInfo("2016-10-19T00:00:00Z", 190,10), |
| 140 | + new DayInfo("2016-10-20T00:00:00Z", 200,18), |
| 141 | + new DayInfo("2016-10-21T00:00:00Z", 210,8), |
| 142 | + new DayInfo("2016-10-22T00:00:00Z", 220,168), |
| 143 | + new DayInfo("2016-10-23T00:00:00Z", 5,2), |
| 144 | + new DayInfo("2016-10-24T00:00:00Z", 45,5) |
| 145 | + ) |
| 146 | + ); |
| 147 | + testTraffic(expectedResult); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void testGetTrafficStatsAccessFailureDueToInsufficientPermissions() throws IOException { |
| 152 | + String errorMsg = "Exception should be thrown, since we don't have permission to access repo traffic info."; |
| 153 | + GitHub gitHub = GitHub.connect(login, null); |
| 154 | + GHRepository repo = gitHub.getUser(login).getRepository(repositoryName); |
| 155 | + try { |
| 156 | + repo.getViewTraffic(); |
| 157 | + Assert.fail(errorMsg); |
| 158 | + } |
| 159 | + catch (HttpException ex){ |
| 160 | + } |
| 161 | + try { |
| 162 | + repo.getCloneTraffic(); |
| 163 | + Assert.fail(errorMsg); |
| 164 | + } |
| 165 | + catch (HttpException ex){ |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments