-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathStreamTest.java
More file actions
97 lines (88 loc) · 3.52 KB
/
StreamTest.java
File metadata and controls
97 lines (88 loc) · 3.52 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
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.intercom.api;
import static org.junit.jupiter.api.Assertions.*;
import com.intercom.api.core.ObjectMappers;
import com.intercom.api.core.Stream;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
public final class StreamTest {
@Test
public void testJsonStream() {
List<Map<String, String>> messages =
Arrays.asList(createMap("message", "hello"), createMap("message", "world"));
List<String> jsonStrings = messages.stream().map(StreamTest::mapToJson).collect(Collectors.toList());
String input = String.join("\n", jsonStrings);
StringReader jsonInput = new StringReader(input);
Stream<Map> jsonStream = Stream.fromJson(Map.class, jsonInput);
int expectedMessages = 2;
int actualMessages = 0;
for (Map jsonObject : jsonStream) {
actualMessages++;
assertTrue(jsonObject.containsKey("message"));
}
assertEquals(expectedMessages, actualMessages);
}
@Test
public void testSseStream() {
List<Map<String, String>> events = Arrays.asList(createMap("event", "start"), createMap("event", "end"));
List<String> sseStrings = events.stream().map(StreamTest::mapToSse).collect(Collectors.toList());
String input = String.join("\n" + "\n", sseStrings);
StringReader sseInput = new StringReader(input);
Stream<Map> sseStream = Stream.fromSse(Map.class, sseInput);
int expectedEvents = 2;
int actualEvents = 0;
for (Map eventData : sseStream) {
actualEvents++;
assertTrue(eventData.containsKey("event"));
}
assertEquals(expectedEvents, actualEvents);
}
@Test
public void testSseStreamWithTerminator() {
List<Map<String, String>> events = Arrays.asList(createMap("message", "first"), createMap("message", "second"));
List<String> sseStrings =
new ArrayList<>(events.stream().map(StreamTest::mapToSse).collect(Collectors.toList()));
sseStrings.add("data: [DONE]");
String input = String.join("\n" + "\n", sseStrings);
StringReader sseInput = new StringReader(input);
Stream<Map> sseStream = Stream.fromSse(Map.class, sseInput, "[DONE]");
int expectedEvents = 2;
int actualEvents = 0;
for (Map eventData : sseStream) {
actualEvents++;
assertTrue(eventData.containsKey("message"));
}
assertEquals(expectedEvents, actualEvents);
}
@Test
public void testStreamResourceManagement() throws IOException {
StringReader testInput = new StringReader("{\"test\":\"data\"}");
Stream<Map> testStream = Stream.fromJson(Map.class, testInput);
testStream.close();
assertFalse(testStream.iterator().hasNext());
}
private static String mapToJson(Map map) {
try {
return ObjectMappers.JSON_MAPPER.writeValueAsString(map);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String mapToSse(Map map) {
return "data: " + mapToJson(map);
}
private static Map<String, String> createMap(String key, String value) {
Map<String, String> map = new HashMap<>();
map.put(key, value);
return map;
}
}