|
1 | 1 | package io.jenkins.tools.pluginmodernizer.core.impl; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
4 | 6 | import static org.junit.jupiter.api.Assertions.fail; |
5 | 7 | import static org.mockito.ArgumentMatchers.any; |
6 | 8 | import static org.mockito.ArgumentMatchers.eq; |
7 | 9 | import static org.mockito.Mockito.*; |
8 | 10 |
|
9 | 11 | import io.jenkins.tools.pluginmodernizer.core.config.Config; |
10 | 12 | import io.jenkins.tools.pluginmodernizer.core.config.Settings; |
| 13 | +import io.jenkins.tools.pluginmodernizer.core.extractor.ModernizationMetadata; |
| 14 | +import io.jenkins.tools.pluginmodernizer.core.extractor.PluginMetadata; |
11 | 15 | import io.jenkins.tools.pluginmodernizer.core.github.GHService; |
| 16 | +import io.jenkins.tools.pluginmodernizer.core.model.DiffStats; |
12 | 17 | import io.jenkins.tools.pluginmodernizer.core.model.Plugin; |
| 18 | +import io.jenkins.tools.pluginmodernizer.core.model.PreconditionError; |
| 19 | +import io.jenkins.tools.pluginmodernizer.core.model.PluginProcessingException; |
13 | 20 | import io.jenkins.tools.pluginmodernizer.core.model.PluginVersionData; |
14 | 21 | import io.jenkins.tools.pluginmodernizer.core.model.Recipe; |
15 | 22 | import io.jenkins.tools.pluginmodernizer.core.utils.PluginService; |
|
18 | 25 | import java.nio.file.Paths; |
19 | 26 | import java.util.Arrays; |
20 | 27 | import java.util.List; |
| 28 | +import java.util.Set; |
21 | 29 | import org.apache.maven.artifact.versioning.ComparableVersion; |
22 | 30 | import org.junit.jupiter.api.BeforeEach; |
23 | 31 | import org.junit.jupiter.api.Test; |
| 32 | +import org.mockito.ArgumentCaptor; |
24 | 33 | import org.mockito.InjectMocks; |
25 | 34 | import org.mockito.Mock; |
26 | 35 | import org.mockito.MockitoAnnotations; |
@@ -361,6 +370,108 @@ void testMetadataOperationsSkippedWhenModernizationMetadataIsNull() throws Excep |
361 | 370 | verify(plugin, never()).openMetadataPullRequest(any()); |
362 | 371 | } |
363 | 372 |
|
| 373 | + @Test |
| 374 | + void testCollectModernizationMetadata_WithPreconditionErrors_ShouldRecordFailureReasons() throws Exception { |
| 375 | + // Arrange |
| 376 | + Plugin plugin = mock(Plugin.class); |
| 377 | + PluginMetadata pluginMetadata = mock(PluginMetadata.class); |
| 378 | + Config pluginConfig = mock(Config.class); |
| 379 | + Recipe recipe = mock(Recipe.class); |
| 380 | + |
| 381 | + when(plugin.getName()).thenReturn("test-plugin"); |
| 382 | + when(plugin.getMetadata()).thenReturn(pluginMetadata); |
| 383 | + when(pluginMetadata.getPluginName()).thenReturn("test-plugin"); |
| 384 | + when(pluginMetadata.getJenkinsVersion()).thenReturn(null); |
| 385 | + when(plugin.getJenkinsBaseline()).thenReturn("2.361"); |
| 386 | + when(plugin.getJenkinsVersion()).thenReturn("2.361.1"); |
| 387 | + when(plugin.getEffectiveBaseline()).thenReturn("2.361"); |
| 388 | + when(plugin.getConfig()).thenReturn(pluginConfig); |
| 389 | + when(pluginConfig.getRecipe()).thenReturn(recipe); |
| 390 | + when(recipe.getDisplayName()).thenReturn("Test Recipe"); |
| 391 | + when(recipe.getDescription()).thenReturn("Test description"); |
| 392 | + when(recipe.getTags()).thenReturn(Set.of()); |
| 393 | + when(recipe.getName()).thenReturn("test-recipe"); |
| 394 | + when(plugin.getPullRequestUrl()).thenReturn(null); |
| 395 | + when(pluginService.extractVersion(plugin)).thenReturn("1.0"); |
| 396 | + when(config.isDryRun()).thenReturn(false); |
| 397 | + when(plugin.getDiffStats(ghService, false)).thenReturn(new DiffStats(0, 0, 0)); |
| 398 | + when(plugin.hasErrors()).thenReturn(false); |
| 399 | + when(plugin.hasPreconditionErrors()).thenReturn(true); |
| 400 | + when(plugin.getPreconditionErrors()).thenReturn(Set.of(PreconditionError.PARENT_POM_1X)); |
| 401 | + when(plugin.getErrors()).thenReturn(List.of()); |
| 402 | + // GHService is not needed here; the existing try-catch in collectModernizationMetadata handles this |
| 403 | + doThrow(new PluginProcessingException("no gh", plugin)).when(ghService).getRepository(any(), any()); |
| 404 | + // Allow save() to proceed without NPE |
| 405 | + when(cacheManager.getLocation()).thenReturn(Path.of("target/test-cache")); |
| 406 | + |
| 407 | + java.lang.reflect.Method method = |
| 408 | + PluginModernizer.class.getDeclaredMethod("collectModernizationMetadata", Plugin.class); |
| 409 | + method.setAccessible(true); |
| 410 | + |
| 411 | + // Act |
| 412 | + method.invoke(pluginModernizer, plugin); |
| 413 | + |
| 414 | + // Assert |
| 415 | + ArgumentCaptor<ModernizationMetadata> captor = ArgumentCaptor.forClass(ModernizationMetadata.class); |
| 416 | + verify(plugin).setModernizationMetadata(captor.capture()); |
| 417 | + ModernizationMetadata saved = captor.getValue(); |
| 418 | + |
| 419 | + assertEquals("fail", saved.getMigrationStatus()); |
| 420 | + assertNotNull(saved.getFailureReasons()); |
| 421 | + assertTrue(saved.getFailureReasons().contains(PreconditionError.PARENT_POM_1X.getError())); |
| 422 | + } |
| 423 | + |
| 424 | + @Test |
| 425 | + void testCollectModernizationMetadata_WithRuntimeErrors_ShouldRecordFailureReasons() throws Exception { |
| 426 | + // Arrange |
| 427 | + Plugin plugin = mock(Plugin.class); |
| 428 | + PluginMetadata pluginMetadata = mock(PluginMetadata.class); |
| 429 | + Config pluginConfig = mock(Config.class); |
| 430 | + Recipe recipe = mock(Recipe.class); |
| 431 | + |
| 432 | + when(plugin.getName()).thenReturn("test-plugin"); |
| 433 | + when(plugin.getMetadata()).thenReturn(pluginMetadata); |
| 434 | + when(pluginMetadata.getPluginName()).thenReturn("test-plugin"); |
| 435 | + when(pluginMetadata.getJenkinsVersion()).thenReturn(null); |
| 436 | + when(plugin.getJenkinsBaseline()).thenReturn("2.361"); |
| 437 | + when(plugin.getJenkinsVersion()).thenReturn("2.361.1"); |
| 438 | + when(plugin.getEffectiveBaseline()).thenReturn("2.361"); |
| 439 | + when(plugin.getConfig()).thenReturn(pluginConfig); |
| 440 | + when(pluginConfig.getRecipe()).thenReturn(recipe); |
| 441 | + when(recipe.getDisplayName()).thenReturn("Test Recipe"); |
| 442 | + when(recipe.getDescription()).thenReturn("Test description"); |
| 443 | + when(recipe.getTags()).thenReturn(Set.of()); |
| 444 | + when(recipe.getName()).thenReturn("test-recipe"); |
| 445 | + when(plugin.getPullRequestUrl()).thenReturn(null); |
| 446 | + when(pluginService.extractVersion(plugin)).thenReturn("1.0"); |
| 447 | + when(config.isDryRun()).thenReturn(false); |
| 448 | + when(plugin.getDiffStats(ghService, false)).thenReturn(new DiffStats(0, 0, 0)); |
| 449 | + when(plugin.hasErrors()).thenReturn(true); |
| 450 | + when(plugin.hasPreconditionErrors()).thenReturn(false); |
| 451 | + when(plugin.getPreconditionErrors()).thenReturn(Set.of()); |
| 452 | + when(plugin.getErrors()).thenReturn(List.of(new PluginProcessingException("Build failed with code: 1", plugin))); |
| 453 | + // GHService is not needed here; the existing try-catch in collectModernizationMetadata handles this |
| 454 | + doThrow(new PluginProcessingException("no gh", plugin)).when(ghService).getRepository(any(), any()); |
| 455 | + // Allow save() to proceed without NPE |
| 456 | + when(cacheManager.getLocation()).thenReturn(Path.of("target/test-cache")); |
| 457 | + |
| 458 | + java.lang.reflect.Method method = |
| 459 | + PluginModernizer.class.getDeclaredMethod("collectModernizationMetadata", Plugin.class); |
| 460 | + method.setAccessible(true); |
| 461 | + |
| 462 | + // Act |
| 463 | + method.invoke(pluginModernizer, plugin); |
| 464 | + |
| 465 | + // Assert |
| 466 | + ArgumentCaptor<ModernizationMetadata> captor = ArgumentCaptor.forClass(ModernizationMetadata.class); |
| 467 | + verify(plugin).setModernizationMetadata(captor.capture()); |
| 468 | + ModernizationMetadata saved = captor.getValue(); |
| 469 | + |
| 470 | + assertEquals("fail", saved.getMigrationStatus()); |
| 471 | + assertNotNull(saved.getFailureReasons()); |
| 472 | + assertTrue(saved.getFailureReasons().contains("Build failed with code: 1")); |
| 473 | + } |
| 474 | + |
364 | 475 | private Recipe createMockRecipe(String name, String description) { |
365 | 476 | Recipe recipe = mock(Recipe.class); |
366 | 477 | when(recipe.getName()).thenReturn(name); |
|
0 commit comments