Convert FilesOwnersResponse to a record Replace the manual implementation with a Java record and update the affected REST tests to use record accessors. Change-Id: Ib859eb52e0d414303b39794a6845e8a4a4a8fa26
diff --git a/owners/src/main/java/com/googlesource/gerrit/owners/entities/FilesOwnersResponse.java b/owners/src/main/java/com/googlesource/gerrit/owners/entities/FilesOwnersResponse.java index 184686b..819b741 100644 --- a/owners/src/main/java/com/googlesource/gerrit/owners/entities/FilesOwnersResponse.java +++ b/owners/src/main/java/com/googlesource/gerrit/owners/entities/FilesOwnersResponse.java
@@ -15,56 +15,12 @@ package com.googlesource.gerrit.owners.entities; -import com.google.common.base.Objects; import java.util.Map; import java.util.Set; /* Files to Owners response API representation */ -public class FilesOwnersResponse { - - public final Map<String, Set<GroupOwner>> files; - public final Map<Integer, Map<String, Integer>> ownersLabels; - public final Map<String, Set<GroupOwner>> filesApproved; - public final Map<String, Set<GroupOwner>> filesAutoApproved; - - public FilesOwnersResponse( - Map<Integer, Map<String, Integer>> ownersLabels, - Map<String, Set<GroupOwner>> files, - Map<String, Set<GroupOwner>> filesApproved, - Map<String, Set<GroupOwner>> filesAutoApproved) { - this.ownersLabels = ownersLabels; - this.files = files; - this.filesApproved = filesApproved; - this.filesAutoApproved = filesAutoApproved; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - FilesOwnersResponse that = (FilesOwnersResponse) o; - return Objects.equal(files, that.files) - && Objects.equal(ownersLabels, that.ownersLabels) - && Objects.equal(filesApproved, that.filesApproved) - && Objects.equal(filesAutoApproved, that.filesAutoApproved); - } - - @Override - public int hashCode() { - return Objects.hashCode(files, ownersLabels, filesApproved, filesAutoApproved); - } - - @Override - public String toString() { - return "FilesOwnersResponse{" - + "files=" - + files - + ", ownersLabels=" - + ownersLabels - + ", filesApproved=" - + filesApproved - + ", filesAutoApproved=" - + filesAutoApproved - + '}'; - } -} +public record FilesOwnersResponse( + Map<Integer, Map<String, Integer>> ownersLabels, + Map<String, Set<GroupOwner>> files, + Map<String, Set<GroupOwner>> filesApproved, + Map<String, Set<GroupOwner>> filesAutoApproved) {}
diff --git a/owners/src/test/java/com/googlesource/gerrit/owners/restapi/GetFilesOwnersITAbstract.java b/owners/src/test/java/com/googlesource/gerrit/owners/restapi/GetFilesOwnersITAbstract.java index e8b3919..3fe642a 100644 --- a/owners/src/test/java/com/googlesource/gerrit/owners/restapi/GetFilesOwnersITAbstract.java +++ b/owners/src/test/java/com/googlesource/gerrit/owners/restapi/GetFilesOwnersITAbstract.java
@@ -125,10 +125,10 @@ Response<FilesOwnersResponse> resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files) + assertThat(resp.value().files()) .containsExactly("a.txt", Sets.newHashSet(new Owner(admin.fullName(), admin.id().get()))); - assertThat(resp.value().ownersLabels).isEmpty(); + assertThat(resp.value().ownersLabels()).isEmpty(); } @Test @@ -140,7 +140,7 @@ Response<FilesOwnersResponse> resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().ownersLabels) + assertThat(resp.value().ownersLabels()) .containsExactly(admin.id().get(), Map.of(LabelId.CODE_REVIEW, 2)); } @@ -154,8 +154,8 @@ Response<FilesOwnersResponse> resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files).isEmpty(); - assertThat(resp.value().filesApproved) + assertThat(resp.value().files()).isEmpty(); + assertThat(resp.value().filesApproved()) .containsExactly("a.txt", Sets.newHashSet(new Owner(admin.fullName(), admin.id().get()))); } @@ -224,9 +224,9 @@ Response<FilesOwnersResponse> response = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId.toString()))); - assertThat(response.value().files).containsExactly(OWNED_JAVA_FILE, owners(admin)); - assertThat(response.value().filesApproved).isEmpty(); - assertThat(response.value().filesAutoApproved).isEmpty(); + assertThat(response.value().files()).containsExactly(OWNED_JAVA_FILE, owners(admin)); + assertThat(response.value().filesApproved()).isEmpty(); + assertThat(response.value().filesAutoApproved()).isEmpty(); } @Test @@ -280,9 +280,9 @@ Response<FilesOwnersResponse> response = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId.toString()))); - assertThat(response.value().files).isEmpty(); - assertThat(response.value().filesApproved).isEmpty(); - assertThat(response.value().filesAutoApproved) + assertThat(response.value().files()).isEmpty(); + assertThat(response.value().filesApproved()).isEmpty(); + assertThat(response.value().filesAutoApproved()) .containsExactly(OWNED_JAVA_FILE, owners(admin), ANOTHER_OWNED_JAVA_FILE, owners(admin)); } @@ -295,9 +295,9 @@ Response<FilesOwnersResponse> resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files) + assertThat(resp.value().files()) .containsExactly("a.txt", Sets.newHashSet(new GroupOwner(admin.username()))); - assertThat(resp.value().filesApproved).isEmpty(); + assertThat(resp.value().filesApproved()).isEmpty(); } @Test @@ -312,8 +312,8 @@ Response<FilesOwnersResponse> resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files).isEmpty(); - assertThat(resp.value().filesApproved) + assertThat(resp.value().files()).isEmpty(); + assertThat(resp.value().filesApproved()) .containsExactly("a.txt", Sets.newHashSet(new GroupOwner(admin.username()))); } @@ -326,9 +326,9 @@ Response<FilesOwnersResponse> resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files) + assertThat(resp.value().files()) .containsExactly("a.txt", Sets.newHashSet(new GroupOwner(admin.username()))); - assertThat(resp.value().filesApproved).isEmpty(); + assertThat(resp.value().filesApproved()).isEmpty(); } @Test @@ -340,9 +340,9 @@ Response<FilesOwnersResponse> resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files) + assertThat(resp.value().files()) .containsExactly("a.txt", Sets.newHashSet(new Owner(admin.fullName(), admin.id().get()))); - assertThat(resp.value().filesApproved).isEmpty(); + assertThat(resp.value().filesApproved()).isEmpty(); } @Test @@ -365,12 +365,12 @@ String changeId = createChange().getChangeId(); Response<FilesOwnersResponse> resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files).containsExactly("a.txt", Sets.newHashSet(rootOwner)); + assertThat(resp.value().files()).containsExactly("a.txt", Sets.newHashSet(rootOwner)); addOwnerFileToProjectConfig(allProjects, true, user); resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files).containsExactly("a.txt", Sets.newHashSet(projectOwner)); - assertThat(resp.value().filesApproved).isEmpty(); + assertThat(resp.value().files()).containsExactly("a.txt", Sets.newHashSet(projectOwner)); + assertThat(resp.value().filesApproved()).isEmpty(); } @Test @@ -443,8 +443,8 @@ Response<FilesOwnersResponse> resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files).containsExactly("a.txt", Sets.newHashSet(rootOwner)); - assertThat(resp.value().filesApproved).isEmpty(); + assertThat(resp.value().files()).containsExactly("a.txt", Sets.newHashSet(rootOwner)); + assertThat(resp.value().filesApproved()).isEmpty(); } private void assertInheritFromProject(Project.NameKey projectNameKey) throws Exception { @@ -455,9 +455,9 @@ Response<FilesOwnersResponse> resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files) + assertThat(resp.value().files()) .containsExactly("a.txt", Sets.newHashSet(rootOwner, projectOwner)); - assertThat(resp.value().filesApproved).isEmpty(); + assertThat(resp.value().filesApproved()).isEmpty(); } private void addBrokenOwnersFileToRoot() throws Exception { @@ -517,17 +517,18 @@ throws Exception { Response<FilesOwnersResponse> response = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(response.value().files).isEmpty(); + assertThat(response.value().files()).isEmpty(); if (explicitlyApprovedOwners.isEmpty()) { - assertThat(response.value().filesApproved).isEmpty(); + assertThat(response.value().filesApproved()).isEmpty(); } else { - assertThat(response.value().filesApproved) + assertThat(response.value().filesApproved()) .containsExactly(filePath, explicitlyApprovedOwners); } if (autoApprovedOwners.isEmpty()) { - assertThat(response.value().filesAutoApproved).isEmpty(); + assertThat(response.value().filesAutoApproved()).isEmpty(); } else { - assertThat(response.value().filesAutoApproved).containsExactly(filePath, autoApprovedOwners); + assertThat(response.value().filesAutoApproved()) + .containsExactly(filePath, autoApprovedOwners); } }
diff --git a/owners/src/test/java/com/googlesource/gerrit/owners/restapi/GetFilesOwnersSubmitRequirementsIT.java b/owners/src/test/java/com/googlesource/gerrit/owners/restapi/GetFilesOwnersSubmitRequirementsIT.java index 3915997..026ebdb 100644 --- a/owners/src/test/java/com/googlesource/gerrit/owners/restapi/GetFilesOwnersSubmitRequirementsIT.java +++ b/owners/src/test/java/com/googlesource/gerrit/owners/restapi/GetFilesOwnersSubmitRequirementsIT.java
@@ -70,19 +70,19 @@ Response<FilesOwnersResponse> resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files) + assertThat(resp.value().files()) .containsExactly("foo", Sets.newHashSet(new Owner(admin.fullName(), admin.id().get()))); - assertThat(resp.value().ownersLabels).isEmpty(); - assertThat(resp.value().filesApproved).isEmpty(); + assertThat(resp.value().ownersLabels()).isEmpty(); + assertThat(resp.value().filesApproved()).isEmpty(); // give CR+1 as requested recommend(changeId); resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files).isEmpty(); - assertThat(resp.value().ownersLabels) + assertThat(resp.value().files()).isEmpty(); + assertThat(resp.value().ownersLabels()) .containsExactly(admin.id().get(), Map.of(LabelId.CODE_REVIEW, 1)); - assertThat(resp.value().filesApproved) + assertThat(resp.value().filesApproved()) .containsExactly("foo", Sets.newHashSet(new Owner(admin.fullName(), admin.id().get()))); } @@ -97,18 +97,18 @@ Response<FilesOwnersResponse> resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files) + assertThat(resp.value().files()) .containsExactly("foo", Sets.newHashSet(new Owner(admin.fullName(), admin.id().get()))); - assertThat(resp.value().ownersLabels).isEmpty(); - assertThat(resp.value().filesApproved).isEmpty(); + assertThat(resp.value().ownersLabels()).isEmpty(); + assertThat(resp.value().filesApproved()).isEmpty(); // give LabelFoo+1 as requested gApi.changes().id(changeId).current().review(new ReviewInput().label(label, 1)); resp = assertResponseOk(ownersApi.apply(parseCurrentRevisionResource(changeId))); - assertThat(resp.value().files).isEmpty(); - assertThat(resp.value().ownersLabels).containsEntry(admin.id().get(), Map.of(label, 1)); - assertThat(resp.value().filesApproved) + assertThat(resp.value().files()).isEmpty(); + assertThat(resp.value().ownersLabels()).containsEntry(admin.id().get(), Map.of(label, 1)); + assertThat(resp.value().filesApproved()) .containsExactly("foo", Sets.newHashSet(new Owner(admin.fullName(), admin.id().get()))); }