package de.philipphager.disclosure.database;

import android.database.sqlite.SQLiteDatabase;
import com.squareup.sqlbrite.BriteDatabase;
import com.squareup.sqlbrite.SqlBrite;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import rx.Scheduler;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@SuppressWarnings("PMD.TooManyStaticImports")
@RunWith(PowerMockRunner.class)
@PrepareForTest({SqlBrite.class, BriteDatabase.class, SQLiteDatabase.class})
public class DatabaseManagerShould {
  @Mock protected DatabaseOpenHelper openHelper;
  protected SqlBrite sqlBrite;
  protected BriteDatabase observableDB;
  protected BriteDatabase secondObservableDB;
  protected SQLiteDatabase writeableDB;
  protected SQLiteDatabase secondWritableDB;
  protected DatabaseManager databaseManager;

  @Before public void setUp() {
    sqlBrite = PowerMockito.mock(SqlBrite.class);
    observableDB = PowerMockito.mock(BriteDatabase.class);
    secondObservableDB = PowerMockito.mock(BriteDatabase.class);
    writeableDB = PowerMockito.mock(SQLiteDatabase.class);
    secondWritableDB = PowerMockito.mock(SQLiteDatabase.class);

    MockitoAnnotations.initMocks(this);
    databaseManager = new DatabaseManager(openHelper, sqlBrite);

    when(openHelper.getWritableDatabase()).thenReturn(writeableDB).thenReturn(secondWritableDB);
    when(sqlBrite.wrapDatabaseHelper(any(DatabaseOpenHelper.class),
        any(Scheduler.class))).thenReturn(observableDB).thenReturn(secondObservableDB);
  }

  @Test public void openNewWritableDBOnFirstOpenCall() {
    SQLiteDatabase db = databaseManager.openWriteable();

    verify(openHelper).getWritableDatabase();
    assertThat(db).isEqualTo(writeableDB);
  }

  @Test public void reuseWritableDBWhenDBAlreadyOpened() {
    SQLiteDatabase firstDB = databaseManager.openWriteable();
    SQLiteDatabase secondDB = databaseManager.openWriteable();

    verify(openHelper, times(1)).getWritableDatabase();
    assertThat(firstDB).isEqualTo(secondDB);
  }

  @Test public void openNewObservableDBOnFirstOpenCall() {
    BriteDatabase db = databaseManager.openObservable();

    verify(sqlBrite.wrapDatabaseHelper(any(DatabaseOpenHelper.class), any(Scheduler.class)));
    assertThat(db).isEqualTo(observableDB);
  }

  @Test public void reuseObservableDBWhenDBAlreadyOpened() {
    BriteDatabase firstDB = databaseManager.openObservable();
    BriteDatabase secondDB = databaseManager.openObservable();

    verify(sqlBrite.wrapDatabaseHelper(any(DatabaseOpenHelper.class), any(Scheduler.class)));
    assertThat(firstDB).isEqualTo(secondDB);
  }
}

@@ -969,23 +969,21 @@
 er;%0A  pr
-otec
+iva
 te
-d
  SqlBrit
@@ -994,31 +994,29 @@
 lBrite;%0A  pr
-otec
+iva
 te
-d
  BriteDataba
@@ -1036,23 +1036,21 @@
 DB;%0A  pr
-otec
+iva
 te
-d
  BriteDa
@@ -1072,39 +1072,37 @@
 servableDB;%0A  pr
-otec
+iva
 te
-d
  SQLiteDatabase 
@@ -1118,23 +1118,21 @@
 DB;%0A  pr
-otec
+iva
 te
-d
  SQLiteD
@@ -1161,23 +1161,21 @@
 DB;%0A  pr
-otec
+iva
 te
-d
  Databas

Make fields package private