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 article detail responses. * Uses fixture from src/test/resources/responses/article_detail.json */ public class ArticleDetailResponseTest { 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(); } } // ── Response structure ────────────────────────────────────────────────── @Test public void articleResponse_hasSuccessTrue() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/article_detail.json")).getAsJsonObject(); assertTrue("Article response should have success=true", response.get("success").getAsBoolean()); } @Test public void articleResponse_hasDataArray() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/article_detail.json")).getAsJsonObject(); assertTrue("Article data should be an array", response.get("data").isJsonArray()); } // ── Core fields ───────────────────────────────────────────────────────── @Test public void article_hasId() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have id", article.has("id")); int id = article.get("id").getAsInt(); assertTrue("Article id should be positive", id > 0); } @Test public void article_hasGuid() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have guid", article.has("guid")); assertFalse("Article guid should not be empty", article.get("guid").getAsString().isEmpty()); } @Test public void article_hasTitle() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have title", article.has("title")); assertFalse("Article title should not be empty", article.get("title").getAsString().isEmpty()); } @Test public void article_hasLink() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have link", article.has("link")); assertFalse("Article link should not be empty", article.get("link").getAsString().isEmpty()); } @Test public void article_hasFeedId() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have feed_id", article.has("feed_id")); assertTrue("Article feed_id should be positive", article.get("feed_id").getAsInt() > 0); } @Test public void article_hasUpdated() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have updated", article.has("updated")); assertTrue("Article updated should be non-negative", article.get("updated").getAsInt() >= 0); } // ── Boolean status fields ─────────────────────────────────────────────── @Test public void article_hasUnreadBoolean() throws IOException { JsonObject article = getFirstArticle(); assertTrue("unread must be a primitive", article.get("unread").isJsonPrimitive()); // Just verify it's a valid boolean value article.get("unread").getAsBoolean(); } @Test public void article_hasMarkedBoolean() throws IOException { JsonObject article = getFirstArticle(); assertTrue("marked must be a primitive", article.get("marked").isJsonPrimitive()); article.get("marked").getAsBoolean(); } @Test public void article_hasPublishedBoolean() throws IOException { JsonObject article = getFirstArticle(); assertTrue("published must be a primitive", article.get("published").isJsonPrimitive()); article.get("published").getAsBoolean(); } // ── Content and metadata ──────────────────────────────────────────────── @Test public void article_hasContent() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have content", article.has("content")); String content = article.get("content").getAsString(); assertFalse("Article content should not be empty", content.isEmpty()); } @Test public void article_hasAuthor() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have author", article.has("author")); // Author can be empty string but field must exist } @Test public void article_hasScore() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have score", article.has("score")); // Score should be an integer article.get("score").getAsInt(); } @Test public void article_hasFeedTitle() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have feed_title", article.has("feed_title")); assertFalse("Article feed_title should not be empty", article.get("feed_title").getAsString().isEmpty()); } // ── Optional fields ───────────────────────────────────────────────────── @Test public void article_hasNote() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have note", article.has("note")); // Note can be null (as in this fixture) } @Test public void article_hasLang() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have lang", article.has("lang")); } @Test public void article_hasComments() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have comments", article.has("comments")); } @Test public void article_hasCommentsField() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have comments", article.has("comments")); } // ── Arrays ────────────────────────────────────────────────────────────── @Test public void article_hasLabelsArray() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have labels", article.has("labels")); assertTrue("labels should be an array", article.get("labels").isJsonArray()); } @Test public void article_hasAttachmentsArray() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have attachments", article.has("attachments")); assertTrue("attachments should be an array", article.get("attachments").isJsonArray()); } @Test public void article_hasEnclosuresArray() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have enclosures", article.has("enclosures")); assertTrue("enclosures should be an array", article.get("enclosures").isJsonArray()); } // ── Flavor data ───────────────────────────────────────────────────────── @Test public void article_hasFlavorImage() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have flavor_image", article.has("flavor_image")); } @Test public void article_hasFlavorStream() throws IOException { JsonObject article = getFirstArticle(); assertTrue("Article should have flavor_stream", article.has("flavor_stream")); } // ── Helper ────────────────────────────────────────────────────────────── private JsonObject getFirstArticle() throws IOException { JsonObject response = JsonParser.parseString(loadFixture("responses/article_detail.json")).getAsJsonObject(); JsonArray articles = response.getAsJsonArray("data"); assertTrue("Article data should not be empty", articles.size() > 0); return articles.get(0).getAsJsonObject(); } }