/**
 * Copyright (c) 2002-2011 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.server.rrd;

import org.junit.Test;
import org.rrd4j.DsType;
import org.rrd4j.core.RrdDb;
import org.rrd4j.core.Sample;

import java.io.IOException;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.stub;
import static org.mockito.Mockito.verify;

public class RrdSamplerTest
{
    @Test
    public void canSampleADatabase() throws IOException
    {
        Sampleable testSamplable = new TestSamplable( "myTest", 15 );

        RrdDb rrd = mock( RrdDb.class );
        final Sample sample = mock( Sample.class );
        stub( rrd.createSample( 0 ) ).toReturn( sample );

        RrdSampler sampler = new RrdSamplerImpl( rrd, testSamplable );
        sampler.updateSample();

        verify( sample ).setValue( "myTest", 15 );
    }

    @Test
    public void shouldIgnoreUnableToSampleExceptions() throws IOException
    {
        Sampleable failingSampleable = new FailingSamplable( "myTest" );

        RrdDb rrd = mock( RrdDb.class );
        final Sample sample = mock( Sample.class );
        stub( rrd.createSample( 0 ) ).toReturn( sample );

        RrdSampler sampler = new RrdSamplerImpl( rrd, failingSampleable );

        sampler.updateSample();

        verify( sample, never() ).setValue( "myTest", 15 );
    }

    private class TestSamplable implements Sampleable
    {
        private String name;
        private double value;

        private TestSamplable( String name, double value )
        {
            this.name = name;
            this.value = value;
        }

        public String getName()
        {
            return name;
        }

        public double getValue()
        {
            return value;
        }

        public DsType getType()
        {
            return DsType.GAUGE;
        }
    }

    private class FailingSamplable implements Sampleable
    {
        private String name;

        private FailingSamplable( String name )
        {
            this.name = name;
        }

        public String getName()
        {
            return name;
        }

        public double getValue()
        {
            throw new UnableToSampleException();
        }

        public DsType getType()
        {
            return DsType.GAUGE;
        }
    }
}

@@ -958,16 +958,60 @@
 ption;%0A%0A
+import static org.mockito.Matchers.anyLong;%0A
 import s
@@ -1042,16 +1042,16 @@
 o.mock;%0A
-
 import s
@@ -1087,48 +1087,8 @@
 er;%0A
-import static org.mockito.Mockito.stub;%0A
 impo
@@ -1124,16 +1124,56 @@
 .verify;
+%0Aimport static org.mockito.Mockito.when;
 %0A%0Apublic
@@ -1432,36 +1432,36 @@
 lass );%0A        
-stub
+when
 ( rrd.createSamp
@@ -1460,32 +1460,42 @@
 eateSample( 
-0
+anyLong()
  ) ).t
-o
+hen
 Return( samp
@@ -1932,12 +1932,12 @@
     
-stub
+when
 ( rr
@@ -1956,16 +1956,26 @@
 le( 
-0
+anyLong()
  ) ).t
-o
+hen
 Retu

fix test