about summary refs log tree commit diff stats
path: root/org.fox.ttrss/src/test/java/org/fox/ttrss/HeadlinesResponseTest.java
blob: e00305fd46c79fea8d429bf2e808c7a30e9ab646 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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());
        }
    }
}