This repository was archived by the owner on Jul 10, 2024. It is now read-only.
forked from dstendardi/snowplow-java-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchEmitterTest.java
More file actions
114 lines (86 loc) · 3.39 KB
/
Copy pathBatchEmitterTest.java
File metadata and controls
114 lines (86 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.snowplowanalytics.snowplow.tracker.emitter;
import com.google.common.collect.Lists;
import com.snowplowanalytics.snowplow.tracker.http.HttpClientAdapter;
import com.snowplowanalytics.snowplow.tracker.payload.SchemaPayload;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import static org.mockito.Mockito.*;
public class BatchEmitterTest {
private HttpClientAdapter httpClientAdapter;
private BatchEmitter emitter;
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Before
public void setUp() throws Exception {
httpClientAdapter = mock(HttpClientAdapter.class);
emitter = spy(new BatchEmitter(httpClientAdapter));
}
@Test
public void addToBuffer_withLess10Payloads_shouldNotFlushBuffer() throws Exception {
// Given
ArgumentCaptor<Payload> argumentCaptor = ArgumentCaptor.forClass(Payload.class);
List<Payload> payloads = createPayloads(2);
// When
for (Payload payload : payloads) {
emitter.emit(payload);
}
// Then
verify(emitter, never()).flushBuffer();
verify(httpClientAdapter, never()).get(argumentCaptor.capture());
Assert.assertEquals(2, emitter.getBuffer().size());
Assert.assertEquals(payloads, emitter.getBuffer());
}
@Test
public void addToBuffer_withMore10Payloads_shouldFlushBuffer() throws Exception {
// Given
ArgumentCaptor<SchemaPayload> argumentCaptor = ArgumentCaptor.forClass(SchemaPayload.class);
List<Payload> payloads = createPayloads(11);
// When
for (Payload payload : payloads) {
emitter.emit(payload);
}
// Then
verify(emitter).flushBuffer();
verify(httpClientAdapter).post(argumentCaptor.capture());
Assert.assertEquals(payloads.subList(0, payloads.size() - 1), argumentCaptor.getValue().getData());
Payload lastPayload = payloads.get(payloads.size() - 1);
Assert.assertTrue(emitter.getBuffer().size() == 1);
Assert.assertEquals(lastPayload, emitter.getBuffer().get(0));
}
@Test
public void emit_withNot200ResponseStatus_shouldThrowRuntimeException() {
// Given
doThrow(RuntimeException.class).when(httpClientAdapter).post(any(SchemaPayload.class));
expectedException.expectMessage("Failed to emit 10 events");
List<Payload> payloads = createPayloads(11);
// When
for (Payload payload : payloads) {
emitter.emit(payload);
}
}
@Test
public void setBufferSize_WithNegativeValue_ThrowInvalidArgumentException() throws Exception {
expectedException.expect(IllegalArgumentException.class);
emitter.setBufferSize(-1);
}
private List<Payload> createPayloads(int nbPayload) {
final List<Payload> payloads = Lists.newArrayList();
for (int i = 0; i < nbPayload; i++) {
payloads.add(createPayload());
}
return payloads;
}
private Payload createPayload() {
Payload payload = new Payload();
payload.put("id", UUID.randomUUID());
return payload;
}
private static class Payload extends HashMap<String,Object> { }
}