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 headlines (articles) responses. * Uses fixture from src/test/resources/responses/headlines.json */ public class HeadlinesResponseTest { 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 headlinesResponse_hasSuccessTrue() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/headlines.json")).getAsJsonObject(); assertTrue("Headlines response should have success=true", response.get("success").getAsBoolean()); } @Test public void headlinesResponse_hasDataArray() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/headlines.json")).getAsJsonObject(); assertTrue("Headlines data should be an array", response.get("data").isJsonArray()); } @Test public void headlinesResponse_hasArticles() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/headlines.json")).getAsJsonObject(); JsonArray articles = response.getAsJsonArray("data"); assertTrue("Headlines should have at least one article", articles.size() > 0); } @Test public void article_entry_hasRequiredFields() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/headlines.json")).getAsJsonObject(); JsonArray articles = response.getAsJsonArray("data"); JsonObject first = articles.get(0).getAsJsonObject(); assertTrue("Article should have id", first.has("id")); assertTrue("Article should have guid", first.has("guid")); assertTrue("Article should have unread", first.has("unread")); assertTrue("Article should have marked", first.has("marked")); assertTrue("Article should have published", first.has("published")); assertTrue("Article should have updated", first.has("updated")); assertTrue("Article should have title", first.has("title")); assertTrue("Article should have link", first.has("link")); assertTrue("Article should have feed_id", first.has("feed_id")); } @Test public void article_entry_hasOptionalFields() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/headlines.json")).getAsJsonObject(); JsonArray articles = response.getAsJsonArray("data"); JsonObject first = articles.get(0).getAsJsonObject(); assertTrue("Article should have is_updated", first.has("is_updated")); assertTrue("Article should have tags", first.has("tags")); assertTrue("Article should have labels", first.has("labels")); assertTrue("Article should have feed_title", first.has("feed_title")); assertTrue("Article should have comments_count", first.has("comments_count")); assertTrue("Article should have comments_link", first.has("comments_link")); } @Test public void article_entry_idsArePositive() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/headlines.json")).getAsJsonObject(); JsonArray articles = response.getAsJsonArray("data"); for (JsonElement article : articles) { int id = article.getAsJsonObject().get("id").getAsInt(); assertTrue("Article id should be positive, got: " + id, id > 0); } } @Test public void article_entry_updatedIsNonNegative() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/headlines.json")).getAsJsonObject(); JsonArray articles = response.getAsJsonArray("data"); for (JsonElement article : articles) { int updated = article.getAsJsonObject().get("updated").getAsInt(); assertTrue("Article updated should be non-negative, got: " + updated, updated >= 0); } } @Test public void article_entry_feedIdIsPositive() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/headlines.json")).getAsJsonObject(); JsonArray articles = response.getAsJsonArray("data"); for (JsonElement article : articles) { int feedId = article.getAsJsonObject().get("feed_id").getAsInt(); assertTrue("Article feed_id should be positive, got: " + feedId, feedId > 0); } } @Test public void article_entry_booleanFieldsAreValid() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/headlines.json")).getAsJsonObject(); JsonArray articles = response.getAsJsonArray("data"); for (JsonElement article : articles) { JsonObject a = article.getAsJsonObject(); assertTrue("unread must be boolean", a.get("unread").isJsonPrimitive()); assertTrue("marked must be boolean", a.get("marked").isJsonPrimitive()); assertTrue("published must be boolean", a.get("published").isJsonPrimitive()); assertTrue("is_updated must be boolean", a.get("is_updated").isJsonPrimitive()); } } @Test public void article_entry_tagsIsArray() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/headlines.json")).getAsJsonObject(); JsonArray articles = response.getAsJsonArray("data"); for (JsonElement article : articles) { JsonObject a = article.getAsJsonObject(); assertTrue("tags should be an array", a.get("tags").isJsonArray()); } } @Test public void article_entry_guidIsPresent() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/headlines.json")).getAsJsonObject(); JsonArray articles = response.getAsJsonArray("data"); for (JsonElement article : articles) { JsonObject a = article.getAsJsonObject(); assertTrue("guid should not be empty", !a.get("guid").getAsString().isEmpty()); } } @Test public void article_entry_titleIsNonEmpty() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/headlines.json")).getAsJsonObject(); JsonArray articles = response.getAsJsonArray("data"); for (JsonElement article : articles) { JsonObject a = article.getAsJsonObject(); assertFalse("Article title should not be empty", a.get("title").getAsString().isEmpty()); } } }