Repository: googleapis/java-datastore
Pull request #1106 (2023-06-08 15:58:01)

fix: add some missing annotations and fix equals/hashcode for DatastoreOptions


Patch:
diff --git a/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/DatastoreOptions.java b/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/DatastoreOptions.java
index e097dc7d..eefd629e 100644
--- a/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/DatastoreOptions.java
+++ b/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/DatastoreOptions.java
@@ -188,6 +188,7 @@ public class DatastoreOptions {
     return projectId;
   }
 
+  @BetaApi
   public String getDatabaseId() {
     return databaseId;
   }
diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/BaseKey.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/BaseKey.java
index 8734efe3..6a015e38 100644
--- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/BaseKey.java
+++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/BaseKey.java
@@ -148,6 +148,7 @@ public abstract class BaseKey implements Serializable {
     return namespace;
   }
 
+  @BetaApi
   public String getDatabaseId() {
     return databaseId;
   }
diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreOptions.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreOptions.java
index 03621502..d4f3be3c 100644
--- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreOptions.java
+++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreOptions.java
@@ -155,6 +155,7 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions
     return namespace;
   }
 
+  @BetaApi
   public String getDatabaseId() {
     return this.databaseId;
   }
@@ -193,7 +194,7 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions
 
   @Override
   public int hashCode() {
-    return Objects.hash(baseHashCode(), namespace);
+    return Objects.hash(baseHashCode(), namespace, databaseId);
   }
 
   @Override
@@ -202,7 +203,9 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions
       return false;
     }
     DatastoreOptions other = (DatastoreOptions) obj;
-    return baseEquals(other) && Objects.equals(namespace, other.namespace);
+    return baseEquals(other)
+        && Objects.equals(namespace, other.namespace)
+        && Objects.equals(databaseId, other.databaseId);
   }
 
   public static Builder newBuilder() {


Test patch:
diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java
index 19dd3f0c..a545580e 100644
--- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java
+++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java
@@ -17,6 +17,7 @@
 package com.google.cloud.datastore;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
@@ -38,7 +39,6 @@ public class DatastoreOptionsTest {
   private DatastoreRpc datastoreRpc;
   private DatastoreOptions.Builder options;
 
-  // todo parameterize
   @Before
   public void setUp() {
     datastoreRpcFactory = EasyMock.createMock(DatastoreRpcFactory.class);
@@ -90,6 +90,12 @@ public class DatastoreOptionsTest {
     assertEquals(original.getHost(), copy.getHost());
     assertEquals(original.getRetrySettings(), copy.getRetrySettings());
     assertEquals(original.getCredentials(), copy.getCredentials());
+    assertEquals(original, copy);
+    assertEquals(original.hashCode(), copy.hashCode());
+
+    DatastoreOptions newOptions = options.setDatabaseId("new-database-id").build();
+    assertNotEquals(original, newOptions);
+    assertNotEquals(original.hashCode(), newOptions.hashCode());
   }
 
   @Test