Repository: firebase/firebase-admin-java
Pull request #361 (2020-02-07 19:17:47)

Add updateRequest method to Tenant class and add unit tests.
This pull request adds some things to the Tenant class and adds a few unit tests. This is part of the initiative to adding multi-tenancy support (see issue #332).

It's worth noting that these changes deviate from the API design doc. I realized that the updateTenant method in my design doc doesn’t match the design of the existing updateUser method. To fix this, I'm adding the tenantId to Tenant.UpdateRequest.

This PR also removes the usage of AutoValue since using it was going to deviate too much from the design of the request classes in UserRecord. This change will also make it easier to do JSON serialization when we sending the requests.


Patch:
diff --git a/src/main/java/com/google/firebase/auth/Tenant.java b/src/main/java/com/google/firebase/auth/Tenant.java
index f64d93a..366d9e9 100644
--- a/src/main/java/com/google/firebase/auth/Tenant.java
+++ b/src/main/java/com/google/firebase/auth/Tenant.java
@@ -16,8 +16,13 @@
 
 package com.google.firebase.auth;
 
+import static com.google.common.base.Preconditions.checkArgument;
+
 import com.google.api.client.util.Key;
-import com.google.auto.value.AutoValue;
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableMap;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * Contains metadata associated with a Firebase tenant.
@@ -26,7 +31,7 @@ import com.google.auto.value.AutoValue;
  */
 public final class Tenant {
 
-  @Key("tenantId")
+  @Key("name")
   private String tenantId;
 
   @Key("displayName")
@@ -55,100 +60,136 @@ public final class Tenant {
   }
 
   /**
-   * Class used to hold the information needs to make a tenant create request.
+   * Returns a new {@link UpdateRequest}, which can be used to update the attributes
+   * of this tenant.
+   *
+   * @return a non-null Tenant.UpdateRequest instance.
    */
-  @AutoValue
-  public abstract static class CreateRequest {
+  public UpdateRequest updateRequest() {
+    return new UpdateRequest(getTenantId());
+  }
+
+  /**
+   * A specification class for creating new user accounts.
+   *
+   * <p>Set the initial attributes of the new tenant by calling various setter methods available in
+   * this class. None of the attributes are required.
+   */
+  public static final class CreateRequest {
+
+    private final Map<String,Object> properties = new HashMap<>();
 
     /**
-     * Returns the display name of this tenant.
+     * Creates a new {@link CreateRequest}, which can be used to create a new tenant.
      *
-     * @return a non-empty display name string.
+     * <p>The returned object should be passed to {@link TenantManager#createTenant(CreateRequest)}
+     * to register the tenant information persistently.
      */
-    public abstract String getDisplayName();
+    public CreateRequest() { }
 
     /**
-     * Returns whether to allow email/password user authentication.
+     * Sets the display name for the new tenant.
      *
-     * @return true if a user can be authenticated using an email and password, and false otherwise.
+     * @param displayName a non-null, non-empty display name string.
      */
-    public abstract boolean isPasswordSignInAllowed();
+    public CreateRequest setDisplayName(String displayName) {
+      checkArgument(!Strings.isNullOrEmpty(displayName), "display name must not be null or empty");
+      properties.put("displayName", displayName);
+      return this;
+    }
 
     /**
-     * Returns whether to enable email link user authentication.
+     * Sets whether to allow email/password user authentication.
      *
-     * @return true if a user can be authenticated using an email link, and false otherwise.
+     * @param passwordSignInAllowed a boolean indicating whether users can be authenticated using
+     *     an email and password, and false otherwise.
      */
-    public abstract boolean isEmailLinkSignInEnabled();
+    public CreateRequest setPasswordSignInAllowed(boolean passwordSignInAllowed) {
+      properties.put("allowPasswordSignup", passwordSignInAllowed);
+      return this;
+    }
 
     /**
-     * Returns a builder for a tenant create request.
+     * Sets whether to  enable email link user authentication.
+     *
+     * @param emailLinkSignInEnabled a boolean indicating whether users can be authenticated using
+     *     an email link, and false otherwise.
      */
-    public static Builder newBuilder() {
-      return new AutoValue_Tenant_CreateRequest.Builder();
+    public CreateRequest setEmailLinkSignInEnabled(boolean emailLinkSignInEnabled) {
+      properties.put("enableEmailLinkSignin", emailLinkSignInEnabled);
+      return this;
     }
 
-    /**
-    * Builder class used to construct a create request.
-    */
-    @AutoValue.Builder
-    abstract static class Builder {
-      public abstract Builder setDisplayName(String displayName);
-
-      public abstract Builder setPasswordSignInAllowed(boolean allowPasswordSignIn);
-
-      public abstract Builder setEmailLinkSignInEnabled(boolean enableEmailLinkSignIn);
-
-      public abstract CreateRequest build();
+    Map<String, Object> getProperties() {
+      return ImmutableMap.copyOf(properties);
     }
   }
 
   /**
-   * Class used to hold the information needs to make a tenant update request. 
+   * A class for updating the attributes of an existing tenant.
+   *
+   * <p>An instance of this class can be obtained via a {@link Tenant} object, or from a tenant ID
+   * string. Specify the changes to be made to the tenant by calling the various setter methods
+   * available in this class.
    */
-  @AutoValue
-  public abstract static class UpdateRequest {
+  public static final class UpdateRequest {
+
+    private final Map<String,Object> properties = new HashMap<>();
 
     /**
-     * Returns the display name of this tenant.
+     * Creates a new {@link UpdateRequest}, which can be used to update the attributes of the
+     * of the tenant identified by the specified tenant ID.
+     *
+     * <p>This method allows updating attributes of a tenant account, without first having to call
+     * {@link TenantManager#getTenant(String)}.
      *
-     * @return a non-empty display name string.
+     * @param tenantId a non-null, non-empty tenant ID string.
+     * @throws IllegalArgumentException If the tenant ID is null or empty.
      */
-    public abstract String getDisplayName();
+    public UpdateRequest(String tenantId) {
+      checkArgument(!Strings.isNullOrEmpty(tenantId), "tenant ID must not be null or empty");
+      properties.put("name", tenantId);
+    }
+
+    String getTenantId() {
+      return (String) properties.get("name");
+    }
 
     /**
-     * Returns whether to allow email/password user authentication.
+     * Sets the display name of the existingtenant.
      *
-     * @return true if a user can be authenticated using an email and password, and false otherwise.
+     * @param displayName a non-null, non-empty display name string.
      */
-    public abstract boolean isPasswordSignInAllowed();
+    public UpdateRequest setDisplayName(String displayName) {
+      checkArgument(!Strings.isNullOrEmpty(displayName), "display name must not be null or empty");
+      properties.put("displayName", displayName);
+      return this;
+    }
 
     /**
-     * Returns whether to enable email link user authentication.
+     * Sets whether to allow email/password user authentication.
      *
-     * @return true if a user can be authenticated using an email link, and false otherwise.
+     * @param passwordSignInAllowed a boolean indicating whether users can be authenticated using
+     *     an email and password, and false otherwise.
      */
-    public abstract boolean isEmailLinkSignInEnabled();
+    public UpdateRequest setPasswordSignInAllowed(boolean passwordSignInAllowed) {
+      properties.put("allowPasswordSignup", passwordSignInAllowed);
+      return this;
+    }
 
     /**
-     * Returns a builder for a tenant update request.
+     * Sets whether to enable email link user authentication.
+     *
+     * @param emailLinkSignInEnabled a boolean indicating whether users can be authenticated using
+     *     an email link, and false otherwise.
      */
-    public static Builder newBuilder() {
-      return new AutoValue_Tenant_UpdateRequest.Builder();
+    public UpdateRequest setEmailLinkSignInEnabled(boolean emailLinkSignInEnabled) {
+      properties.put("enableEmailLinkSignin", emailLinkSignInEnabled);
+      return this;
     }
 
-    /**
-    * Builder class used to construct a update request.
-    */
-    @AutoValue.Builder
-    abstract static class Builder {
-      public abstract Builder setDisplayName(String displayName);
-
-      public abstract Builder setPasswordSignInAllowed(boolean allowPasswordSignIn);
-
-      public abstract Builder setEmailLinkSignInEnabled(boolean enableEmailLinkSignIn);
-
-      public abstract UpdateRequest build();
+    Map<String, Object> getProperties() {
+      return ImmutableMap.copyOf(properties);
     }
   }
 }
diff --git a/src/main/java/com/google/firebase/auth/UserRecord.java b/src/main/java/com/google/firebase/auth/UserRecord.java
index e004500..1e199e0 100644
--- a/src/main/java/com/google/firebase/auth/UserRecord.java
+++ b/src/main/java/com/google/firebase/auth/UserRecord.java
@@ -357,10 +357,10 @@ public class UserRecord implements UserInfo {
     /**
      * Sets the display name for the new user.
      *
-     * @param displayName a non-null, non-empty display name string.
+     * @param displayName a non-null display name string.
      */
     public CreateRequest setDisplayName(String displayName) {
-      checkNotNull(displayName, "displayName cannot be null or empty");
+      checkNotNull(displayName, "displayName cannot be null");
       properties.put("displayName", displayName);
       return this;
     }


Test patch:
diff --git a/src/test/java/com/google/firebase/auth/ListTenantsPageTest.java b/src/test/java/com/google/firebase/auth/ListTenantsPageTest.java
index 227e3dc..c1d09bc 100644
--- a/src/test/java/com/google/firebase/auth/ListTenantsPageTest.java
+++ b/src/test/java/com/google/firebase/auth/ListTenantsPageTest.java
@@ -321,7 +321,7 @@ public class ListTenantsPageTest {
 
   private static Tenant newTenant(String tenantId) throws IOException {
     return Utils.getDefaultJsonFactory().fromString(
-        String.format("{\"tenantId\":\"%s\"}", tenantId), Tenant.class);
+        String.format("{\"name\":\"%s\"}", tenantId), Tenant.class);
   }
 
   private static class TestTenantSource implements ListTenantsPage.TenantSource {
diff --git a/src/test/java/com/google/firebase/auth/TenantTest.java b/src/test/java/com/google/firebase/auth/TenantTest.java
new file mode 100644
index 0000000..22209e8
--- /dev/null
+++ b/src/test/java/com/google/firebase/auth/TenantTest.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2020 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.firebase.auth;
+
+import static com.google.firebase.auth.Tenant.UpdateRequest;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import com.google.api.client.googleapis.util.Utils;
+import com.google.api.client.json.JsonFactory;
+import java.io.IOException;
+import java.util.Map;
+import org.junit.Test;
+
+public class TenantTest {
+
+  private static final JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
+
+  private static final String TENANT_JSON_STRING = 
+      "{"
+        + "\"name\":\"TENANT_ID\","
+        + "\"displayName\":\"DISPLAY_NAME\","
+        + "\"allowPasswordSignup\":true,"
+        + "\"enableEmailLinkSignin\":false"
+        + "}";
+
+  @Test
+  public void testJsonSerialization() throws IOException {
+    Tenant tenant = jsonFactory.fromString(TENANT_JSON_STRING, Tenant.class);
+
+    assertEquals(tenant.getTenantId(), "TENANT_ID");
+    assertEquals(tenant.getDisplayName(), "DISPLAY_NAME");
+    assertTrue(tenant.isPasswordSignInAllowed());
+    assertFalse(tenant.isEmailLinkSignInEnabled());
+  }
+
+  @Test
+  public void testUpdateRequestFromTenant() throws IOException {
+    Tenant tenant = jsonFactory.fromString(TENANT_JSON_STRING, Tenant.class);
+
+    Tenant.UpdateRequest updateRequest = tenant.updateRequest();
+
+    assertEquals("TENANT_ID", updateRequest.getTenantId());
+    Map<String,Object> properties = updateRequest.getProperties();
+    assertEquals(properties.size(), 1);
+    assertEquals("TENANT_ID", (String) properties.get("name"));
+  }
+
+  @Test
+  public void testUpdateRequestFromTenantId() throws IOException {
+    Tenant.UpdateRequest updateRequest = new Tenant.UpdateRequest("TENANT_ID");
+    updateRequest
+      .setDisplayName("DISPLAY_NAME")
+      .setPasswordSignInAllowed(false)
+      .setEmailLinkSignInEnabled(true);
+
+    assertEquals("TENANT_ID", updateRequest.getTenantId());
+    Map<String,Object> properties = updateRequest.getProperties();
+    assertEquals(properties.size(), 4);
+    assertEquals("TENANT_ID", (String) properties.get("name"));
+    assertEquals("DISPLAY_NAME", (String) properties.get("displayName"));
+    assertFalse((boolean) properties.get("allowPasswordSignup"));
+    assertTrue((boolean) properties.get("enableEmailLinkSignin"));
+  }
+
+  @Test
+  public void testCreateRequest() throws IOException {
+    Tenant.CreateRequest createRequest = new Tenant.CreateRequest();
+    createRequest
+      .setDisplayName("DISPLAY_NAME")
+      .setPasswordSignInAllowed(false)
+      .setEmailLinkSignInEnabled(true);
+
+    Map<String,Object> properties = createRequest.getProperties();
+    assertEquals(properties.size(), 3);
+    assertEquals("DISPLAY_NAME", (String) properties.get("displayName"));
+    assertFalse((boolean) properties.get("allowPasswordSignup"));
+    assertTrue((boolean) properties.get("enableEmailLinkSignin"));
+  }
+}
+