Test toString methods of AutoValue classes

Strictly speaking the toString methods are not used, but having them is
useful for debugging.

The test add test coverage for the toString methods and verify that all
data is included in the string representation. E.g. such a test would
start failing if new data is added to the AutoValue class, but the
toString method was not adapted to include it.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I6ef47f607b9dc4ef52570c2fc539f10bec3a10d2
diff --git a/javatests/com/google/gerrit/plugins/codeowners/backend/AbstractAutoValueTest.java b/javatests/com/google/gerrit/plugins/codeowners/backend/AbstractAutoValueTest.java
new file mode 100644
index 0000000..1a3c163
--- /dev/null
+++ b/javatests/com/google/gerrit/plugins/codeowners/backend/AbstractAutoValueTest.java
@@ -0,0 +1,35 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// 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.gerrit.plugins.codeowners.backend;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.gerrit.plugins.codeowners.acceptance.AbstractCodeOwnersTest;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+/** Base class for tests of AutoValue classes. */
+abstract class AbstractAutoValueTest extends AbstractCodeOwnersTest {
+  protected <T> void assertThatToStringIncludesAllData(
+      T autoValueObjectToTest, Class<T> autoValueClass) throws Exception {
+    for (Method method : autoValueClass.getDeclaredMethods()) {
+      if (Modifier.isAbstract(method.getModifiers())) {
+        Object result = method.invoke(autoValueObjectToTest);
+        assertThat(autoValueObjectToTest.toString())
+            .contains(String.format("%s=%s", method.getName(), result));
+      }
+    }
+  }
+}
diff --git a/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerResolverResultTest.java b/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerResolverResultTest.java
new file mode 100644
index 0000000..d64f5bf
--- /dev/null
+++ b/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerResolverResultTest.java
@@ -0,0 +1,34 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// 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.gerrit.plugins.codeowners.backend;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import org.junit.Test;
+
+/** Tests for {@link CodeOwnerResolverResult}. */
+public class CodeOwnerResolverResultTest extends AbstractAutoValueTest {
+  @Test
+  public void toStringIncludesAllData() throws Exception {
+    CodeOwnerResolverResult codeOwnerResolverResult =
+        CodeOwnerResolverResult.create(
+            ImmutableSet.of(CodeOwner.create(admin.id())),
+            /* ownedByAllUsers= */ false,
+            /* hasUnresolvedCodeOwners= */ false,
+            /* hasUnresolvedImports= */ false,
+            ImmutableList.of("test message"));
+    assertThatToStringIncludesAllData(codeOwnerResolverResult, CodeOwnerResolverResult.class);
+  }
+}
diff --git a/javatests/com/google/gerrit/plugins/codeowners/backend/PathCodeOwnersResultTest.java b/javatests/com/google/gerrit/plugins/codeowners/backend/PathCodeOwnersResultTest.java
new file mode 100644
index 0000000..d270fdc
--- /dev/null
+++ b/javatests/com/google/gerrit/plugins/codeowners/backend/PathCodeOwnersResultTest.java
@@ -0,0 +1,46 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// 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.gerrit.plugins.codeowners.backend;
+
+import com.google.common.collect.ImmutableList;
+import java.nio.file.Paths;
+import org.eclipse.jgit.lib.ObjectId;
+import org.junit.Test;
+
+/** Tests for {@link PathCodeOwnersResult}. */
+public class PathCodeOwnersResultTest extends AbstractAutoValueTest {
+  private static final ObjectId TEST_REVISION =
+      ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
+
+  @Test
+  public void toStringIncludesAllData() throws Exception {
+    CodeOwnerConfig.Key codeOwnerConfigKey = CodeOwnerConfig.Key.create(project, "master", "/");
+    CodeOwnerConfigReference codeOwnerConfigReference =
+        CodeOwnerConfigReference.create(CodeOwnerConfigImportMode.ALL, "/bar/OWNERS");
+    PathCodeOwnersResult pathCodeOwnersResult =
+        PathCodeOwnersResult.create(
+            Paths.get("/foo/bar/baz.md"),
+            CodeOwnerConfig.builder(codeOwnerConfigKey, TEST_REVISION)
+                .addImport(codeOwnerConfigReference)
+                .build(),
+            ImmutableList.of(
+                UnresolvedImport.create(
+                    codeOwnerConfigKey,
+                    CodeOwnerConfig.Key.create(project, "master", "/bar/"),
+                    codeOwnerConfigReference,
+                    "test message")));
+    assertThatToStringIncludesAllData(pathCodeOwnersResult, PathCodeOwnersResult.class);
+  }
+}
diff --git a/javatests/com/google/gerrit/plugins/codeowners/backend/UnresolvedImportTest.java b/javatests/com/google/gerrit/plugins/codeowners/backend/UnresolvedImportTest.java
new file mode 100644
index 0000000..ce0494f
--- /dev/null
+++ b/javatests/com/google/gerrit/plugins/codeowners/backend/UnresolvedImportTest.java
@@ -0,0 +1,31 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// 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.gerrit.plugins.codeowners.backend;
+
+import org.junit.Test;
+
+/** Tests for {@link UnresolvedImport}. */
+public class UnresolvedImportTest extends AbstractAutoValueTest {
+  @Test
+  public void toStringIncludesAllData() throws Exception {
+    UnresolvedImport unresolvedImport =
+        UnresolvedImport.create(
+            CodeOwnerConfig.Key.create(project, "master", "/"),
+            CodeOwnerConfig.Key.create(project, "master", "/bar/"),
+            CodeOwnerConfigReference.create(CodeOwnerConfigImportMode.ALL, "/bar/OWNERS"),
+            "test message");
+    assertThatToStringIncludesAllData(unresolvedImport, UnresolvedImport.class);
+  }
+}