Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save musketyr/dc00716b6f792bff7a864ce0e477e920 to your computer and use it in GitHub Desktop.

Select an option

Save musketyr/dc00716b6f792bff7a864ce0e477e920 to your computer and use it in GitHub Desktop.
Spock to JUnit 5 Migration Diff
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Spock → JUnit 5 Migration: RoiPdfCapabilitiesSpec</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f5f5f5; padding: 20px; line-height: 1.6; }
h1 { text-align: center; margin-bottom: 20px; color: #333; }
.section { background: white; border-radius: 8px; margin-bottom: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); overflow: hidden; }
.section-header { background: #2d3748; color: white; padding: 12px 16px; cursor: pointer; display: flex; justify-content: space-between; align-items: center; }
.section-header:hover { background: #4a5568; }
.section-header h2 { font-size: 16px; font-weight: 600; }
.section-header .toggle { font-size: 12px; }
.section-content { display: block; }
.section-content.collapsed { display: none; }
.diff-container { display: grid; grid-template-columns: 1fr 1fr; }
.diff-panel { padding: 16px; overflow-x: auto; }
.diff-panel.left { background: #fff5f5; border-right: 1px solid #e2e8f0; }
.diff-panel.right { background: #f0fff4; }
.diff-panel h3 { font-size: 12px; text-transform: uppercase; color: #718096; margin-bottom: 12px; letter-spacing: 0.5px; }
pre { font-family: 'SF Mono', Monaco, 'Courier New', monospace; font-size: 13px; white-space: pre-wrap; word-wrap: break-word; }
.left pre { color: #c53030; }
.right pre { color: #276749; }
@media (max-width: 768px) { .diff-container { grid-template-columns: 1fr; } .diff-panel.left { border-right: none; border-bottom: 1px solid #e2e8f0; } }
</style>
</head>
<body>
<h1>🦀 Spock → JUnit 5 Migration: RoiPdfCapabilitiesSpec</h1>
<p style="text-align: center; color: #666; margin-bottom: 20px;">Story: <a href="https://app.shortcut.com/agorapulse/story/185888">sc185888</a> | PR: <a href="https://github.com/agorapulse/platform/pull/72988">#72988</a></p>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>📦 Imports</h2>
<span class="toggle">▼</span>
</div>
<div class="section-content">
<div class="diff-container">
<div class="diff-panel left">
<h3>Spock (Groovy)</h3>
<pre>package agorapulse.roi.pdf.export.utils
import agorapulse.commons.chart.models.HighchartsExporterParameters
import agorapulse.commons.export.ExportSettingsDTO
import agorapulse.commons.export.pdf.models.PdfDocument
import agorapulse.commons.export.pdf.models.PdfMetric
import agorapulse.commons.export.services.ImageService
import agorapulse.commons.messagesource.interpolation.InterpolatingMessageSource
import agorapulse.roi.core.models.Roi
import agorapulse.roi.pdf.chart.generators.RoiHighchartsService
import agorapulse.roi.pdf.chart.models.RoiSection
import agorapulse.roi.pdf.export.generators.RoiPdfService
import com.lowagie.text.Document
import com.lowagie.text.PageSize
import spock.lang.Specification
import javax.imageio.ImageIO
import java.awt.image.BufferedImage</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre>package agorapulse.roi.pdf.export.utils;
import agorapulse.commons.chart.models.HighchartsExporterParameters;
import agorapulse.commons.export.ExportSettingsDTO;
import agorapulse.commons.export.pdf.models.PdfDocument;
import agorapulse.commons.export.pdf.models.PdfMetric;
import agorapulse.commons.export.services.ImageService;
import agorapulse.commons.messagesource.interpolation.InterpolatingMessageSource;
import agorapulse.roi.core.models.Roi;
import agorapulse.roi.pdf.chart.generators.RoiHighchartsService;
import agorapulse.roi.pdf.chart.models.RoiSection;
import agorapulse.roi.pdf.export.generators.RoiPdfService;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>🏗️ Class & Fields</h2>
<span class="toggle">▼</span>
</div>
<div class="section-content">
<div class="diff-container">
<div class="diff-panel left">
<h3>Spock (Groovy)</h3>
<pre>class RoiPdfCapabilitiesSpec extends Specification {
private static final byte[] RED_SQUARE = redSquare
ImageService imageService = Mock()
InterpolatingMessageSource interpolatingMessageSource = Mock()
RoiHighchartsService roiHighchartsService = Mock()
RoiPdfCapabilities service = new RoiPdfCapabilities(imageService, interpolatingMessageSource, roiHighchartsService)
</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre>@ExtendWith(MockitoExtension.class)
class RoiPdfCapabilitiesTest {
private static final byte[] RED_SQUARE = createRedSquare();
@Mock
private ImageService imageService;
@Mock
private InterpolatingMessageSource interpolatingMessageSource;
@Mock
private RoiHighchartsService roiHighchartsService;
private RoiPdfCapabilities service;
@BeforeEach
void setUp() {
service = new RoiPdfCapabilities(imageService, interpolatingMessageSource, roiHighchartsService);
}
</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>🧪 should addHighChartsToPage - exception</h2>
<span class="toggle">▼</span>
</div>
<div class="section-content">
<div class="diff-container">
<div class="diff-panel left">
<h3>Spock (Groovy)</h3>
<pre> void &#x27;should addHighChartsToPage - exception&#x27;() {
when:
service.addHighChartsToPage(null, null, null, null, 80, 16, 12)
then:
thrown(IllegalArgumentException)
}</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre> @Test
void shouldThrowExceptionWhenAddHighChartsToPageWithNullRoi() {
assertThatThrownBy(() -&gt; service.addHighChartsToPage(null, null, null, null, 80, 16, 12))
.isInstanceOf(IllegalArgumentException.class);
}</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>🧪 should addHighChartsToPage - exception 2</h2>
<span class="toggle">▼</span>
</div>
<div class="section-content">
<div class="diff-container">
<div class="diff-panel left">
<h3>Spock (Groovy)</h3>
<pre> void &#x27;should addHighChartsToPage - exception 2&#x27;() {
when:
service.addHighChartsToPage(new Roi(), null, null, null, 80, 16, 12)
then:
thrown(IllegalArgumentException)
}</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre> @Test
void shouldThrowExceptionWhenAddHighChartsToPageWithNullAccountName() {
assertThatThrownBy(() -&gt; service.addHighChartsToPage(new Roi(), null, null, null, 80, 16, 12))
.isInstanceOf(IllegalArgumentException.class);
}</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>🧪 should addHighChartsToPage</h2>
<span class="toggle">▼</span>
</div>
<div class="section-content">
<div class="diff-container">
<div class="diff-panel left">
<h3>Spock (Groovy)</h3>
<pre> void &#x27;should addHighChartsToPage&#x27;() {
given:
Roi roi = new Roi(accountName: &#x27;test&#x27;)
Document document = new Document(PageSize.A4.rotate())
PdfDocument pdf = RoiPdfService.initDocument(document, &#x27;roi.pdf&#x27;)
ExportSettingsDTO settings = new ExportSettingsDTO(timeFormat: &#x27;24&#x27;)
List&lt;RoiSection&gt; sections = [RoiSection.VISITOR, RoiSection.GENERATED_REVENUE]
HighchartsExporterParameters visitorsParams = new HighchartsExporterParameters(jsonConfig: [test: 2])
HighchartsExporterParameters revenueParams = new HighchartsExporterParameters(jsonConfig: [test: 3])
when:
boolean ok = service.addHighChartsToPage(roi, pdf, settings, sections, 80, 16, 12)
document.close()
then:
1 * roiHighchartsService.buildHighchartsExportParameters(roi, settings, RoiSection.VISITOR) &gt;&gt; visitorsParams
1 * roiHighchartsService.generateGraph(visitorsParams, &#x27;VISITOR&#x27;) &gt;&gt; RED_SQUARE
1 * roiHighchartsService.buildHighchartsExportParameters(roi, settings, RoiSection.GENERATED_REVENUE) &gt;&gt; revenueParams
1 * roiHighchartsService.generateGraph(revenueParams, &#x27;GENERATED_REVENUE&#x27;) &gt;&gt; RED_SQUARE
ok
}</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre> @Test
void shouldAddHighChartsToPage() {
// given
Roi roi = new Roi();
roi.setAccountName(&quot;test&quot;);
Document document = new Document(PageSize.A4.rotate());
PdfDocument pdf = RoiPdfService.initDocument(document, &quot;roi.pdf&quot;);
ExportSettingsDTO settings = new ExportSettingsDTO();
settings.setTimeFormat(&quot;24&quot;);
List&lt;RoiSection&gt; sections = List.of(RoiSection.VISITOR, RoiSection.GENERATED_REVENUE);
HighchartsExporterParameters visitorsParams = new HighchartsExporterParameters();
visitorsParams.setJsonConfig(Map.of(&quot;test&quot;, 2));
HighchartsExporterParameters revenueParams = new HighchartsExporterParameters();
revenueParams.setJsonConfig(Map.of(&quot;test&quot;, 3));
when(roiHighchartsService.buildHighchartsExportParameters(roi, settings, RoiSection.VISITOR)).thenReturn(visitorsParams);
when(roiHighchartsService.generateGraph(visitorsParams, &quot;VISITOR&quot;)).thenReturn(RED_SQUARE);
when(roiHighchartsService.buildHighchartsExportParameters(roi, settings, RoiSection.GENERATED_REVENUE)).thenReturn(revenueParams);
when(roiHighchartsService.generateGraph(revenueParams, &quot;GENERATED_REVENUE&quot;)).thenReturn(RED_SQUARE);
// when
boolean ok = service.addHighChartsToPage(roi, pdf, settings, sections, 80, 16, 12);
document.close();
// then
assertThat(ok).isTrue();
}</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>🧪 should addHighChartsToPage - missing chart</h2>
<span class="toggle">▼</span>
</div>
<div class="section-content">
<div class="diff-container">
<div class="diff-panel left">
<h3>Spock (Groovy)</h3>
<pre> void &#x27;should addHighChartsToPage - missing chart&#x27;() {
given:
Roi roi = new Roi(accountName: &#x27;test&#x27;)
Document document = new Document(PageSize.A4.rotate())
PdfDocument pdf = RoiPdfService.initDocument(document, &#x27;roi.pdf&#x27;)
ExportSettingsDTO settings = new ExportSettingsDTO(timeFormat: &#x27;24&#x27;)
List&lt;RoiSection&gt; sections = [RoiSection.VISITOR, RoiSection.GENERATED_REVENUE]
HighchartsExporterParameters visitorsParams = new HighchartsExporterParameters(jsonConfig: [test: 2])
HighchartsExporterParameters revenueParams = new HighchartsExporterParameters(jsonConfig: [test: 3])
when:
boolean ok = service.addHighChartsToPage(roi, pdf, settings, sections, 80, 16, 12)
document.close()
then:
1 * roiHighchartsService.buildHighchartsExportParameters(roi, settings, RoiSection.VISITOR) &gt;&gt; visitorsParams
1 * roiHighchartsService.generateGraph(visitorsParams, &#x27;VISITOR&#x27;) &gt;&gt; RED_SQUARE
1 * roiHighchartsService.buildHighchartsExportParameters(roi, settings, RoiSection.GENERATED_REVENUE) &gt;&gt; revenueParams
1 * roiHighchartsService.generateGraph(revenueParams, &#x27;GENERATED_REVENUE&#x27;) &gt;&gt; null
!ok
}</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre> @Test
void shouldReturnFalseWhenAddHighChartsToPageWithMissingChart() {
// given
Roi roi = new Roi();
roi.setAccountName(&quot;test&quot;);
Document document = new Document(PageSize.A4.rotate());
PdfDocument pdf = RoiPdfService.initDocument(document, &quot;roi.pdf&quot;);
ExportSettingsDTO settings = new ExportSettingsDTO();
settings.setTimeFormat(&quot;24&quot;);
List&lt;RoiSection&gt; sections = List.of(RoiSection.VISITOR, RoiSection.GENERATED_REVENUE);
HighchartsExporterParameters visitorsParams = new HighchartsExporterParameters();
visitorsParams.setJsonConfig(Map.of(&quot;test&quot;, 2));
HighchartsExporterParameters revenueParams = new HighchartsExporterParameters();
revenueParams.setJsonConfig(Map.of(&quot;test&quot;, 3));
when(roiHighchartsService.buildHighchartsExportParameters(roi, settings, RoiSection.VISITOR)).thenReturn(visitorsParams);
when(roiHighchartsService.generateGraph(visitorsParams, &quot;VISITOR&quot;)).thenReturn(RED_SQUARE);
when(roiHighchartsService.buildHighchartsExportParameters(roi, settings, RoiSection.GENERATED_REVENUE)).thenReturn(revenueParams);
when(roiHighchartsService.generateGraph(revenueParams, &quot;GENERATED_REVENUE&quot;)).thenReturn(null);
// when
boolean ok = service.addHighChartsToPage(roi, pdf, settings, sections, 80, 16, 12);
document.close();
// then
assertThat(ok).isFalse();
}</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>🧪 should createMetricPage - no data</h2>
<span class="toggle">▼</span>
</div>
<div class="section-content">
<div class="diff-container">
<div class="diff-panel left">
<h3>Spock (Groovy)</h3>
<pre> void &#x27;should createMetricPage - no data&#x27;() {
given:
Roi roi = new Roi(accountName: &#x27;test&#x27;)
Document document = new Document(PageSize.A4.rotate())
PdfDocument pdf = RoiPdfService.initDocument(document, &#x27;roi.pdf&#x27;)
ExportSettingsDTO settings = new ExportSettingsDTO(timeFormat: &#x27;24&#x27;)
PdfMetric&lt;RoiSection&gt; pdfMetric = new PdfMetric&lt;RoiSection&gt;(sections: [RoiSection.VISITOR])
HighchartsExporterParameters visitorsParams = new HighchartsExporterParameters(jsonConfig: [test: 2])
when:
service.createMetricPage(roi, pdf, settings, pdfMetric, 16, 12)
document.close()
then:
1 * roiHighchartsService.buildHighchartsExportParameters(roi, settings, RoiSection.VISITOR) &gt;&gt; visitorsParams
1 * roiHighchartsService.generateGraph(visitorsParams, &#x27;VISITOR&#x27;) &gt;&gt; null
1 * interpolatingMessageSource.get(&#x27;analytics.pdf.data-unavailable&#x27;, Locale.ENGLISH) &gt;&gt; &#x27;Data unavailable&#x27;
0 * _
}</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre> @Test
void shouldCreateMetricPageWithNoData() {
// given
Roi roi = new Roi();
roi.setAccountName(&quot;test&quot;);
Document document = new Document(PageSize.A4.rotate());
PdfDocument pdf = RoiPdfService.initDocument(document, &quot;roi.pdf&quot;);
ExportSettingsDTO settings = new ExportSettingsDTO();
settings.setTimeFormat(&quot;24&quot;);
PdfMetric&lt;RoiSection&gt; pdfMetric = new PdfMetric&lt;&gt;();
pdfMetric.setSections(List.of(RoiSection.VISITOR));
HighchartsExporterParameters visitorsParams = new HighchartsExporterParameters();
visitorsParams.setJsonConfig(Map.of(&quot;test&quot;, 2));
when(roiHighchartsService.buildHighchartsExportParameters(roi, settings, RoiSection.VISITOR)).thenReturn(visitorsParams);
when(roiHighchartsService.generateGraph(visitorsParams, &quot;VISITOR&quot;)).thenReturn(null);
when(interpolatingMessageSource.get(&quot;analytics.pdf.data-unavailable&quot;, Locale.ENGLISH)).thenReturn(&quot;Data unavailable&quot;);
// when
service.createMetricPage(roi, pdf, settings, pdfMetric, 16, 12);
document.close();
// then
verify(interpolatingMessageSource).get(&quot;analytics.pdf.data-unavailable&quot;, Locale.ENGLISH);
verifyNoMoreInteractions(interpolatingMessageSource);
}</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>🧪 should createMetricPage - data</h2>
<span class="toggle">▼</span>
</div>
<div class="section-content">
<div class="diff-container">
<div class="diff-panel left">
<h3>Spock (Groovy)</h3>
<pre> void &#x27;should createMetricPage - data&#x27;() {
given:
Roi roi = new Roi(accountName: &#x27;test&#x27;)
Document document = new Document(PageSize.A4.rotate())
PdfDocument pdf = RoiPdfService.initDocument(document, &#x27;roi.pdf&#x27;)
ExportSettingsDTO settings = new ExportSettingsDTO(timeFormat: &#x27;24&#x27;)
PdfMetric&lt;RoiSection&gt; pdfMetric = new PdfMetric&lt;RoiSection&gt;(sections: [RoiSection.VISITOR])
HighchartsExporterParameters visitorsParams = new HighchartsExporterParameters(jsonConfig: [test: 2])
when:
service.createMetricPage(roi, pdf, settings, pdfMetric, 16, 12)
document.close()
then:
1 * roiHighchartsService.buildHighchartsExportParameters(roi, settings, RoiSection.VISITOR) &gt;&gt; visitorsParams
1 * roiHighchartsService.generateGraph(visitorsParams, &#x27;VISITOR&#x27;) &gt;&gt; RED_SQUARE
0 * _
}</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre> @Test
void shouldCreateMetricPageWithData() {
// given
Roi roi = new Roi();
roi.setAccountName(&quot;test&quot;);
Document document = new Document(PageSize.A4.rotate());
PdfDocument pdf = RoiPdfService.initDocument(document, &quot;roi.pdf&quot;);
ExportSettingsDTO settings = new ExportSettingsDTO();
settings.setTimeFormat(&quot;24&quot;);
PdfMetric&lt;RoiSection&gt; pdfMetric = new PdfMetric&lt;&gt;();
pdfMetric.setSections(List.of(RoiSection.VISITOR));
HighchartsExporterParameters visitorsParams = new HighchartsExporterParameters();
visitorsParams.setJsonConfig(Map.of(&quot;test&quot;, 2));
when(roiHighchartsService.buildHighchartsExportParameters(roi, settings, RoiSection.VISITOR)).thenReturn(visitorsParams);
when(roiHighchartsService.generateGraph(visitorsParams, &quot;VISITOR&quot;)).thenReturn(RED_SQUARE);
// when
service.createMetricPage(roi, pdf, settings, pdfMetric, 16, 12);
document.close();
// then
verifyNoMoreInteractions(interpolatingMessageSource);
}</pre>
</div>
</div>
</div>
</div>
<script>
function toggleSection(header) {
const content = header.nextElementSibling;
const toggle = header.querySelector('.toggle');
content.classList.toggle('collapsed');
toggle.textContent = content.classList.contains('collapsed') ? '▶' : '▼';
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment