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,21 @@ 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- }
64+ @ Override
65+ @ BeforeEach
66+ void beforeEach () throws Exception {
67+ super .beforeEach ();
7668
77- @ Before
78- public void setUp () throws Exception {
79- resetAllScenarios ();
69+ githubApi .resetScenarios ();
70+ githubRaw .resetScenarios ();
8071
8172 handler = new RingBufferLogHandler (1000 );
8273 final Logger logger = Logger .getLogger (getClass ().getName ());
@@ -98,13 +89,12 @@ public void setUp() throws Exception {
9889 ApiRateLimitChecker .resetLocalChecker ();
9990 }
10091
101- @ After
102- public void tearDown () throws Exception {
92+ @ AfterEach
93+ void afterEach () {
10394 GitHubConfiguration .get ().setEndpoints (new ArrayList <>());
10495 }
10596
10697 private void setupStubs (List <RateLimit > scenarios ) throws Exception {
107-
10898 githubApi .stubFor (get (urlEqualTo ("/meta" ))
10999 .willReturn (aResponse ().withStatus (200 ).withBody ("{\" verifiable_password_authentication\" : false}" )));
110100
@@ -159,13 +149,13 @@ private void setupStubs(List<RateLimit> scenarios) throws Exception {
159149 githubApi .stubFor (scenario );
160150 }
161151
162- github = Connector .connect ("http://localhost:" + githubApi .port (), null );
152+ github = Connector .connect ("http://localhost:" + githubApi .getPort (), null );
163153 initialRequestCount = getRequestCount (githubApi );
164154 assertEquals (2 , initialRequestCount );
165155 }
166156
167157 @ Test
168- public void NoCheckerConfigured () throws Exception {
158+ void NoCheckerConfigured () throws Exception {
169159 // set up scenarios
170160 List <RateLimit > scenarios = new ArrayList <>();
171161 long now = System .currentTimeMillis ();
@@ -204,7 +194,7 @@ public void NoCheckerConfigured() throws Exception {
204194 }
205195
206196 @ Test
207- public void NoCheckerConfiguredWithEndpoint () throws Exception {
197+ void NoCheckerConfiguredWithEndpoint () throws Exception {
208198 // set up scenarios
209199 List <RateLimit > scenarios = new ArrayList <>();
210200 long now = System .currentTimeMillis ();
@@ -240,7 +230,7 @@ public void NoCheckerConfiguredWithEndpoint() throws Exception {
240230 * @author Julian V. Modesto
241231 */
242232 @ Test
243- public void ThrottleOnOverTestWithQuota () throws Exception {
233+ void ThrottleOnOverTestWithQuota () throws Exception {
244234 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleOnOver );
245235
246236 // set up scenarios
@@ -269,7 +259,7 @@ public void ThrottleOnOverTestWithQuota() throws Exception {
269259 * @author Julian V. Modesto
270260 */
271261 @ Test
272- public void ThrottleOnNormalizeTestWithQuota () throws Exception {
262+ void ThrottleOnNormalizeTestWithQuota () throws Exception {
273263 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleForNormalize );
274264
275265 // set up scenarios
@@ -296,7 +286,7 @@ public void ThrottleOnNormalizeTestWithQuota() throws Exception {
296286 * @author Marc Salles Navarro
297287 */
298288 @ Test
299- public void NoThrottleTestShouldNotThrottle () throws Exception {
289+ void NoThrottleTestShouldNotThrottle () throws Exception {
300290 // set up scenarios
301291 List <RateLimit > scenarios = new ArrayList <>();
302292 int limit = 5000 ;
@@ -324,8 +314,7 @@ public void NoThrottleTestShouldNotThrottle() throws Exception {
324314 * @author Marc Salles Navarro
325315 */
326316 @ Test
327- public void NoThrottleTestShouldNotThrottle404 () throws Exception {
328-
317+ void NoThrottleTestShouldNotThrottle404 () throws Exception {
329318 setupStubs (new ArrayList <>());
330319 GHRateLimit .Record initial = github .lastRateLimit ().getCore ();
331320 assertEquals (2 , getRequestCount (githubApi ));
@@ -341,7 +330,8 @@ public void NoThrottleTestShouldNotThrottle404() throws Exception {
341330 github .getMeta ();
342331
343332 // The core should be unknown, but different from initial
344- assertTrue (github .rateLimit ().getCore () instanceof GHRateLimit .UnknownLimitRecord );
333+ assertInstanceOf (
334+ GHRateLimit .UnknownLimitRecord .class , github .rateLimit ().getCore ());
345335 assertNotEquals (initial , github .rateLimit ().getCore ());
346336
347337 // there should be no output
@@ -357,7 +347,7 @@ public void NoThrottleTestShouldNotThrottle404() throws Exception {
357347 * @author Marc Salles Navarro
358348 */
359349 @ Test
360- public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom () throws Exception {
350+ void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom () throws Exception {
361351 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleOnOver );
362352
363353 // set up scenarios
@@ -391,7 +381,7 @@ public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws
391381 * @author Julian V. Modesto
392382 */
393383 @ Test
394- public void ThrottleOnOverTest () throws Exception {
384+ void ThrottleOnOverTest () throws Exception {
395385 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleOnOver );
396386
397387 // set up scenarios
@@ -462,7 +452,7 @@ public void ThrottleOnOverTest() throws Exception {
462452 * @author Julian V. Modesto
463453 */
464454 @ Test
465- public void ThrottleForNormalizeTestWithinIdeal () throws Exception {
455+ void ThrottleForNormalizeTestWithinIdeal () throws Exception {
466456 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleForNormalize );
467457
468458 List <RateLimit > scenarios = new ArrayList <>();
@@ -552,7 +542,7 @@ public void ThrottleForNormalizeTestWithinIdeal() throws Exception {
552542 * @author Julian V. Modesto
553543 */
554544 @ Test
555- public void NormalizeThrottleWithBurnedBuffer () throws Exception {
545+ void NormalizeThrottleWithBurnedBuffer () throws Exception {
556546 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleForNormalize );
557547
558548 long now = System .currentTimeMillis ();
@@ -614,7 +604,7 @@ public void NormalizeThrottleWithBurnedBuffer() throws Exception {
614604 * @author Alex Taylor
615605 */
616606 @ Test
617- public void OnOverThrottleTimingRateLimitCheck () throws Exception {
607+ void OnOverThrottleTimingRateLimitCheck () throws Exception {
618608 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleOnOver );
619609
620610 // Longer timings that test defaults for more consistent measurements.
@@ -695,7 +685,7 @@ public void OnOverThrottleTimingRateLimitCheck() throws Exception {
695685 * @author Alex Taylor
696686 */
697687 @ Test
698- public void NormalizeThrottleTimingRateLimitCheck () throws Exception {
688+ void NormalizeThrottleTimingRateLimitCheck () throws Exception {
699689 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleForNormalize );
700690
701691 ApiRateLimitChecker .setExpirationWaitMillis (60 );
@@ -763,7 +753,7 @@ public void NormalizeThrottleTimingRateLimitCheck() throws Exception {
763753 * @author Alex Taylor
764754 */
765755 @ Test
766- public void NormalizeExpectedIdealOverTime () throws Exception {
756+ void NormalizeExpectedIdealOverTime () throws Exception {
767757 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleForNormalize );
768758
769759 // Set up scenarios
@@ -840,7 +830,7 @@ public void NormalizeExpectedIdealOverTime() throws Exception {
840830 * @author Alex Taylor
841831 */
842832 @ Test
843- public void OnOverExpectedIdealOverTime () throws Exception {
833+ void OnOverExpectedIdealOverTime () throws Exception {
844834 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleOnOver );
845835
846836 long start = System .currentTimeMillis ();
@@ -894,7 +884,7 @@ public void OnOverExpectedIdealOverTime() throws Exception {
894884 * @author Alex Taylor
895885 */
896886 @ Test
897- public void ExpectedResetTimingNormalize () throws Exception {
887+ void ExpectedResetTimingNormalize () throws Exception {
898888 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleForNormalize );
899889
900890 // Use a longer notification interval to make the test produce stable output
@@ -940,7 +930,7 @@ public void ExpectedResetTimingNormalize() throws Exception {
940930 * @author Alex Taylor
941931 */
942932 @ Test
943- public void ExpectedResetTimingOnOver () throws Exception {
933+ void ExpectedResetTimingOnOver () throws Exception {
944934 GitHubConfiguration .get ().setApiRateLimitChecker (ApiRateLimitChecker .ThrottleOnOver );
945935
946936 // Use a longer notification interval to make the test produce stable output
0 commit comments