Skip to content

Commit

Permalink
chore: add equals, hashCode, and toString methods to client interface…
Browse files Browse the repository at this point in the history
… impls (#5681)
vcrfxia authored Jun 25, 2020
1 parent 4d860f8 commit 9bbd2ae
Showing 14 changed files with 548 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

public class ClientImpl implements Client {
@@ -392,4 +393,30 @@ private static String createBasicAuthHeader(final ClientOptions clientOptions) {
Base64.getEncoder().encodeToString(creds.getBytes(Charset.defaultCharset()));
return "Basic " + base64creds;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ClientImpl client = (ClientImpl) o;
return clientOptions.equals(client.clientOptions)
&& vertx.equals(client.vertx);
}

@Override
public int hashCode() {
return Objects.hash(clientOptions, vertx);
}

@Override
public String toString() {
return "Client{"
+ "clientOptions=" + clientOptions
+ ", vertx=" + vertx
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -210,4 +210,54 @@ public ClientOptions copy() {
basicAuthUsername, basicAuthPassword,
executeQueryMaxResultRows);
}

// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
@Override
public boolean equals(final Object o) {
// CHECKSTYLE_RULES.ON: CyclomaticComplexity
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ClientOptionsImpl that = (ClientOptionsImpl) o;
return port == that.port
&& useTls == that.useTls
&& verifyHost == that.verifyHost
&& useAlpn == that.useAlpn
&& executeQueryMaxResultRows == that.executeQueryMaxResultRows
&& host.equals(that.host)
&& Objects.equals(trustStorePath, that.trustStorePath)
&& Objects.equals(trustStorePassword, that.trustStorePassword)
&& Objects.equals(keyStorePath, that.keyStorePath)
&& Objects.equals(keyStorePassword, that.keyStorePassword)
&& Objects.equals(basicAuthUsername, that.basicAuthUsername)
&& Objects.equals(basicAuthPassword, that.basicAuthPassword);
}

@Override
public int hashCode() {
return Objects.hash(host, port, useTls, verifyHost, useAlpn, trustStorePath,
trustStorePassword, keyStorePath, keyStorePassword, basicAuthUsername, basicAuthPassword,
executeQueryMaxResultRows);
}

@Override
public String toString() {
return "ClientOptions{"
+ "host='" + host + '\''
+ ", port=" + port
+ ", useTls=" + useTls
+ ", verifyHost=" + verifyHost
+ ", useAlpn=" + useAlpn
+ ", trustStorePath='" + trustStorePath + '\''
+ ", trustStorePassword='" + trustStorePassword + '\''
+ ", keyStorePath='" + keyStorePath + '\''
+ ", keyStorePassword='" + keyStorePassword + '\''
+ ", basicAuthUsername='" + basicAuthUsername + '\''
+ ", basicAuthPassword='" + basicAuthPassword + '\''
+ ", executeQueryMaxResultRows=" + executeQueryMaxResultRows
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -51,4 +51,11 @@ public boolean equals(final Object o) {
public int hashCode() {
return Objects.hash(type);
}

@Override
public String toString() {
return "ColumnType{"
+ "type=" + type
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -32,7 +32,6 @@ public class RowImpl implements Row {
private final KsqlArray values;
private final Map<String, Integer> columnNameToIndex;

@SuppressWarnings("unchecked")
public RowImpl(
final List<String> columnNames,
final List<ColumnType> columnTypes,
@@ -171,4 +170,32 @@ private int indexFromName(final String columnName) {
}
return index;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final RowImpl row = (RowImpl) o;
return columnNames.equals(row.columnNames)
&& columnTypes.equals(row.columnTypes)
&& values.equals(row.values);
}

@Override
public int hashCode() {
return Objects.hash(columnNames, columnTypes, values);
}

@Override
public String toString() {
return "Row{"
+ "columnNames=" + columnNames
+ ", columnTypes=" + columnTypes
+ ", values=" + values
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -44,4 +44,32 @@ public String getTopic() {
public String getFormat() {
return format;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final StreamInfoImpl that = (StreamInfoImpl) o;
return name.equals(that.name)
&& topicName.equals(that.topicName)
&& format.equals(that.format);
}

@Override
public int hashCode() {
return Objects.hash(name, topicName, format);
}

@Override
public String toString() {
return "StreamInfo{"
+ "name='" + name + '\''
+ ", topicName='" + topicName + '\''
+ ", format='" + format + '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -56,4 +56,34 @@ public String getFormat() {
public boolean isWindowed() {
return isWindowed;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final TableInfoImpl tableInfo = (TableInfoImpl) o;
return isWindowed == tableInfo.isWindowed
&& name.equals(tableInfo.name)
&& topicName.equals(tableInfo.topicName)
&& format.equals(tableInfo.format);
}

@Override
public int hashCode() {
return Objects.hash(name, topicName, format, isWindowed);
}

@Override
public String toString() {
return "TableInfo{"
+ "name='" + name + '\''
+ ", topicName='" + topicName + '\''
+ ", format='" + format + '\''
+ ", isWindowed=" + isWindowed
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -45,4 +45,32 @@ public int getPartitions() {
public List<Integer> getReplicasPerPartition() {
return replicasPerPartition;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final TopicInfoImpl topicInfo = (TopicInfoImpl) o;
return partitions == topicInfo.partitions
&& name.equals(topicInfo.name)
&& replicasPerPartition.equals(topicInfo.replicasPerPartition);
}

@Override
public int hashCode() {
return Objects.hash(name, partitions, replicasPerPartition);
}

@Override
public String toString() {
return "TopicInfo{"
+ "name='" + name + '\''
+ ", partitions=" + partitions
+ ", replicasPerPartition=" + replicasPerPartition
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2020 Confluent Inc.
*
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.confluent.ksql.api.client.impl;

import com.google.common.testing.EqualsTester;
import io.confluent.ksql.api.client.Client;
import io.confluent.ksql.api.client.ClientOptions;
import io.vertx.core.Vertx;
import org.junit.Before;
import org.junit.Test;

public class ClientImplTest {

private static final ClientOptions OPTIONS_1 = ClientOptions.create();
private static final ClientOptions OPTIONS_2 = ClientOptions.create().setUseTls(true);

private Vertx vertx;

@Before
public void setUp() {
vertx = Vertx.vertx();
}

@Test
public void shouldImplementHashCodeAndEquals() {
new EqualsTester()
.addEqualityGroup(
Client.create(OPTIONS_1)
)
.addEqualityGroup(
Client.create(OPTIONS_1, vertx),
Client.create(OPTIONS_1, vertx)
)
.addEqualityGroup(
Client.create(OPTIONS_2, vertx)
)
.testEquals();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2020 Confluent Inc.
*
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.confluent.ksql.api.client.impl;

import com.google.common.testing.EqualsTester;
import io.confluent.ksql.api.client.ClientOptions;
import org.junit.Test;

public class ClientOptionsImplTest {

@Test
public void shouldImplementHashCodeAndEquals() {
new EqualsTester()
.addEqualityGroup(
ClientOptions.create(),
ClientOptions.create()
.setHost(ClientOptions.DEFAULT_HOST)
.setPort(ClientOptions.DEFAULT_HOST_PORT)
)
.addEqualityGroup(
ClientOptions.create().setHost("foo")
)
.addEqualityGroup(
ClientOptions.create().setPort(443)
)
.addEqualityGroup(
ClientOptions.create().setUseTls(true)
)
.addEqualityGroup(
ClientOptions.create().setVerifyHost(false)
)
.addEqualityGroup(
ClientOptions.create().setUseAlpn(true)
)
.addEqualityGroup(
ClientOptions.create().setTrustStore("trust_store")
)
.addEqualityGroup(
ClientOptions.create().setTrustStorePassword("trust_store_pass")
)
.addEqualityGroup(
ClientOptions.create().setKeyStore("key_store")
)
.addEqualityGroup(
ClientOptions.create().setKeyStorePassword("key_store_pass")
)
.addEqualityGroup(
ClientOptions.create().setBasicAuthCredentials("user", "pass")
)
.addEqualityGroup(
ClientOptions.create().setExecuteQueryMaxResultRows(10)
)
.testEquals();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2020 Confluent Inc.
*
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.confluent.ksql.api.client.impl;

import com.google.common.testing.EqualsTester;
import org.junit.Test;

public class ColumnTypeImplTest {

@Test
public void shouldImplementHashCodeAndEquals() {
new EqualsTester()
.addEqualityGroup(
new ColumnTypeImpl("STRING"),
new ColumnTypeImpl("STRING")
)
.addEqualityGroup(
new ColumnTypeImpl("INTEGER")
)
.addEqualityGroup(
new ColumnTypeImpl("BIGINT")
)
.addEqualityGroup(
new ColumnTypeImpl("DOUBLE")
)
.addEqualityGroup(
new ColumnTypeImpl("BOOLEAN")
)
.addEqualityGroup(
new ColumnTypeImpl("DECIMAL")
)
.addEqualityGroup(
new ColumnTypeImpl("ARRAY")
)
.addEqualityGroup(
new ColumnTypeImpl("MAP")
)
.addEqualityGroup(
new ColumnTypeImpl("STRUCT")
)
.testEquals();
}

}
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester;
import io.confluent.ksql.api.client.ColumnType;
import io.confluent.ksql.api.client.KsqlArray;
import io.confluent.ksql.api.client.KsqlObject;
@@ -159,4 +160,44 @@ public void shouldGetAsObject() {
assertThat(obj.getKsqlObject("f_map"), is(new KsqlObject(ImmutableMap.of("k1", "v1", "k2", "v2"))));
assertThat(obj.getKsqlObject("f_struct"), is(new KsqlObject(ImmutableMap.of("f1", "baz", "f2", 12))));
}

@Test
public void shouldImplementHashCodeAndEquals() {
new EqualsTester()
.addEqualityGroup(
new RowImpl(
ImmutableList.of("col1, col2"),
ImmutableList.of(new ColumnTypeImpl("STRING"), new ColumnTypeImpl("BIGINT")),
new JsonArray().add("foo").add(3L),
ImmutableMap.of("col1", 0, "col2", 1)),
new RowImpl(
ImmutableList.of("col1, col2"),
ImmutableList.of(new ColumnTypeImpl("STRING"), new ColumnTypeImpl("BIGINT")),
new JsonArray().add("foo").add(3L),
ImmutableMap.of("col1", 0, "col2", 1))
)
.addEqualityGroup(
new RowImpl(
ImmutableList.of("col1, col3"),
ImmutableList.of(new ColumnTypeImpl("STRING"), new ColumnTypeImpl("BIGINT")),
new JsonArray().add("foo").add(3L),
ImmutableMap.of("col1", 0, "col3", 1))
)
.addEqualityGroup(
new RowImpl(
ImmutableList.of("col1, col2"),
ImmutableList.of(new ColumnTypeImpl("BIGINT"), new ColumnTypeImpl("STRING")),
new JsonArray().add("foo").add(3L),
ImmutableMap.of("col1", 0, "col2", 1))
)
.addEqualityGroup(
new RowImpl(
ImmutableList.of("col1, col2"),
ImmutableList.of(new ColumnTypeImpl("STRING"), new ColumnTypeImpl("BIGINT")),
new JsonArray().add("bar").add(3L),
ImmutableMap.of("col1", 0, "col2", 1))
)
.testEquals();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2020 Confluent Inc.
*
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.confluent.ksql.api.client.impl;

import com.google.common.testing.EqualsTester;
import org.junit.Test;

public class StreamInfoImplTest {

@Test
public void shouldImplementHashCodeAndEquals() {
new EqualsTester()
.addEqualityGroup(
new StreamInfoImpl("name", "topic", "JSON"),
new StreamInfoImpl("name", "topic", "JSON")
)
.addEqualityGroup(
new StreamInfoImpl("other_name", "topic", "JSON")
)
.addEqualityGroup(
new StreamInfoImpl("name", "other_topic", "JSON")
)
.addEqualityGroup(
new StreamInfoImpl("name", "topic", "AVRO")
)
.testEquals();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2020 Confluent Inc.
*
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.confluent.ksql.api.client.impl;

import com.google.common.testing.EqualsTester;
import org.junit.Test;

public class TableInfoImplTest {

@Test
public void shouldImplementHashCodeAndEquals() {
new EqualsTester()
.addEqualityGroup(
new TableInfoImpl("name", "topic", "JSON", true),
new TableInfoImpl("name", "topic", "JSON", true)
)
.addEqualityGroup(
new TableInfoImpl("other_name", "topic", "JSON", true)
)
.addEqualityGroup(
new TableInfoImpl("name", "other_topic", "JSON", true)
)
.addEqualityGroup(
new TableInfoImpl("name", "topic", "AVRO", true)
)
.addEqualityGroup(
new TableInfoImpl("name", "topic", "JSON", false)
)
.testEquals();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2020 Confluent Inc.
*
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.confluent.ksql.api.client.impl;

import com.google.common.collect.ImmutableList;
import com.google.common.testing.EqualsTester;
import org.junit.Test;

public class TopicInfoImplTest {

@Test
public void shouldImplementHashCodeAndEquals() {
new EqualsTester()
.addEqualityGroup(
new TopicInfoImpl("name", 3, ImmutableList.of(2, 2, 2)),
new TopicInfoImpl("name", 3, ImmutableList.of(2, 2, 2))
)
.addEqualityGroup(
new TopicInfoImpl("other_name", 3, ImmutableList.of(2, 2, 2))
)
.addEqualityGroup(
new TopicInfoImpl("name", 1, ImmutableList.of(2))
)
.addEqualityGroup(
new TopicInfoImpl("name", 3, ImmutableList.of(1, 1, 1))
)
.testEquals();
}

}

0 comments on commit 9bbd2ae

Please sign in to comment.