diff options
| -rwxr-xr-x | org.fox.ttrss/build.gradle | 3 | ||||
| -rw-r--r-- | org.fox.ttrss/src/test/java/org/fox/ttrss/LoginResponseTest.java | 130 |
2 files changed, 133 insertions, 0 deletions
diff --git a/org.fox.ttrss/build.gradle b/org.fox.ttrss/build.gradle index 9aefb525..de549142 100755 --- a/org.fox.ttrss/build.gradle +++ b/org.fox.ttrss/build.gradle @@ -138,6 +138,9 @@ def getVersion() { } dependencies { + testImplementation 'junit:junit:4.13.2' + testImplementation 'org.mockito:mockito-core:5.14.0' + implementation 'com.squareup.okhttp3:okhttp:3.12.5' implementation 'org.jsoup:jsoup:1.11.3' implementation 'com.bogdwellers:pinchtozoom:0.1' diff --git a/org.fox.ttrss/src/test/java/org/fox/ttrss/LoginResponseTest.java b/org.fox.ttrss/src/test/java/org/fox/ttrss/LoginResponseTest.java new file mode 100644 index 00000000..b06d692d --- /dev/null +++ b/org.fox.ttrss/src/test/java/org/fox/ttrss/LoginResponseTest.java @@ -0,0 +1,130 @@ +package org.fox.ttrss; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +import org.junit.Before; +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 login responses. + * Uses fixture from src/test/resources/responses/login.json + */ +public class LoginResponseTest { + + private Gson gson; + + @Before + public void setUp() { + gson = new Gson(); + } + + 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 loginResponse_hasSuccessTrue() throws IOException { + String json = loadFixture("responses/login.json"); + JsonObject response = JsonParser.parseString(json).getAsJsonObject(); + + assertTrue("Login response should have success=true", + response.get("success").getAsBoolean()); + } + + @Test + public void loginResponse_hasDataObject() throws IOException { + String json = loadFixture("responses/login.json"); + JsonObject response = JsonParser.parseString(json).getAsJsonObject(); + + assertNotNull("Login response should have data", response.has("data")); + assertTrue("Login data should be an object", response.get("data").isJsonObject()); + } + + @Test + public void loginResponse_hasSessionId() throws IOException { + String json = loadFixture("responses/login.json"); + JsonObject response = JsonParser.parseString(json).getAsJsonObject(); + JsonObject data = response.getAsJsonObject("data"); + + assertTrue("Login data should have session_id", data.has("session_id")); + String sessionId = data.get("session_id").getAsString(); + assertFalse("Session ID should not be empty", sessionId.isEmpty()); + } + + @Test + public void loginResponse_hasApiLevel() throws IOException { + String json = loadFixture("responses/login.json"); + JsonObject response = JsonParser.parseString(json).getAsJsonObject(); + JsonObject data = response.getAsJsonObject("data"); + + assertTrue("Login data should have api_level", data.has("api_level")); + int apiLevel = data.get("api_level").getAsInt(); + assertTrue("API level should be positive", apiLevel > 0); + } + + @Test + public void loginResponse_hasConfig() throws IOException { + String json = loadFixture("responses/login.json"); + JsonObject response = JsonParser.parseString(json).getAsJsonObject(); + JsonObject data = response.getAsJsonObject("data"); + JsonObject config = data.getAsJsonObject("config"); + + assertNotNull("Login data should have config", config); + assertTrue("Config should have icons_url", config.has("icons_url")); + assertTrue("Config should have num_feeds", config.has("num_feeds")); + } + + @Test + public void loginResponse_errorIsNullOnSuccess() throws IOException { + String json = loadFixture("responses/login.json"); + JsonObject response = JsonParser.parseString(json).getAsJsonObject(); + + // error may be present but must be null on success + JsonElement error = response.get("error"); + if (error != null) { + assertTrue("Login response error should be null on success", + error.isJsonNull()); + } + } + + @Test + public void loginError_response_hasSuccessFalse() throws IOException { + String json = loadFixture("responses/login_error.json"); + JsonObject response = JsonParser.parseString(json).getAsJsonObject(); + + assertFalse("Failed login should have success=false", + response.get("success").getAsBoolean()); + } + + @Test + public void loginError_response_hasErrorObject() throws IOException { + String json = loadFixture("responses/login_error.json"); + JsonObject response = JsonParser.parseString(json).getAsJsonObject(); + + assertTrue("Failed login should have error", response.has("error")); + JsonObject error = response.getAsJsonObject("error"); + assertEquals("Error code should be LOGIN_ERROR", + "LOGIN_ERROR", error.get("code").getAsString()); + } +} |
