/** * Constructs a Client ID from JSON file stream. * * @param stream Stream of the downloaded JSON file. * @return The constructed ClientID instance based on the JSON in the stream. * @throws IOException The JSON could not be read or parsed. */ public static ClientId fromStream(InputStream stream) throws IOException { Preconditions.checkNotNull(stream); JsonObjectParser parser = new JsonObjectParser(OAuth2Utils.JSON_FACTORY); GenericJson parsedJson = parser.parseAndClose( stream, StandardCharsets.UTF_8, GenericJson.class); return fromJson(parsedJson); }
/** * Constructs a Client ID from JSON file stream. * * @param stream Stream of the downloaded JSON file. * @return The constructed ClientID instance based on the JSON in the stream. * @throws IOException The JSON could not be read or parsed. */ public static ClientId fromStream(InputStream stream) throws IOException { Preconditions.checkNotNull(stream); JsonObjectParser parser = new JsonObjectParser(OAuth2Utils.JSON_FACTORY); GenericJson parsedJson = parser.parseAndClose( stream, StandardCharsets.UTF_8, GenericJson.class); return fromJson(parsedJson); }
@Test(expected = IOException.class) public void fromJson_zeroLengthClientId_throws() throws IOException { GenericJson json = writeClientIdJson("web", "", null); ClientId.fromJson(json); }
@Test(expected = IOException.class) public void fromJson_invalidType_throws() throws IOException { GenericJson json = writeClientIdJson("invalid", CLIENT_ID, null); ClientId.fromJson(json); }
@Test(expected = IOException.class) public void fromJson_noClientId_throws() throws IOException { GenericJson json = writeClientIdJson("web", null, null); ClientId.fromJson(json); }
@Test public void fromJson_installedNoSecret() throws IOException { GenericJson json = writeClientIdJson("installed", CLIENT_ID, null); ClientId clientId = ClientId.fromJson(json); assertEquals(CLIENT_ID, clientId.getClientId()); assertNull(clientId.getClientSecret()); }
@Test public void fromJson_web() throws IOException { GenericJson json = writeClientIdJson("web", CLIENT_ID, CLIENT_SECRET); ClientId clientId = ClientId.fromJson(json); assertEquals(CLIENT_ID, clientId.getClientId()); assertEquals(CLIENT_SECRET, clientId.getClientSecret()); }
@Test public void fromJson_installed() throws IOException { GenericJson json = writeClientIdJson("installed", CLIENT_ID, CLIENT_SECRET); ClientId clientId = ClientId.fromJson(json); assertEquals(CLIENT_ID, clientId.getClientId()); assertEquals(CLIENT_SECRET, clientId.getClientSecret()); }