package info.losd.galen.respository;

import info.losd.galen.repository.InfluxdbStatisticsRepo;
import info.losd.galen.repository.Statistic;
import org.influxdb.InfluxDB;
import org.influxdb.dto.Point;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.StringStartsWith.startsWith;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
 * The MIT License (MIT)
 * <p>
 * Copyright (c) 2015 Andrew Braithwaite
 * <p>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p>
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * <p>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
public class TestInfluxdbStatisticsRepo {
    @Mock
    private InfluxDB db;

    @InjectMocks
    InfluxdbStatisticsRepo repo;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void test_it_writes_to_influxdb() {
        Statistic stat = Statistic.duration(100L).statusCode(200).build();

        repo.save(stat);

        ArgumentCaptor<Point> point = ArgumentCaptor.forClass(Point.class);
        ArgumentCaptor<String> dbname = ArgumentCaptor.forClass(String.class);
        ArgumentCaptor<String> retentionPolicy = ArgumentCaptor.forClass(String.class);

        verify(db, times(1)).write(dbname.capture(), retentionPolicy.capture(), point.capture());
        assertThat("point", point.getValue().lineProtocol(), startsWith("statistic response_time=100,status_code=200"));
        assertThat("dbname", dbname.getValue(), is(equalTo("galen")));
        assertThat("retentionPolicy", retentionPolicy.getValue(), is(equalTo("default")));
    }
}

@@ -428,16 +428,59 @@
 rtThat;%0A
+import static org.hamcrest.Matchers.allOf;%0A
 import s
@@ -601,29 +601,31 @@
 ring
-StartsWith.startsWith
+Contains.containsString
 ;%0Aim
@@ -2642,46 +2642,66 @@
 e().
-lineProtocol(), startsWith(%22statistic 
+toString(),%0A                allOf(containsString(%22fields=%7B
 resp
@@ -2714,16 +2714,17 @@
 ime=100,
+ 
 status_c
@@ -2734,13 +2734,157 @@
 =200
+%7D
 %22
+),%0A                        containsString(%22name=statistic%22),%0A                        containsString(%22precision=MILLISECONDS%22)%0A                
 ));%0A
+%0A
     

Refactor test