Java Code Examples for com.google.api.client.json.JsonFactory#fromString()

The following examples show how to use com.google.api.client.json.JsonFactory#fromString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FirebaseUserManagerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testImportUsers() throws Exception {
  TestResponseInterceptor interceptor = initializeAppForUserManagement("{}");
  ImportUserRecord user1 = ImportUserRecord.builder().setUid("user1").build();
  ImportUserRecord user2 = ImportUserRecord.builder().setUid("user2").build();

  List<ImportUserRecord> users = ImmutableList.of(user1, user2);
  UserImportResult result = FirebaseAuth.getInstance().importUsersAsync(users, null).get();
  checkRequestHeaders(interceptor);
  assertEquals(2, result.getSuccessCount());
  assertEquals(0, result.getFailureCount());
  assertTrue(result.getErrors().isEmpty());

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  interceptor.getResponse().getRequest().getContent().writeTo(out);
  JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
  GenericJson parsed = jsonFactory.fromString(new String(out.toByteArray()), GenericJson.class);
  assertEquals(1, parsed.size());
  List<Map<String, Object>> expected = ImmutableList.of(
      user1.getProperties(jsonFactory),
      user2.getProperties(jsonFactory)
  );
  assertEquals(expected, parsed.get("users"));
}
 
Example 2
Source File: FirebaseUserManagerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateSessionCookie() throws Exception {
  TestResponseInterceptor interceptor = initializeAppForUserManagement(
      TestUtils.loadResource("createSessionCookie.json"));
  SessionCookieOptions options = SessionCookieOptions.builder()
      .setExpiresIn(TimeUnit.HOURS.toMillis(1))
      .build();
  String cookie = FirebaseAuth.getInstance().createSessionCookieAsync("testToken", options).get();
  assertEquals("MockCookieString", cookie);
  checkRequestHeaders(interceptor);

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  interceptor.getResponse().getRequest().getContent().writeTo(out);
  JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
  GenericJson parsed = jsonFactory.fromString(new String(out.toByteArray()), GenericJson.class);
  assertEquals(2, parsed.size());
  assertEquals("testToken", parsed.get("idToken"));
  assertEquals(new BigDecimal(3600), parsed.get("validDuration"));
}
 
Example 3
Source File: FirebaseUserManagerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGeneratePasswordResetLinkWithSettings() throws Exception {
  TestResponseInterceptor interceptor = initializeAppForUserManagement(
          TestUtils.loadResource("generateEmailLink.json"));
  String link = FirebaseAuth.getInstance()
          .generatePasswordResetLinkAsync("[email protected]", ACTION_CODE_SETTINGS).get();
  assertEquals("https://mock-oob-link.for.auth.tests", link);
  checkRequestHeaders(interceptor);

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  interceptor.getResponse().getRequest().getContent().writeTo(out);
  JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
  GenericJson parsed = jsonFactory.fromString(new String(out.toByteArray()), GenericJson.class);
  assertEquals(3 + ACTION_CODE_SETTINGS_MAP.size(), parsed.size());
  assertEquals("[email protected]", parsed.get("email"));
  assertEquals("PASSWORD_RESET", parsed.get("requestType"));
  assertTrue((Boolean) parsed.get("returnOobLink"));
  for (Map.Entry<String, Object> entry : ACTION_CODE_SETTINGS_MAP.entrySet()) {
    assertEquals(entry.getValue(), parsed.get(entry.getKey()));
  }
}
 
Example 4
Source File: FirebaseUserManagerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGeneratePasswordResetLink() throws Exception {
  TestResponseInterceptor interceptor = initializeAppForUserManagement(
          TestUtils.loadResource("generateEmailLink.json"));
  String link = FirebaseAuth.getInstance()
          .generatePasswordResetLinkAsync("[email protected]").get();
  assertEquals("https://mock-oob-link.for.auth.tests", link);
  checkRequestHeaders(interceptor);

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  interceptor.getResponse().getRequest().getContent().writeTo(out);
  JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
  GenericJson parsed = jsonFactory.fromString(new String(out.toByteArray()), GenericJson.class);
  assertEquals(3, parsed.size());
  assertEquals("[email protected]", parsed.get("email"));
  assertEquals("PASSWORD_RESET", parsed.get("requestType"));
  assertTrue((Boolean) parsed.get("returnOobLink"));
}
 
