|
| 1 | +package org.hjug.refactorfirst.report; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | + |
| 6 | +import java.time.Instant; |
| 7 | +import java.util.List; |
| 8 | +import org.hjug.cbc.RankedDisharmony; |
| 9 | +import org.hjug.refactorfirst.report.model.DisharmonySectionDTO; |
| 10 | +import org.hjug.refactorfirst.report.model.DisharmonyTableCellDTO; |
| 11 | +import org.junit.jupiter.api.DisplayName; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.mockito.Mockito; |
| 14 | + |
| 15 | +/** |
| 16 | + * Pins the JSON contract of the disharmony table cells: the Class cell carries the |
| 17 | + * plain file name and the source path relative to the project root, and no |
| 18 | + * server-rendered link — viewers combine the path with project.repoUrl. |
| 19 | + */ |
| 20 | +class JsonGeneratorDisharmonySectionTest { |
| 21 | + |
| 22 | + /** Creates a ranked God Class finding for the file at the given path. */ |
| 23 | + private RankedDisharmony mockRankedDisharmony(String fileName, String path) { |
| 24 | + RankedDisharmony rd = Mockito.mock(RankedDisharmony.class); |
| 25 | + Mockito.when(rd.getFileName()).thenReturn(fileName); |
| 26 | + Mockito.when(rd.getPath()).thenReturn(path); |
| 27 | + Mockito.when(rd.getPriority()).thenReturn(1); |
| 28 | + Mockito.when(rd.getEffortRank()).thenReturn(5); |
| 29 | + Mockito.when(rd.getChangePronenessRank()).thenReturn(10); |
| 30 | + Mockito.when(rd.getMostRecentCommitTime()).thenReturn(Instant.now()); |
| 31 | + Mockito.when(rd.getCommitCount()).thenReturn(1); |
| 32 | + Mockito.when(rd.getDuplicationPartners()).thenReturn(null); |
| 33 | + return rd; |
| 34 | + } |
| 35 | + |
| 36 | + @DisplayName("the disharmony Class cell carries the plain file name and the source path, not a rendered link") |
| 37 | + @Test |
| 38 | + void buildDisharmonySection_classCellCarriesPathAndFileName() { |
| 39 | + // Given: one ranked God Class finding |
| 40 | + JsonGenerator generator = new JsonGenerator(); |
| 41 | + RankedDisharmony rd = mockRankedDisharmony("TestClass.java", "src/main/java/com/example/TestClass.java"); |
| 42 | + |
| 43 | + // When: the disharmony section is built without detail columns |
| 44 | + DisharmonySectionDTO section = |
| 45 | + generator.buildDisharmonySection(SimpleHtmlReport.DISHARMONY_SPECS.get(0), false, List.of(rd)); |
| 46 | + |
| 47 | + // Then: the Class cell lists the file name and the path relative to the |
| 48 | + // project root; the URL is the viewer's to construct |
| 49 | + DisharmonyTableCellDTO classCell = |
| 50 | + section.getTable().getRows().get(0).getCells().get(0); |
| 51 | + assertEquals("TestClass.java", classCell.getContent()); |
| 52 | + assertEquals("src/main/java/com/example/TestClass.java", classCell.getPath()); |
| 53 | + assertFalse(classCell.getContent().contains("<a"), "The Class cell must not embed a server-built link"); |
| 54 | + assertFalse( |
| 55 | + classCell.getContent().contains("https://github.com/example/repo"), |
| 56 | + "The Class cell must not embed the repository URL"); |
| 57 | + } |
| 58 | +} |
0 commit comments