InfluxDbSenderProvider implements Provider<InfluxDbSender> { @Override public InfluxDbSender get() { try { final URI uri = configuration.getUri().parseServerAuthority(); final String protocol = uri.getScheme().toLowerCase(Locale.ENGLISH); final String host = uri.getHost(); final int port = uri.getPort(); final String database = uri.getPath().replace("/", ""); if (!Pattern.matches("[a-z][a-z0-9_]*", database)) { throw new IllegalArgumentException("Invalid database name \"" + database + "\""); } final int socketTimeout = Ints.saturatedCast(configuration.getSocketTimeout().toMilliseconds()); final int connectTimeout = Ints.saturatedCast(configuration.getConnectTimeout().toMilliseconds()); final int readTimeout = Ints.saturatedCast(configuration.getReadTimeout().toMilliseconds()); final TimeUnit timePrecision = configuration.getTimePrecision(); switch (protocol) { case "http": case "https": return new InfluxDbHttpSender( protocol, host, port, database, uri.getUserInfo(), timePrecision, connectTimeout, readTimeout, ""); case "tcp": return new InfluxDbTcpSender( host, port, socketTimeout, database, ""); case "udp": return new InfluxDbUdpSender( host, port, socketTimeout, database, ""); default: throw new IllegalArgumentException("Unsupported protocol \"" + protocol + "\""); } } catch (Exception e) { throw Throwables.propagate(e); } } @Inject  InfluxDbSenderProvider(MetricsInfluxDbReporterConfiguration configuration); @Override InfluxDbSender get();  }
@Test public void getWithInvalidScheme() throws Exception { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Unsupported protocol \"invalid\""); final MetricsInfluxDbReporterConfiguration configuration = new MetricsInfluxDbReporterConfiguration() { @Override public URI getUri() { return URI.create("invalid: } }; final InfluxDbSenderProvider provider = new InfluxDbSenderProvider(configuration); provider.get(); }
@Test public void getCreatesHttpSender() throws Exception { final MetricsInfluxDbReporterConfiguration configuration = new MetricsInfluxDbReporterConfiguration() { @Override public URI getUri() { return URI.create("http: } }; final InfluxDbSenderProvider provider = new InfluxDbSenderProvider(configuration); final InfluxDbSender influxDbSender = provider.get(); assertTrue(influxDbSender instanceof InfluxDbHttpSender); }
@Test public void getCreatesTcpSender() throws Exception { final MetricsInfluxDbReporterConfiguration configuration = new MetricsInfluxDbReporterConfiguration() { @Override public URI getUri() { return URI.create("tcp: } }; final InfluxDbSenderProvider provider = new InfluxDbSenderProvider(configuration); final InfluxDbSender influxDbSender = provider.get(); assertTrue(influxDbSender instanceof InfluxDbTcpSender); }
@Test public void getCreatesUdpSender() throws Exception { final MetricsInfluxDbReporterConfiguration configuration = new MetricsInfluxDbReporterConfiguration() { @Override public URI getUri() { return URI.create("udp: } }; final InfluxDbSenderProvider provider = new InfluxDbSenderProvider(configuration); final InfluxDbSender influxDbSender = provider.get(); assertTrue(influxDbSender instanceof InfluxDbUdpSender); }
@Test public void getCreatesSenderWithCorrectDatabaseName() throws Exception { final MetricsInfluxDbReporterConfiguration configuration = new MetricsInfluxDbReporterConfiguration() { @Override public URI getUri() { return URI.create("udp: } }; final InfluxDbSenderProvider provider = new InfluxDbSenderProvider(configuration); final InfluxDbSender influxDbSender = provider.get(); final Field field = Class.forName("com.izettle.metrics.influxdb.InfluxDbBaseSender").getDeclaredField("influxDbWriteObject"); field.setAccessible(true); final InfluxDbWriteObject influxDbWriteObject = (InfluxDbWriteObject) field.get(influxDbSender); assertEquals("data_base_1", influxDbWriteObject.getDatabase()); }
@Test public void getWithInvalidDatabaseName() throws Exception { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Invalid database name \"1database\""); final MetricsInfluxDbReporterConfiguration configuration = new MetricsInfluxDbReporterConfiguration() { @Override public URI getUri() { return URI.create("udp: } }; final InfluxDbSenderProvider provider = new InfluxDbSenderProvider(configuration); provider.get(); }