Fix plugin to match latest master

Instead of passing a provider, we do a .get() and pass the entry.
The tests have been updated to use @After instead of manual teardown,
this to prevent warnings from JUnit.

Change-Id: I2867bbd69f15d6f14d9cae3ca4bc04959d259a87
diff --git a/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLStore.java b/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLStore.java
index 7224b38..920f5d7 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLStore.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLStore.java
@@ -21,13 +21,11 @@
 import com.google.gerrit.extensions.events.LifecycleListener;
 import com.google.gerrit.extensions.restapi.AuthException;
 import com.google.gerrit.reviewdb.client.Project;
-import com.google.gerrit.server.CurrentUser;
 import com.google.gerrit.server.events.ProjectEvent;
 import com.google.gerrit.server.permissions.PermissionBackend;
 import com.google.gerrit.server.permissions.PermissionBackendException;
 import com.google.gerrit.server.permissions.ProjectPermission;
 import com.google.inject.Inject;
-import com.google.inject.Provider;
 import com.google.inject.Singleton;
 
 import com.ericsson.gerrit.plugins.eventslog.EventPool;
@@ -58,7 +56,6 @@
   private static final Logger log = LoggerFactory.getLogger(SQLStore.class);
   private static final String H2_DB_SUFFIX = ".h2.db";
 
-  private final Provider<CurrentUser> userProvider;
   private SQLClient eventsDb;
   private SQLClient localEventsDb;
   private final int maxAge;
@@ -73,8 +70,7 @@
   private Path localPath;
 
   @Inject
