package com.hpcloud.mon.infrastructure.persistence.influxdb;

import com.hpcloud.mon.MonApiConfiguration;
import com.hpcloud.mon.infrastructure.persistence.influxdb.AlarmStateHistoryInfluxDbRepositoryImpl;

import org.influxdb.InfluxDB;
import org.joda.time.DateTime;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.skife.jdbi.v2.DBI;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import scala.actors.threadpool.Arrays;

@Test
public class AlarmStateHistoryInfluxDbRepositoryImplTest {

  @Mock(name = "mysql")
  private DBI mysql;

  @Mock
  private MonApiConfiguration monApiConfiguration;

  @Mock
  private InfluxDB influxDB;

  @InjectMocks
  private AlarmStateHistoryInfluxDbRepositoryImpl alarmStateHistoryInfluxDBRepository;

  @BeforeMethod(alwaysRun = true)
  public void initMocks() {
    MockitoAnnotations.initMocks(this);
  }

  public void buildQueryForFindByIdTest() throws Exception {

    String er =
        "select alarm_id, old_state, new_state, reason, "
            + "reason_data from alarm_state_history where tenant_id = 'tenant-id' and alarm_id = "
            + "'alarm-id'";
    String r =
        this.alarmStateHistoryInfluxDBRepository.buildQueryForFindById("tenant-id", "alarm-id");

    assert (er.equals(r));

  }

  public void buildTimePartTest() {
    String er = " and time > 1388559600s and time < 1388559601s";
    String r =
        this.alarmStateHistoryInfluxDBRepository.buildTimePart(new DateTime(2014, 01, 01, 0, 0, 0),
            new DateTime(2014, 01, 01, 0, 0, 1));
    assert (er.equals(r));

  }

  @SuppressWarnings("unchecked")
  public void buildAlarmsPartTest() {
    String er = " and ( alarm_id = 'id-1'  or  alarm_id = 'id-2' )";
    String r =
        this.alarmStateHistoryInfluxDBRepository.buildAlarmsPart(Arrays.asList(new String[] {
            "id-1", "id-2"}));
    assert (er.equals(r));
  }

  public void buildQueryForFindTest() throws Exception {
    String er =
        "select alarm_id, old_state, new_state, reason, "
            + "reason_data from alarm_state_history where tenant_id = 'tenant-id'  and time > "
            + "1388559600s and time < 1388559601s  and ( alarm_id = 'id-1'  or  alarm_id = 'id-2' )";
    String r =
        this.alarmStateHistoryInfluxDBRepository.buildQueryForFind("tenant-id",
            " and time > 1388559600s and time < 1388559601s", " and ( alarm_id = 'id-1'  or  "
                + "alarm_id" + " = 'id-2' )");

    assert (er.equals(r));
  }

}

@@ -103,109 +103,8 @@
 on;%0A
-import com.hpcloud.mon.infrastructure.persistence.influxdb.AlarmStateHistoryInfluxDbRepositoryImpl;%0A%0A
 impo
@@ -160,16 +160,51 @@
 teTime;%0A
+import org.joda.time.DateTimeZone;%0A
 import o
@@ -401,17 +401,16 @@
 s.Test;%0A
-%0A
 import s
@@ -934,32 +934,24 @@
  String er =
-%0A       
  %22select ala
@@ -980,32 +980,34 @@
 state, reason, %22
+ +
 %0A            + %22
@@ -991,35 +991,34 @@
 on, %22 +%0A        
-   
+%22%22
  + %22reason_data 
@@ -1097,20 +1097,16 @@
         
-    
 + %22'alar
@@ -1123,32 +1123,24 @@
   String r =
-%0A       
  this.alarmS
@@ -1203,16 +1203,24 @@
 ant-id%22,
+%0A       
  %22alarm-
@@ -1321,35 +1321,35 @@
 and time %3E 13885
-596
+344
 00s and time %3C 1
@@ -1348,27 +1348,27 @@
 time %3C 13885
-596
+344
 01s%22;%0A    St
@@ -1371,32 +1371,24 @@
   String r =
-%0A       
  this.alarmS
@@ -1461,30 +1461,44 @@
  01,
- 0, 0, 0),%0A           
+%0A        0, 0, 0, DateTimeZone.UTC),
  new
@@ -1528,16 +1528,34 @@
  0, 0, 1
+, DateTimeZone.UTC
 ));%0A    
@@ -1733,32 +1733,24 @@
   String r =
-%0A       
  this.alarmS
@@ -1812,16 +1812,24 @@
 List(new
+%0A       
  String%5B
@@ -1833,23 +1833,9 @@
 ng%5B%5D
- %7B%0A            
+%7B
 %22id-
@@ -1953,24 +1953,16 @@
 ing er =
-%0A       
  %22select
@@ -2003,16 +2003,18 @@
 eason, %22
+ +
 %0A       
@@ -2010,27 +2010,26 @@
 %22 +%0A        
-   
+%22%22
  + %22reason_d
@@ -2104,22 +2104,18 @@
  %3E %22
-%0A    
+ +%0A
        
- +
  %2213
@@ -2213,24 +2213,16 @@
 ring r =
-%0A       
  this.al
@@ -2294,20 +2294,16 @@
         
- 
-   
 %22 and ti
@@ -2376,16 +2376,18 @@
 '  or  %22
+ +
 %0A       
@@ -2390,22 +2390,16 @@
         
-     +
  %22alarm_

Make tests use UTC dates.