diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-06-06 12:51:28 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-06-06 12:51:28 +0300 |
| commit | e811d8cd71b4ea5b54583a0157077944fcc27ea9 (patch) | |
| tree | b419bba7e9c46da0e9d170b7b394254122fbd3c8 /org.fox.ttrss | |
| parent | 0e97aedda5e05a1f056186f6d02010fce7f8d6e0 (diff) | |
| download | tt-rss-android-e811d8cd71b4ea5b54583a0157077944fcc27ea9.tar.gz tt-rss-android-e811d8cd71b4ea5b54583a0157077944fcc27ea9.tar.xz | |
add some more unit tests
Diffstat (limited to 'org.fox.ttrss')
3 files changed, 509 insertions, 0 deletions
diff --git a/org.fox.ttrss/src/test/java/org/fox/ttrss/ArticleDetailResponseTest.java b/org.fox.ttrss/src/test/java/org/fox/ttrss/ArticleDetailResponseTest.java new file mode 100644 index 00000000..49c7aa86 --- /dev/null +++ b/org.fox.ttrss/src/test/java/org/fox/ttrss/ArticleDetailResponseTest.java @@ -0,0 +1,225 @@ +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(); + } +} diff --git a/org.fox.ttrss/src/test/java/org/fox/ttrss/FeedsResponseTest.java b/org.fox.ttrss/src/test/java/org/fox/ttrss/FeedsResponseTest.java new file mode 100644 index 00000000..abc85e55 --- /dev/null +++ b/org.fox.ttrss/src/test/java/org/fox/ttrss/FeedsResponseTest.java @@ -0,0 +1,117 @@ +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()); + } + } +} diff --git a/org.fox.ttrss/src/test/java/org/fox/ttrss/HeadlinesResponseTest.java b/org.fox.ttrss/src/test/java/org/fox/ttrss/HeadlinesResponseTest.java new file mode 100644 index 00000000..e00305fd --- /dev/null +++ b/org.fox.ttrss/src/test/java/org/fox/ttrss/HeadlinesResponseTest.java @@ -0,0 +1,167 @@ +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()); + } + } +} |