-  SQLStore(Provider<CurrentUser> userProvider,
-      EventsLogConfig cfg,
+  SQLStore(EventsLogConfig cfg,
       @EventsDb SQLClient eventsDb,
       @LocalEventsDb SQLClient localEventsDb,
       @EventPool ScheduledExecutorService pool,
@@ -84,7 +80,6 @@
     this.waitTime = cfg.getWaitTime();
     this.connectTime = cfg.getConnectTime();
     this.copyLocal = cfg.getCopyLocal();
-    this.userProvider = userProvider;
     this.eventsDb = eventsDb;
     this.localEventsDb = localEventsDb;
     this.pool = pool;
@@ -122,7 +117,7 @@
       String projectName = entry.getKey();
       try {
         permissionBackend
-            .user(userProvider)
+            .currentUser()
             .project(new Project.NameKey(projectName))
             .check(ProjectPermission.ACCESS);
         entries.addAll(entry.getValue());
diff --git a/src/test/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLStoreTest.java b/src/test/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLStoreTest.java
index 8a95c9a..b257d89 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLStoreTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLStoreTest.java
@@ -26,18 +26,17 @@
 
 import com.google.common.collect.ImmutableList;
 import com.google.gerrit.reviewdb.client.Project;
-import com.google.gerrit.server.CurrentUser;
 import com.google.gerrit.server.events.ProjectEvent;
 import com.google.gerrit.server.permissions.PermissionBackend;
 import com.google.gerrit.server.permissions.PermissionBackendException;
 import com.google.gerrit.server.permissions.ProjectPermission;
 import com.google.gson.Gson;
-import com.google.inject.Provider;
 
 import com.ericsson.gerrit.plugins.eventslog.EventsLogConfig;
 import com.ericsson.gerrit.plugins.eventslog.MalformedQueryException;
 import com.ericsson.gerrit.plugins.eventslog.ServiceUnavailableException;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -74,8 +73,6 @@
   private static final String GENERIC_QUERY = "SELECT * FROM " + TABLE_NAME;
 
   @Mock
-  private Provider<CurrentUser> userProviderMock;
-  @Mock
   private EventsLogConfig cfgMock;
   @Mock
   private PermissionBackend permissionBackendMock;
@@ -105,14 +102,15 @@
     when(cfgMock.getLocalStorePath()).thenReturn(testFolder.getRoot().toPath());
   }
 
+  @After
   public void tearDown() throws Exception {
-    stat.execute("DROP TABLE " + TABLE_NAME);
+    stat.execute("DROP TABLE IF EXISTS " + TABLE_NAME);
     store.stop();
   }
 
   @Test
   public void storeThenQueryVisible() throws Exception {
-    when(permissionBackendMock.user(userProviderMock)).thenReturn(withUserMock);
+    when(permissionBackendMock.currentUser()).thenReturn(withUserMock);
     when(withUserMock.project(any(Project.NameKey.class)))
         .thenReturn(forProjectMock);
     doNothing().when(forProjectMock).check(ProjectPermission.ACCESS);
@@ -121,12 +119,11 @@
     List<String> events = store.queryChangeEvents(GENERIC_QUERY);
     String json = new Gson().toJson(mockEvent);
     assertThat(events).containsExactly(json);
-    tearDown();
   }
 
   @Test
   public void storeThenQueryNotVisible() throws Exception {
-    when(permissionBackendMock.user(userProviderMock)).thenReturn(withUserMock);
+    when(permissionBackendMock.currentUser()).thenReturn(withUserMock);
     when(withUserMock.project(any(Project.NameKey.class)))
         .thenReturn(forProjectMock);
     doThrow(new PermissionBackendException("")).when(forProjectMock)
@@ -135,7 +132,6 @@
     store.storeEvent(mockEvent);
     List<String> events = store.queryChangeEvents(GENERIC_QUERY);
     assertThat(events).isEmpty();
-    tearDown();
   }
 
   @Test(expected = MalformedQueryException.class)
@@ -147,7 +143,7 @@
 
   @Test
   public void notReturnEventWithNoVisibilityInfo() throws Exception {
-    when(permissionBackendMock.user(userProviderMock)).thenReturn(withUserMock);
+    when(permissionBackendMock.currentUser()).thenReturn(withUserMock);
     when(withUserMock.project(any(Project.NameKey.class)))
         .thenReturn(forProjectMock);
     doThrow(new PermissionBackendException("")).when(forProjectMock)
@@ -156,7 +152,6 @@
     store.storeEvent(mockEvent);
     List<String> events = store.queryChangeEvents(GENERIC_QUERY);
     assertThat(events).isEmpty();
-    tearDown();
   }
 
   @Test
@@ -167,7 +162,7 @@
     setUpClientMock();
     doThrow(exceptions).doNothing().when(eventsDb).storeEvent(mockEvent);
     doThrow(exceptions).doNothing().when(eventsDb).queryOne();
-    store = new SQLStore(userProviderMock, cfgMock, eventsDb,
+    store = new SQLStore(cfgMock, eventsDb,
         localEventsDb, poolMock, permissionBackendMock);
     store.start();
     store.storeEvent(mockEvent);
@@ -183,7 +178,7 @@
     setUpClientMock();
     doThrow(exceptions).doNothing().when(eventsDb).storeEvent(mockEvent);
     doThrow(exceptions).doNothing().when(eventsDb).queryOne();
-    store = new SQLStore(userProviderMock, cfgMock, eventsDb,
+    store = new SQLStore(cfgMock, eventsDb,
         localEventsDb, poolMock, permissionBackendMock);
     store.start();
     store.storeEvent(mockEvent);
@@ -196,7 +191,7 @@
     when(cfgMock.getMaxTries()).thenReturn(3);
     setUpClientMock();
     doThrow(new SQLException(MSG)).when(eventsDb).storeEvent(mockEvent);
-    store = new SQLStore(userProviderMock, cfgMock, eventsDb,
+    store = new SQLStore(cfgMock, eventsDb,
         localEventsDb, poolMock, permissionBackendMock);
     store.start();
     store.storeEvent(mockEvent);
@@ -211,7 +206,7 @@
     setUpClientMock();
     doThrow(exceptions).doNothing().when(eventsDb).storeEvent(mockEvent);
     doThrow(exceptions).doNothing().when(eventsDb).queryOne();
-    store = new SQLStore(userProviderMock, cfgMock, eventsDb,
+    store = new SQLStore(cfgMock, eventsDb,
         localEventsDb, poolMock, permissionBackendMock);
     store.start();
     store.storeEvent(mockEvent);
@@ -224,7 +219,7 @@
     doThrow(new SQLException(new ConnectException())).when(eventsDb)
         .createDBIfNotCreated();
     doThrow(new SQLException()).when(eventsDb).queryOne();
-    store = new SQLStore(userProviderMock, cfgMock, eventsDb,
+    store = new SQLStore(cfgMock, eventsDb,
         localEventsDb, poolMock, permissionBackendMock);
     store.start();
     store.storeEvent(mockEvent);
@@ -235,14 +230,14 @@
   public void restoreEventsFromLocalDb() throws Exception {
     MockEvent mockEvent = new MockEvent();
     MockEvent mockEvent2 = new MockEvent("proj");
-    when(permissionBackendMock.user(userProviderMock)).thenReturn(withUserMock);
+    when(permissionBackendMock.currentUser()).thenReturn(withUserMock);
     when(withUserMock.project(any(Project.NameKey.class)))
         .thenReturn(forProjectMock);
     doNothing().when(forProjectMock).check(ProjectPermission.ACCESS);
 
     eventsDb = new SQLClient(TEST_DRIVER, TEST_URL, TEST_OPTIONS);
     localEventsDb = new SQLClient(TEST_DRIVER, TEST_LOCAL_URL, TEST_OPTIONS);
-    store = new SQLStore(userProviderMock, cfgMock, eventsDb,
+    store = new SQLStore(cfgMock, eventsDb,
         localEventsDb, poolMock, permissionBackendMock);
 
     localEventsDb.createDBIfNotCreated();
@@ -255,7 +250,6 @@
     String json = gson.toJson(mockEvent);
     String json2 = gson.toJson(mockEvent2);
     assertThat(events).containsExactly(json, json2);
-    tearDown();
   }
 
   @Test
@@ -264,7 +258,7 @@
     doThrow(new SQLException(new ConnectException())).when(eventsDb)
         .createDBIfNotCreated();
     doThrow(new SQLException()).when(eventsDb).queryOne();
-    store = new SQLStore(userProviderMock, cfgMock, eventsDb,
+    store = new SQLStore(cfgMock, eventsDb,
         localEventsDb, poolMock, permissionBackendMock);
     store.start();
     verify(localEventsDb).createDBIfNotCreated();
@@ -276,7 +270,7 @@
     doThrow(new SQLException(new ConnectException())).when(eventsDb)
         .createDBIfNotCreated();
     doThrow(new SQLException()).when(eventsDb).queryOne();
-    store = new SQLStore(userProviderMock, cfgMock, eventsDb,
+    store = new SQLStore(cfgMock, eventsDb,
         localEventsDb, poolMock, permissionBackendMock);
     store.start();
     store.storeEvent(mockEvent);
@@ -290,7 +284,7 @@
     doThrow(new SQLException(new ConnectException())).when(eventsDb)
         .createDBIfNotCreated();
     doThrow(new SQLException()).when(eventsDb).queryOne();
-    store = new SQLStore(userProviderMock, cfgMock, eventsDb,
+    store = new SQLStore(cfgMock, eventsDb,
         localEventsDb, poolMock, permissionBackendMock);
     store.start();
     store.storeEvent(mockEvent);
@@ -300,7 +294,7 @@
   private void setUpClient() {
     eventsDb = new SQLClient(TEST_DRIVER, TEST_URL, TEST_OPTIONS);
     localEventsDb = new SQLClient(TEST_DRIVER, TEST_LOCAL_URL, TEST_OPTIONS);
-    store = new SQLStore(userProviderMock, cfgMock, eventsDb,
+    store = new SQLStore(cfgMock, eventsDb,
         localEventsDb, poolMock, permissionBackendMock);
     store.start();
   }
@@ -324,7 +318,7 @@
     when(localEventsDb.dbExists()).thenReturn(true);
     when(localEventsDb.getAll())
         .thenReturn(ImmutableList.of(mock(SQLEntry.class)));
-    store = new SQLStore(userProviderMock, cfgMock, eventsDb,
+    store = new SQLStore(cfgMock, eventsDb,
         localEventsDb, poolMock, permissionBackendMock);
     store.start();
     poolMock.scheduleWithFixedDelay(store.new CheckConnectionTask(), 0, 0,
@@ -354,7 +348,7 @@
       when(cfgMock.getCopyLocal()).thenReturn(true);
     }
 
-    store = new SQLStore(userProviderMock, cfgMock, eventsDb,
+    store = new SQLStore(cfgMock, eventsDb,
         localEventsDb, poolMock, permissionBackendMock);
     store.start();
     verify(eventsDb).queryOne();