/** * Constructs a Client ID from JSON file stored as a resource. * * @param relativeClass A class in the same namespace as the resource. * @param resourceName The name of the resource * @return The constructed ClientID instance based on the JSON in the resource. * @throws IOException The JSON could not be loaded or parsed. */ public static ClientId fromResource(Class<?> relativeClass, String resourceName) throws IOException { InputStream stream = relativeClass.getResourceAsStream(resourceName); return fromStream(stream); }
/** * Constructs a Client ID from JSON file stored as a resource. * * @param relativeClass A class in the same namespace as the resource. * @param resourceName The name of the resource * @return The constructed ClientID instance based on the JSON in the resource. * @throws IOException The JSON could not be loaded or parsed. */ public static ClientId fromResource(Class<?> relativeClass, String resourceName) throws IOException { InputStream stream = relativeClass.getResourceAsStream(resourceName); return fromStream(stream); }
@Test public void fromStream_invalidJson_throws() { String invalidJson = "{" + "\"web\": {" + "\"client_id\" : \"" + CLIENT_ID + "\"," + "\"client_secret\" : \"" + CLIENT_SECRET + "\"" + "}"; // No closing brace InputStream stream = TestUtils.stringToInputStream(invalidJson); try { ClientId.fromStream(stream); fail(); } catch (IOException expected) { // Expected } }
@Test public void fromStream() throws IOException { String text = "{" + "\"web\": {" + "\"client_id\" : \"" + CLIENT_ID + "\"," + "\"client_secret\" : \"" + CLIENT_SECRET + "\"" + "}" + "}"; InputStream stream = TestUtils.stringToInputStream(text); ClientId clientId = ClientId.fromStream(stream); assertEquals(CLIENT_ID, clientId.getClientId()); assertEquals(CLIENT_SECRET, clientId.getClientSecret()); }