Make namespace optional and log what namespace is set to

If the namespace is not set we use the hostname as a namespace.

Solves: Jira GER-1637
Change-Id: I67d1c46f79696ad0e4644fb6dc67e905de2bf698
diff --git a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/config/EiffelConfig.java b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/config/EiffelConfig.java
index dfd8f9d..5224a50 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/config/EiffelConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/config/EiffelConfig.java
@@ -14,14 +14,17 @@
 
 package com.googlesource.gerrit.plugins.eventseiffel.config;
 
+import com.google.common.flogger.FluentLogger;
 import com.google.gerrit.extensions.annotations.PluginName;
 import com.google.gerrit.server.config.PluginConfigFactory;
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
+import java.util.Optional;
 import org.eclipse.jgit.lib.Config;
 
 @Singleton
 public class EiffelConfig {
+  private static final FluentLogger logger = FluentLogger.forEnclosingClass();
 
   public static class Provider implements com.google.inject.Provider<EiffelConfig> {
     private static final String EIFFELEVENT = "EiffelEvent";
@@ -40,13 +43,19 @@
     }
   }
 
-  private final String nameSpace;
+  private final Optional<String> nameSpace;
 
   public EiffelConfig(String nameSpace) {
-    this.nameSpace = nameSpace;
+    if (nameSpace == null) {
+      this.nameSpace = Optional.empty();
+      logger.atInfo().log("namespace is not set in the config");
+    } else {
+      this.nameSpace = Optional.of(nameSpace);
+      logger.atInfo().log("namespace is set to: %d", nameSpace);
+    }
   }
 
-  public String nameSpace() {
+  public Optional<String> nameSpace() {
     return nameSpace;
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/mapping/EiffelEventFactory.java b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/mapping/EiffelEventFactory.java
index 07522fd..dc7eb92 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/mapping/EiffelEventFactory.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/mapping/EiffelEventFactory.java
@@ -42,6 +42,7 @@
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
+import java.util.Optional;
 import java.util.UUID;
 
 public class EiffelEventFactory {
@@ -61,7 +62,7 @@
     private final String webUrl;
     private final String pluginName;
     private final String sshAddress;
-    private final String nameSpace;
+    private final Optional<String> nameSpace;
 
     @Inject
     public Provider(
@@ -105,7 +106,7 @@
       String pluginVersion,
       String webUrl,
       String configuredSshAddress,
-      String nameSpace) {
+      Optional<String> nameSpace) {
     StringBuilder purl = new StringBuilder(PURL_BASE + pluginName);
     if (pluginVersion != null) {
       purl.append("@" + pluginVersion);
@@ -113,7 +114,7 @@
     this.packageUrl = purl.toString();
     this.host = URI.create(webUrl).getHost();
     this.pluginName = pluginName;
-    this.nameSpace = nameSpace;
+    this.nameSpace = nameSpace.orElse(host);
 
     StringBuilder commitEndpoint = new StringBuilder();
     commitEndpoint.append(webUrl);
@@ -132,7 +133,7 @@
     }
     cloneUrl.append("%s");
     projectCloneUrl = cloneUrl.toString();
-    tagPURLTemplate = "pkg:generic/" + nameSpace + "/%s@%s?vcs_url=git%%2B%s%%40%s";
+    tagPURLTemplate = "pkg:generic/" + this.nameSpace + "/%s@%s?vcs_url=git%%2B%s%%40%s";
   }
 
   private String parseSshAddress(String webUrl, String configuredSshAddress) {
diff --git a/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/EiffelEventsTest.java b/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/EiffelEventsTest.java
index ce326f4..c847bb0 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/EiffelEventsTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/EiffelEventsTest.java
@@ -24,7 +24,6 @@
 import com.google.gerrit.entities.RefNames;
 import com.google.gerrit.extensions.api.projects.TagInput;
 import com.google.inject.Provider;
-import com.googlesource.gerrit.plugins.eventseiffel.config.EiffelConfig;
 import com.googlesource.gerrit.plugins.eventseiffel.eiffel.EventKey;
 import com.googlesource.gerrit.plugins.eventseiffel.eiffel.SourceChangeEventKey;
 import com.googlesource.gerrit.plugins.eventseiffel.eiffel.api.EventStorageException;
@@ -189,15 +188,8 @@
 
     @Override
     public EiffelEventFactory get() {
-      return new EiffelEventFactory(PLUGIN_NAME, VERSION, WEB_URL, "*:" + SSH_PORT, NAMESPACE);
-    }
-  }
-
-  public static class TestEiffelConfigProvider implements Provider<EiffelConfig> {
-
-    @Override
-    public EiffelConfig get() {
-      return new EiffelConfig(NAMESPACE);
+      return new EiffelEventFactory(
+          PLUGIN_NAME, VERSION, WEB_URL, "*:" + SSH_PORT, Optional.of(NAMESPACE));
     }
   }
 }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/listeners/GerritEventListenersIT.java b/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/listeners/GerritEventListenersIT.java
index ab53d14..126eb50 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/listeners/GerritEventListenersIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/listeners/GerritEventListenersIT.java
@@ -320,9 +320,7 @@
       bind(EventListenersConfig.class).toProvider(TestEventListenerConfigProvider.class);
       DynamicSet.bind(binder(), RevisionCreatedListener.class).to(PatchsetCreatedListener.class);
       DynamicSet.bind(binder(), GitReferenceUpdatedListener.class).to(RefUpdateListener.class);
-      bind(EiffelConfig.class)
-          .toProvider(EiffelEventsTest.TestEiffelConfigProvider.class)
-          .in(Scopes.SINGLETON);
+      bind(EiffelConfig.class).toProvider(EiffelConfig.Provider.class).in(Scopes.SINGLETON);
     }
   }
 }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParserIT.java b/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParserIT.java
