From ac03d4f39801f713e74a24b23a6b53ed27114270 Mon Sep 17 00:00:00 2001 From: Ryan Stewart Date: Fri, 1 Jul 2022 16:40:23 -0500 Subject: [PATCH] improve existing Elasticsearch IT Some tests don't actually assert anything. Some aren't annotated as tests. There's also a branch of code, related to wildcards, that isn't covered by any existing test. This cleans those things up so that this integration test is stricter than before. --- .../blueflood/io/BaseElasticTest.java | 18 ---- .../io/ElasticIOIntegrationTest.java | 93 +++++++++++-------- 2 files changed, 53 insertions(+), 58 deletions(-) diff --git a/blueflood-integration-tests/src/integration-test/java/com/rackspacecloud/blueflood/io/BaseElasticTest.java b/blueflood-integration-tests/src/integration-test/java/com/rackspacecloud/blueflood/io/BaseElasticTest.java index 66815ca3d..829e804fe 100644 --- a/blueflood-integration-tests/src/integration-test/java/com/rackspacecloud/blueflood/io/BaseElasticTest.java +++ b/blueflood-integration-tests/src/integration-test/java/com/rackspacecloud/blueflood/io/BaseElasticTest.java @@ -107,22 +107,4 @@ protected static List createTestMetricsFromInterface(String tenantId) { } return metrics; } - - protected void verifyResults(List results, Set expectedResults) { - Set formattedResults = formatForComparision(results); - assertTrue("Expected results does not contain all of api results", expectedResults.containsAll(formattedResults)); - assertTrue("API results does not contain all of expected results", formattedResults.containsAll(expectedResults)); - Assert.assertEquals("Invalid total number of results", expectedResults.size(), results.size()); - } - - protected Set formatForComparision(List results) { - final String DELIMITER = "|"; - - Set formattedResults = new HashSet(); - for (MetricName metricName : results) { - formattedResults.add(metricName.getName() + DELIMITER + metricName.isCompleteName()); - } - - return formattedResults; - } } diff --git a/blueflood-integration-tests/src/integration-test/java/com/rackspacecloud/blueflood/io/ElasticIOIntegrationTest.java b/blueflood-integration-tests/src/integration-test/java/com/rackspacecloud/blueflood/io/ElasticIOIntegrationTest.java index beab8e5f1..702c4773a 100644 --- a/blueflood-integration-tests/src/integration-test/java/com/rackspacecloud/blueflood/io/ElasticIOIntegrationTest.java +++ b/blueflood-integration-tests/src/integration-test/java/com/rackspacecloud/blueflood/io/ElasticIOIntegrationTest.java @@ -45,6 +45,8 @@ import java.util.stream.Stream; import static java.util.stream.Collectors.toList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasItem; import static org.junit.Assert.assertEquals; @RunWith( JUnitParamsRunner.class ) @@ -230,8 +232,8 @@ public void testBatchQueryWithNoWildCards() throws Exception { queries.add(query2); results = elasticIO.search(tenantId, queries); assertEquals(results.size(), 2); //we searched for 2 unique metrics - results.contains(new SearchResult(TENANT_A, query1, UNIT)); - results.contains(new SearchResult(TENANT_A, query2, UNIT)); + assertThat(results, hasItem(new SearchResult(TENANT_A, query1, UNIT))); + assertThat(results, hasItem(new SearchResult(TENANT_A, query2, UNIT))); } @Test @@ -261,6 +263,8 @@ public void testBatchQueryWithWildCards2() throws Exception { assertEquals(results.size(), 2); } + @Test + @Parameters({"ratanasv, horse length", "someotherguy, horse length", "someothergal, horse length"}) public void testWildcard(String tenantId, String unit) throws Exception { SearchResult entry; List results; @@ -269,7 +273,7 @@ public void testWildcard(String tenantId, String unit) throws Exception { assertEquals(locators.size(), results.size()); for (Locator locator : locators) { entry = new SearchResult(tenantId, locator.getMetricName(), unit); - Assert.assertTrue((results.contains(entry))); + assertThat(results, hasItem(entry)); } results = elasticIO.search(tenantId, "*.fourA.*"); @@ -277,7 +281,7 @@ public void testWildcard(String tenantId, String unit) throws Exception { for (int x = 0; x < NUM_PARENT_ELEMENTS; x++) { for (int z = 0; z < NUM_GRANDCHILD_ELEMENTS; z++) { entry = createExpectedResult(tenantId, x, "A", z, unit); - Assert.assertTrue(results.contains(entry)); + assertThat(results, hasItem(entry)); } } @@ -286,7 +290,7 @@ public void testWildcard(String tenantId, String unit) throws Exception { for (int x = 10; x < 20; x++) { for (String y : CHILD_ELEMENTS) { entry = createExpectedResult(tenantId, x, y, 2, unit); - Assert.assertTrue(results.contains(entry)); + assertThat(results, hasItem(entry)); } } } @@ -295,8 +299,8 @@ public void testWildcard(String tenantId, String unit) throws Exception { public void testGlobMatching() throws Exception { List results = elasticIO.search(TENANT_A, "one.two.{three00,three01}.fourA.five0"); assertEquals(results.size(), 2); - results.contains(new SearchResult(TENANT_A, "one.two.three00.fourA.five0", UNIT)); - results.contains(new SearchResult(TENANT_A, "one.two.three01.fourA.five0", UNIT)); + assertThat(results, hasItem(new SearchResult(TENANT_A, "one.two.three00.fourA.five0", UNIT))); + assertThat(results, hasItem(new SearchResult(TENANT_A, "one.two.three01.fourA.five0", UNIT))); } @Test @@ -332,13 +336,8 @@ public void testDeDupMetrics() throws Exception { // Insert metric into the new index elasticIO.setINDEX_NAME_WRITE(ES_DUP); - List metricList = new ArrayList(); - metricList.add(new Metric(createTestLocator(TENANT_A, 0, "A", 0), 987654321L, 0, new TimeValue(1, TimeUnit.DAYS), UNIT)); - - // Calling insertDiscovery with single metric in loop rather than metrics collection to improve on code coverage. - for(IMetric metric : metricList){ - elasticIO.insertDiscovery(metric); - } + Metric metric = new Metric(createTestLocator(TENANT_A, 0, "A", 0), 987654321L, 0, new TimeValue(1, TimeUnit.DAYS), UNIT); + elasticIO.insertDiscovery(metric); helper.refreshIndex(ES_DUP); // Historically, this line was here, but I don't know why. In init-es.sh, "metric_metadata_read" is an alias for @@ -389,13 +388,9 @@ public void testGetMetricNamesMultipleMetrics(String type) throws Exception { List results = getDiscoveryIO(type).getMetricNames(tenantId, query); - Set expectedResults = new HashSet() {{ - add("one|false"); - add("foo|false"); - }}; - assertEquals("Invalid total number of results", 2, results.size()); - verifyResults(results, expectedResults); + assertThat(results, hasItem(new MetricName("one", false))); + assertThat(results, hasItem(new MetricName("foo", false))); } @Test @@ -423,13 +418,10 @@ public void testGetMetricNamesWithWildCardPrefixMultipleLevels(String type) thro List results = getDiscoveryIO(type).getMetricNames(tenantId, query); - Set expectedResults = new HashSet() {{ - add("one.two|false"); - add("foo.bar|false"); - }}; - + // On failure, see note in testGetMetricNamesWithWildCardPrefixAtTheEnd() assertEquals("Invalid total number of results", 2, results.size()); - verifyResults(results, expectedResults); + assertThat(results, hasItem(new MetricName("one.two", false))); + assertThat(results, hasItem(new MetricName("foo.bar", false))); } @Test @@ -446,6 +438,34 @@ public void testGetMetricNamesWithMultiLevelPrefix(String type) throws Exception } } + @Test + @Parameters({"elasticIO", "elasticTokensIO"}) + public void testGetIncompleteMetricNamesWithGlobPatternAtTheEnd(String type) throws Exception { + String tenantId = TENANT_A; + String query = "one.two.three00.four*"; + + List results = getDiscoveryIO(type).getMetricNames(tenantId, query); + + assertEquals("Invalid total number of results", 3, results.size()); + assertThat(results, hasItem(new MetricName("one.two.three00.fourA", false))); + assertThat(results, hasItem(new MetricName("one.two.three00.fourB", false))); + assertThat(results, hasItem(new MetricName("one.two.three00.fourC", false))); + } + + @Test + @Parameters({"elasticIO", "elasticTokensIO"}) + public void testGetCompleteMetricNamesWithGlobPatternAtTheEnd(String type) throws Exception { + String tenantId = TENANT_A; + String query = "one.two.three00.fourA.five*"; + + List results = getDiscoveryIO(type).getMetricNames(tenantId, query); + + assertEquals("Invalid total number of results", 3, results.size()); + assertThat(results, hasItem(new MetricName("one.two.three00.fourA.five0", true))); + assertThat(results, hasItem(new MetricName("one.two.three00.fourA.five1", true))); + assertThat(results, hasItem(new MetricName("one.two.three00.fourA.five2", true))); + } + @Test @Parameters({"elasticIO", "elasticTokensIO"}) public void testGetMetricNamesWithWildCardPrefixAtTheEnd(String type) throws Exception { @@ -492,15 +512,12 @@ public void testGetMetricNamesWithWildCardAndBracketsPrefix(String type) throws List results = getDiscoveryIO(type).getMetricNames(tenantId, query); - Set expectedResults = new HashSet() {{ - add("one.two.three00.fourA|false"); - add("one.two.three00.fourB|false"); - add("one.two.three00.fourC|false"); - add("one.foo.three00.bar|false"); - }}; - + // On failure, see note in testGetMetricNamesWithWildCardPrefixAtTheEnd() assertEquals("Invalid total number of results", CHILD_ELEMENTS.size() + 1, results.size()); - verifyResults(results, expectedResults); + assertThat(results, hasItem(new MetricName("one.two.three00.fourA", false))); + assertThat(results, hasItem(new MetricName("one.two.three00.fourB", false))); + assertThat(results, hasItem(new MetricName("one.two.three00.fourC", false))); + assertThat(results, hasItem(new MetricName("one.foo.three00.bar", false))); } @Test @@ -529,12 +546,8 @@ public void testGetMetricNamesWithoutWildCard(String type) throws Exception { List results = getDiscoveryIO(type).getMetricNames(tenantId, query); - Set expectedResults = new HashSet() {{ - add("one.foo.three00.bar.baz|true"); - }}; - - assertEquals("Invalid total number of results", expectedResults.size(), results.size()); - verifyResults(results, expectedResults); + assertEquals("Invalid total number of results", 1, results.size()); + assertThat(results, hasItem(new MetricName("one.foo.three00.bar.baz", true))); } @Test