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