Adjust tests to reflect real life situation

For PROJECTS cache CacheEvictionEvent.key is a string representing the
project name. Current tests are serialising Project.NameKey to a Json
string e.g: gson.toJson(project) which encodes project name with extra
quotes. To match real life situation pass project name as string instead
of string encoded as json.

Also add tests to cover issue with cache eviction for projects with '/'
in project name.

Bug: Issue 14564
Change-Id: I23262487222ecb55ce22286fddf2707fb05f8f7b
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/event/CacheEvictionEventRouterTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/event/CacheEvictionEventRouterTest.java
index bf5c5d9..76c001f 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/event/CacheEvictionEventRouterTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/event/CacheEvictionEventRouterTest.java
@@ -16,7 +16,10 @@
 
 import static org.mockito.Mockito.verify;
 
+import com.google.gerrit.entities.Project;
+import com.google.gerrit.server.events.EventGsonProvider;
 import com.google.gson.Gson;
+import com.googlesource.gerrit.plugins.multisite.cache.Constants;
 import com.googlesource.gerrit.plugins.multisite.forwarder.CacheEntry;
 import com.googlesource.gerrit.plugins.multisite.forwarder.CacheKeyJsonParser;
 import com.googlesource.gerrit.plugins.multisite.forwarder.ForwardedCacheEvictionHandler;
@@ -31,12 +34,13 @@
 @RunWith(MockitoJUnitRunner.class)
 public class CacheEvictionEventRouterTest {
 
+  private static Gson gson = new EventGsonProvider().get();
   private CacheEvictionEventRouter router;
   @Mock private ForwardedCacheEvictionHandler cacheEvictionHandler;
 
   @Before
   public void setUp() {
-    router = new CacheEvictionEventRouter(cacheEvictionHandler, new CacheKeyJsonParser(new Gson()));
+    router = new CacheEvictionEventRouter(cacheEvictionHandler, new CacheKeyJsonParser(gson));
   }
 
   @Test
@@ -48,11 +52,21 @@
   }
 
   @Test
-  public void routerShouldSendEventsToTheAppropriateHandler_ProjectCacheEvictionWithSlash()
+  public void routerShouldSendEventsToTheAppropriateHandler_CacheEvictionWithSlash()
       throws Exception {
-    final CacheEvictionEvent event = new CacheEvictionEvent("cache", "some/project");
+    final CacheEvictionEvent event = new CacheEvictionEvent("cache", "some/key");
     router.route(event);
 
     verify(cacheEvictionHandler).evict(CacheEntry.from(event.cacheName, event.key));
   }
+
+  @Test
+  public void routerShouldSendEventsToTheAppropriateHandler_ProjectCacheEvictionWithSlash()
+      throws Exception {
+    final CacheEvictionEvent event = new CacheEvictionEvent(Constants.PROJECTS, "some/project");
+    router.route(event);
+
+    verify(cacheEvictionHandler)
+        .evict(CacheEntry.from(event.cacheName, Project.nameKey((String) event.key)));
+  }
 }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/CacheKeyJsonParserTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/CacheKeyJsonParserTest.java
index 7efd4dd..e29c8cd 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/CacheKeyJsonParserTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/CacheKeyJsonParserTest.java
@@ -53,9 +53,10 @@
 
   @Test
   public void projectNameKeyParse() {
-    Project.NameKey name = Project.nameKey("foo");
-    String json = gson.toJson(name);
-    assertThat(name).isEqualTo(gsonParser.fromJson(Constants.PROJECTS, json));
+    String projectNameString = "foo";
+    Project.NameKey projectNameKey = Project.nameKey(projectNameString);
+    assertThat(projectNameKey)
+        .isEqualTo(gsonParser.fromJson(Constants.PROJECTS, projectNameString));
   }
 
   @Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedCacheEvictionHandlerIT.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedCacheEvictionHandlerIT.java
index fb2a50b..826e154 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedCacheEvictionHandlerIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedCacheEvictionHandlerIT.java
@@ -22,6 +22,8 @@
 import com.google.common.collect.Sets;
 import com.google.gerrit.acceptance.LightweightPluginDaemonTest;
 import com.google.gerrit.acceptance.TestPlugin;
+import com.google.gerrit.entities.Project;
+import com.google.gerrit.extensions.api.projects.ProjectInput;
 import com.google.gerrit.extensions.registration.DynamicSet;
 import com.google.gerrit.extensions.registration.RegistrationHandle;
 import com.google.gerrit.server.cache.CacheRemovalListener;
@@ -122,11 +124,32 @@
 
   @Test
   public void shouldEvictProjectCache() throws Exception {
-    objectUnderTest.route(
-        new CacheEvictionEvent(ProjectCacheImpl.CACHE_NAME, gson.toJson(project)));
+    objectUnderTest.route(new CacheEvictionEvent(ProjectCacheImpl.CACHE_NAME, project.get()));
     evictionsCacheTracker.waitForExpectedEvictions();
 
     assertThat(evictionsCacheTracker.trackedEvictionsFor(ProjectCacheImpl.CACHE_NAME))
         .contains(project);
   }
+
+  @Test
+  public void shouldEvictProjectCacheWithSlash() throws Exception {
+    ProjectInput in = new ProjectInput();
+    in.name = name("my/project");
+    gApi.projects().create(in);
+    Project.NameKey projectNameKey = Project.nameKey(in.name);
+
+    restartCacheEvictionsTracking();
+
+    objectUnderTest.route(
+        new CacheEvictionEvent(ProjectCacheImpl.CACHE_NAME, projectNameKey.get()));
+
+    evictionsCacheTracker.waitForExpectedEvictions();
+    assertThat(evictionsCacheTracker.trackedEvictionsFor(ProjectCacheImpl.CACHE_NAME))
+        .contains(projectNameKey);
+  }
+
+  private void restartCacheEvictionsTracking() {
+    stopTrackingCacheEvictions();
+    startTrackingCacheEvictions();
+  }
 }