From d5c3b4a0b8b4a1e4b6f2eaf9508d526c9361f732 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 14 Sep 2018 21:59:12 -0400 Subject: [PATCH 1/3] Move CCR monitoring tests to ccr sub-project This commit moves the CCR monitoring tests from the monitoring sub-project to the ccr sub-project. --- x-pack/plugin/ccr/build.gradle | 1 + .../collector/ccr/CcrStatsCollectorTests.java | 72 +++++++++-------- .../ccr/CcrStatsMonitoringDocTests.java | 80 ++++++++++--------- 3 files changed, 81 insertions(+), 72 deletions(-) rename x-pack/plugin/{monitoring => ccr}/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java (70%) rename x-pack/plugin/{monitoring => ccr}/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsMonitoringDocTests.java (75%) diff --git a/x-pack/plugin/ccr/build.gradle b/x-pack/plugin/ccr/build.gradle index 0b1f889a2c121..ea8aa89777703 100644 --- a/x-pack/plugin/ccr/build.gradle +++ b/x-pack/plugin/ccr/build.gradle @@ -49,6 +49,7 @@ dependencies { compileOnly project(path: xpackModule('core'), configuration: 'default') testCompile project(path: xpackModule('core'), configuration: 'testArtifacts') + testCompile project(path: xpackModule('monitoring'), configuration: 'testArtifacts') } dependencyLicenses { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java similarity index 70% rename from x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java rename to x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java index aaf3a61643b5e..61e38d89a9337 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java @@ -6,11 +6,13 @@ package org.elasticsearch.xpack.monitoring.collector.ccr; +import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.action.ActionFuture; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction; @@ -18,6 +20,9 @@ import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.BaseCollectorTestCase; +import org.elasticsearch.xpack.monitoring.MonitoringTestUtils; +import org.hamcrest.Matchers; +import org.junit.Assert; import org.mockito.ArgumentMatcher; import java.util.ArrayList; @@ -27,7 +32,6 @@ import java.util.List; import static java.util.Collections.emptyList; -import static org.elasticsearch.xpack.monitoring.MonitoringTestUtils.randomMonitoringNode; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.hasSize; @@ -41,9 +45,9 @@ public class CcrStatsCollectorTests extends BaseCollectorTestCase { public void testShouldCollectReturnsFalseIfMonitoringNotAllowed() { - final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); - final boolean ccrAllowed = randomBoolean(); - final boolean isElectedMaster = randomBoolean(); + final Settings settings = ESTestCase.randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); + final boolean ccrAllowed = ESTestCase.randomBoolean(); + final boolean isElectedMaster = ESTestCase.randomBoolean(); whenLocalNodeElectedMaster(isElectedMaster); // this controls the blockage @@ -52,7 +56,7 @@ public void testShouldCollectReturnsFalseIfMonitoringNotAllowed() { final CcrStatsCollector collector = new CcrStatsCollector(settings, clusterService, licenseState, client); - assertThat(collector.shouldCollect(isElectedMaster), is(false)); + Assert.assertThat(collector.shouldCollect(isElectedMaster), is(false)); if (isElectedMaster) { verify(licenseState).isMonitoringAllowed(); } @@ -60,31 +64,31 @@ public void testShouldCollectReturnsFalseIfMonitoringNotAllowed() { public void testShouldCollectReturnsFalseIfNotMaster() { // regardless of CCR being enabled - final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); + final Settings settings = ESTestCase.randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); - when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean()); - when(licenseState.isCcrAllowed()).thenReturn(randomBoolean()); + when(licenseState.isMonitoringAllowed()).thenReturn(ESTestCase.randomBoolean()); + when(licenseState.isCcrAllowed()).thenReturn(ESTestCase.randomBoolean()); // this controls the blockage final boolean isElectedMaster = false; final CcrStatsCollector collector = new CcrStatsCollector(settings, clusterService, licenseState, client); - assertThat(collector.shouldCollect(isElectedMaster), is(false)); + Assert.assertThat(collector.shouldCollect(isElectedMaster), is(false)); } public void testShouldCollectReturnsFalseIfCCRIsDisabled() { // this is controls the blockage final Settings settings = ccrDisabledSettings(); - when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean()); - when(licenseState.isCcrAllowed()).thenReturn(randomBoolean()); + when(licenseState.isMonitoringAllowed()).thenReturn(ESTestCase.randomBoolean()); + when(licenseState.isCcrAllowed()).thenReturn(ESTestCase.randomBoolean()); - final boolean isElectedMaster = randomBoolean(); + final boolean isElectedMaster = ESTestCase.randomBoolean(); whenLocalNodeElectedMaster(isElectedMaster); final CcrStatsCollector collector = new CcrStatsCollector(settings, clusterService, licenseState, client); - assertThat(collector.shouldCollect(isElectedMaster), is(false)); + Assert.assertThat(collector.shouldCollect(isElectedMaster), is(false)); if (isElectedMaster) { verify(licenseState).isMonitoringAllowed(); @@ -92,17 +96,17 @@ public void testShouldCollectReturnsFalseIfCCRIsDisabled() { } public void testShouldCollectReturnsFalseIfCCRIsNotAllowed() { - final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); + final Settings settings = ESTestCase.randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); - when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean()); + when(licenseState.isMonitoringAllowed()).thenReturn(ESTestCase.randomBoolean()); // this is controls the blockage when(licenseState.isCcrAllowed()).thenReturn(false); - final boolean isElectedMaster = randomBoolean(); + final boolean isElectedMaster = ESTestCase.randomBoolean(); whenLocalNodeElectedMaster(isElectedMaster); final CcrStatsCollector collector = new CcrStatsCollector(settings, clusterService, licenseState, client); - assertThat(collector.shouldCollect(isElectedMaster), is(false)); + Assert.assertThat(collector.shouldCollect(isElectedMaster), is(false)); if (isElectedMaster) { verify(licenseState).isMonitoringAllowed(); @@ -118,24 +122,24 @@ public void testShouldCollectReturnsTrue() { final CcrStatsCollector collector = new CcrStatsCollector(settings, clusterService, licenseState, client); - assertThat(collector.shouldCollect(isElectedMaster), is(true)); + Assert.assertThat(collector.shouldCollect(isElectedMaster), is(true)); verify(licenseState).isMonitoringAllowed(); } public void testDoCollect() throws Exception { - final String clusterUuid = randomAlphaOfLength(5); + final String clusterUuid = ESTestCase.randomAlphaOfLength(5); whenClusterStateWithUUID(clusterUuid); - final MonitoringDoc.Node node = randomMonitoringNode(random()); + final MonitoringDoc.Node node = MonitoringTestUtils.randomMonitoringNode(LuceneTestCase.random()); final CcrClient client = mock(CcrClient.class); final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); - final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(1, 120)); + final TimeValue timeout = TimeValue.timeValueSeconds(ESTestCase.randomIntBetween(1, 120)); withCollectionTimeout(CcrStatsCollector.CCR_STATS_TIMEOUT, timeout); final CcrStatsCollector collector = new CcrStatsCollector(Settings.EMPTY, clusterService, licenseState, client, threadContext); - assertEquals(timeout, collector.getCollectionTimeout()); + Assert.assertEquals(timeout, collector.getCollectionTimeout()); final List statuses = mockStatuses(); @@ -148,32 +152,32 @@ public void testDoCollect() throws Exception { when(client.stats(statsRequestEq(request))).thenReturn(future); when(future.actionGet(timeout)).thenReturn(responses); - final long interval = randomNonNegativeLong(); + final long interval = ESTestCase.randomNonNegativeLong(); final Collection documents = collector.doCollect(node, interval, clusterState); verify(clusterState).metaData(); verify(metaData).clusterUUID(); - assertThat(documents, hasSize(statuses.size())); + Assert.assertThat(documents, hasSize(statuses.size())); int index = 0; for (final Iterator it = documents.iterator(); it.hasNext(); index++) { final CcrStatsMonitoringDoc document = (CcrStatsMonitoringDoc)it.next(); final CcrStatsAction.StatsResponse status = statuses.get(index); - assertThat(document.getCluster(), is(clusterUuid)); - assertThat(document.getTimestamp(), greaterThan(0L)); - assertThat(document.getIntervalMillis(), equalTo(interval)); - assertThat(document.getNode(), equalTo(node)); - assertThat(document.getSystem(), is(MonitoredSystem.ES)); - assertThat(document.getType(), is(CcrStatsMonitoringDoc.TYPE)); - assertThat(document.getId(), nullValue()); - assertThat(document.status(), is(status.status())); + Assert.assertThat(document.getCluster(), is(clusterUuid)); + Assert.assertThat(document.getTimestamp(), greaterThan(0L)); + Assert.assertThat(document.getIntervalMillis(), equalTo(interval)); + Assert.assertThat(document.getNode(), equalTo(node)); + Assert.assertThat(document.getSystem(), is(MonitoredSystem.ES)); + Assert.assertThat(document.getType(), Matchers.is(CcrStatsMonitoringDoc.TYPE)); + Assert.assertThat(document.getId(), nullValue()); + Assert.assertThat(document.status(), is(status.status())); } } private List mockStatuses() { - final int count = randomIntBetween(1, 8); + final int count = ESTestCase.randomIntBetween(1, 8); final List statuses = new ArrayList<>(count); for (int i = 0; i < count; ++i) { @@ -188,7 +192,7 @@ private List mockStatuses() { private Settings ccrEnabledSettings() { // since it's the default, we want to ensure we test both with/without it - return randomBoolean() ? Settings.EMPTY : Settings.builder().put(XPackSettings.CCR_ENABLED_SETTING.getKey(), true).build(); + return ESTestCase.randomBoolean() ? Settings.EMPTY : Settings.builder().put(XPackSettings.CCR_ENABLED_SETTING.getKey(), true).build(); } private Settings ccrDisabledSettings() { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsMonitoringDocTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsMonitoringDocTests.java similarity index 75% rename from x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsMonitoringDocTests.java rename to x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsMonitoringDocTests.java index 9124e1d524595..e59cb00025907 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsMonitoringDocTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsMonitoringDocTests.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.monitoring.collector.ccr; +import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; @@ -14,13 +15,16 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase; +import org.hamcrest.Matchers; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; +import org.junit.Assert; import org.junit.Before; import java.io.IOException; @@ -52,8 +56,8 @@ public void setUp() throws Exception { public void testConstructorStatusMustNotBeNull() { final NullPointerException e = - expectThrows(NullPointerException.class, () -> new CcrStatsMonitoringDoc(cluster, timestamp, interval, node, null)); - assertThat(e, hasToString(containsString("status"))); + LuceneTestCase.expectThrows(NullPointerException.class, () -> new CcrStatsMonitoringDoc(cluster, timestamp, interval, node, null)); + Assert.assertThat(e, hasToString(containsString("status"))); } @Override @@ -70,10 +74,10 @@ protected CcrStatsMonitoringDoc createMonitoringDoc( @Override protected void assertMonitoringDoc(CcrStatsMonitoringDoc document) { - assertThat(document.getSystem(), is(MonitoredSystem.ES)); - assertThat(document.getType(), is(CcrStatsMonitoringDoc.TYPE)); - assertThat(document.getId(), nullValue()); - assertThat(document.status(), is(status)); + Assert.assertThat(document.getSystem(), is(MonitoredSystem.ES)); + Assert.assertThat(document.getType(), Matchers.is(CcrStatsMonitoringDoc.TYPE)); + Assert.assertThat(document.getId(), nullValue()); + Assert.assertThat(document.status(), is(status)); } @Override @@ -83,30 +87,30 @@ public void testToXContent() throws IOException { final long nodeTimestamp = System.currentTimeMillis(); final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", nodeTimestamp); // these random values do not need to be internally consistent, they are only for testing formatting - final int shardId = randomIntBetween(0, Integer.MAX_VALUE); - final long leaderGlobalCheckpoint = randomNonNegativeLong(); - final long leaderMaxSeqNo = randomNonNegativeLong(); - final long followerGlobalCheckpoint = randomNonNegativeLong(); - final long followerMaxSeqNo = randomNonNegativeLong(); - final long lastRequestedSeqNo = randomNonNegativeLong(); - final int numberOfConcurrentReads = randomIntBetween(1, Integer.MAX_VALUE); - final int numberOfConcurrentWrites = randomIntBetween(1, Integer.MAX_VALUE); - final int numberOfQueuedWrites = randomIntBetween(0, Integer.MAX_VALUE); - final long mappingVersion = randomIntBetween(0, Integer.MAX_VALUE); - final long totalFetchTimeMillis = randomLongBetween(0, 4096); - final long numberOfSuccessfulFetches = randomNonNegativeLong(); - final long numberOfFailedFetches = randomLongBetween(0, 8); - final long operationsReceived = randomNonNegativeLong(); - final long totalTransferredBytes = randomNonNegativeLong(); - final long totalIndexTimeMillis = randomNonNegativeLong(); - final long numberOfSuccessfulBulkOperations = randomNonNegativeLong(); - final long numberOfFailedBulkOperations = randomNonNegativeLong(); - final long numberOfOperationsIndexed = randomNonNegativeLong(); + final int shardId = ESTestCase.randomIntBetween(0, Integer.MAX_VALUE); + final long leaderGlobalCheckpoint = ESTestCase.randomNonNegativeLong(); + final long leaderMaxSeqNo = ESTestCase.randomNonNegativeLong(); + final long followerGlobalCheckpoint = ESTestCase.randomNonNegativeLong(); + final long followerMaxSeqNo = ESTestCase.randomNonNegativeLong(); + final long lastRequestedSeqNo = ESTestCase.randomNonNegativeLong(); + final int numberOfConcurrentReads = ESTestCase.randomIntBetween(1, Integer.MAX_VALUE); + final int numberOfConcurrentWrites = ESTestCase.randomIntBetween(1, Integer.MAX_VALUE); + final int numberOfQueuedWrites = ESTestCase.randomIntBetween(0, Integer.MAX_VALUE); + final long mappingVersion = ESTestCase.randomIntBetween(0, Integer.MAX_VALUE); + final long totalFetchTimeMillis = ESTestCase.randomLongBetween(0, 4096); + final long numberOfSuccessfulFetches = ESTestCase.randomNonNegativeLong(); + final long numberOfFailedFetches = ESTestCase.randomLongBetween(0, 8); + final long operationsReceived = ESTestCase.randomNonNegativeLong(); + final long totalTransferredBytes = ESTestCase.randomNonNegativeLong(); + final long totalIndexTimeMillis = ESTestCase.randomNonNegativeLong(); + final long numberOfSuccessfulBulkOperations = ESTestCase.randomNonNegativeLong(); + final long numberOfFailedBulkOperations = ESTestCase.randomNonNegativeLong(); + final long numberOfOperationsIndexed = ESTestCase.randomNonNegativeLong(); final NavigableMap> fetchExceptions = new TreeMap<>(Collections.singletonMap( - randomNonNegativeLong(), - Tuple.tuple(randomIntBetween(0, Integer.MAX_VALUE), new ElasticsearchException("shard is sad")))); - final long timeSinceLastFetchMillis = randomNonNegativeLong(); + ESTestCase.randomNonNegativeLong(), + Tuple.tuple(ESTestCase.randomIntBetween(0, Integer.MAX_VALUE), new ElasticsearchException("shard is sad")))); + final long timeSinceLastFetchMillis = ESTestCase.randomNonNegativeLong(); final ShardFollowNodeTaskStatus status = new ShardFollowNodeTaskStatus( "cluster_alias:leader_index", "follower_index", @@ -133,7 +137,7 @@ public void testToXContent() throws IOException { timeSinceLastFetchMillis); final CcrStatsMonitoringDoc document = new CcrStatsMonitoringDoc("_cluster", timestamp, intervalMillis, node, status); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - assertThat( + Assert.assertThat( xContent.utf8ToString(), equalTo( "{" @@ -221,29 +225,29 @@ public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { XContentHelper.convertToMap(XContentType.JSON.xContent(), MonitoringTemplateUtils.loadTemplate("es"), false); Map ccrStatsMapping = (Map) XContentMapValues.extractValue("mappings.doc.properties.ccr_stats.properties", template); - assertThat(serializedStatus.size(), equalTo(ccrStatsMapping.size())); + Assert.assertThat(serializedStatus.size(), equalTo(ccrStatsMapping.size())); for (Map.Entry entry : serializedStatus.entrySet()) { String fieldName = entry.getKey(); Map fieldMapping = (Map) ccrStatsMapping.get(fieldName); - assertThat(fieldMapping, notNullValue()); + Assert.assertThat(fieldMapping, notNullValue()); Object fieldValue = entry.getValue(); String fieldType = (String) fieldMapping.get("type"); if (fieldValue instanceof Long || fieldValue instanceof Integer) { - assertThat("expected long field type for field [" + fieldName + "]", fieldType, + Assert.assertThat("expected long field type for field [" + fieldName + "]", fieldType, anyOf(equalTo("long"), equalTo("integer"))); } else if (fieldValue instanceof String) { - assertThat("expected keyword field type for field [" + fieldName + "]", fieldType, + Assert.assertThat("expected keyword field type for field [" + fieldName + "]", fieldType, anyOf(equalTo("keyword"), equalTo("text"))); } else { // Manual test specific object fields and if not just fail: if (fieldName.equals("fetch_exceptions")) { - assertThat(((Map) fieldMapping.get("properties")).size(), equalTo(3)); - assertThat(XContentMapValues.extractValue("properties.from_seq_no.type", fieldMapping), equalTo("long")); - assertThat(XContentMapValues.extractValue("properties.retries.type", fieldMapping), equalTo("integer")); - assertThat(XContentMapValues.extractValue("properties.exception.type", fieldMapping), equalTo("text")); + Assert.assertThat(((Map) fieldMapping.get("properties")).size(), equalTo(3)); + Assert.assertThat(XContentMapValues.extractValue("properties.from_seq_no.type", fieldMapping), equalTo("long")); + Assert.assertThat(XContentMapValues.extractValue("properties.retries.type", fieldMapping), equalTo("integer")); + Assert.assertThat(XContentMapValues.extractValue("properties.exception.type", fieldMapping), equalTo("text")); } else { - fail("unexpected field value type [" + fieldValue.getClass() + "] for field [" + fieldName + "]"); + Assert.fail("unexpected field value type [" + fieldValue.getClass() + "] for field [" + fieldName + "]"); } } } From 453d6e68c80aa44c7e4ce5f905f9dcaf4ca7725f Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 14 Sep 2018 22:04:00 -0400 Subject: [PATCH 2/3] Revert IDE changes --- .../collector/ccr/CcrStatsCollectorTests.java | 69 ++++++++-------- .../ccr/CcrStatsMonitoringDocTests.java | 80 +++++++++---------- 2 files changed, 71 insertions(+), 78 deletions(-) diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java index 61e38d89a9337..0502248f6e71c 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java @@ -6,13 +6,11 @@ package org.elasticsearch.xpack.monitoring.collector.ccr; -import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.action.ActionFuture; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction; @@ -20,8 +18,6 @@ import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.BaseCollectorTestCase; -import org.elasticsearch.xpack.monitoring.MonitoringTestUtils; -import org.hamcrest.Matchers; import org.junit.Assert; import org.mockito.ArgumentMatcher; @@ -32,6 +28,7 @@ import java.util.List; import static java.util.Collections.emptyList; +import static org.elasticsearch.xpack.monitoring.MonitoringTestUtils.randomMonitoringNode; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.hasSize; @@ -45,9 +42,9 @@ public class CcrStatsCollectorTests extends BaseCollectorTestCase { public void testShouldCollectReturnsFalseIfMonitoringNotAllowed() { - final Settings settings = ESTestCase.randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); - final boolean ccrAllowed = ESTestCase.randomBoolean(); - final boolean isElectedMaster = ESTestCase.randomBoolean(); + final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); + final boolean ccrAllowed = randomBoolean(); + final boolean isElectedMaster = randomBoolean(); whenLocalNodeElectedMaster(isElectedMaster); // this controls the blockage @@ -56,7 +53,7 @@ public void testShouldCollectReturnsFalseIfMonitoringNotAllowed() { final CcrStatsCollector collector = new CcrStatsCollector(settings, clusterService, licenseState, client); - Assert.assertThat(collector.shouldCollect(isElectedMaster), is(false)); + assertThat(collector.shouldCollect(isElectedMaster), is(false)); if (isElectedMaster) { verify(licenseState).isMonitoringAllowed(); } @@ -64,31 +61,31 @@ public void testShouldCollectReturnsFalseIfMonitoringNotAllowed() { public void testShouldCollectReturnsFalseIfNotMaster() { // regardless of CCR being enabled - final Settings settings = ESTestCase.randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); + final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); - when(licenseState.isMonitoringAllowed()).thenReturn(ESTestCase.randomBoolean()); - when(licenseState.isCcrAllowed()).thenReturn(ESTestCase.randomBoolean()); + when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean()); + when(licenseState.isCcrAllowed()).thenReturn(randomBoolean()); // this controls the blockage final boolean isElectedMaster = false; final CcrStatsCollector collector = new CcrStatsCollector(settings, clusterService, licenseState, client); - Assert.assertThat(collector.shouldCollect(isElectedMaster), is(false)); + assertThat(collector.shouldCollect(isElectedMaster), is(false)); } public void testShouldCollectReturnsFalseIfCCRIsDisabled() { // this is controls the blockage final Settings settings = ccrDisabledSettings(); - when(licenseState.isMonitoringAllowed()).thenReturn(ESTestCase.randomBoolean()); - when(licenseState.isCcrAllowed()).thenReturn(ESTestCase.randomBoolean()); + when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean()); + when(licenseState.isCcrAllowed()).thenReturn(randomBoolean()); - final boolean isElectedMaster = ESTestCase.randomBoolean(); + final boolean isElectedMaster = randomBoolean(); whenLocalNodeElectedMaster(isElectedMaster); final CcrStatsCollector collector = new CcrStatsCollector(settings, clusterService, licenseState, client); - Assert.assertThat(collector.shouldCollect(isElectedMaster), is(false)); + assertThat(collector.shouldCollect(isElectedMaster), is(false)); if (isElectedMaster) { verify(licenseState).isMonitoringAllowed(); @@ -96,17 +93,17 @@ public void testShouldCollectReturnsFalseIfCCRIsDisabled() { } public void testShouldCollectReturnsFalseIfCCRIsNotAllowed() { - final Settings settings = ESTestCase.randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); + final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); - when(licenseState.isMonitoringAllowed()).thenReturn(ESTestCase.randomBoolean()); + when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean()); // this is controls the blockage when(licenseState.isCcrAllowed()).thenReturn(false); - final boolean isElectedMaster = ESTestCase.randomBoolean(); + final boolean isElectedMaster = randomBoolean(); whenLocalNodeElectedMaster(isElectedMaster); final CcrStatsCollector collector = new CcrStatsCollector(settings, clusterService, licenseState, client); - Assert.assertThat(collector.shouldCollect(isElectedMaster), is(false)); + assertThat(collector.shouldCollect(isElectedMaster), is(false)); if (isElectedMaster) { verify(licenseState).isMonitoringAllowed(); @@ -122,20 +119,20 @@ public void testShouldCollectReturnsTrue() { final CcrStatsCollector collector = new CcrStatsCollector(settings, clusterService, licenseState, client); - Assert.assertThat(collector.shouldCollect(isElectedMaster), is(true)); + assertThat(collector.shouldCollect(isElectedMaster), is(true)); verify(licenseState).isMonitoringAllowed(); } public void testDoCollect() throws Exception { - final String clusterUuid = ESTestCase.randomAlphaOfLength(5); + final String clusterUuid = randomAlphaOfLength(5); whenClusterStateWithUUID(clusterUuid); - final MonitoringDoc.Node node = MonitoringTestUtils.randomMonitoringNode(LuceneTestCase.random()); + final MonitoringDoc.Node node = randomMonitoringNode(random()); final CcrClient client = mock(CcrClient.class); final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); - final TimeValue timeout = TimeValue.timeValueSeconds(ESTestCase.randomIntBetween(1, 120)); + final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(1, 120)); withCollectionTimeout(CcrStatsCollector.CCR_STATS_TIMEOUT, timeout); final CcrStatsCollector collector = new CcrStatsCollector(Settings.EMPTY, clusterService, licenseState, client, threadContext); @@ -152,32 +149,32 @@ public void testDoCollect() throws Exception { when(client.stats(statsRequestEq(request))).thenReturn(future); when(future.actionGet(timeout)).thenReturn(responses); - final long interval = ESTestCase.randomNonNegativeLong(); + final long interval = randomNonNegativeLong(); final Collection documents = collector.doCollect(node, interval, clusterState); verify(clusterState).metaData(); verify(metaData).clusterUUID(); - Assert.assertThat(documents, hasSize(statuses.size())); + assertThat(documents, hasSize(statuses.size())); int index = 0; for (final Iterator it = documents.iterator(); it.hasNext(); index++) { final CcrStatsMonitoringDoc document = (CcrStatsMonitoringDoc)it.next(); final CcrStatsAction.StatsResponse status = statuses.get(index); - Assert.assertThat(document.getCluster(), is(clusterUuid)); - Assert.assertThat(document.getTimestamp(), greaterThan(0L)); - Assert.assertThat(document.getIntervalMillis(), equalTo(interval)); - Assert.assertThat(document.getNode(), equalTo(node)); - Assert.assertThat(document.getSystem(), is(MonitoredSystem.ES)); - Assert.assertThat(document.getType(), Matchers.is(CcrStatsMonitoringDoc.TYPE)); - Assert.assertThat(document.getId(), nullValue()); - Assert.assertThat(document.status(), is(status.status())); + assertThat(document.getCluster(), is(clusterUuid)); + assertThat(document.getTimestamp(), greaterThan(0L)); + assertThat(document.getIntervalMillis(), equalTo(interval)); + assertThat(document.getNode(), equalTo(node)); + assertThat(document.getSystem(), is(MonitoredSystem.ES)); + assertThat(document.getType(), is(CcrStatsMonitoringDoc.TYPE)); + assertThat(document.getId(), nullValue()); + assertThat(document.status(), is(status.status())); } } private List mockStatuses() { - final int count = ESTestCase.randomIntBetween(1, 8); + final int count = randomIntBetween(1, 8); final List statuses = new ArrayList<>(count); for (int i = 0; i < count; ++i) { @@ -192,7 +189,7 @@ private List mockStatuses() { private Settings ccrEnabledSettings() { // since it's the default, we want to ensure we test both with/without it - return ESTestCase.randomBoolean() ? Settings.EMPTY : Settings.builder().put(XPackSettings.CCR_ENABLED_SETTING.getKey(), true).build(); + return randomBoolean() ? Settings.EMPTY : Settings.builder().put(XPackSettings.CCR_ENABLED_SETTING.getKey(), true).build(); } private Settings ccrDisabledSettings() { diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsMonitoringDocTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsMonitoringDocTests.java index e59cb00025907..9124e1d524595 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsMonitoringDocTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsMonitoringDocTests.java @@ -6,7 +6,6 @@ package org.elasticsearch.xpack.monitoring.collector.ccr; -import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; @@ -15,16 +14,13 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.support.XContentMapValues; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase; -import org.hamcrest.Matchers; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; -import org.junit.Assert; import org.junit.Before; import java.io.IOException; @@ -56,8 +52,8 @@ public void setUp() throws Exception { public void testConstructorStatusMustNotBeNull() { final NullPointerException e = - LuceneTestCase.expectThrows(NullPointerException.class, () -> new CcrStatsMonitoringDoc(cluster, timestamp, interval, node, null)); - Assert.assertThat(e, hasToString(containsString("status"))); + expectThrows(NullPointerException.class, () -> new CcrStatsMonitoringDoc(cluster, timestamp, interval, node, null)); + assertThat(e, hasToString(containsString("status"))); } @Override @@ -74,10 +70,10 @@ protected CcrStatsMonitoringDoc createMonitoringDoc( @Override protected void assertMonitoringDoc(CcrStatsMonitoringDoc document) { - Assert.assertThat(document.getSystem(), is(MonitoredSystem.ES)); - Assert.assertThat(document.getType(), Matchers.is(CcrStatsMonitoringDoc.TYPE)); - Assert.assertThat(document.getId(), nullValue()); - Assert.assertThat(document.status(), is(status)); + assertThat(document.getSystem(), is(MonitoredSystem.ES)); + assertThat(document.getType(), is(CcrStatsMonitoringDoc.TYPE)); + assertThat(document.getId(), nullValue()); + assertThat(document.status(), is(status)); } @Override @@ -87,30 +83,30 @@ public void testToXContent() throws IOException { final long nodeTimestamp = System.currentTimeMillis(); final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", nodeTimestamp); // these random values do not need to be internally consistent, they are only for testing formatting - final int shardId = ESTestCase.randomIntBetween(0, Integer.MAX_VALUE); - final long leaderGlobalCheckpoint = ESTestCase.randomNonNegativeLong(); - final long leaderMaxSeqNo = ESTestCase.randomNonNegativeLong(); - final long followerGlobalCheckpoint = ESTestCase.randomNonNegativeLong(); - final long followerMaxSeqNo = ESTestCase.randomNonNegativeLong(); - final long lastRequestedSeqNo = ESTestCase.randomNonNegativeLong(); - final int numberOfConcurrentReads = ESTestCase.randomIntBetween(1, Integer.MAX_VALUE); - final int numberOfConcurrentWrites = ESTestCase.randomIntBetween(1, Integer.MAX_VALUE); - final int numberOfQueuedWrites = ESTestCase.randomIntBetween(0, Integer.MAX_VALUE); - final long mappingVersion = ESTestCase.randomIntBetween(0, Integer.MAX_VALUE); - final long totalFetchTimeMillis = ESTestCase.randomLongBetween(0, 4096); - final long numberOfSuccessfulFetches = ESTestCase.randomNonNegativeLong(); - final long numberOfFailedFetches = ESTestCase.randomLongBetween(0, 8); - final long operationsReceived = ESTestCase.randomNonNegativeLong(); - final long totalTransferredBytes = ESTestCase.randomNonNegativeLong(); - final long totalIndexTimeMillis = ESTestCase.randomNonNegativeLong(); - final long numberOfSuccessfulBulkOperations = ESTestCase.randomNonNegativeLong(); - final long numberOfFailedBulkOperations = ESTestCase.randomNonNegativeLong(); - final long numberOfOperationsIndexed = ESTestCase.randomNonNegativeLong(); + final int shardId = randomIntBetween(0, Integer.MAX_VALUE); + final long leaderGlobalCheckpoint = randomNonNegativeLong(); + final long leaderMaxSeqNo = randomNonNegativeLong(); + final long followerGlobalCheckpoint = randomNonNegativeLong(); + final long followerMaxSeqNo = randomNonNegativeLong(); + final long lastRequestedSeqNo = randomNonNegativeLong(); + final int numberOfConcurrentReads = randomIntBetween(1, Integer.MAX_VALUE); + final int numberOfConcurrentWrites = randomIntBetween(1, Integer.MAX_VALUE); + final int numberOfQueuedWrites = randomIntBetween(0, Integer.MAX_VALUE); + final long mappingVersion = randomIntBetween(0, Integer.MAX_VALUE); + final long totalFetchTimeMillis = randomLongBetween(0, 4096); + final long numberOfSuccessfulFetches = randomNonNegativeLong(); + final long numberOfFailedFetches = randomLongBetween(0, 8); + final long operationsReceived = randomNonNegativeLong(); + final long totalTransferredBytes = randomNonNegativeLong(); + final long totalIndexTimeMillis = randomNonNegativeLong(); + final long numberOfSuccessfulBulkOperations = randomNonNegativeLong(); + final long numberOfFailedBulkOperations = randomNonNegativeLong(); + final long numberOfOperationsIndexed = randomNonNegativeLong(); final NavigableMap> fetchExceptions = new TreeMap<>(Collections.singletonMap( - ESTestCase.randomNonNegativeLong(), - Tuple.tuple(ESTestCase.randomIntBetween(0, Integer.MAX_VALUE), new ElasticsearchException("shard is sad")))); - final long timeSinceLastFetchMillis = ESTestCase.randomNonNegativeLong(); + randomNonNegativeLong(), + Tuple.tuple(randomIntBetween(0, Integer.MAX_VALUE), new ElasticsearchException("shard is sad")))); + final long timeSinceLastFetchMillis = randomNonNegativeLong(); final ShardFollowNodeTaskStatus status = new ShardFollowNodeTaskStatus( "cluster_alias:leader_index", "follower_index", @@ -137,7 +133,7 @@ public void testToXContent() throws IOException { timeSinceLastFetchMillis); final CcrStatsMonitoringDoc document = new CcrStatsMonitoringDoc("_cluster", timestamp, intervalMillis, node, status); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - Assert.assertThat( + assertThat( xContent.utf8ToString(), equalTo( "{" @@ -225,29 +221,29 @@ public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { XContentHelper.convertToMap(XContentType.JSON.xContent(), MonitoringTemplateUtils.loadTemplate("es"), false); Map ccrStatsMapping = (Map) XContentMapValues.extractValue("mappings.doc.properties.ccr_stats.properties", template); - Assert.assertThat(serializedStatus.size(), equalTo(ccrStatsMapping.size())); + assertThat(serializedStatus.size(), equalTo(ccrStatsMapping.size())); for (Map.Entry entry : serializedStatus.entrySet()) { String fieldName = entry.getKey(); Map fieldMapping = (Map) ccrStatsMapping.get(fieldName); - Assert.assertThat(fieldMapping, notNullValue()); + assertThat(fieldMapping, notNullValue()); Object fieldValue = entry.getValue(); String fieldType = (String) fieldMapping.get("type"); if (fieldValue instanceof Long || fieldValue instanceof Integer) { - Assert.assertThat("expected long field type for field [" + fieldName + "]", fieldType, + assertThat("expected long field type for field [" + fieldName + "]", fieldType, anyOf(equalTo("long"), equalTo("integer"))); } else if (fieldValue instanceof String) { - Assert.assertThat("expected keyword field type for field [" + fieldName + "]", fieldType, + assertThat("expected keyword field type for field [" + fieldName + "]", fieldType, anyOf(equalTo("keyword"), equalTo("text"))); } else { // Manual test specific object fields and if not just fail: if (fieldName.equals("fetch_exceptions")) { - Assert.assertThat(((Map) fieldMapping.get("properties")).size(), equalTo(3)); - Assert.assertThat(XContentMapValues.extractValue("properties.from_seq_no.type", fieldMapping), equalTo("long")); - Assert.assertThat(XContentMapValues.extractValue("properties.retries.type", fieldMapping), equalTo("integer")); - Assert.assertThat(XContentMapValues.extractValue("properties.exception.type", fieldMapping), equalTo("text")); + assertThat(((Map) fieldMapping.get("properties")).size(), equalTo(3)); + assertThat(XContentMapValues.extractValue("properties.from_seq_no.type", fieldMapping), equalTo("long")); + assertThat(XContentMapValues.extractValue("properties.retries.type", fieldMapping), equalTo("integer")); + assertThat(XContentMapValues.extractValue("properties.exception.type", fieldMapping), equalTo("text")); } else { - Assert.fail("unexpected field value type [" + fieldValue.getClass() + "] for field [" + fieldName + "]"); + fail("unexpected field value type [" + fieldValue.getClass() + "] for field [" + fieldName + "]"); } } } From d742c2b8c4b2ae4ea365f953a7ade3340acb059b Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 14 Sep 2018 22:04:39 -0400 Subject: [PATCH 3/3] Remove one more formatting change from IDE --- .../xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java index 0502248f6e71c..aaf3a61643b5e 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollectorTests.java @@ -18,7 +18,6 @@ import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.BaseCollectorTestCase; -import org.junit.Assert; import org.mockito.ArgumentMatcher; import java.util.ArrayList; @@ -136,7 +135,7 @@ public void testDoCollect() throws Exception { withCollectionTimeout(CcrStatsCollector.CCR_STATS_TIMEOUT, timeout); final CcrStatsCollector collector = new CcrStatsCollector(Settings.EMPTY, clusterService, licenseState, client, threadContext); - Assert.assertEquals(timeout, collector.getCollectionTimeout()); + assertEquals(timeout, collector.getCollectionTimeout()); final List statuses = mockStatuses();