|
| 1 | +package io.jenkins.plugins.analysis.core.model; |
| 2 | + |
| 3 | +import org.eclipse.collections.impl.factory.Lists; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import edu.hm.hafner.analysis.IssueBuilder; |
| 7 | +import edu.hm.hafner.analysis.Report; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.StringReader; |
| 11 | +import java.nio.charset.Charset; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | + |
| 14 | +import hudson.model.Run; |
| 15 | + |
| 16 | +import io.jenkins.plugins.analysis.core.testutil.IntegrationTestWithJenkinsPerSuite; |
| 17 | +import io.jenkins.plugins.analysis.core.util.BuildFolderFacade; |
| 18 | +import io.jenkins.plugins.prism.SourceCodeViewModel; |
| 19 | +import io.jenkins.plugins.util.JenkinsFacade; |
| 20 | + |
| 21 | +import static io.jenkins.plugins.analysis.core.testutil.Assertions.*; |
| 22 | +import static org.mockito.ArgumentMatchers.*; |
| 23 | +import static org.mockito.Mockito.*; |
| 24 | + |
| 25 | +/** |
| 26 | + * Integration tests for {@link DetailFactory} source file reading functionality. |
| 27 | + * These tests require a Jenkins instance because SourceCodeViewModel.create() checks permissions. |
| 28 | + * |
| 29 | + * @author Akash Manna |
| 30 | + */ |
| 31 | +class DetailFactorySourceFileITest extends IntegrationTestWithJenkinsPerSuite { |
| 32 | + private static final Charset ENCODING = StandardCharsets.UTF_8; |
| 33 | + private static final String AFFECTED_FILE_CONTENT = "public class Test { }"; |
| 34 | + private static final Report NEW_ISSUES = new Report(); |
| 35 | + private static final Report OUTSTANDING_ISSUES = new Report(); |
| 36 | + private static final Report FIXED_ISSUES = new Report(); |
| 37 | + |
| 38 | + /** |
| 39 | + * Checks that the error message is shown if an affected file could not be read. |
| 40 | + */ |
| 41 | + @Test |
| 42 | + void shouldShowExceptionMessageIfAffectedFileIsNotReadable() throws IOException { |
| 43 | + BuildFolderFacade buildFolder = mock(BuildFolderFacade.class); |
| 44 | + when(buildFolder.readFile(any(), anyString(), any())).thenThrow(new IOException("file error")); |
| 45 | + |
| 46 | + var details = createDetails(buildFolder, "a-file"); |
| 47 | + |
| 48 | + assertThat(details).isInstanceOfSatisfying(SourceCodeViewModel.class, |
| 49 | + s -> assertThat(s.getSourceCode()).contains("IOException: file error")); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Checks that a link to a source returns a SourceDetail-View. |
| 54 | + */ |
| 55 | + @Test |
| 56 | + void shouldReturnSourceDetailWhenCalledWithSourceLinkAndIssueNotInConsoleLog() throws IOException { |
| 57 | + BuildFolderFacade buildFolder = mock(BuildFolderFacade.class); |
| 58 | + when(buildFolder.readFile(any(), anyString(), any())).thenReturn(new StringReader(AFFECTED_FILE_CONTENT)); |
| 59 | + |
| 60 | + var details = createDetails(buildFolder, "a-file"); |
| 61 | + |
| 62 | + assertThat(details).isInstanceOfSatisfying(SourceCodeViewModel.class, |
| 63 | + s -> assertThat(s.getSourceCode()).contains(AFFECTED_FILE_CONTENT)); |
| 64 | + } |
| 65 | + |
| 66 | + private Object createDetails(final BuildFolderFacade buildFolder, |
| 67 | + final String fileName) { |
| 68 | + try (var issueBuilder = new IssueBuilder()) { |
| 69 | + var detailFactory = new DetailFactory(new JenkinsFacade(), buildFolder); |
| 70 | + |
| 71 | + issueBuilder.setFileName(fileName); |
| 72 | + var issue = issueBuilder.build(); |
| 73 | + |
| 74 | + var report = new Report(); |
| 75 | + report.add(issue); |
| 76 | + |
| 77 | + var project = createFreeStyleProject(); |
| 78 | + buildSuccessfully(project); |
| 79 | + Run<?, ?> run = project.getLastBuild(); |
| 80 | + |
| 81 | + return detailFactory.createTrendDetails("source." + issue.getId().toString(), |
| 82 | + run, createAnalysisResult(), report, NEW_ISSUES, OUTSTANDING_ISSUES, FIXED_ISSUES, ENCODING, |
| 83 | + createParent()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private AnalysisResult createAnalysisResult() { |
| 88 | + AnalysisResult result = mock(AnalysisResult.class); |
| 89 | + when(result.getErrorMessages()).thenReturn(Lists.immutable.empty()); |
| 90 | + when(result.getInfoMessages()).thenReturn(Lists.immutable.empty()); |
| 91 | + return result; |
| 92 | + } |
| 93 | + |
| 94 | + private IssuesDetail createParent() { |
| 95 | + IssuesDetail parent = mock(IssuesDetail.class); |
| 96 | + StaticAnalysisLabelProvider labelProvider = mock(StaticAnalysisLabelProvider.class); |
| 97 | + when(labelProvider.getName()).thenReturn("Test"); |
| 98 | + when(labelProvider.getSmallIconUrl()).thenReturn("/icon"); |
| 99 | + when(labelProvider.getLargeIconUrl()).thenReturn("/icon"); |
| 100 | + when(parent.getLabelProvider()).thenReturn(labelProvider); |
| 101 | + return parent; |
| 102 | + } |
| 103 | +} |
0 commit comments