diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/BookieServiceInfo.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/BookieServiceInfo.java index 8b23a70143b..c465938311f 100644 --- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/BookieServiceInfo.java +++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/BookieServiceInfo.java @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.function.Supplier; /** @@ -150,6 +151,26 @@ public void setExtensions(List extensions) { this.extensions = extensions; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof Endpoint)) { + return false; + } + final Endpoint endpoint = (Endpoint) o; + return port == endpoint.port && Objects.equals(id, endpoint.id) && Objects.equals(host, endpoint.host) + && Objects.equals(protocol, endpoint.protocol) && Objects.equals(auth, endpoint.auth) + && Objects.equals(extensions, endpoint.extensions); + } + + @Override + public int hashCode() { + return Objects.hash(id, port, host, protocol, auth, extensions); + } + @Override public String toString() { return "EndpointInfo{" + "id=" + id + ", port=" + port + ", host=" + host + ", protocol=" + protocol + ", " @@ -158,6 +179,23 @@ public String toString() { } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof BookieServiceInfo)) { + return false; + } + final BookieServiceInfo that = (BookieServiceInfo) o; + return Objects.equals(properties, that.properties) && Objects.equals(endpoints, that.endpoints); + } + + @Override + public int hashCode() { + return Objects.hash(properties, endpoints); + } + @Override public String toString() { return "BookieServiceInfo{" + "properties=" + properties + ", endpoints=" + endpoints + '}'; diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/discover/BookieServiceInfoTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/discover/BookieServiceInfoTest.java index 4173d630002..3bbda71c510 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/discover/BookieServiceInfoTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/discover/BookieServiceInfoTest.java @@ -22,6 +22,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -29,6 +32,7 @@ import java.util.Objects; import org.apache.bookkeeper.discover.BookieServiceInfo.Endpoint; import org.apache.bookkeeper.net.BookieId; +import org.junit.Assert; import org.junit.Test; /** @@ -90,4 +94,22 @@ private void assertBookieServiceInfoEquals(BookieServiceInfo expected, BookieSer assertEquals(expected.getProperties(), provided.getProperties()); } + @Test + public void testComparableBookieServerInfo() throws Exception { + final BookieServiceInfo.Endpoint endpoint = new BookieServiceInfo.Endpoint( + "http", 8080, "host1", "HTTP", + Lists.newArrayList("auth1"), Lists.newArrayList("ext1") + ); + final Map properties1 = Maps.newHashMap(); + properties1.put("key", "value"); + final BookieServiceInfo info = new BookieServiceInfo( + properties1, + Lists.newArrayList(endpoint) + ); + final ObjectMapper objectMapper = new ObjectMapper(); + final String bData = objectMapper.writeValueAsString(info); + final BookieServiceInfo another = objectMapper.readValue(bData, BookieServiceInfo.class); + Assert.assertEquals(info, another); + } + }