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); } } } }