Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 46a141d

Browse filesBrowse files
committed
Use consistent templating for all tests
1 parent 66de069 commit 46a141d
Copy full SHA for 46a141d

File tree

Expand file treeCollapse file tree

40 files changed

+108
-99
lines changed
Filter options
Expand file treeCollapse file tree

40 files changed

+108
-99
lines changed

‎src/test/java/org/kohsuke/github/AbstractGitHubWireMockTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/AbstractGitHubWireMockTest.java
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.kohsuke.github;
22

33
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
4+
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
5+
import com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.HandlebarsCurrentDateHelper;
46
import org.apache.commons.io.IOUtils;
57
import org.hamcrest.Description;
68
import org.hamcrest.Matcher;
@@ -11,6 +13,8 @@
1113
import org.junit.Before;
1214
import org.junit.Rule;
1315
import org.kohsuke.github.junit.GitHubWireMockRule;
16+
import wiremock.com.github.jknack.handlebars.Helper;
17+
import wiremock.com.github.jknack.handlebars.Options;
1418

1519
import java.io.File;
1620
import java.io.FileInputStream;
@@ -48,6 +52,8 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
4852
@Rule
4953
public final GitHubWireMockRule mockGitHub;
5054

55+
protected final TemplatingHelper templating = new TemplatingHelper();
56+
5157
public AbstractGitHubWireMockTest() {
5258
mockGitHub = new GitHubWireMockRule(this.getWireMockOptions());
5359
}
@@ -285,4 +291,23 @@ public static void assertFalse(Boolean condition) {
285291
assertThat(condition, Matchers.is(false));
286292
}
287293

294+
protected static class TemplatingHelper {
295+
public Date testStartDate = new Date();
296+
297+
public ResponseTemplateTransformer newResponseTransformer() {
298+
testStartDate = new Date();
299+
return ResponseTemplateTransformer.builder()
300+
.global(true)
301+
.maxCacheEntries(0L)
302+
.helper("testStartDate", new Helper<Object>() {
303+
private HandlebarsCurrentDateHelper helper = new HandlebarsCurrentDateHelper();
304+
@Override
305+
public Object apply(final Object context, final Options options) throws IOException {
306+
return this.helper.apply(TemplatingHelper.this.testStartDate, options);
307+
}
308+
})
309+
.build();
310+
}
311+
}
312+
288313
}

‎src/test/java/org/kohsuke/github/AbuseLimitHandlerTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/AbuseLimitHandlerTest.java
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.kohsuke.github;
22

33
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
4-
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
54
import org.junit.Test;
65

