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()); } }