package com.yammer.dropwizard.db.tests;

import com.yammer.dropwizard.db.DatabaseConfiguration;
import com.yammer.dropwizard.db.ManagedDataSource;
import com.yammer.dropwizard.db.ManagedDataSourceFactory;
import com.yammer.dropwizard.db.ManagedPooledDataSource;
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Gauge;
import com.yammer.metrics.core.Metric;
import com.yammer.metrics.core.MetricName;
import com.yammer.metrics.core.MetricPredicate;

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

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.SortedMap;
import java.util.TreeMap;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.fest.assertions.api.Assertions.extractProperty;
import static org.junit.Assert.assertEquals;

public class ManagedDataSourceFactoryTest {
    private final ManagedDataSourceFactory factory = new ManagedDataSourceFactory();

    private ManagedDataSource dataSource;
    private DatabaseConfiguration config;

    @Before
    public void setUp() throws Exception {
        config = new DatabaseConfiguration();
        config.setUrl("jdbc:hsqldb:mem:DbTest-" + System.currentTimeMillis());
        config.setUser("sa");
        config.setDriverClass("org.hsqldb.jdbcDriver");
        config.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");

        this.dataSource = factory.build(config);
    }

    @After
    public void tearDown() throws Exception {
        dataSource.stop();
        Metrics.defaultRegistry().shutdown();
    }

    @Test
    public void buildsAConnectionPoolToTheDatabase() throws Exception {
        final Connection connection = dataSource.getConnection();
        try {
            final PreparedStatement statement = connection.prepareStatement("select 1 from INFORMATION_SCHEMA.SYSTEM_USERS");
            try {
                final ResultSet set = statement.executeQuery();
                try {
                    while (set.next()) {
                        assertThat(set.getInt(1)).isEqualTo(1);
                    }
                } finally {
                    set.close();
                }
            } finally {
                statement.close();
            }
        } finally {
            connection.close();
        }
    }

    /** Extract any Gauges created by ManagedDataSourceFactory */
    private SortedMap<MetricName, Metric> getDataSourceGauges() {
        final String className = ManagedPooledDataSource.class.getName();
        SortedMap<String, SortedMap<MetricName, Metric>> gauges = Metrics.defaultRegistry().groupedMetrics(new MetricPredicate() {
            @Override
            public boolean matches(MetricName name, Metric metric) {
                final String fullName = name.getGroup() + "." + name.getType();
                return metric instanceof Gauge && fullName.equals(className);
            }
        });

        //flatten the map
        SortedMap<MetricName, Metric> results = new TreeMap<MetricName, Metric>();
        for (SortedMap<MetricName, Metric> map : gauges.values()) {
            results.putAll(map);
        }
        return results;
    }

    @Test
    public void createsGauges() throws Exception {
        SortedMap<MetricName, Metric> gauges = getDataSourceGauges();
        assertEquals(2, gauges.size());

        assertThat(extractProperty("name").from(gauges.keySet())).contains("numActive", "numIdle");
    }
}

@@ -220,250 +220,24 @@
 mer.
-dropwizard.db.ManagedPooledDataSource;%0Aimport com.yammer.metrics.Metrics;%0Aimport com.yammer.metrics.core.Gauge;%0Aimport com.yammer.metrics.core.Metric;%0Aimport com.yammer.metrics.core.MetricName;%0Aimport com.yammer.metrics.core.MetricPredicate;%0A
+metrics.Metrics;
 %0Aimp
@@ -399,62 +399,8 @@
 Set;
-%0Aimport java.util.SortedMap;%0Aimport java.util.TreeMap;
 %0A%0Aim
@@ -461,119 +461,8 @@
 hat;
-%0Aimport static org.fest.assertions.api.Assertions.extractProperty;%0Aimport static org.junit.Assert.assertEquals;
 %0A%0Apu
@@ -634,50 +634,8 @@
 rce;
-%0A    private DatabaseConfiguration config;
 %0A%0A  
@@ -691,24 +691,52 @@
 n %7B%0A        
+final DatabaseConfiguration 
 config = new
@@ -1919,32 +1919,32 @@
 ection.close();%0A
+
         %7D%0A    %7D%0A
@@ -1946,1150 +1946,7 @@
    %7D
-%0A%0A    /** Extract any Gauges created by ManagedDataSourceFactory */%0A    private SortedMap%3CMetricName, Metric%3E getDataSourceGauges() %7B%0A        final String className = ManagedPooledDataSource.class.getName();%0A        SortedMap%3CString, SortedMap%3CMetricName, Metric%3E%3E gauges = Metrics.defaultRegistry().groupedMetrics(new MetricPredicate() %7B%0A            @Override%0A            public boolean matches(MetricName name, Metric metric) %7B%0A                final String fullName = name.getGroup() + %22.%22 + name.getType();%0A                return metric instanceof Gauge && fullName.equals(className);%0A            %7D%0A        %7D);%0A%0A        //flatten the map%0A        SortedMap%3CMetricName, Metric%3E results = new TreeMap%3CMetricName, Metric%3E();%0A        for (SortedMap%3CMetricName, Metric%3E map : gauges.values()) %7B%0A            results.putAll(map);%0A        %7D%0A        return results;%0A    %7D%0A%0A    @Test%0A    public void createsGauges() throws Exception %7B%0A        SortedMap%3CMetricName, Metric%3E gauges = getDataSourceGauges();%0A        assertEquals(2, gauges.size());%0A%0A        assertThat(extractProperty(%22name%22).from(gauges.keySet())).contains(%22numActive%22, %22numIdle%22);%0A    %7D
 %0A%7D%0A

Remove metrics tests.