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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
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 counters responses.
* Uses fixture from src/test/resources/responses/counters.json
*/
public class CountersResponseTest {
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 countersResponse_hasSuccessTrue() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/counters.json")).getAsJsonObject();
assertTrue("Counters response should have success=true", response.get("success").getAsBoolean());
}
@Test
public void countersResponse_hasDataArray() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/counters.json")).getAsJsonObject();
assertTrue("Counters data should be an array", response.get("data").isJsonArray());
}
@Test
public void countersResponse_hasGlobalUnread() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/counters.json")).getAsJsonObject();
JsonArray counters = response.getAsJsonArray("data");
for (JsonElement entry : counters) {
JsonObject c = entry.getAsJsonObject();
if ("global-unread".equals(c.get("id").getAsString())) {
assertTrue("global-unread should have a counter", c.has("counter"));
return;
}
}
fail("Counters should have a global-unread entry");
}
@Test
public void countersResponse_hasSubscribedFeeds() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/counters.json")).getAsJsonObject();
JsonArray counters = response.getAsJsonArray("data");
for (JsonElement entry : counters) {
JsonObject c = entry.getAsJsonObject();
if ("subscribed-feeds".equals(c.get("id").getAsString())) {
assertTrue("subscribed-feeds should have a counter", c.has("counter"));
int count = c.get("counter").getAsInt();
assertTrue("subscribed-feeds count should be positive", count > 0);
return;
}
}
fail("Counters should have a subscribed-feeds entry");
}
@Test
public void countersResponse_hasStarredCounter() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/counters.json")).getAsJsonObject();
JsonArray counters = response.getAsJsonArray("data");
for (JsonElement entry : counters) {
JsonObject c = entry.getAsJsonObject();
if ("-1".equals(c.get("id").getAsString())) {
assertTrue("starred counter should have counter", c.has("counter"));
assertTrue("starred counter should have markedcounter", c.has("markedcounter"));
return;
}
}
fail("Counters should have a starred (-1) entry");
}
@Test
public void countersResponse_hasPublishedCounter() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/counters.json")).getAsJsonObject();
JsonArray counters = response.getAsJsonArray("data");
for (JsonElement entry : counters) {
JsonObject c = entry.getAsJsonObject();
if ("-2".equals(c.get("id").getAsString())) {
assertTrue("published counter should have counter", c.has("counter"));
assertTrue("published counter should have publishedcounter", c.has("publishedcounter"));
return;
}
}
fail("Counters should have a published (-2) entry");
}
@Test
public void countersResponse_hasAllArticlesCounter() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/counters.json")).getAsJsonObject();
JsonArray counters = response.getAsJsonArray("data");
for (JsonElement entry : counters) {
JsonObject c = entry.getAsJsonObject();
if ("-4".equals(c.get("id").getAsString())) {
assertTrue("all-articles counter should have counter", c.has("counter"));
assertTrue("all-articles counter should have auxcounter", c.has("auxcounter"));
return;
}
}
fail("Counters should have an all-articles (-4) entry");
}
@Test
public void countersResponse_hasCategoryEntries() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/counters.json")).getAsJsonObject();
JsonArray counters = response.getAsJsonArray("data");
int categoryCount = 0;
for (JsonElement entry : counters) {
JsonObject c = entry.getAsJsonObject();
// Categories have a description field and negative IDs (e.g. -1298)
if (c.has("description") && c.get("id").getAsInt() < 0) {
categoryCount++;
}
}
assertTrue("Counters should have category entries", categoryCount > 0);
}
@Test
public void countersResponse_categoryHasCounter() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/counters.json")).getAsJsonObject();
JsonArray counters = response.getAsJsonArray("data");
for (JsonElement entry : counters) {
JsonObject c = entry.getAsJsonObject();
if (c.has("description")) {
assertTrue("Category counter should have counter", c.has("counter"));
assertTrue("Category counter should have auxcounter", c.has("auxcounter"));
// counter and auxcounter should be non-negative
assertTrue("counter should be non-negative", c.get("counter").getAsInt() >= 0);
assertTrue("auxcounter should be non-negative", c.get("auxcounter").getAsInt() >= 0);
}
}
}
@Test
public void countersResponse_categoryHasDescription() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/counters.json")).getAsJsonObject();
JsonArray counters = response.getAsJsonArray("data");
for (JsonElement entry : counters) {
JsonObject c = entry.getAsJsonObject();
if (c.has("description")) {
assertFalse("Category description should not be empty",
c.get("description").getAsString().isEmpty());
}
}
}
@Test
public void countersResponse_specialFeedIdsAreNegative() throws IOException {
JsonObject response = JsonParser.parseString(loadFixture("responses/counters.json")).getAsJsonObject();
JsonArray counters = response.getAsJsonArray("data");
for (JsonElement entry : counters) {
JsonObject c = entry.getAsJsonObject();
String id = c.get("id").getAsString();
// Skip special string IDs
if ("global-unread".equals(id) || "subscribed-feeds".equals(id)) continue;
int feedId = c.get("id").getAsInt();
// Special feeds (-1 to -6) should be negative
if (feedId < 0) {
assertTrue("Special feed id should be negative", feedId < 0);
}
}
}
}
|