package com.custardsource.parfait;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import javax.measure.unit.SI;
import javax.measure.unit.Unit;

import org.junit.Before;
import org.junit.Test;

import com.custardsource.parfait.PeriodicValueBuilder.Period;

public class PeriodicValueBuilderTest {
	private MonitorableRegistry registry;
	private Monitorable<Long> template;
	private PeriodicValueBuilder builder;

	@Before
	public void setUp() {
		registry = new MonitorableRegistry();
		template = new MonitoredCounter("foo", "bar", registry,
				Unit.ONE.times(1000));
		builder = new PeriodicValueBuilder(registry);
		builder.addPeriod(Period.of(1000, 5000, "5s"));
	}

	@Test
	public void metricsShouldBeCreatedWithProvidedValues() {
		builder.build("baz", "moop", SI.LUX);
		assertTrue(registry.containsMetric("baz.5s"));
		assertEquals("moop [5s]", registry.getMetric("baz.5s").getDescription());
		assertEquals(SI.LUX, registry.getMetric("baz.5s").getUnit());
	}

	@Test
	public void metricsShouldBeCreatedWithValuesCopiedFromTemplate() {
		builder.copyFrom(template);
		assertTrue(registry.containsMetric("foo.5s"));
		assertEquals("bar [5s]", registry.getMetric("foo.5s").getDescription());
		assertEquals(Unit.ONE.times(1000), registry.getMetric("foo.5s")
				.getUnit());
	}
}

@@ -1320,11 +1320,881 @@
 t());%0A%09%7D
+%0A%0A%09@Test%0A%09public void wrapCounterShouldProduceNewMetricsWithCopiedValues() %7B%0A%09%09MonitoredCounter counter = new MonitoredCounter(%22plink%22, %22plunk%22, registry, SI.FARAD);%0A%09%09builder.wrapCounter(counter);%0A%09%09assertTrue(registry.containsMetric(%22plink.5s%22));%0A%09%09assertEquals(%22plunk %5B5s%5D%22, registry.getMetric(%22plink.5s%22).getDescription());%0A%09%09assertEquals(SI.FARAD, registry.getMetric(%22plink.5s%22)%0A%09%09%09%09.getUnit());%0A%09%7D%0A%0A%09@Test%0A%09public void wrappedCounterShouldIncrementOriginalWhenIncremented() %7B%0A%09%09MonitoredCounter counter = new MonitoredCounter(%22iggle%22, %22piggle%22, registry, SI.SIEVERT);%0A%09%09CompositeCounter wrapped = builder.wrapCounter(counter);%0A%09%09wrapped.inc(23);%0A%09%09assertEquals(Long.valueOf(23L), counter.get());%0A%09%09// TODO Should have this assertion, but neet better framework for PollingMonitoredValue%0A%09%09// assertEquals(Long.valueOf(23L), registry.getMetric(%22iggle.5s%22).get());%0A%09%7D
 %0A%7D%0A

Add tests for counter wrapping