76
import java.io.IOException;
@@ -36,8 +35,7 @@ public AbuseLimitHandlerTest() {
3635

3736
@Override
3837
protected WireMockConfiguration getWireMockOptions() {
39-
return super.getWireMockOptions()
40-
.extensions(ResponseTemplateTransformer.builder().global(true).maxCacheEntries(0L).build());
38+
return super.getWireMockOptions().extensions(templating.newResponseTransformer());
4139
}
4240

4341
@Test

‎src/test/java/org/kohsuke/github/GHRateLimitTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/GHRateLimitTest.java
+31-17Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
44
import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
55
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
6-
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
76
import org.hamcrest.CoreMatchers;
87
import org.junit.Test;
98

109
import java.io.IOException;
10+
import java.time.Duration;
1111
import java.util.Date;
1212

1313
import static org.hamcrest.CoreMatchers.*;
14-
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
1514
import static org.hamcrest.core.IsInstanceOf.instanceOf;
1615

1716
/**
@@ -43,8 +42,7 @@ public GHRateLimitTest() {
4342

4443
@Override
4544
protected WireMockConfiguration getWireMockOptions() {
46-
return super.getWireMockOptions()
47-
.extensions(ResponseTemplateTransformer.builder().global(true).maxCacheEntries(0L).build());
45+
return super.getWireMockOptions().extensions(templating.newResponseTransformer());
4846
}
4947

5048
@Test
@@ -55,11 +53,9 @@ public void testGitHubRateLimit() throws Exception {
5553
assertThat(mockGitHub.getRequestCount(), equalTo(0));
5654

5755
// 4897 is just the what the limit was when the snapshot was taken
58-
previousLimit = GHRateLimit
59-
.fromHeaderRecord(new GHRateLimit.Record(5000, 4897, System.currentTimeMillis() / 1000L));
60-
61-
// Give this a moment
62-
Thread.sleep(1000);
56+
previousLimit = GHRateLimit.fromHeaderRecord(new GHRateLimit.Record(5000,
57+
4897,
58+
(templating.testStartDate.getTime() + Duration.ofHours(1).toMillis()) / 1000L));
6359

6460
// -------------------------------------------------------------
6561
// /user gets response with rate limit information
@@ -90,6 +86,9 @@ public void testGitHubRateLimit() throws Exception {
9086
rateLimit = gitHub.getRateLimit();
9187
assertThat(mockGitHub.getRequestCount(), equalTo(2));
9288

89+
// Because remaining and reset date are unchanged, the header should be unchanged as well
90+
assertThat(gitHub.lastRateLimit(), sameInstance(headerRateLimit));
91+
9392
// rate limit request is free, remaining is unchanged
9493
verifyRateLimitValues(previousLimit, previousLimit.getRemaining());
9594
previousLimit = rateLimit;
@@ -101,13 +100,18 @@ public void testGitHubRateLimit() throws Exception {
101100
rateLimit = gitHub.getRateLimit();
102101
assertThat(mockGitHub.getRequestCount(), equalTo(3));
103102

103+
// Because remaining and reset date are unchanged, the header should be unchanged as well
104+
assertThat(gitHub.lastRateLimit(), sameInstance(headerRateLimit));
105+
104106
// rate limit request is free, remaining is unchanged
105107
verifyRateLimitValues(previousLimit, previousLimit.getRemaining());
106108
previousLimit = rateLimit;
107109

108110
gitHub.getOrganization(GITHUB_API_TEST_ORG);
109111
assertThat(mockGitHub.getRequestCount(), equalTo(4));
110112

113+
// Because remaining has changed the header should be different
114+
assertThat(gitHub.lastRateLimit(), not(sameInstance(headerRateLimit)));
111115
assertThat(gitHub.lastRateLimit(), not(equalTo(headerRateLimit)));
112116
rateLimit = gitHub.lastRateLimit();
113117

@@ -118,19 +122,21 @@ public void testGitHubRateLimit() throws Exception {
118122
headerRateLimit = rateLimit;
119123

120124
// ratelimit() should prefer headerRateLimit when it is most recent and not expired
121-
assertThat(gitHub.rateLimit(), equalTo(headerRateLimit));
125+
assertThat(gitHub.rateLimit(), sameInstance(headerRateLimit));
122126

123127
assertThat(mockGitHub.getRequestCount(), equalTo(4));
124128

129+
// AT THIS POINT WE SIMULATE A RATE LIMIT RESET
130+
125131
// Give this a moment
126132
Thread.sleep(2000);
127133

128134
// Always requests new info
129135
rateLimit = gitHub.getRateLimit();
130136
assertThat(mockGitHub.getRequestCount(), equalTo(5));
131137

132-
// rate limit request is free, remaining is unchanged
133-
verifyRateLimitValues(previousLimit, previousLimit.getRemaining());
138+
// rate limit request is free, remaining is unchanged date is later
139+
verifyRateLimitValues(previousLimit, previousLimit.getRemaining(), true);
134140
previousLimit = rateLimit;
135141

136142
// When getRateLimit() succeeds, headerRateLimit updates as usual as well (if needed)
@@ -142,7 +148,7 @@ public void testGitHubRateLimit() throws Exception {
142148

143149
// Verify different instances can be compared
144150
// TODO: This is not work currently because the header rate limit has unknowns for records other than core.
145-
// assertThat(gitHub.rateLimit().getCore(), equalTo(rateLimit.getCore()));
151+
// assertThat(gitHub.rateLimit(), equalTo(rateLimit));
146152

147153
assertThat(gitHub.rateLimit(), not(sameInstance(headerRateLimit)));
148154
assertThat(gitHub.rateLimit(), sameInstance(gitHub.lastRateLimit()));
@@ -151,13 +157,20 @@ public void testGitHubRateLimit() throws Exception {
151157
}
152158

153159
private void verifyRateLimitValues(GHRateLimit previousLimit, int remaining) {
160+
verifyRateLimitValues(previousLimit, remaining, false);
161+
}
162+
163+
private void verifyRateLimitValues(GHRateLimit previousLimit, int remaining, boolean changedResetDate) {
164+
// newer or unchange
165+
int resetComparisionValue = changedResetDate ? 1 : 0;
166+
154167
// Basic checks of values
155168
assertThat(rateLimit, notNullValue());
156169
assertThat(rateLimit.getLimit(), equalTo(previousLimit.getLimit()));
157170
assertThat(rateLimit.getRemaining(), equalTo(remaining));
158171

159172
// Check that the reset date of the current limit is not older than the previous one
160-
assertThat(rateLimit.getResetDate().compareTo(previousLimit.getResetDate()), greaterThanOrEqualTo(0));
173+
assertThat(rateLimit.getResetDate().compareTo(previousLimit.getResetDate()), equalTo(resetComparisionValue));
161174

162175
// Additional checks for record values
163176
assertThat(rateLimit.getCore().getLimit(), equalTo(rateLimit.getLimit()));
@@ -257,7 +270,8 @@ public void testGitHubEnterpriseDoesNotHaveRateLimit() throws Exception {
257270
assertThat(rateLimit, notNullValue());
258271
assertThat(rateLimit.getLimit(), equalTo(5000));
259272
assertThat(rateLimit.getRemaining(), equalTo(4978));
260-
assertThat(rateLimit.getResetDate().compareTo(lastReset), greaterThanOrEqualTo(0));
273+
// The previous record was an "Unknown", so even though this records resets sooner we take it
274+
assertThat(rateLimit.getResetDate().compareTo(lastReset), equalTo(-1));
261275
lastReset = rateLimit.getResetDate();
262276

263277
GHRateLimit headerRateLimit = rateLimit;
@@ -266,7 +280,7 @@ public void testGitHubEnterpriseDoesNotHaveRateLimit() throws Exception {
266280
Thread.sleep(1000);
267281

268282
// ratelimit() uses headerRateLimit if available and headerRateLimit is not expired
269-
assertThat(gitHub.rateLimit(), equalTo(headerRateLimit));
283+
assertThat(gitHub.rateLimit(), sameInstance(headerRateLimit));
270284

271285
assertThat(mockGitHub.getRequestCount(), equalTo(5));
272286

@@ -365,7 +379,7 @@ private void executeExpirationTest() throws Exception {
365379
assertThat("rateLimit() selects header instance when not expired, does not ask server",
366380
gitHub.rateLimit(),
367381
sameInstance(headerRateLimit));
368-
assertThat("rateLimit() selects header instance when not expired, does not ask server",
382+
assertThat("lastRateLimit() always selects header instance, does not ask server",
369383
gitHub.lastRateLimit(),
370384
sameInstance(headerRateLimit));
371385

‎src/test/java/org/kohsuke/github/RateLimitCheckerTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/RateLimitCheckerTest.java
+2-22Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package org.kohsuke.github;
22

33
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
4-
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
5-
import com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.HandlebarsCurrentDateHelper;
64
import org.junit.Test;
7-
import wiremock.com.github.jknack.handlebars.Helper;
8-
import wiremock.com.github.jknack.handlebars.Options;
95

106
import java.io.IOException;
117
import java.util.Date;
@@ -22,26 +18,14 @@ public class RateLimitCheckerTest extends AbstractGitHubWireMockTest {
2218

2319
GHRateLimit rateLimit = null;
2420
GHRateLimit previousLimit = null;
25-
Date testStartDate = new Date();
2621

2722
public RateLimitCheckerTest() {
2823
useDefaultGitHub = false;
2924
}
3025

3126
@Override
3227
protected WireMockConfiguration getWireMockOptions() {
33-
34-
return super.getWireMockOptions().extensions(ResponseTemplateTransformer.builder()
35-
.global(true)
36-
.maxCacheEntries(0L)
37-
.helper("testStartDate", new Helper<Object>() {
38-
private HandlebarsCurrentDateHelper helper = new HandlebarsCurrentDateHelper();
39-
@Override
40-
public Object apply(final Object context, final Options options) throws IOException {
41-
return this.helper.apply(RateLimitCheckerTest.this.testStartDate, options);
42-
}
43-
})
44-
.build());
28+
return super.getWireMockOptions().extensions(templating.newResponseTransformer());
4529
}
4630

4731
@Test
@@ -51,14 +35,10 @@ public void testGitHubRateLimit() throws Exception {
5135

5236
assertThat(mockGitHub.getRequestCount(), equalTo(0));
5337

54-
// // 4897 is just the what the limit was when the snapshot was taken
55-
// previousLimit = GHRateLimit
56-
// .fromHeaderRecord(new GHRateLimit.Record(5000, 4897, System.currentTimeMillis() / 1000L));
57-
5838
// Give this a moment
5939
Thread.sleep(1000);
6040

61-
testStartDate = new Date();
41+
templating.testStartDate = new Date();
6242
// -------------------------------------------------------------
6343
// /user gets response with rate limit information
6444
gitHub = getGitHubBuilder().withRateLimitChecker(new RateLimitChecker.LiteralValue(4500))

‎src/test/java/org/kohsuke/github/RateLimitHandlerTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/RateLimitHandlerTest.java
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.kohsuke.github;
22

33
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
4-
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
54
import org.junit.Test;
65

76
import java.io.IOException;
@@ -36,8 +35,7 @@ public RateLimitHandlerTest() {
3635

3736
@Override
3837
protected WireMockConfiguration getWireMockOptions() {
39-
return super.getWireMockOptions()
40-
.extensions(ResponseTemplateTransformer.builder().global(true).maxCacheEntries(0L).build());
38+
return super.getWireMockOptions().extensions(templating.newResponseTransformer());
4139
}
4240

4341
@Test

‎src/test/java/org/kohsuke/github/extras/GitHubCachingTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/extras/GitHubCachingTest.java
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.kohsuke.github.extras;
22

33
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
4-
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
54
import com.squareup.okhttp.Cache;
65
import com.squareup.okhttp.OkHttpClient;
76
import com.squareup.okhttp.OkUrlFactory;
@@ -36,8 +35,7 @@ public GitHubCachingTest() {
3635

3736
@Override
3837
protected WireMockConfiguration getWireMockOptions() {
39-
return super.getWireMockOptions()
40-
.extensions(ResponseTemplateTransformer.builder().global(true).maxCacheEntries(0L).build());
38+
return super.getWireMockOptions().extensions(templating.newResponseTransformer());
4139
}
4240

4341
@Before

‎src/test/java/org/kohsuke/github/extras/OkHttpConnectorTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/extras/OkHttpConnectorTest.java
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.kohsuke.github.extras;
22

33
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
4-
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
54
import com.squareup.okhttp.Cache;
65
import com.squareup.okhttp.OkHttpClient;
76
import com.squareup.okhttp.OkUrlFactory;
@@ -66,8 +65,7 @@ public OkHttpConnectorTest() {
6665

6766
@Override
6867
protected WireMockConfiguration getWireMockOptions() {
69-
return super.getWireMockOptions()
70-
.extensions(ResponseTemplateTransformer.builder().global(true).maxCacheEntries(0L).build());
68+
return super.getWireMockOptions().extensions(templating.newResponseTransformer());
7169
}
7270

7371
@Before

‎src/test/java/org/kohsuke/github/extras/okhttp3/GitHubCachingTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/extras/okhttp3/GitHubCachingTest.java
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.kohsuke.github.extras.okhttp3;
22

33
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
4-
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
54
import okhttp3.Cache;
65
import okhttp3.OkHttpClient;
76
import org.apache.commons.io.FileUtils;
@@ -38,7 +37,7 @@ protected WireMockConfiguration getWireMockOptions() {
3837
return super.getWireMockOptions()
3938
// Use the same data files as the 2.x test
4039
.usingFilesUnderDirectory(baseRecordPath.replace("/okhttp3/", "/"))
41-
.extensions(ResponseTemplateTransformer.builder().global(true).maxCacheEntries(0L).build());
40+
.extensions(templating.newResponseTransformer());
4241
}
4342

4443
@Before

‎src/test/java/org/kohsuke/github/extras/okhttp3/OkHttpConnectorTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/extras/okhttp3/OkHttpConnectorTest.java
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.kohsuke.github.extras.okhttp3;
22

33
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
4-
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
54
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
65
import okhttp3.Cache;
76
import okhttp3.OkHttpClient;
@@ -72,7 +71,7 @@ protected WireMockConfiguration getWireMockOptions() {
7271
return super.getWireMockOptions()
7372
// Use the same data files as the 2.x test
7473
.usingFilesUnderDirectory(baseRecordPath.replace("/okhttp3/", "/"))
75-
.extensions(ResponseTemplateTransformer.builder().global(true).maxCacheEntries(0L).build());
74+
.extensions(templating.newResponseTransformer());
7675
}
7776

7877
@Before

‎src/test/resources/org/kohsuke/github/AbuseLimitHandlerTest/wiremock/testHandler_Fail/mappings/repos_github-api-test-org_temp-testratelimithandler_fail-2-79fb10.json

Copy file name to clipboardExpand all lines: src/test/resources/org/kohsuke/github/AbuseLimitHandlerTest/wiremock/testHandler_Fail/mappings/repos_github-api-test-org_temp-testratelimithandler_fail-2-79fb10.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"Retry-After": "30",
2222
"X-RateLimit-Limit": "5000",
2323
"X-RateLimit-Remaining": "4000",
24-
"X-RateLimit-Reset": "{{now offset='2 seconds' format='unix'}}",
24+
"X-RateLimit-Reset": "{{testStartDate offset='3 seconds' format='unix'}}",
2525
"Cache-Control": "private, max-age=60, s-maxage=60",
2626
"Vary": [
2727
"Accept, Authorization, Cookie, X-GitHub-OTP",

‎src/test/resources/org/kohsuke/github/AbuseLimitHandlerTest/wiremock/testHandler_Fail/mappings/repos_github-api-test-org_temp-testratelimithandler_fail-3-574da1.json

Copy file name to clipboardExpand all lines: src/test/resources/org/kohsuke/github/AbuseLimitHandlerTest/wiremock/testHandler_Fail/mappings/repos_github-api-test-org_temp-testratelimithandler_fail-3-574da1.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"Status": "200 OK",
2121
"X-RateLimit-Limit": "5000",
2222
"X-RateLimit-Remaining": "4922",
23-
"X-RateLimit-Reset": "{{now offset='2 seconds' format='unix'}}",
23+
"X-RateLimit-Reset": "{{testStartDate offset='3 seconds' format='unix'}}",
2424
"Cache-Control": "private, max-age=60, s-maxage=60",
2525
"Vary": [
2626
"Accept, Authorization, Cookie, X-GitHub-OTP",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.