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
115
116
117
|
package org.fox.ttrss;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static org.junit.Assert.*;
/**
* Tests for parsing TT-RSS API v2 feeds responses.
* Uses fixture from src/test/resources/responses/feeds.json
*/
public class FeedsResponseTest {
private String loadFixture(String path) throws IOException {
InputStream is = getClass().getClassLoader().getResourceAsStream(path);
if (is == null) {
throw new IOException("Fixture not found: " + path);
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
}
@Test
public void feedsResponse_hasSuccessTrue() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/feeds.json")).getAsJsonObject();
assertTrue("Feeds response should have success=true", response.get("success").getAsBoolean());
}
@Test
public void feedsResponse_hasDataArray() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/feeds.json")).getAsJsonObject();
JsonElement data = response.get("data");
assertNotNull("Feeds response should have data", data);
assertTrue("Feeds data should be an array", data.isJsonArray());
}
@Test
public void feedsResponse_hasMultipleFeeds() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/feeds.json")).getAsJsonObject();
JsonArray feeds = response.getAsJsonArray("data");
assertTrue("Feeds should have at least one entry", feeds.size() > 0);
}
@Test
public void feed_entry_hasRequiredFields() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/feeds.json")).getAsJsonObject();
JsonArray feeds = response.getAsJsonArray("data");
JsonObject first = feeds.get(0).getAsJsonObject();
assertTrue("Feed should have feed_url", first.has("feed_url"));
assertTrue("Feed should have title", first.has("title"));
assertTrue("Feed should have id", first.has("id"));
assertTrue("Feed should have unread", first.has("unread"));
assertTrue("Feed should have cat_id", first.has("cat_id"));
assertTrue("Feed should have last_updated", first.has("last_updated"));
assertTrue("Feed should have order_id", first.has("order_id"));
assertTrue("Feed should have update_interval", first.has("update_interval"));
}
@Test
public void feed_entry_hasOptionalFields() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/feeds.json")).getAsJsonObject();
JsonArray feeds = response.getAsJsonArray("data");
JsonObject first = feeds.get(0).getAsJsonObject();
assertTrue("Feed should have has_icon", first.has("has_icon"));
assertTrue("Feed should have last_error", first.has("last_error"));
}
@Test
public void feed_entry_idsArePositive() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/feeds.json")).getAsJsonObject();
JsonArray feeds = response.getAsJsonArray("data");
for (JsonElement feed : feeds) {
int id = feed.getAsJsonObject().get("id").getAsInt();
assertTrue("Feed id should be positive, got: " + id, id > 0);
}
}
@Test
public void feed_entry_unreadIsNonNegative() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/feeds.json")).getAsJsonObject();
JsonArray feeds = response.getAsJsonArray("data");
for (JsonElement feed : feeds) {
int unread = feed.getAsJsonObject().get("unread").getAsInt();
assertTrue("Feed unread should be non-negative, got: " + unread, unread >= 0);
}
}
@Test
public void feed_entry_hasNonEmptyTitle() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/feeds.json")).getAsJsonObject();
JsonArray feeds = response.getAsJsonArray("data");
for (JsonElement feed : feeds) {
String title = feed.getAsJsonObject().get("title").getAsString();
assertFalse("Feed title should not be empty", title.isEmpty());
}
}
}
|