TransportProvider implements Provider<Transport> { @Override public Transport get() { switch (configuration.getTransport()) { case HTTP: { final HostAndPort proxy = configuration.getHttpProxy(); final Duration connectTimeout = configuration.getHttpConnectTimeout(); final Duration socketTimeout = configuration.getHttpSocketTimeout(); final HttpTransport.Builder builder = new HttpTransport.Builder() .withApiKey(configuration.getApiKey()) .withConnectTimeout(Ints.saturatedCast(connectTimeout.toMilliseconds())) .withSocketTimeout(Ints.saturatedCast(socketTimeout.toMilliseconds())); if (proxy != null) { builder.withProxy(proxy.getHost(), proxy.getPortOrDefault(8080)); } return builder.build(); } case UDP: { final HostAndPort udpAddress = configuration.getUdpAddress(); return new UdpTransport.Builder() .withStatsdHost(udpAddress.getHost()) .withPort(udpAddress.getPortOrDefault(8125)) .withPrefix(configuration.getUdpPrefix()) .build(); } default: throw new IllegalArgumentException("Unknown Datadog transport \"" + configuration.getTransport() + "\""); } } @Inject  TransportProvider(MetricsDatadogReporterConfiguration configuration); @Override Transport get();  }
@Test public void getReturnsGraphite() throws Exception { final MetricsDatadogReporterConfiguration configuration = new MetricsDatadogReporterConfiguration() { @Override public DatadogTransport getTransport() { return DatadogTransport.HTTP; } }; final TransportProvider provider = new TransportProvider(configuration); final Transport transport = provider.get(); assertTrue(transport instanceof HttpTransport); }
@Test public void getReturnsGraphiteUDP() throws Exception { final MetricsDatadogReporterConfiguration configuration = new MetricsDatadogReporterConfiguration() { @Override public DatadogTransport getTransport() { return DatadogTransport.UDP; } }; final TransportProvider provider = new TransportProvider(configuration); final Transport transport = provider.get(); assertTrue(transport instanceof UdpTransport); }