Add tests for new features

This commit is contained in:
Dave Syer 2015-05-11 14:50:04 +01:00 committed by Andy Wilkinson
parent 270d5e3205
commit 7be13b28bd
5 changed files with 155 additions and 12 deletions

View File

@ -63,7 +63,7 @@ public class MetricExportProperties {
}
@PostConstruct
public void setDefaults() {
public void setUpDefaults() {
Export defaults = null;
for (Entry<String, Export> entry : this.writers.entrySet()) {
String key = entry.getKey();
@ -91,13 +91,13 @@ public class MetricExportProperties {
}
for (Export value : this.writers.values()) {
if (value.isIgnoreTimestamps() == null) {
value.setIgnoreTimestamps(false);
value.setIgnoreTimestamps(defaults.isIgnoreTimestamps());
}
if (value.isSendLatest() == null) {
value.setSendLatest(true);
value.setSendLatest(defaults.isSendLatest());
}
if (value.getDelayMillis() == null) {
value.setDelayMillis(5000);
value.setDelayMillis(defaults.getDelayMillis());
}
}
}
@ -210,6 +210,6 @@ public class MetricExportProperties {
return value;
}
}
return null;
return this.export;
}
}

View File

@ -31,6 +31,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
/**
@ -43,11 +44,11 @@ import org.springframework.web.client.RestTemplate;
*
* @author Dave Syer
*/
public class OpenTsdbHttpMetricWriter implements MetricWriter {
public class OpenTsdbMetricWriter implements MetricWriter {
private static final Log logger = LogFactory.getLog(OpenTsdbHttpMetricWriter.class);
private static final Log logger = LogFactory.getLog(OpenTsdbMetricWriter.class);
private RestTemplate restTemplate = new RestTemplate();
private RestOperations restTemplate = new RestTemplate();
/**
* URL for POSTing data. Defaults to http://localhost:4242/api/put.
@ -69,11 +70,11 @@ public class OpenTsdbHttpMetricWriter implements MetricWriter {
private OpenTsdbNamingStrategy namingStrategy = new DefaultOpenTsdbNamingStrategy();
public RestTemplate getRestTemplate() {
public RestOperations getRestTemplate() {
return this.restTemplate;
}
public void setRestTemplate(RestTemplate restTemplate) {
public void setRestTemplate(RestOperations restTemplate) {
this.restTemplate = restTemplate;
}

View File

@ -0,0 +1,60 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.metrics.export;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.boot.actuate.metrics.reader.MetricReader;
import org.springframework.boot.actuate.metrics.writer.MetricWriter;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
public class MetricExportersTests {
private MetricExporters exporters;
private MetricExportProperties export = new MetricExportProperties();
private Map<String, MetricWriter> writers = new LinkedHashMap<String, MetricWriter>();
private MetricReader reader = Mockito.mock(MetricReader.class);
private MetricWriter writer = Mockito.mock(MetricWriter.class);
@Test
public void emptyWriters() {
this.exporters = new MetricExporters(this.reader, this.writers, this.export);
this.exporters.configureTasks(new ScheduledTaskRegistrar());
assertNotNull(this.exporters.getExporters());
assertEquals(0, this.exporters.getExporters().size());
}
@Test
public void oneWriter() {
this.export.setUpDefaults();
this.writers.put("foo", this.writer);
this.exporters = new MetricExporters(this.reader, this.writers, this.export);
this.exporters.configureTasks(new ScheduledTaskRegistrar());
assertNotNull(this.exporters.getExporters());
assertEquals(1, this.exporters.getExporters().size());
}
}

View File

@ -0,0 +1,82 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.metrics.opentsdb;
import java.util.Collections;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestOperations;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
/**
* @author Dave Syer
*/
public class OpenTsdbMetricWriterTests {
private OpenTsdbMetricWriter writer;
private RestOperations restTemplate = Mockito.mock(RestOperations.class);
@Before
public void init() {
this.writer = new OpenTsdbMetricWriter();
this.writer.setRestTemplate(this.restTemplate);
}
@Test
public void postSuccessfullyOnFlush() {
this.writer.set(new Metric<Double>("foo", 2.4));
given(
this.restTemplate.postForEntity(Matchers.anyString(),
Matchers.any(Object.class), anyMap()))
.willReturn(emptyResponse());
this.writer.flush();
verify(this.restTemplate).postForEntity(Matchers.anyString(),
Matchers.any(Object.class), anyMap());
}
@Test
public void flushAutomaticlly() {
given(
this.restTemplate.postForEntity(Matchers.anyString(),
Matchers.any(Object.class), anyMap()))
.willReturn(emptyResponse());
this.writer.setBufferSize(0);
this.writer.set(new Metric<Double>("foo", 2.4));
verify(this.restTemplate).postForEntity(Matchers.anyString(),
Matchers.any(Object.class), anyMap());
}
@SuppressWarnings("rawtypes")
private ResponseEntity<Map> emptyResponse() {
return new ResponseEntity<Map>(Collections.emptyMap(), HttpStatus.OK);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private Class<Map> anyMap() {
return Matchers.any(Class.class);
}
}

View File

@ -18,7 +18,7 @@ package sample.metrics.opentsdb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.metrics.opentsdb.DefaultOpenTsdbNamingStrategy;
import org.springframework.boot.actuate.metrics.opentsdb.OpenTsdbHttpMetricWriter;
import org.springframework.boot.actuate.metrics.opentsdb.OpenTsdbMetricWriter;
import org.springframework.boot.actuate.metrics.opentsdb.OpenTsdbNamingStrategy;
import org.springframework.boot.actuate.metrics.writer.MetricWriter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -35,7 +35,7 @@ public class SampleOpenTsdbExportApplication {
@Bean
@ConfigurationProperties("metrics.export")
public MetricWriter openTsdbMetricWriter() {
OpenTsdbHttpMetricWriter writer = new OpenTsdbHttpMetricWriter();
OpenTsdbMetricWriter writer = new OpenTsdbMetricWriter();
writer.setNamingStrategy(namingStrategy());
return writer;
}