From 4d153a808924ff290925cbb8bc04c9250e96659c Mon Sep 17 00:00:00 2001
From: Sonia Zorba <sonia.zorba@inaf.it>
Date: Fri, 19 Mar 2021 16:47:32 +0100
Subject: [PATCH] Group names bugfix

---
 .../it/inaf/ia2/gms/service/GroupNameService.java |  9 +++++++--
 .../ia2/gms/service/GroupNameServiceTest.java     | 15 +++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/gms/src/main/java/it/inaf/ia2/gms/service/GroupNameService.java b/gms/src/main/java/it/inaf/ia2/gms/service/GroupNameService.java
index 4519f2e..ffb5d2b 100644
--- a/gms/src/main/java/it/inaf/ia2/gms/service/GroupNameService.java
+++ b/gms/src/main/java/it/inaf/ia2/gms/service/GroupNameService.java
@@ -85,8 +85,13 @@ public class GroupNameService {
     }
 
     public String getShortGroupName(String completeGroupName, Optional<String> groupPrefix) {
-        if (groupPrefix.isPresent()) {
-            return completeGroupName.substring(groupPrefix.get().length() + 1);
+        if (groupPrefix.isPresent() && !groupPrefix.get().isBlank()) {
+            if (groupPrefix.get().endsWith(".")) {
+                // this branch is kept for retro-compatibility with old API, it will be removed in the future
+                return completeGroupName.substring(groupPrefix.get().length());
+            } else {
+                return completeGroupName.substring(groupPrefix.get().length() + 1);
+            }
         }
         return completeGroupName;
     }
diff --git a/gms/src/test/java/it/inaf/ia2/gms/service/GroupNameServiceTest.java b/gms/src/test/java/it/inaf/ia2/gms/service/GroupNameServiceTest.java
index 46bc11f..6cec8b7 100644
--- a/gms/src/test/java/it/inaf/ia2/gms/service/GroupNameServiceTest.java
+++ b/gms/src/test/java/it/inaf/ia2/gms/service/GroupNameServiceTest.java
@@ -99,4 +99,19 @@ public class GroupNameServiceTest {
     public void extractGroupNamesTestNull() {
         assertTrue(groupNameService.extractGroupNames(null).isEmpty());
     }
+
+    @Test
+    public void testGetShortGroupNameOld() {
+        assertEquals("INAF", groupNameService.getShortGroupName("LBT.INAF", Optional.of("LBT.")));
+    }
+
+    @Test
+    public void testGetShortGroupName() {
+        assertEquals("INAF", groupNameService.getShortGroupName("LBT.INAF", Optional.of("LBT")));
+    }
+
+    @Test
+    public void testGetShortGroupNameEmpty() {
+        assertEquals("LBT.INAF", groupNameService.getShortGroupName("LBT.INAF", Optional.of("")));
+    }
 }
-- 
GitLab