Example 5
Source File: FirebaseUserManagerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateSignInWithEmailLinkWithSettings() throws Exception {
  TestResponseInterceptor interceptor = initializeAppForUserManagement(
      TestUtils.loadResource("generateEmailLink.json"));
  String link = FirebaseAuth.getInstance()
      .generateSignInWithEmailLinkAsync("[email protected]", ACTION_CODE_SETTINGS).get();
  assertEquals("https://mock-oob-link.for.auth.tests", link);
  checkRequestHeaders(interceptor);

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  interceptor.getResponse().getRequest().getContent().writeTo(out);
  JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
  GenericJson parsed = jsonFactory.fromString(new String(out.toByteArray()), GenericJson.class);
  assertEquals(3 + ACTION_CODE_SETTINGS_MAP.size(), parsed.size());
  assertEquals("[email protected]", parsed.get("email"));
  assertEquals("EMAIL_SIGNIN", parsed.get("requestType"));
  assertTrue((Boolean) parsed.get("returnOobLink"));
  for (Map.Entry<String, Object> entry : ACTION_CODE_SETTINGS_MAP.entrySet()) {
    assertEquals(entry.getValue(), parsed.get(entry.getKey()));
  }
}
 
Example 6
Source File: FirebaseUserManagerTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateEmailVerificationLink() throws Exception {
  TestResponseInterceptor interceptor = initializeAppForUserManagement(
      TestUtils.loadResource("generateEmailLink.json"));
  String link = FirebaseAuth.getInstance()
      .generateEmailVerificationLinkAsync("[email protected]").get();
  assertEquals("https://mock-oob-link.for.auth.tests", link);
  checkRequestHeaders(interceptor);

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  interceptor.getResponse().getRequest().getContent().writeTo(out);
  JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
  GenericJson parsed = jsonFactory.fromString(new String(out.toByteArray()), GenericJson.class);
  assertEquals(3, parsed.size());
  assertEquals("[email protected]", parsed.get("email"));
  assertEquals("VERIFY_EMAIL", parsed.get("requestType"));
  assertTrue((Boolean) parsed.get("returnOobLink"));
}
 
Example 7
Source File: TokenErrorResponseTest.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
public void test() throws Exception {
  JsonFactory jsonFactory = new JacksonFactory();
  TokenErrorResponse response = jsonFactory.fromString(JSON, TokenErrorResponse.class);
  assertEquals("invalid_request", response.getError());
  assertEquals("http://www.example.com/error", response.getErrorUri());
  assertEquals("error description", response.getErrorDescription());
}
 
Example 8
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void subtestParse_void(String value) throws Exception {
  JsonFactory factory = newFactory();
  String inputString = "{" + (value == null ? "" : "\"v\":" + value + ",") + "\"s\":\"svalue\"}";
  V v = factory.fromString(inputString, V.class);
  assertNull(v.v);
  assertEquals("svalue", v.s);
  assertNull(factory.fromString(inputString, Void.class));
}
 
Example 9
Source File: TokenResponseTest.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
public void test() throws Exception {
  JsonFactory jsonFactory = new JacksonFactory();
  TokenResponse response = jsonFactory.fromString(JSON, TokenResponse.class);
  assertEquals("2YotnFZFEjr1zCsicMWpAA", response.getAccessToken());
  assertEquals("example", response.getTokenType());
  assertEquals(3600L, response.getExpiresInSeconds().longValue());
  assertEquals("tGzv3JOkF0XG5Qx2TlKWIA", response.getRefreshToken());
  assertEquals("example_value", response.get("example_parameter"));
}
 
Example 10
Source File: TokenResponseTest.java    From oic-auth-plugin with MIT License 5 votes vote down vote up
@Test
public void parseAbsent() throws IOException {
    JsonFactory jsonFactory = new JacksonFactory();
    TokenResponse response = jsonFactory.fromString(JSON_WITH_ABSENT, TokenResponse.class);
    assertEquals("2YotnFZFEjr1zCsicMWpAA", response.getAccessToken());
    assertEquals("example", response.getTokenType());
    assertEquals(null, response.getExpiresInSeconds());
    assertEquals("tGzv3JOkF0XG5Qx2TlKWIA", response.getRefreshToken());
    assertEquals("example_value", response.get("example_parameter"));
}
 
