Skip to content

Commit

Permalink
Add tests for new features
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyer authored and wilkinsona committed May 13, 2015
1 parent 270d5e3 commit 7be13b2
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Map<String, Export> getWriters() {
}

@PostConstruct
public void setDefaults() {
public void setUpDefaults() {
Export defaults = null;
for (Entry<String, Export> entry : this.writers.entrySet()) {
String key = entry.getKey();
Expand Down Expand Up @@ -91,13 +91,13 @@ public void setDefaults() {
}
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());
}
}
}
Expand Down Expand Up @@ -210,6 +210,6 @@ public Export findExport(String name) {
return value;
}
}
return null;
return this.export;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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;

/**
Expand All @@ -43,11 +44,11 @@
*
* @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.
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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;
Expand All @@ -35,7 +35,7 @@ public static void main(String[] args) throws Exception {
@Bean
@ConfigurationProperties("metrics.export")
public MetricWriter openTsdbMetricWriter() {
OpenTsdbHttpMetricWriter writer = new OpenTsdbHttpMetricWriter();
OpenTsdbMetricWriter writer = new OpenTsdbMetricWriter();
writer.setNamingStrategy(namingStrategy());
return writer;
}
Expand Down

0 comments on commit 7be13b2

Please sign in to comment.