test: replaced try-catch statements with expectThrows(...)

This commit is contained in:
Martijn van Groningen 2018-01-05 14:29:37 +01:00
parent 7180e539de
commit fdb9b50747
No known key found for this signature in database
GPG Key ID: AB236F4FCF2AF12A
2 changed files with 21 additions and 46 deletions

View File

@ -175,13 +175,9 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
asnOnlyProperties.remove(GeoIpProcessor.Property.IP); asnOnlyProperties.remove(GeoIpProcessor.Property.IP);
String asnProperty = RandomPicks.randomFrom(Randomness.get(), asnOnlyProperties).toString(); String asnProperty = RandomPicks.randomFrom(Randomness.get(), asnOnlyProperties).toString();
config.put("properties", Collections.singletonList(asnProperty)); config.put("properties", Collections.singletonList(asnProperty));
try { Exception e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, config));
factory.create(null, null, config); assertThat(e.getMessage(), equalTo("[properties] illegal property value [" + asnProperty +
fail("Exception expected"); "]. valid values are [IP, COUNTRY_ISO_CODE, COUNTRY_NAME, CONTINENT_NAME]"));
} catch (ElasticsearchParseException e) {
assertThat(e.getMessage(), equalTo("[properties] illegal property value [" + asnProperty +
"]. valid values are [IP, COUNTRY_ISO_CODE, COUNTRY_NAME, CONTINENT_NAME]"));
}
} }
public void testBuildWithAsnDbAndCityFields() throws Exception { public void testBuildWithAsnDbAndCityFields() throws Exception {
@ -193,13 +189,9 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
cityOnlyProperties.remove(GeoIpProcessor.Property.IP); cityOnlyProperties.remove(GeoIpProcessor.Property.IP);
String cityProperty = RandomPicks.randomFrom(Randomness.get(), cityOnlyProperties).toString(); String cityProperty = RandomPicks.randomFrom(Randomness.get(), cityOnlyProperties).toString();
config.put("properties", Collections.singletonList(cityProperty)); config.put("properties", Collections.singletonList(cityProperty));
try { Exception e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, config));
factory.create(null, null, config); assertThat(e.getMessage(), equalTo("[properties] illegal property value [" + cityProperty +
fail("Exception expected"); "]. valid values are [IP, ASN, ORGANIZATION_NAME]"));
} catch (ElasticsearchParseException e) {
assertThat(e.getMessage(), equalTo("[properties] illegal property value [" + cityProperty +
"]. valid values are [IP, ASN, ORGANIZATION_NAME]"));
}
} }
public void testBuildNonExistingDbFile() throws Exception { public void testBuildNonExistingDbFile() throws Exception {
@ -208,12 +200,8 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
Map<String, Object> config = new HashMap<>(); Map<String, Object> config = new HashMap<>();
config.put("field", "_field"); config.put("field", "_field");
config.put("database_file", "does-not-exist.mmdb.gz"); config.put("database_file", "does-not-exist.mmdb.gz");
try { Exception e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, config));
factory.create(null, null, config); assertThat(e.getMessage(), equalTo("[database_file] database file [does-not-exist.mmdb.gz] doesn't exist"));
fail("Exception expected");
} catch (ElasticsearchParseException e) {
assertThat(e.getMessage(), equalTo("[database_file] database file [does-not-exist.mmdb.gz] doesn't exist"));
}
} }
public void testBuildFields() throws Exception { public void testBuildFields() throws Exception {
@ -239,26 +227,18 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
public void testBuildIllegalFieldOption() throws Exception { public void testBuildIllegalFieldOption() throws Exception {
GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders); GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
Map<String, Object> config = new HashMap<>(); Map<String, Object> config1 = new HashMap<>();
config.put("field", "_field"); config1.put("field", "_field");
config.put("properties", Collections.singletonList("invalid")); config1.put("properties", Collections.singletonList("invalid"));
try { Exception e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, config1));
factory.create(null, null, config); assertThat(e.getMessage(), equalTo("[properties] illegal property value [invalid]. valid values are [IP, COUNTRY_ISO_CODE, " +
fail("exception expected"); "COUNTRY_NAME, CONTINENT_NAME, REGION_NAME, CITY_NAME, TIMEZONE, LOCATION]"));
} catch (ElasticsearchParseException e) {
assertThat(e.getMessage(), equalTo("[properties] illegal property value [invalid]. valid values are [IP, COUNTRY_ISO_CODE, " +
"COUNTRY_NAME, CONTINENT_NAME, REGION_NAME, CITY_NAME, TIMEZONE, LOCATION]"));
}
config = new HashMap<>(); Map<String, Object> config2 = new HashMap<>();
config.put("field", "_field"); config2.put("field", "_field");
config.put("properties", "invalid"); config2.put("properties", "invalid");
try { e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, config2));
factory.create(null, null, config); assertThat(e.getMessage(), equalTo("[properties] property isn't a list, but of type [java.lang.String]"));
fail("exception expected");
} catch (ElasticsearchParseException e) {
assertThat(e.getMessage(), equalTo("[properties] property isn't a list, but of type [java.lang.String]"));
}
} }
public void testLazyLoading() throws Exception { public void testLazyLoading() throws Exception {

View File

@ -228,13 +228,8 @@ public class GeoIpProcessorTests extends ESTestCase {
Map<String, Object> document = new HashMap<>(); Map<String, Object> document = new HashMap<>();
document.put("source_field", "www.google.com"); document.put("source_field", "www.google.com");
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
try { Exception e = expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument));
processor.execute(ingestDocument); assertThat(e.getMessage(), containsString("not an IP string literal"));
fail("did not get expected exception");
} catch (IllegalArgumentException expected) {
assertNotNull(expected.getMessage());
assertThat(expected.getMessage(), containsString("not an IP string literal"));
}
} }
private static InputStream getDatabaseFileInputStream(String path) throws IOException { private static InputStream getDatabaseFileInputStream(String path) throws IOException {