Example 11
Source File: TokenResponseTest.java    From oic-auth-plugin with MIT License 5 votes vote down vote up
@Test
public void parseStringWithLong() throws IOException {
    JsonFactory jsonFactory = new JacksonFactory();
    TokenResponse response = jsonFactory.fromString(JSON_WITH_LONG_AS_STRING, TokenResponse.class);
    assertEquals("2YotnFZFEjr1zCsicMWpAA", response.getAccessToken());
    assertEquals("example", response.getTokenType());
    assertEquals(3600L, response.getExpiresInSeconds().longValue());
    assertEquals("tGzv3JOkF0XG5Qx2TlKWIA", response.getRefreshToken());
    assertEquals("example_value", response.get("example_parameter"));
}
 
Example 12
Source File: TokenResponseTest.java    From oic-auth-plugin with MIT License 5 votes vote down vote up
@Test
public void parseLongLiteral() throws IOException {
    JsonFactory jsonFactory = new JacksonFactory();
    TokenResponse response = jsonFactory.fromString(JSON_WITH_LONG_LITERAL, TokenResponse.class);
    assertEquals("2YotnFZFEjr1zCsicMWpAA", response.getAccessToken());
    assertEquals("example", response.getTokenType());
    assertEquals(3600L, response.getExpiresInSeconds().longValue());
    assertEquals("tGzv3JOkF0XG5Qx2TlKWIA", response.getRefreshToken());
    assertEquals("example_value", response.get("example_parameter"));
}
 
Example 13
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testParse_boolean() throws Exception {
  JsonFactory factory = newFactory();
  BooleanTypes parsed;
  // empty
  parsed = factory.fromString(BOOLEAN_TYPE_EMPTY, BooleanTypes.class);
  assertFalse(parsed.bool);
  assertNull(parsed.boolObj);
  assertEquals(BOOLEAN_TYPE_EMPTY_OUTPUT, factory.toString(parsed));
  // true
  parsed = factory.fromString(BOOLEAN_TYPE_TRUE, BooleanTypes.class);
  assertTrue(parsed.bool);
  assertTrue(parsed.boolObj.booleanValue() && !Data.isNull(parsed.boolObj));
  assertEquals(BOOLEAN_TYPE_TRUE, factory.toString(parsed));
  // false
  parsed = factory.fromString(BOOLEAN_TYPE_FALSE, BooleanTypes.class);
  assertFalse(parsed.bool);
  assertTrue(!parsed.boolObj.booleanValue() && !Data.isNull(parsed.boolObj));
  assertEquals(BOOLEAN_TYPE_FALSE, factory.toString(parsed));
  // null
  parsed = factory.fromString(BOOLEAN_TYPE_NULL, BooleanTypes.class);
  assertFalse(parsed.bool);
  assertTrue(Data.isNull(parsed.boolObj));
  assertEquals(BOOLEAN_TYPE_NULL_OUTPUT, factory.toString(parsed));
  // wrong
  try {
    factory.fromString(BOOLEAN_TYPE_WRONG, BooleanTypes.class);
    fail("expected " + IllegalArgumentException.class);
  } catch (IllegalArgumentException e) {
  }
}
 
Example 14
Source File: FirebaseUserManagerTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testImportUsersWithHash() throws Exception {
  TestResponseInterceptor interceptor = initializeAppForUserManagement("{}");
  ImportUserRecord user1 = ImportUserRecord.builder()
      .setUid("user1")
      .build();
  ImportUserRecord user2 = ImportUserRecord.builder()
      .setUid("user2")
      .setPasswordHash("password".getBytes())
      .build();

  List<ImportUserRecord> users = ImmutableList.of(user1, user2);
  UserImportHash hash = new UserImportHash("MOCK_HASH") {
    @Override
    protected Map<String, Object> getOptions() {
      return ImmutableMap.<String, Object>of("key1", "value1", "key2", true);
    }
  };
  UserImportResult result = FirebaseAuth.getInstance().importUsersAsync(users,
      UserImportOptions.withHash(hash)).get();
  checkRequestHeaders(interceptor);
  assertEquals(2, result.getSuccessCount());
  assertEquals(0, result.getFailureCount());
  assertTrue(result.getErrors().isEmpty());

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  interceptor.getResponse().getRequest().getContent().writeTo(out);
  JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
  GenericJson parsed = jsonFactory.fromString(new String(out.toByteArray()), GenericJson.class);
  assertEquals(4, parsed.size());
  List<Map<String, Object>> expected = ImmutableList.of(
      user1.getProperties(jsonFactory),
      user2.getProperties(jsonFactory)
  );
  assertEquals(expected, parsed.get("users"));
  assertEquals("MOCK_HASH", parsed.get("hashAlgorithm"));
  assertEquals("value1", parsed.get("key1"));
  assertEquals(Boolean.TRUE, parsed.get("key2"));
}
 
