22
33import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
44import static com .github .tomakehurst .wiremock .client .WireMock .get ;
5- import static com .github .tomakehurst .wiremock .client .WireMock .resetAllScenarios ;
65import static com .github .tomakehurst .wiremock .client .WireMock .urlEqualTo ;
7- import static org .junit .Assert .assertEquals ;
8- import static org .junit .Assert .assertFalse ;
9- import static org .junit .Assert .assertNotEquals ;
10- import static org .junit .Assert .assertTrue ;
6+ import static org .junit .jupiter .api .Assertions .*;
117
12- import com .github .tomakehurst .wiremock .WireMockServer ;
138import com .github .tomakehurst .wiremock .client .ScenarioMappingBuilder ;
9+ import com .github .tomakehurst .wiremock .junit5 .WireMockExtension ;
1410import com .github .tomakehurst .wiremock .matching .RequestPatternBuilder ;
1511import com .github .tomakehurst .wiremock .stubbing .Scenario ;
1612import hudson .util .LogTaskListener ;
2521import java .util .logging .Logger ;
2622import java .util .stream .Stream ;
2723import org .jenkinsci .plugins .github .config .GitHubServerConfig ;
28- import org .junit .After ;
29- import org .junit .Before ;
30- import org .junit .Test ;
24+ import org .junit .jupiter . api . AfterEach ;
25+ import org .junit .jupiter . api . BeforeEach ;
26+ import org .junit .jupiter . api . Test ;
3127import org .kohsuke .github .GHRateLimit ;
3228import org .kohsuke .github .GitHub ;
3329import org .mockito .Mockito ;
3430
35- public class ApiRateLimitCheckerTest extends AbstractGitHubWireMockTest {
31+ class ApiRateLimitCheckerTest extends AbstractGitHubWireMockTest {
3632
3733 private RingBufferLogHandler handler ;
3834 private LogTaskListener listener ;
@@ -57,26 +53,18 @@ private long countOfOutputLinesContaining(String substring) {
5753 return countOfOutputLines (m -> m .contains (substring ));
5854 }
5955
60- public static int getRequestCount (WireMockServer server ) {
61- return server .countRequestsMatching (RequestPatternBuilder .allRequests ().build ())
56+ private static int getRequestCount (WireMockExtension extension ) {
57+ return extension
58+ .countRequestsMatching (RequestPatternBuilder .allRequests ().build ())
6259 .getCount ();
6360 }
6461
65- private class RateLimit {
66- final int remaining ;
67- final int limit ;
68- final Date reset ;
62+ private record RateLimit (int limit , int remaining , Date reset ) {}
6963
70- RateLimit (int limit , int remaining , Date reset ) {
71- this .limit = limit ;
72- this .remaining = remaining ;
73- this .reset = reset ;
74- }
75- }
76-
77- @ Before
78- public void setUp () throws Exception {
79- resetAllScenarios ();
64+ @ BeforeEach
65+ void beforeEach () {
66+ githubApi .resetScenarios ();
67+ githubRaw .resetScenarios ();
8068
8169 handler = new RingBufferLogHandler (1000 );
8270 final Logger logger = Logger .getLogger (getClass ().getName ());
@@ -98,13 +86,12 @@ public void setUp() throws Exception {
9886 ApiRateLimitChecker .resetLocalChecker ();
9987 }
10088
101- @ After
102- public void tearDown () throws Exception {
89+ @ AfterEach
90+ void afterEach () {
10391 GitHubConfiguration .get ().setEndpoints (new ArrayList <>());
10492 }
10593
10694 private void setupStubs (List <RateLimit > scenarios ) throws Exception {
107-
10895 githubApi .stubFor (get (urlEqualTo ("/meta" ))
10996 .willReturn (aResponse ().withStatus (200 ).withBody ("{\" verifiable_password_authentication\" : false}" )));
11097
@@ -159,13 +146,13 @@ private void setupStubs(List<RateLimit> scenarios) throws Exception {
159146 githubApi .stubFor (scenario );
160147 }
161148
162- github = Connector .connect ("http://localhost:" + githubApi .port (), null );
149+ github = Connector .connect ("http://localhost:" + githubApi .getPort (), null );
163150 initialRequestCount = getRequestCount (githubApi );
164151 assertEquals (2 , initialRequestCount );
165152 }
166153
167154 @ Test
168- public void NoCheckerConfigured () throws Exception {
155+ void NoCheckerConfigured () throws Exception {
169156 // set up scenarios
170157 List <RateLimit > scenarios = new ArrayList <>();
171158 long now = System .currentTimeMillis ();
@@ -204,7 +191,7 @@ public void NoCheckerConfigured() throws Exception {
204191 }
205192
206193 @ Test
207- public void NoCheckerConfiguredWithEndpoint () throws Exception {
194+ void NoCheckerConfiguredWithEndpoint () throws Exception {
208195 // set up scenarios
209196 List <RateLimit > scenarios = new ArrayList <>();
210197 long now = System .currentTimeMillis ();
@@ -240,7 +227,7 @@ public void NoCheckerConfiguredWithEndpoint() throws Exception {
240227 * @author Julian V. Modesto
241228 */
242229 @ Test
243- public void ThrottleOnOverTestWithQuota () throws Exception {
230+ void ThrottleOnOverTestWithQuota () throws Exception {
244231 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleOnOver );
245232
246233 // set up scenarios
@@ -269,7 +256,7 @@ public void ThrottleOnOverTestWithQuota() throws Exception {
269256 * @author Julian V. Modesto
270257 */
271258 @ Test
272- public void ThrottleOnNormalizeTestWithQuota () throws Exception {
259+ void ThrottleOnNormalizeTestWithQuota () throws Exception {
273260 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleForNormalize );
274261
275262 // set up scenarios
@@ -296,7 +283,7 @@ public void ThrottleOnNormalizeTestWithQuota() throws Exception {
296283 * @author Marc Salles Navarro
297284 */
298285 @ Test
299- public void NoThrottleTestShouldNotThrottle () throws Exception {
286+ void NoThrottleTestShouldNotThrottle () throws Exception {
300287 // set up scenarios
301288 List <RateLimit > scenarios = new ArrayList <>();
302289 int limit = 5000 ;
@@ -324,8 +311,7 @@ public void NoThrottleTestShouldNotThrottle() throws Exception {
324311 * @author Marc Salles Navarro
325312 */
326313 @ Test
327- public void NoThrottleTestShouldNotThrottle404 () throws Exception {
328-
314+ void NoThrottleTestShouldNotThrottle404 () throws Exception {
329315 setupStubs (new ArrayList <>());
330316 GHRateLimit .Record initial = github .lastRateLimit ().getCore ();
331317 assertEquals (2 , getRequestCount (githubApi ));
@@ -341,7 +327,8 @@ public void NoThrottleTestShouldNotThrottle404() throws Exception {
341327 github .getMeta ();
342328
343329 // The core should be unknown, but different from initial
344- assertTrue (github .rateLimit ().getCore () instanceof GHRateLimit .UnknownLimitRecord );
330+ assertInstanceOf (
331+ GHRateLimit .UnknownLimitRecord .class , github .rateLimit ().getCore ());
345332 assertNotEquals (initial , github .rateLimit ().getCore ());
346333
347334 // there should be no output
@@ -357,7 +344,7 @@ public void NoThrottleTestShouldNotThrottle404() throws Exception {
357344 * @author Marc Salles Navarro
358345 */
359346 @ Test
360- public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom () throws Exception {
347+ void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom () throws Exception {
361348 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleOnOver );
362349
363350 // set up scenarios
@@ -391,7 +378,7 @@ public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws
391378 * @author Julian V. Modesto
392379 */
393380 @ Test
394- public void ThrottleOnOverTest () throws Exception {
381+ void ThrottleOnOverTest () throws Exception {
395382 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleOnOver );
396383
397384 // set up scenarios
@@ -462,7 +449,7 @@ public void ThrottleOnOverTest() throws Exception {
462449 * @author Julian V. Modesto
463450 */
464451 @ Test
465- public void ThrottleForNormalizeTestWithinIdeal () throws Exception {
452+ void ThrottleForNormalizeTestWithinIdeal () throws Exception {
466453 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleForNormalize );
467454
468455 List <RateLimit > scenarios = new ArrayList <>();
@@ -552,7 +539,7 @@ public void ThrottleForNormalizeTestWithinIdeal() throws Exception {
552539 * @author Julian V. Modesto
553540 */
554541 @ Test
555- public void NormalizeThrottleWithBurnedBuffer () throws Exception {
542+ void NormalizeThrottleWithBurnedBuffer () throws Exception {
556543 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleForNormalize );
557544
558545 long now = System .currentTimeMillis ();
@@ -614,7 +601,7 @@ public void NormalizeThrottleWithBurnedBuffer() throws Exception {
614601 * @author Alex Taylor
615602 */
616603 @ Test
617- public void OnOverThrottleTimingRateLimitCheck () throws Exception {
604+ void OnOverThrottleTimingRateLimitCheck () throws Exception {
618605 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleOnOver );
619606
620607 // Longer timings that test defaults for more consistent measurements.
@@ -695,7 +682,7 @@ public void OnOverThrottleTimingRateLimitCheck() throws Exception {
695682 * @author Alex Taylor
696683 */
697684 @ Test
698- public void NormalizeThrottleTimingRateLimitCheck () throws Exception {
685+ void NormalizeThrottleTimingRateLimitCheck () throws Exception {
699686 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleForNormalize );
700687
701688 ApiRateLimitChecker .setExpirationWaitMillis (60 );
@@ -763,7 +750,7 @@ public void NormalizeThrottleTimingRateLimitCheck() throws Exception {
763750 * @author Alex Taylor
764751 */
765752 @ Test
766- public void NormalizeExpectedIdealOverTime () throws Exception {
753+ void NormalizeExpectedIdealOverTime () throws Exception {
767754 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleForNormalize );
768755
769756 // Set up scenarios
@@ -840,7 +827,7 @@ public void NormalizeExpectedIdealOverTime() throws Exception {
840827 * @author Alex Taylor
841828 */
842829 @ Test
843- public void OnOverExpectedIdealOverTime () throws Exception {
830+ void OnOverExpectedIdealOverTime () throws Exception {
844831 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleOnOver );
845832
846833 long start = System .currentTimeMillis ();
@@ -894,7 +881,7 @@ public void OnOverExpectedIdealOverTime() throws Exception {
894881 * @author Alex Taylor
895882 */
896883 @ Test
897- public void ExpectedResetTimingNormalize () throws Exception {
884+ void ExpectedResetTimingNormalize () throws Exception {
898885 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleForNormalize );
899886
900887 // Use a longer notification interval to make the test produce stable output
@@ -940,7 +927,7 @@ public void ExpectedResetTimingNormalize() throws Exception {
940927 * @author Alex Taylor
941928 */
942929 @ Test
943- public void ExpectedResetTimingOnOver () throws Exception {
930+ void ExpectedResetTimingOnOver () throws Exception {
944931 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleOnOver );
945932
946933 // Use a longer notification interval to make the test produce stable output
0 commit comments