AWSMetricSender implements MetricSender { @Override public void sendMetric(MetricInfo metric, Double value) { PutMetricDataRequest req = new PutMetricDataRequest(); req.setNamespace(metric.getNamespace()); MetricDatum metricDatum = new MetricDatum(); metricDatum.withMetricName(metric.getMetric()); metricDatum.withUnit(this.convertUnit(metric.getUnit())); metricDatum.withDimensions(this.getDimensionsFromMetricInfo(metric)); metricDatum.withValue(value); req.setMetricData(Collections.singleton(metricDatum)); this.cloudWatch.putMetricData(req); }  @Override void sendMetric(MetricInfo metric, Double value);  }
@Test public void sendMetric() throws Exception { final String ns = "My/Namespace"; final String metric = "Test metric"; final MetricUnit unit = MetricUnit.Count; MetricInfo info = new MetricInfo(ns, metric, unit); final int val = new Random().nextInt(100); Mockito.doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { PutMetricDataRequest req = (PutMetricDataRequest) invocationOnMock.getArguments()[0]; Assert.assertEquals(ns, req.getNamespace()); List<MetricDatum> data = req.getMetricData(); Assert.assertEquals(1, data.size()); MetricDatum datum = data.get(0); Assert.assertEquals(metric, datum.getMetricName()); Assert.assertEquals(unit.toString(), datum.getUnit()); Assert.assertEquals(Double.valueOf(val), datum.getValue()); return null; } }).when(this.cloudWatch).putMetricData(Mockito.any(PutMetricDataRequest.class)); this.sender.sendMetric(info, (double) val); }
@Test public void sendMetricWithDimension() throws Exception { final String ns = "My/Namespace"; final String metric = "Test metric"; final MetricUnit unit = MetricUnit.Count; final String dimensionName = "id"; final String dimensionValue = "someId"; MetricInfo info = new MetricInfo(ns, metric, unit); info.withDimension(dimensionName, dimensionValue); final int val = new Random().nextInt(100); Mockito.doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { PutMetricDataRequest req = (PutMetricDataRequest) invocationOnMock.getArguments()[0]; Assert.assertEquals(ns, req.getNamespace()); List<MetricDatum> data = req.getMetricData(); Assert.assertEquals(1, data.size()); MetricDatum datum = data.get(0); Assert.assertEquals(metric, datum.getMetricName()); Assert.assertEquals(unit.toString(), datum.getUnit()); Assert.assertEquals(Double.valueOf(val), datum.getValue()); Assert.assertEquals(1, datum.getDimensions().size()); Dimension dimension = datum.getDimensions().get(0); Assert.assertEquals(dimensionName, dimension.getName()); Assert.assertEquals(dimensionValue, dimension.getValue()); return null; } }).when(this.cloudWatch).putMetricData(Mockito.any(PutMetricDataRequest.class)); this.sender.sendMetric(info, (double) val); }