Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

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

Select an option

Save musketyr/cb80c17a6a0dd12c60a3dda89d3d9965 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: GoogleAnalyticsResponseUtilsSpec</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: GoogleAnalyticsResponseUtilsSpec</h1>
<p style="text-align: center; color: #666; margin-bottom: 20px;">Story: <a href="https://app.shortcut.com/agorapulse/story/185894">sc185894</a> | PR: <a href="https://github.com/agorapulse/platform/pull/72991">#72991</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.service.google.client.analytics.api
import agorapulse.roi.service.google.client.analytics.api.Ga4InsightsApiResponse.Header
import agorapulse.roi.service.google.client.analytics.api.Ga4InsightsApiResponse.Row
import agorapulse.roi.service.google.client.analytics.api.Ga4InsightsApiResponse.Value
import spock.lang.Specification</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre>package agorapulse.roi.service.google.client.analytics.api;
import agorapulse.roi.service.google.client.analytics.api.Ga4InsightsApiResponse.Header;
import agorapulse.roi.service.google.client.analytics.api.Ga4InsightsApiResponse.Row;
import agorapulse.roi.service.google.client.analytics.api.Ga4InsightsApiResponse.Value;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;</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 GoogleAnalyticsResponseUtilsSpec extends Specification {
</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre>class GoogleAnalyticsResponseUtilsTest {
</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>🧪 Should map response headers with values</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 map response headers with values&#x27;() {
given:
Ga4InsightsApiResponse eventsResponse = new Ga4InsightsApiResponse(
dimensionHeaders: [new Header(name: &#x27;date&#x27;)],
metricHeaders: [new Header(name: &#x27;eventValue&#x27;), new Header(name: &#x27;eventCount&#x27;), new Header(name: &#x27;transactions&#x27;), new Header(name: &#x27;purchaseRevenue&#x27;)],
rows: [
new Row(dimensionValues: [new Value(value: &#x27;20230101&#x27;)], metricValues: [new Value(value: &#x27;2.0&#x27;), new Value(value: &#x27;7&#x27;), new Value(value: &#x27;17&#x27;), new Value(value: &#x27;29.0&#x27;)]),
]
)
when:
Map&lt;String, String&gt; response = GoogleAnalyticsResponseUtils.mapHeadersWithValues(eventsResponse.metricHeaders, eventsResponse.rows.get(0).metricValues)
then:
response == [eventValue: &#x27;2.0&#x27;, eventCount: &#x27;7&#x27;, transactions: &#x27;17&#x27;, purchaseRevenue: &#x27;29.0&#x27;]
}</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre> @Test
void shouldMapResponseHeadersWithValues() {
// given
Ga4InsightsApiResponse eventsResponse = new Ga4InsightsApiResponse();
eventsResponse.setDimensionHeaders(List.of(new Header(&quot;date&quot;)));
eventsResponse.setMetricHeaders(List.of(
new Header(&quot;eventValue&quot;),
new Header(&quot;eventCount&quot;),
new Header(&quot;transactions&quot;),
new Header(&quot;purchaseRevenue&quot;)
));
eventsResponse.setRows(List.of(
createRow(List.of(&quot;20230101&quot;), List.of(&quot;2.0&quot;, &quot;7&quot;, &quot;17&quot;, &quot;29.0&quot;))
));
// when
Map&lt;String, String&gt; response = GoogleAnalyticsResponseUtils.mapHeadersWithValues(
eventsResponse.getMetricHeaders(),
eventsResponse.getRows().get(0).getMetricValues()
);
// then
assertThat(response).containsExactlyInAnyOrderEntriesOf(Map.of(
&quot;eventValue&quot;, &quot;2.0&quot;,
&quot;eventCount&quot;, &quot;7&quot;,
&quot;transactions&quot;, &quot;17&quot;,
&quot;purchaseRevenue&quot;, &quot;29.0&quot;
));
}</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>🧪 Should map response headers with values - headers size greater than values size</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 map response headers with values - headers size greater than values size&#x27;() {
given:
Ga4InsightsApiResponse eventsResponse = new Ga4InsightsApiResponse(
dimensionHeaders: [new Header(name: &#x27;date&#x27;)],
metricHeaders: [new Header(name: &#x27;eventValue&#x27;), new Header(name: &#x27;eventCount&#x27;), new Header(name: &#x27;transactions&#x27;), new Header(name: &#x27;purchaseRevenue&#x27;)],
rows: [
new Row(dimensionValues: [new Value(value: &#x27;20230101&#x27;)], metricValues: [new Value(value: &#x27;2.0&#x27;)]),
]
)
when:
Map&lt;String, String&gt; response = GoogleAnalyticsResponseUtils.mapHeadersWithValues(eventsResponse.metricHeaders, eventsResponse.rows.get(0).metricValues)
then:
response == [eventValue: &#x27;2.0&#x27;, eventCount: &#x27;0&#x27;, transactions: &#x27;0&#x27;, purchaseRevenue: &#x27;0.0&#x27;]
}</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre> @Test
void shouldMapResponseHeadersWithValuesWhenHeadersSizeGreaterThanValuesSize() {
// given
Ga4InsightsApiResponse eventsResponse = new Ga4InsightsApiResponse();
eventsResponse.setDimensionHeaders(List.of(new Header(&quot;date&quot;)));
eventsResponse.setMetricHeaders(List.of(
new Header(&quot;eventValue&quot;),
new Header(&quot;eventCount&quot;),
new Header(&quot;transactions&quot;),
new Header(&quot;purchaseRevenue&quot;)
));
eventsResponse.setRows(List.of(
createRow(List.of(&quot;20230101&quot;), List.of(&quot;2.0&quot;))
));
// when
Map&lt;String, String&gt; response = GoogleAnalyticsResponseUtils.mapHeadersWithValues(
eventsResponse.getMetricHeaders(),
eventsResponse.getRows().get(0).getMetricValues()
);
// then
assertThat(response).containsExactlyInAnyOrderEntriesOf(Map.of(
&quot;eventValue&quot;, &quot;2.0&quot;,
&quot;eventCount&quot;, &quot;0&quot;,
&quot;transactions&quot;, &quot;0&quot;,
&quot;purchaseRevenue&quot;, &quot;0.0&quot;
));
}</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>🧪 Should map response headers with values - values size greater than headers size</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 map response headers with values - values size greater than headers size&#x27;() {
given:
Ga4InsightsApiResponse eventsResponse = new Ga4InsightsApiResponse(
dimensionHeaders: [new Header(name: &#x27;date&#x27;)],
metricHeaders: [new Header(name: &#x27;eventValue&#x27;), new Header(name: &#x27;eventCount&#x27;)],
rows: [
new Row(dimensionValues: [new Value(value: &#x27;20230101&#x27;)], metricValues: [new Value(value: &#x27;2.0&#x27;), new Value(value: &#x27;7&#x27;), new Value(value: &#x27;17&#x27;), new Value(value: &#x27;29.0&#x27;)]),
]
)
when:
GoogleAnalyticsResponseUtils.mapHeadersWithValues(eventsResponse.metricHeaders, eventsResponse.rows.get(0).metricValues)
then:
Exception e = thrown(IllegalArgumentException)
e.message == &quot;Headers should have a size greater or equals than values, header size = 2, values size = 4&quot;
}</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre> @Test
void shouldThrowExceptionWhenValuesSizeGreaterThanHeadersSize() {
// given
Ga4InsightsApiResponse eventsResponse = new Ga4InsightsApiResponse();
eventsResponse.setDimensionHeaders(List.of(new Header(&quot;date&quot;)));
eventsResponse.setMetricHeaders(List.of(
new Header(&quot;eventValue&quot;),
new Header(&quot;eventCount&quot;)
));
eventsResponse.setRows(List.of(
createRow(List.of(&quot;20230101&quot;), List.of(&quot;2.0&quot;, &quot;7&quot;, &quot;17&quot;, &quot;29.0&quot;))
));
// when/then
assertThatThrownBy(() -&gt; GoogleAnalyticsResponseUtils.mapHeadersWithValues(
eventsResponse.getMetricHeaders(),
eventsResponse.getRows().get(0).getMetricValues()
))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(&quot;Headers should have a size greater or equals than values, header size = 2, values size = 4&quot;);
}</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>🧪 Should merge batch responses</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 merge batch responses&#x27;() {
given:
Ga4InsightsApiResponse visitorsResponse = new Ga4InsightsApiResponse(
dimensionHeaders: [new Header(name: &#x27;date&#x27;)],
metricHeaders: [new Header(name: &#x27;totalUsers&#x27;)],
rows: [
new Row(dimensionValues: [new Value(value: &#x27;20230101&#x27;)], metricValues: [new Value(value: &#x27;10&#x27;)]),
new Row(dimensionValues: [new Value(value: &#x27;20230102&#x27;)], metricValues: [new Value(value: &#x27;13&#x27;)]),
],
totals: [new Row(metricValues: [new Value(value: &#x27;15&#x27;)])]
)
Ga4InsightsApiResponse eventsResponse = new Ga4InsightsApiResponse(
dimensionHeaders: [new Header(name: &#x27;date&#x27;), new Header(name: &#x27;eventName&#x27;)],
metricHeaders: [new Header(name: &#x27;eventValue&#x27;), new Header(name: &#x27;eventCount&#x27;), new Header(name: &#x27;transactions&#x27;), new Header(name: &#x27;purchaseRevenue&#x27;)],
rows: [
new Row(dimensionValues: [new Value(value: &#x27;20230101&#x27;)], metricValues: [new Value(value: &#x27;2.0&#x27;), new Value(value: &#x27;7&#x27;), new Value(value: &#x27;17&#x27;), new Value(value: &#x27;29.0&#x27;)]),
],
totals: [new Row(metricValues: [new Value(value: &#x27;2.0&#x27;), new Value(value: &#x27;7&#x27;), new Value(value: &#x27;17&#x27;), new Value(value: &#x27;29.0&#x27;)])]
)
when:
Ga4InsightsApiResponse mergedResponse = GoogleAnalyticsResponseUtils.mergeBatchResponse(visitorsResponse, eventsResponse)
then:
mergedResponse.dimensionHeaders == [new Header(name: &#x27;date&#x27;)]
mergedResponse.metricHeaders == [new Header(name: &#x27;totalUsers&#x27;), new Header(name: &#x27;eventValue&#x27;), new Header(name: &#x27;eventCount&#x27;), new Header(name: &#x27;transactions&#x27;), new Header(name: &#x27;purchaseRevenue&#x27;)]
mergedResponse.rows == [
new Row(dimensionValues: [new Value(value: &#x27;20230101&#x27;)], metricValues: [new Value(value: &#x27;10&#x27;), new Value(value: &#x27;2.0&#x27;), new Value(value: &#x27;7&#x27;), new Value(value: &#x27;17&#x27;), new Value(value: &#x27;29.0&#x27;)]),
new Row(dimensionValues: [new Value(value: &#x27;20230102&#x27;)], metricValues: [new Value(value: &#x27;13&#x27;), new Value(value: &#x27;0.0&#x27;), new Value(value: &#x27;0&#x27;), new Value(value: &#x27;0&#x27;), new Value(value: &#x27;0.0&#x27;)]),
]
mergedResponse.totals == [
new Row(metricValues: [new Value(value: &#x27;15&#x27;), new Value(value: &#x27;2.0&#x27;), new Value(value: &#x27;7&#x27;), new Value(value: &#x27;17&#x27;), new Value(value: &#x27;29.0&#x27;)])
]
}</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre> @Test
void shouldMergeBatchResponses() {
// given
Ga4InsightsApiResponse visitorsResponse = new Ga4InsightsApiResponse();
visitorsResponse.setDimensionHeaders(List.of(new Header(&quot;date&quot;)));
visitorsResponse.setMetricHeaders(List.of(new Header(&quot;totalUsers&quot;)));
visitorsResponse.setRows(List.of(
createRow(List.of(&quot;20230101&quot;), List.of(&quot;10&quot;)),
createRow(List.of(&quot;20230102&quot;), List.of(&quot;13&quot;))
));
visitorsResponse.setTotals(List.of(createRowWithMetricsOnly(List.of(&quot;15&quot;))));
Ga4InsightsApiResponse eventsResponse = new Ga4InsightsApiResponse();
eventsResponse.setDimensionHeaders(List.of(new Header(&quot;date&quot;), new Header(&quot;eventName&quot;)));
eventsResponse.setMetricHeaders(List.of(
new Header(&quot;eventValue&quot;),
new Header(&quot;eventCount&quot;),
new Header(&quot;transactions&quot;),
new Header(&quot;purchaseRevenue&quot;)
));
eventsResponse.setRows(List.of(
createRow(List.of(&quot;20230101&quot;), List.of(&quot;2.0&quot;, &quot;7&quot;, &quot;17&quot;, &quot;29.0&quot;))
));
eventsResponse.setTotals(List.of(createRowWithMetricsOnly(List.of(&quot;2.0&quot;, &quot;7&quot;, &quot;17&quot;, &quot;29.0&quot;))));
// when
Ga4InsightsApiResponse mergedResponse = GoogleAnalyticsResponseUtils.mergeBatchResponse(visitorsResponse, eventsResponse);
// then
assertThat(mergedResponse.getDimensionHeaders()).containsExactly(new Header(&quot;date&quot;));
assertThat(mergedResponse.getMetricHeaders()).containsExactly(
new Header(&quot;totalUsers&quot;),
new Header(&quot;eventValue&quot;),
new Header(&quot;eventCount&quot;),
new Header(&quot;transactions&quot;),
new Header(&quot;purchaseRevenue&quot;)
);
assertThat(mergedResponse.getRows()).containsExactly(
createRow(List.of(&quot;20230101&quot;), List.of(&quot;10&quot;, &quot;2.0&quot;, &quot;7&quot;, &quot;17&quot;, &quot;29.0&quot;)),
createRow(List.of(&quot;20230102&quot;), List.of(&quot;13&quot;, &quot;0.0&quot;, &quot;0&quot;, &quot;0&quot;, &quot;0.0&quot;))
);
assertThat(mergedResponse.getTotals()).containsExactly(
createRowWithMetricsOnly(List.of(&quot;15&quot;, &quot;2.0&quot;, &quot;7&quot;, &quot;17&quot;, &quot;29.0&quot;))
);
}</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>🧪 Should merge batch responses - 0 event</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 merge batch responses - 0 event&#x27;() {
given:
Ga4InsightsApiResponse visitorsResponse = new Ga4InsightsApiResponse(
dimensionHeaders: [new Header(name: &#x27;date&#x27;)],
metricHeaders: [new Header(name: &#x27;totalUsers&#x27;)],
rows: [
new Row(dimensionValues: [new Value(value: &#x27;20230101&#x27;)], metricValues: [new Value(value: &#x27;10&#x27;)]),
new Row(dimensionValues: [new Value(value: &#x27;20230102&#x27;)], metricValues: [new Value(value: &#x27;13&#x27;)]),
],
totals: [new Row(metricValues: [new Value(value: &#x27;15&#x27;)])]
)
Ga4InsightsApiResponse eventsResponse = new Ga4InsightsApiResponse(
dimensionHeaders: [new Header(name: &#x27;date&#x27;), new Header(name: &#x27;eventName&#x27;)],
metricHeaders: [new Header(name: &#x27;eventValue&#x27;), new Header(name: &#x27;eventCount&#x27;), new Header(name: &#x27;transactions&#x27;), new Header(name: &#x27;purchaseRevenue&#x27;)],
totals: [new Row()]
)
when:
Ga4InsightsApiResponse mergedResponse = GoogleAnalyticsResponseUtils.mergeBatchResponse(visitorsResponse, eventsResponse)
then:
mergedResponse.dimensionHeaders == [new Header(name: &#x27;date&#x27;)]
mergedResponse.metricHeaders == [new Header(name: &#x27;totalUsers&#x27;), new Header(name: &#x27;eventValue&#x27;), new Header(name: &#x27;eventCount&#x27;), new Header(name: &#x27;transactions&#x27;), new Header(name: &#x27;purchaseRevenue&#x27;)]
mergedResponse.rows == [
new Row(dimensionValues: [new Value(value: &#x27;20230101&#x27;)], metricValues: [new Value(value: &#x27;10&#x27;), new Value(value: &#x27;0.0&#x27;), new Value(value: &#x27;0&#x27;), new Value(value: &#x27;0&#x27;), new Value(value: &#x27;0.0&#x27;)]),
new Row(dimensionValues: [new Value(value: &#x27;20230102&#x27;)], metricValues: [new Value(value: &#x27;13&#x27;), new Value(value: &#x27;0.0&#x27;), new Value(value: &#x27;0&#x27;), new Value(value: &#x27;0&#x27;), new Value(value: &#x27;0.0&#x27;)]),
]
mergedResponse.totals == [
new Row(metricValues: [new Value(value: &#x27;15&#x27;), new Value(value: &#x27;0.0&#x27;), new Value(value: &#x27;0&#x27;), new Value(value: &#x27;0&#x27;), new Value(value: &#x27;0.0&#x27;)])
]
}</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre> @Test
void shouldMergeBatchResponsesWithZeroEvents() {
// given
Ga4InsightsApiResponse visitorsResponse = new Ga4InsightsApiResponse();
visitorsResponse.setDimensionHeaders(List.of(new Header(&quot;date&quot;)));
visitorsResponse.setMetricHeaders(List.of(new Header(&quot;totalUsers&quot;)));
visitorsResponse.setRows(List.of(
createRow(List.of(&quot;20230101&quot;), List.of(&quot;10&quot;)),
createRow(List.of(&quot;20230102&quot;), List.of(&quot;13&quot;))
));
visitorsResponse.setTotals(List.of(createRowWithMetricsOnly(List.of(&quot;15&quot;))));
Ga4InsightsApiResponse eventsResponse = new Ga4InsightsApiResponse();
eventsResponse.setDimensionHeaders(List.of(new Header(&quot;date&quot;), new Header(&quot;eventName&quot;)));
eventsResponse.setMetricHeaders(List.of(
new Header(&quot;eventValue&quot;),
new Header(&quot;eventCount&quot;),
new Header(&quot;transactions&quot;),
new Header(&quot;purchaseRevenue&quot;)
));
eventsResponse.setTotals(List.of(new Row()));
// when
Ga4InsightsApiResponse mergedResponse = GoogleAnalyticsResponseUtils.mergeBatchResponse(visitorsResponse, eventsResponse);
// then
assertThat(mergedResponse.getDimensionHeaders()).containsExactly(new Header(&quot;date&quot;));
assertThat(mergedResponse.getMetricHeaders()).containsExactly(
new Header(&quot;totalUsers&quot;),
new Header(&quot;eventValue&quot;),
new Header(&quot;eventCount&quot;),
new Header(&quot;transactions&quot;),
new Header(&quot;purchaseRevenue&quot;)
);
assertThat(mergedResponse.getRows()).containsExactly(
createRow(List.of(&quot;20230101&quot;), List.of(&quot;10&quot;, &quot;0.0&quot;, &quot;0&quot;, &quot;0&quot;, &quot;0.0&quot;)),
createRow(List.of(&quot;20230102&quot;), List.of(&quot;13&quot;, &quot;0.0&quot;, &quot;0&quot;, &quot;0&quot;, &quot;0.0&quot;))
);
assertThat(mergedResponse.getTotals()).containsExactly(
createRowWithMetricsOnly(List.of(&quot;15&quot;, &quot;0.0&quot;, &quot;0&quot;, &quot;0&quot;, &quot;0.0&quot;))
);
}</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header" onclick="toggleSection(this)">
<h2>🧪 Should merge batch responses - all events</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 merge batch responses - all events&#x27;() {
given:
Ga4InsightsApiResponse visitorsResponse = new Ga4InsightsApiResponse(
dimensionHeaders: [new Header(name: &#x27;date&#x27;)],
metricHeaders: [new Header(name: &#x27;totalUsers&#x27;)],
rows: [
new Row(dimensionValues: [new Value(value: &#x27;20230101&#x27;)], metricValues: [new Value(value: &#x27;10&#x27;)]),
new Row(dimensionValues: [new Value(value: &#x27;20230102&#x27;)], metricValues: [new Value(value: &#x27;13&#x27;)]),
],
totals: [new Row(metricValues: [new Value(value: &#x27;15&#x27;)])]
)
Ga4InsightsApiResponse eventsResponse = new Ga4InsightsApiResponse(
dimensionHeaders: [new Header(name: &#x27;date&#x27;), new Header(name: &#x27;eventName&#x27;)],
metricHeaders: [new Header(name: &#x27;eventValue&#x27;), new Header(name: &#x27;eventCount&#x27;), new Header(name: &#x27;transactions&#x27;), new Header(name: &#x27;purchaseRevenue&#x27;)],
rows: [
new Row(dimensionValues: [new Value(value: &#x27;20230101&#x27;)], metricValues: [new Value(value: &#x27;5.0&#x27;), new Value(value: &#x27;18&#x27;), new Value(value: &#x27;36&#x27;), new Value(value: &#x27;60.0&#x27;)]),
new Row(dimensionValues: [new Value(value: &#x27;20230102&#x27;)], metricValues: [new Value(value: &#x27;5.0&#x27;), new Value(value: &#x27;13&#x27;), new Value(value: &#x27;23&#x27;), new Value(value: &#x27;37.0&#x27;)]),
],
totals: [new Row(metricValues: [new Value(value: &#x27;10.0&#x27;), new Value(value: &#x27;31&#x27;), new Value(value: &#x27;59&#x27;), new Value(value: &#x27;97.0&#x27;)])]
)
when:
Ga4InsightsApiResponse mergedResponse = GoogleAnalyticsResponseUtils.mergeBatchResponse(visitorsResponse, eventsResponse)
then:
mergedResponse.dimensionHeaders == [new Header(name: &#x27;date&#x27;)]
mergedResponse.metricHeaders == [new Header(name: &#x27;totalUsers&#x27;), new Header(name: &#x27;eventValue&#x27;), new Header(name: &#x27;eventCount&#x27;), new Header(name: &#x27;transactions&#x27;), new Header(name: &#x27;purchaseRevenue&#x27;)]
mergedResponse.rows == [
new Row(dimensionValues: [new Value(value: &#x27;20230101&#x27;)], metricValues: [new Value(value: &#x27;10&#x27;), new Value(value: &#x27;5.0&#x27;), new Value(value: &#x27;18&#x27;), new Value(value: &#x27;36&#x27;), new Value(value: &#x27;60.0&#x27;)]),
new Row(dimensionValues: [new Value(value: &#x27;20230102&#x27;)], metricValues: [new Value(value: &#x27;13&#x27;), new Value(value: &#x27;5.0&#x27;), new Value(value: &#x27;13&#x27;), new Value(value: &#x27;23&#x27;), new Value(value: &#x27;37.0&#x27;)]),
]
mergedResponse.totals == [
new Row(metricValues: [new Value(value: &#x27;15&#x27;), new Value(value: &#x27;10.0&#x27;), new Value(value: &#x27;31&#x27;), new Value(value: &#x27;59&#x27;), new Value(value: &#x27;97.0&#x27;)])
]
}</pre>
</div>
<div class="diff-panel right">
<h3>JUnit 5 (Java)</h3>
<pre> @Test
void shouldMergeBatchResponsesWithAllEvents() {
// given
Ga4InsightsApiResponse visitorsResponse = new Ga4InsightsApiResponse();
visitorsResponse.setDimensionHeaders(List.of(new Header(&quot;date&quot;)));
visitorsResponse.setMetricHeaders(List.of(new Header(&quot;totalUsers&quot;)));
visitorsResponse.setRows(List.of(
createRow(List.of(&quot;20230101&quot;), List.of(&quot;10&quot;)),
createRow(List.of(&quot;20230102&quot;), List.of(&quot;13&quot;))
));
visitorsResponse.setTotals(List.of(createRowWithMetricsOnly(List.of(&quot;15&quot;))));
Ga4InsightsApiResponse eventsResponse = new Ga4InsightsApiResponse();
eventsResponse.setDimensionHeaders(List.of(new Header(&quot;date&quot;), new Header(&quot;eventName&quot;)));
eventsResponse.setMetricHeaders(List.of(
new Header(&quot;eventValue&quot;),
new Header(&quot;eventCount&quot;),
new Header(&quot;transactions&quot;),
new Header(&quot;purchaseRevenue&quot;)
));
eventsResponse.setRows(List.of(
createRow(List.of(&quot;20230101&quot;), List.of(&quot;5.0&quot;, &quot;18&quot;, &quot;36&quot;, &quot;60.0&quot;)),
createRow(List.of(&quot;20230102&quot;), List.of(&quot;5.0&quot;, &quot;13&quot;, &quot;23&quot;, &quot;37.0&quot;))
));
eventsResponse.setTotals(List.of(createRowWithMetricsOnly(List.of(&quot;10.0&quot;, &quot;31&quot;, &quot;59&quot;, &quot;97.0&quot;))));
// when
Ga4InsightsApiResponse mergedResponse = GoogleAnalyticsResponseUtils.mergeBatchResponse(visitorsResponse, eventsResponse);
// then
assertThat(mergedResponse.getDimensionHeaders()).containsExactly(new Header(&quot;date&quot;));
assertThat(mergedResponse.getMetricHeaders()).containsExactly(
new Header(&quot;totalUsers&quot;),
new Header(&quot;eventValue&quot;),
new Header(&quot;eventCount&quot;),
new Header(&quot;transactions&quot;),
new Header(&quot;purchaseRevenue&quot;)
);
assertThat(mergedResponse.getRows()).containsExactly(
createRow(List.of(&quot;20230101&quot;), List.of(&quot;10&quot;, &quot;5.0&quot;, &quot;18&quot;, &quot;36&quot;, &quot;60.0&quot;)),
createRow(List.of(&quot;20230102&quot;), List.of(&quot;13&quot;, &quot;5.0&quot;, &quot;13&quot;, &quot;23&quot;, &quot;37.0&quot;))
);
assertThat(mergedResponse.getTotals()).containsExactly(
createRowWithMetricsOnly(List.of(&quot;15&quot;, &quot;10.0&quot;, &quot;31&quot;, &quot;59&quot;, &quot;97.0&quot;))
);
}</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