package de.philipphager.disclosure.database.app;

import android.database.sqlite.SQLiteDatabase;
import com.squareup.sqlbrite.BriteDatabase;
import de.philipphager.disclosure.database.app.model.App;
import de.philipphager.disclosure.database.util.query.BriteQuery;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
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 static de.philipphager.disclosure.database.app.MockApp.getTestContentValues;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@PrepareForTest({BriteDatabase.class, BriteDatabase.Transaction.class})
public class AppRepositoryShould {
  @Mock protected BriteQuery briteQuery;
  @InjectMocks protected AppRepository appRepository;
  protected BriteDatabase database;
  protected BriteDatabase.Transaction transaction;

  @Before public void setUp() {
    database = PowerMockito.mock(BriteDatabase.class);
    transaction = PowerMockito.mock(BriteDatabase.Transaction.class);
    when(database.newTransaction()).thenReturn(transaction);
    MockitoAnnotations.initMocks(this);
  }

  @Test @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
  public void insertAppIntoDatabase() {
    appRepository.add(database, MockApp.TEST);

    verify(database).insert(App.TABLE_NAME, getTestContentValues(MockApp.TEST),
        SQLiteDatabase.CONFLICT_IGNORE);
  }

  @Test @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
  public void insertMultipleAppsIntoDatabase() {
    List<App> appList = Arrays.asList(MockApp.TEST, MockApp.TEST, MockApp.TEST);
    appRepository.add(database, appList);

    verify(database, times(3)).insert(App.TABLE_NAME, getTestContentValues(MockApp.TEST),
        SQLiteDatabase.CONFLICT_IGNORE);
  }

  @Test @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
  public void updateAppInDatabase() {
    appRepository.update(database, MockApp.TEST);

    String where = String.format("id=%s", MockApp.TEST.id());

    verify(database).update(App.TABLE_NAME, getTestContentValues(MockApp.TEST), where);
  }
}

@@ -269,47 +269,66 @@
 ort 
-java.util.Arrays;%0Aimport java.util.List
+de.philipphager.disclosure.database.util.query.SQLSelector
 ;%0Aim
@@ -757,49 +757,8 @@
 es;%0A
-import static org.mockito.Mockito.times;%0A
 impo
@@ -1827,30 +1827,19 @@
 oid 
-insertMultipl
+updat
 eApp
-s
 In
-to
 Data
@@ -1855,68 +1855,37 @@
     
-List%3CApp%3E appList = Arrays.asList(MockApp.TEST, MockApp.TEST
+appRepository.update(database
 , Mo
@@ -1901,47 +1901,68 @@
 T);%0A
+%0A
     
-appRepository.add(database, appList
+String where = String.format(%22id=%25s%22, MockApp.TEST.id()
 );%0A%0A
@@ -1984,26 +1984,16 @@
 base
-, times(3)).insert
+).update
 (App
@@ -2044,47 +2044,14 @@
 ST),
-%0A        SQLiteDatabase.CONFLICT_IGNORE
+ where
 );%0A 
@@ -2127,35 +2127,37 @@
 public void 
-updateAppIn
+removeAppFrom
 Database() %7B
@@ -2165,37 +2165,44 @@
     
-appRepository.update(database
+String where = String.format(%22id=%25s%22
 , Mo
@@ -2215,71 +2215,107 @@
 TEST
+.id()
 );%0A
-%0A
     S
-tring where = String.format(%22id=%25s%22, MockApp.TEST.id()
+QLSelector mockSelector = () -%3E where;%0A    appRepository.remove(database, mockSelector
 );%0A%0A
@@ -2327,36 +2327,36 @@
 erify(database).
-upda
+dele
 te(App.TABLE_NAM
@@ -2361,44 +2361,8 @@
 AME,
- getTestContentValues(MockApp.TEST),
  whe

Update tests