66
77import com .github .tomakehurst .wiremock .WireMockServer ;
88import com .github .tomakehurst .wiremock .client .WireMock ;
9+ import com .github .tomakehurst .wiremock .junit5 .WireMockRuntimeInfo ;
10+ import com .github .tomakehurst .wiremock .junit5 .WireMockTest ;
911import com .google .inject .Guice ;
1012import io .jenkins .tools .pluginmodernizer .core .GuiceModule ;
1113import io .jenkins .tools .pluginmodernizer .core .config .Config ;
3335
3436@ ExtendWith ({MockitoExtension .class })
3537@ Execution (ExecutionMode .CONCURRENT )
38+ @ WireMockTest
3639class PluginServiceTest {
3740
3841 @ TempDir
@@ -157,11 +160,10 @@ public void shouldExtractRepoNameWithGitSuffix() throws Exception {
157160 }
158161
159162 @ Test
160- public void shouldDownloadPluginVersionDataUpdateCenterData () throws Exception {
163+ public void shouldDownloadPluginVersionDataUpdateCenterData (WireMockRuntimeInfo wmRuntimeInfo ) throws Exception {
161164
162- WireMockServer server = new WireMockServer (40465 );
163- server .start ();
164- WireMock wireMock = new WireMock (40465 );
165+ WireMock wireMock = wmRuntimeInfo .getWireMock ();
166+ int portNumber = wmRuntimeInfo .getHttpPort ();
165167
166168 CacheManager cacheManager = Mockito .mock (CacheManager .class );
167169 Path cacheRoot = Mockito .mock (Path .class );
@@ -173,7 +175,7 @@ public void shouldDownloadPluginVersionDataUpdateCenterData() throws Exception {
173175 .willReturn (WireMock .okJson (JsonUtils .toJson (updateCenterData ))));
174176
175177 // No found from cache
176- doReturn (new URL ("http://localhost:40465 /update-center.json" ))
178+ doReturn (new URL ("http://localhost:%d /update-center.json" . formatted ( portNumber ) ))
177179 .when (config )
178180 .getJenkinsUpdateCenter ();
179181
@@ -222,9 +224,10 @@ public void shouldFailIfSCMFormatIsInvalid() throws Exception {
222224 @ Test
223225 public void shouldDownloadPluginVersionDataPluginHealthScore () throws Exception {
224226
225- WireMockServer server = new WireMockServer (40466 );
227+ WireMockServer server = new WireMockServer ();
226228 server .start ();
227- WireMock wireMock = new WireMock (40466 );
229+ int portNumber = server .getOptions ().portNumber ();
230+ WireMock wireMock = new WireMock (server .getOptions ().portNumber ());
228231
229232 CacheManager cacheManager = Mockito .mock (CacheManager .class );
230233 Path cacheRoot = Mockito .mock (Path .class );
@@ -235,7 +238,9 @@ public void shouldDownloadPluginVersionDataPluginHealthScore() throws Exception
235238 .willReturn (WireMock .okJson (JsonUtils .toJson (healthScoreData ))));
236239
237240 // No found from cache
238- doReturn (new URL ("http://localhost:40466/api/scores" )).when (config ).getPluginHealthScore ();
241+ doReturn (new URL ("http://localhost:%d/api/scores" .formatted (portNumber )))
242+ .when (config )
243+ .getPluginHealthScore ();
239244
240245 // Get result
241246 PluginService service = getService (config , cacheManager );
@@ -244,11 +249,10 @@ public void shouldDownloadPluginVersionDataPluginHealthScore() throws Exception
244249 }
245250
246251 @ Test
247- public void shouldDownloadPluginInstallationsData () throws Exception {
252+ public void shouldDownloadPluginInstallationsData (WireMockRuntimeInfo wmRuntimeInfo ) throws Exception {
248253
249- WireMockServer server = new WireMockServer (40467 );
250- server .start ();
251- WireMock wireMock = new WireMock (40467 );
254+ WireMock wireMock = wmRuntimeInfo .getWireMock ();
255+ int portNumber = wmRuntimeInfo .getHttpPort ();
252256
253257 CacheManager cacheManager = Mockito .mock (CacheManager .class );
254258 Path cacheRoot = Mockito .mock (Path .class );
@@ -262,7 +266,7 @@ public void shouldDownloadPluginInstallationsData() throws Exception {
262266 wireMock .register (WireMock .get (WireMock .urlEqualTo ("/jenkins-stats/svg/202406-plugins.csv" ))
263267 .willReturn (WireMock .ok ("\" valid-plugin\" ,\" 1\" \n " + "\" valid-plugin2\" ,\" 1\" " )));
264268
265- URL url = new URL ("http://localhost:40467 /jenkins-stats/svg/202406-plugins.csv" );
269+ URL url = new URL ("http://localhost:%d /jenkins-stats/svg/202406-plugins.csv" . formatted ( portNumber ) );
266270
267271 // No found from cache
268272 doReturn (url ).when (config ).getPluginStatsInstallations ();
0 commit comments