index 2941b38..4fcdb78 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParserIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParserIT.java
@@ -20,6 +20,8 @@
 
 import com.google.gerrit.acceptance.PushOneCommit;
 import com.google.gerrit.acceptance.TestPlugin;
+import com.google.gerrit.acceptance.UseLocalDisk;
+import com.google.gerrit.acceptance.config.GlobalPluginConfig;
 import com.google.gerrit.extensions.common.AccountInfo;
 import com.google.inject.AbstractModule;
 import com.google.inject.Scopes;
@@ -40,6 +42,8 @@
 import com.googlesource.gerrit.plugins.eventseiffel.eiffel.dto.EiffelSourceChangeSubmittedEventInfo;
 import com.googlesource.gerrit.plugins.eventseiffel.mapping.EiffelEventFactory;
 import com.googlesource.gerrit.plugins.eventseiffel.mapping.EiffelEventMapper;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.time.Instant;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.revwalk.RevCommit;
@@ -266,14 +270,25 @@
     assertEquals(0, TestEventHub.EVENTS.size());
   }
 
+  @Test
+  @UseLocalDisk
+  @GlobalPluginConfig(pluginName = PLUGIN_NAME, name = "EiffelEvent.namespace", value = NAMESPACE)
+  public void artcQueuedscsHandledWithCustomNameSpace() throws Exception {
+    assertArtcQueuedScsHandled(false, NAMESPACE);
+  }
+
   private void assertArtcQueuedScsHandled(boolean annotated) throws Exception {
+    assertArtcQueuedScsHandled(annotated, "localhost");
+  }
+
+  private void assertArtcQueuedScsHandled(boolean annotated, String nameSpace) throws Exception {
     setScsHandled();
     String ref =
         createTagRef(getHead(repo(), "HEAD").getName(), annotated)
             .substring(Constants.R_TAGS.length());
-    ArtifactEventKey artc = ArtifactEventKey.create(tagPURL(project.get(), ref));
+    ArtifactEventKey artc = ArtifactEventKey.create(tagPURL(project.get(), ref, nameSpace));
     CompositionDefinedEventKey cd =
-        CompositionDefinedEventKey.create(tagCompositionName(project.get()), ref);
+        CompositionDefinedEventKey.create(tagCompositionName(project.get(), nameSpace), ref);
 
     eventParser.createAndScheduleArtc(project.get(), ref, EPOCH_MILLIS, false);
     assertEquals(2, TestEventHub.EVENTS.size());
@@ -298,10 +313,22 @@
     assertEquals(expected, actual);
   }
 
+  protected static String tagCompositionName(String projectName) {
+    return tagCompositionName(projectName, "localhost");
+  }
+
+  protected static String tagCompositionName(String projectName, String nameSpace) {
+    return "scmtag/git/" + nameSpace + "/" + URLEncoder.encode(projectName, StandardCharsets.UTF_8);
+  }
+
   protected static String tagPURL(String projectName, String tagName) {
+    return tagPURL(projectName, tagName, "localhost");
+  }
+
+  protected static String tagPURL(String projectName, String tagName, String nameSpace) {
     return String.format(
         TAG_PURL_TEMPLATE,
-        NAMESPACE,
+        nameSpace,
         projectName,
         tagName,
         String.format("ssh://%s/%s", HOST_NAME, projectName),
@@ -318,9 +345,7 @@
       bind(EventMappingConfig.class).toProvider(EventMappingConfig.Provider.class);
       bind(EiffelEventMapper.class);
       bind(EiffelEventParser.class);
-      bind(EiffelConfig.class)
-          .toProvider(EiffelEventsTest.TestEiffelConfigProvider.class)
-          .in(Scopes.SINGLETON);
+      bind(EiffelConfig.class).toProvider(EiffelConfig.Provider.class).in(Scopes.SINGLETON);
       bind(EiffelEventFactory.class)
           .toProvider(EiffelEventFactory.Provider.class)
           .in(Scopes.SINGLETON);