Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Hamcrest matchers and assertThat() in ReindexRenamedSettingTests to improve readability #2503

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

package org.opensearch.index.reindex;

import org.hamcrest.MatcherAssert;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
import org.opensearch.test.OpenSearchTestCase;

import java.util.Arrays;
import java.util.List;

import static org.hamcrest.Matchers.equalTo;
Copy link
Collaborator Author

@tlfeng tlfeng Mar 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to suggestion from @andrross , I updated all the assertions to use assertThat() and Matchers API from Hamcrest, in this file.


/**
* A unit test to validate the former name of the setting 'reindex.remote.allowlist' still take effect,
* after it is deprecated, so that the backwards compatibility is maintained.
Expand All @@ -28,7 +31,7 @@ public class ReindexRenamedSettingTests extends OpenSearchTestCase {
*/
public void testReindexSettingsExist() {
List<Setting<?>> settings = plugin.getSettings();
assertTrue(
MatcherAssert.assertThat(
"Both 'reindex.remote.allowlist' and its predecessor should be supported settings of Reindex plugin",
settings.containsAll(
Arrays.asList(TransportReindexAction.REMOTE_CLUSTER_WHITELIST, TransportReindexAction.REMOTE_CLUSTER_ALLOWLIST)
Expand All @@ -40,9 +43,9 @@ public void testReindexSettingsExist() {
* Validate the default value of the both settings is the same.
*/
public void testSettingFallback() {
assertEquals(
MatcherAssert.assertThat(
TransportReindexAction.REMOTE_CLUSTER_ALLOWLIST.get(Settings.EMPTY),
TransportReindexAction.REMOTE_CLUSTER_WHITELIST.get(Settings.EMPTY)
equalTo(TransportReindexAction.REMOTE_CLUSTER_WHITELIST.get(Settings.EMPTY))
);
}

Expand All @@ -51,10 +54,10 @@ public void testSettingFallback() {
*/
public void testSettingGetValue() {
Settings settings = Settings.builder().put("reindex.remote.allowlist", "127.0.0.1:*").build();
assertEquals(TransportReindexAction.REMOTE_CLUSTER_ALLOWLIST.get(settings), Arrays.asList("127.0.0.1:*"));
assertEquals(
MatcherAssert.assertThat(TransportReindexAction.REMOTE_CLUSTER_ALLOWLIST.get(settings), equalTo(Arrays.asList("127.0.0.1:*")));
MatcherAssert.assertThat(
TransportReindexAction.REMOTE_CLUSTER_WHITELIST.get(settings),
TransportReindexAction.REMOTE_CLUSTER_WHITELIST.getDefault(Settings.EMPTY)
equalTo(TransportReindexAction.REMOTE_CLUSTER_WHITELIST.getDefault(Settings.EMPTY))
);
}

Expand All @@ -63,7 +66,7 @@ public void testSettingGetValue() {
*/
public void testSettingGetValueWithFallback() {
Settings settings = Settings.builder().put("reindex.remote.whitelist", "127.0.0.1:*").build();
assertEquals(TransportReindexAction.REMOTE_CLUSTER_ALLOWLIST.get(settings), Arrays.asList("127.0.0.1:*"));
MatcherAssert.assertThat(TransportReindexAction.REMOTE_CLUSTER_ALLOWLIST.get(settings), equalTo(Arrays.asList("127.0.0.1:*")));
assertSettingDeprecationsAndWarnings(new Setting<?>[] { TransportReindexAction.REMOTE_CLUSTER_WHITELIST });
}

Expand All @@ -75,8 +78,11 @@ public void testSettingGetValueWhenBothAreConfigured() {
.put("reindex.remote.allowlist", "127.0.0.1:*")
.put("reindex.remote.whitelist", "[::1]:*, 127.0.0.1:*")
.build();
assertEquals(TransportReindexAction.REMOTE_CLUSTER_ALLOWLIST.get(settings), Arrays.asList("127.0.0.1:*"));
assertEquals(TransportReindexAction.REMOTE_CLUSTER_WHITELIST.get(settings), Arrays.asList("[::1]:*", "127.0.0.1:*"));
MatcherAssert.assertThat(TransportReindexAction.REMOTE_CLUSTER_ALLOWLIST.get(settings), equalTo(Arrays.asList("127.0.0.1:*")));
MatcherAssert.assertThat(
TransportReindexAction.REMOTE_CLUSTER_WHITELIST.get(settings),
equalTo(Arrays.asList("[::1]:*", "127.0.0.1:*"))
);
assertSettingDeprecationsAndWarnings(new Setting<?>[] { TransportReindexAction.REMOTE_CLUSTER_WHITELIST });
}

Expand Down