|
1 | 1 | package org.hjug.refactorfirst.report; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
4 | 5 |
|
5 | 6 | import com.fasterxml.jackson.databind.ObjectMapper; |
6 | 7 | import java.io.File; |
@@ -152,6 +153,79 @@ void given_createBubble_when_urlProvided_then_urlIsSet() { |
152 | 153 | "https://github.com/example/repo/blob/main/src/main/java/com/example/TestClass.java", bubble.getUrl()); |
153 | 154 | } |
154 | 155 |
|
| 156 | + /** Verifies that generated and serialized chart bubbles carry repoUrl + source path. */ |
| 157 | + @Test |
| 158 | + void given_repoWithOrigin_when_reportGenerated_then_bubbleUrlsPointToSourceFiles() throws Exception { |
| 159 | + // Given |
| 160 | + File repoDir = tempDir.toFile(); |
| 161 | + File srcDir = new File(repoDir, "src/main/java/com/example"); |
| 162 | + srcDir.mkdirs(); |
| 163 | + |
| 164 | + StringBuilder complexMethod = new StringBuilder(); |
| 165 | + for (int i = 0; i < 40; i++) { |
| 166 | + complexMethod |
| 167 | + .append(" if (a > ") |
| 168 | + .append(i) |
| 169 | + .append(") { for (int j = 0; j < a; j++) { if (j % 2 == 0 && b) { c += j; } else { c -= j; } } }\n"); |
| 170 | + } |
| 171 | + Files.writeString( |
| 172 | + new File(srcDir, "ComplexService.java").toPath(), |
| 173 | + "package com.example;\n\n" |
| 174 | + + "public class ComplexService {\n" |
| 175 | + + " private int c;\n" |
| 176 | + + " public int compute(int a, boolean b) {\n" |
| 177 | + + complexMethod |
| 178 | + + " return c;\n" |
| 179 | + + " }\n" |
| 180 | + + "}\n"); |
| 181 | + |
| 182 | + new ProcessBuilder("git", "init").directory(repoDir).start().waitFor(); |
| 183 | + new ProcessBuilder("git", "config", "user.email", "test@test.com") |
| 184 | + .directory(repoDir) |
| 185 | + .start() |
| 186 | + .waitFor(); |
| 187 | + new ProcessBuilder("git", "config", "user.name", "Test") |
| 188 | + .directory(repoDir) |
| 189 | + .start() |
| 190 | + .waitFor(); |
| 191 | + new ProcessBuilder("git", "remote", "add", "origin", "https://github.com/example/repo.git") |
| 192 | + .directory(repoDir) |
| 193 | + .start() |
| 194 | + .waitFor(); |
| 195 | + new ProcessBuilder("git", "add", ".").directory(repoDir).start().waitFor(); |
| 196 | + new ProcessBuilder("git", "commit", "-m", "initial") |
| 197 | + .directory(repoDir) |
| 198 | + .start() |
| 199 | + .waitFor(); |
| 200 | + |
| 201 | + // When |
| 202 | + new JsonGenerator().execute(0, true, false, true, "src/test", "BubbleProject", "1.0.0", repoDir, null); |
| 203 | + |
| 204 | + // Then |
| 205 | + Path jsonFile = tempDir.resolve(".refactorfirst").resolve("refactor-first.json"); |
| 206 | + RefactorFirstReportDTO report = objectMapper.readValue(jsonFile.toFile(), RefactorFirstReportDTO.class); |
| 207 | + String repoUrl = report.getProject().getRepoUrl(); |
| 208 | + assertTrue( |
| 209 | + repoUrl.startsWith("https://github.com/example/repo/blob/"), |
| 210 | + "repoUrl should be derived from origin: " + repoUrl); |
| 211 | + |
| 212 | + var bubbles = report.getDisharmonies() == null |
| 213 | + ? java.util.List.<ChartJsBubbleDTO>of() |
| 214 | + : report.getDisharmonies().stream() |
| 215 | + .filter(section -> section.getChart() != null) |
| 216 | + .flatMap(section -> section.getChart().getBubbles().stream()) |
| 217 | + .toList(); |
| 218 | + assumeTrue(!bubbles.isEmpty(), "Fixture produced no disharmony bubbles"); |
| 219 | + |
| 220 | + for (ChartJsBubbleDTO bubble : bubbles) { |
| 221 | + assertNotNull(bubble.getUrl(), "Bubble " + bubble.getLabel() + " should have a url"); |
| 222 | + assertTrue(bubble.getUrl().startsWith(repoUrl), "Bubble url should start with repoUrl: " + bubble.getUrl()); |
| 223 | + assertTrue( |
| 224 | + bubble.getUrl().endsWith("src/main/java/com/example/" + bubble.getLabel()), |
| 225 | + "Bubble url should point to the source file: " + bubble.getUrl()); |
| 226 | + } |
| 227 | + } |
| 228 | + |
155 | 229 | /** Verifies report generation against a minimal Git repository fixture. */ |
156 | 230 | @Test |
157 | 231 | void testGenerateReportDataWithGitRepoFixture() throws Exception { |
|
0 commit comments