Fix NPE with StoredCommentLinkInfoSerializer when enabled is null

The 'enabled' field in the StoredCommentLinkInfo AutoValue is
optional and, when null, its default value is 'true'.

Implement the logic into the serialization of the AutoValue,
avoiding NPE that would fail to store the data to the persistent
cache.

Bug: Issue 13754
Change-Id: If30030b66912f4c1ea96d957dc9cc24349867bd0
diff --git a/java/com/google/gerrit/server/cache/serialize/entities/StoredCommentLinkInfoSerializer.java b/java/com/google/gerrit/server/cache/serialize/entities/StoredCommentLinkInfoSerializer.java
index d7bd373..a7a84f7 100644
--- a/java/com/google/gerrit/server/cache/serialize/entities/StoredCommentLinkInfoSerializer.java
+++ b/java/com/google/gerrit/server/cache/serialize/entities/StoredCommentLinkInfoSerializer.java
@@ -19,6 +19,7 @@
 
 import com.google.gerrit.entities.StoredCommentLinkInfo;
 import com.google.gerrit.server.cache.proto.Cache;
+import java.util.Optional;
 
 /** Helper to (de)serialize values for caches. */
 public class StoredCommentLinkInfoSerializer {
@@ -38,7 +39,7 @@
         .setMatch(nullToEmpty(autoValue.getMatch()))
         .setLink(nullToEmpty(autoValue.getLink()))
         .setHtml(nullToEmpty(autoValue.getHtml()))
-        .setEnabled(autoValue.getEnabled())
+        .setEnabled(Optional.ofNullable(autoValue.getEnabled()).orElse(true))
         .setOverrideOnly(autoValue.getOverrideOnly())
         .build();
   }
diff --git a/javatests/com/google/gerrit/server/cache/serialize/entities/StoredCommentLinkInfoSerializerTest.java b/javatests/com/google/gerrit/server/cache/serialize/entities/StoredCommentLinkInfoSerializerTest.java
index 3a51b70..e293493 100644
--- a/javatests/com/google/gerrit/server/cache/serialize/entities/StoredCommentLinkInfoSerializerTest.java
+++ b/javatests/com/google/gerrit/server/cache/serialize/entities/StoredCommentLinkInfoSerializerTest.java
@@ -56,4 +56,19 @@
             .build();
     assertThat(deserialize(serialize(autoValue))).isEqualTo(autoValue);
   }
+
+  @Test
+  public void nullEnabled_roundTrip() {
+    StoredCommentLinkInfo sourceAutoValue =
+        StoredCommentLinkInfo.builder("name").setLink("<p>html").setMatch("*").build();
+
+    StoredCommentLinkInfo storedAutoValue =
+        StoredCommentLinkInfo.builder("name")
+            .setLink("<p>html")
+            .setMatch("*")
+            .setEnabled(true)
+            .build();
+
+    assertThat(deserialize(serialize(sourceAutoValue))).isEqualTo(storedAutoValue);
+  }
 }