Example 15
Source File: FirebaseUserManagerTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testImportUsersError() throws Exception {
  TestResponseInterceptor interceptor = initializeAppForUserManagement(
      TestUtils.loadResource("importUsersError.json"));
  ImportUserRecord user1 = ImportUserRecord.builder()
      .setUid("user1")
      .build();
  ImportUserRecord user2 = ImportUserRecord.builder()
      .setUid("user2")
      .build();
  ImportUserRecord user3 = ImportUserRecord.builder()
      .setUid("user3")
      .build();

  List<ImportUserRecord> users = ImmutableList.of(user1, user2, user3);
  UserImportResult result = FirebaseAuth.getInstance().importUsersAsync(users, null).get();
  checkRequestHeaders(interceptor);
  assertEquals(1, result.getSuccessCount());
  assertEquals(2, result.getFailureCount());
  assertEquals(2, result.getErrors().size());

  ErrorInfo error = result.getErrors().get(0);
  assertEquals(0, error.getIndex());
  assertEquals("Some error occurred in user1", error.getReason());
  error = result.getErrors().get(1);
  assertEquals(2, error.getIndex());
  assertEquals("Another error occurred in user3", error.getReason());

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  interceptor.getResponse().getRequest().getContent().writeTo(out);
  JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
  GenericJson parsed = jsonFactory.fromString(new String(out.toByteArray()), GenericJson.class);
  assertEquals(1, parsed.size());
  List<Map<String, Object>> expected = ImmutableList.of(
      user1.getProperties(jsonFactory),
      user2.getProperties(jsonFactory),
      user3.getProperties(jsonFactory)
  );
  assertEquals(expected, parsed.get("users"));
}
 
Example 16
Source File: FirebaseUserManagerTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testRevokeRefreshTokens() throws Exception {
  TestResponseInterceptor interceptor = initializeAppForUserManagement(
      TestUtils.loadResource("createUser.json"));
  // should not throw
  FirebaseAuth.getInstance().revokeRefreshTokensAsync("testuser").get();
  checkRequestHeaders(interceptor);

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  interceptor.getResponse().getRequest().getContent().writeTo(out);
  JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
  GenericJson parsed = jsonFactory.fromString(new String(out.toByteArray()), GenericJson.class);
  assertEquals("testuser", parsed.get("localId"));
  assertNotNull(parsed.get("validSince"));
}
 
Example 17
Source File: CustomTokenResponseTest.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
public void testStringExpires() throws Exception {
  JsonFactory jsonFactory = new JacksonFactory();
  TokenResponse response = jsonFactory.fromString(JSON, StringExpiresTokenResponse.class);
  assertEquals("2YotnFZFEjr1zCsicMWpAA", response.getAccessToken());
  assertEquals("example", response.getTokenType());
  assertEquals(3600L, response.getExpiresInSeconds().longValue());
  assertEquals("tGzv3JOkF0XG5Qx2TlKWIA", response.getRefreshToken());
  assertEquals("example_value", response.get("example_parameter"));
}
 
Example 18
Source File: ListUsersPageTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
private static ExportedUserRecord newUser(String uid, String passwordHash) throws IOException {
  JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
  DownloadAccountResponse.User parsed = jsonFactory.fromString(
      String.format("{\"localId\":\"%s\", \"passwordHash\":\"%s\"}", uid, passwordHash),
      DownloadAccountResponse.User.class);
  return new ExportedUserRecord(parsed, jsonFactory);
}
 
Example 19
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testToFromString() throws Exception {
  JsonFactory factory = newFactory();
  NumberTypes result = factory.fromString(NUMBER_TYPES, NumberTypes.class);
  assertEquals(NUMBER_TYPES, factory.toString(result));
}
 
Example 20
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void testToFromString_UTF8() throws Exception {
  JsonFactory factory = newFactory();
  GenericJson result = factory.fromString(UTF8_JSON, GenericJson.class);
  assertEquals(UTF8_VALUE, result.get("value"));
  assertEquals(UTF8_JSON, factory.toString(result));
}