From 2bd1f5f9a2497bc0afb36d7d354ca197a270efe5 Mon Sep 17 00:00:00 2001
From: RIHTARSIC Joze <joze.rihtarsic@ext.ec.europa.eu>
Date: Wed, 18 Sep 2024 18:24:52 +0200
Subject: [PATCH] [EDELIVERY-13936] Delete domain with multiple groups

---
 .../smp/services/ui/UIDomainAdminService.java | 24 +++++------
 .../smp/services/ui/UIDomainServiceTest.java  | 43 ++++++++++++-------
 2 files changed, 38 insertions(+), 29 deletions(-)

diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainAdminService.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainAdminService.java
index cbe6ffc57..c2048e8b1 100644
--- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainAdminService.java
+++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainAdminService.java
@@ -57,6 +57,7 @@ import java.util.stream.Collectors;
 public class UIDomainAdminService extends UIServiceBase<DBDomain, DomainRO> {
 
     private static final SMPLogger LOG = SMPLoggerFactory.getLogger(UIDomainAdminService.class);
+    public static final String DOMAIN_DOES_NOT_EXIST_IN_DATABASE = "Domain does not exist in database!";
 
     private final DomainDao domainDao;
     private final DomainConfigurationDao domainConfigurationDao;
@@ -65,7 +66,6 @@ public class UIDomainAdminService extends UIServiceBase<DBDomain, DomainRO> {
     private final ResourceDefDao resourceDefDao;
     private final DomainResourceDefDao domainResourceDefDao;
     private final ConversionService conversionService;
-    private final GroupDao groupDao;
     private final GroupMemberDao groupMemberDao;
     private final SMLIntegrationService smlIntegrationService;
 
@@ -76,7 +76,6 @@ public class UIDomainAdminService extends UIServiceBase<DBDomain, DomainRO> {
                                 ResourceDao resourceDao,
                                 ResourceDefDao resourceDefDao,
                                 DomainResourceDefDao domainResourceDefDao,
-                                GroupDao groupDao,
                                 GroupMemberDao groupMemberDao,
                                 SMLIntegrationService smlIntegrationService) {
         this.conversionService = conversionService;
@@ -86,7 +85,6 @@ public class UIDomainAdminService extends UIServiceBase<DBDomain, DomainRO> {
         this.resourceDefDao = resourceDefDao;
         this.domainResourceDefDao = domainResourceDefDao;
         this.domainMemberDao = domainMemberDao;
-        this.groupDao = groupDao;
         this.groupMemberDao = groupMemberDao;
         this.smlIntegrationService = smlIntegrationService;
     }
@@ -146,14 +144,13 @@ public class UIDomainAdminService extends UIServiceBase<DBDomain, DomainRO> {
      *
      * @param domainId
      * @param data
-     * @return
      */
     @Transactional
     public void updateBasicDomainData(Long domainId, DomainRO data) {
         DBDomain domain = domainDao.find(domainId);
         if (domain == null) {
             LOG.warn("Can not update domain for ID [{}], because it does not exists!", domainId);
-            throw new BadRequestException(ErrorBusinessCode.NOT_FOUND, "Domain does not exist in database!");
+            throw new BadRequestException(ErrorBusinessCode.NOT_FOUND, DOMAIN_DOES_NOT_EXIST_IN_DATABASE);
         }
         domain.setDomainCode(data.getDomainCode());
         domain.setDefaultResourceTypeIdentifier(data.getDefaultResourceTypeIdentifier());
@@ -165,7 +162,7 @@ public class UIDomainAdminService extends UIServiceBase<DBDomain, DomainRO> {
     public void updateDomainSmlIntegrationData(Long domainId, DomainRO data) {
         DBDomain domain = domainDao.find(domainId);
         if (domain == null) {
-            throw new BadRequestException(ErrorBusinessCode.NOT_FOUND, "Domain does not exist in database!");
+            throw new BadRequestException(ErrorBusinessCode.NOT_FOUND, DOMAIN_DOES_NOT_EXIST_IN_DATABASE);
         }
         if (domain.isSmlRegistered() && !StringUtils.equals(data.getSmlSmpId(), domain.getSmlSmpId())) {
             String msg = "SMP-SML identifier must not change for registered domain [" + domain.getDomainCode() + "]!";
@@ -196,7 +193,7 @@ public class UIDomainAdminService extends UIServiceBase<DBDomain, DomainRO> {
         LOG.info("add resources: [{}]", resourceDefIds);
         if (domain == null) {
             LOG.warn("Can not delete domain for ID [{}], because it does not exists!", domainId);
-            throw new BadRequestException(ErrorBusinessCode.NOT_FOUND, "Domain does not exist in database!");
+            throw new BadRequestException(ErrorBusinessCode.NOT_FOUND,DOMAIN_DOES_NOT_EXIST_IN_DATABASE);
         }
 
         //filter and validate resources to be removed
@@ -239,7 +236,7 @@ public class UIDomainAdminService extends UIServiceBase<DBDomain, DomainRO> {
     public List<DomainPropertyRO> getDomainProperties(Long domainId) {
         DBDomain domain = domainDao.find(domainId);
         if (domain == null) {
-            throw new BadRequestException(ErrorBusinessCode.NOT_FOUND, "Domain does not exist in database!");
+            throw new BadRequestException(ErrorBusinessCode.NOT_FOUND,DOMAIN_DOES_NOT_EXIST_IN_DATABASE);
         }
         return domainConfigurationDao.getDomainPropertiesForRole(domain, SMPRole.SYSTEM_ADMIN).stream()
                 .map(domainConfiguration -> conversionService.convert(domainConfiguration, DomainPropertyRO.class))
@@ -250,7 +247,7 @@ public class UIDomainAdminService extends UIServiceBase<DBDomain, DomainRO> {
     public List<DomainPropertyRO> updateDomainProperties(Long domainId, List<DomainPropertyRO> domainProperties) {
         DBDomain domain = domainDao.find(domainId);
         if (domain == null) {
-            throw new BadRequestException(ErrorBusinessCode.NOT_FOUND, "Domain does not exist in database!");
+            throw new BadRequestException(ErrorBusinessCode.NOT_FOUND, DOMAIN_DOES_NOT_EXIST_IN_DATABASE);
         }
         return domainConfigurationDao.updateDomainPropertiesForRole(domain, domainProperties, SMPRole.SYSTEM_ADMIN)
         .stream()
@@ -275,7 +272,7 @@ public class UIDomainAdminService extends UIServiceBase<DBDomain, DomainRO> {
         DBDomain domain = domainDao.find(domainId);
         if (domain == null) {
             LOG.warn("Can not delete domain for ID [{}], because it does not exists!", domainId);
-            throw new BadRequestException(ErrorBusinessCode.NOT_FOUND, "Domain does not exist in database!");
+            throw new BadRequestException(ErrorBusinessCode.NOT_FOUND, DOMAIN_DOES_NOT_EXIST_IN_DATABASE);
         }
         if (domain.isSmlRegistered()) {
             LOG.info("Can not delete domain for ID [{}], is registered to SML!", domainId);
@@ -294,10 +291,10 @@ public class UIDomainAdminService extends UIServiceBase<DBDomain, DomainRO> {
             domainMemberDao.remove(member);
         }
         // delete all groups
-        List<DBGroup> groupList = domain.getDomainGroups();
+        List<DBGroup> groupList = Collections.unmodifiableList(domain.getDomainGroups());
         for (DBGroup group : groupList) {
             // all groups should be without resources see the check above:  getResourceCountForDomain
-            deleteDomainGroup(group);
+            deleteGroupMembers(group);
         }
         // finally remove the domain
         domainDao.remove(domain);
@@ -306,11 +303,10 @@ public class UIDomainAdminService extends UIServiceBase<DBDomain, DomainRO> {
         return domainRO;
     }
 
-    private void deleteDomainGroup(DBGroup group) {
+    private void deleteGroupMembers(DBGroup group) {
         List<DBGroupMember> memberList = groupMemberDao.getGroupMembers(group.getId(), -1, -1, null);
         for (DBGroupMember member : memberList) {
             groupMemberDao.remove(member);
         }
-        groupDao.remove(group);
     }
 }
diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainServiceTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainServiceTest.java
index f728f540d..b17d2c624 100644
--- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainServiceTest.java
+++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainServiceTest.java
@@ -8,9 +8,9 @@
  * versions of the EUPL (the "Licence");
  * You may not use this work except in compliance with the Licence.
  * You may obtain a copy of the Licence at:
- * 
+ *
  * [PROJECT_HOME]\license\eupl-1.2\license.txt or https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software distributed under the Licence is
  * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the Licence for the specific language governing permissions and limitations under the Licence.
@@ -46,7 +46,7 @@ class UIDomainServiceTest extends AbstractServiceTest {
     private UIDomainAdminService testInstance;
     @Autowired
     private DomainDao domainDao;
-//     @Autowired
+    //     @Autowired
     @SpyBean
     private SMLIntegrationService smlIntegrationService;
 
@@ -55,7 +55,6 @@ class UIDomainServiceTest extends AbstractServiceTest {
         testUtilsDao.clearData();
         testUtilsDao.createResourceDefinitionsForDomains();
 
-//        smlIntegrationService = Mockito.spy(smlIntegrationService);
         ReflectionTestUtils.setField(testInstance, "smlIntegrationService", smlIntegrationService);
     }
 
@@ -102,9 +101,9 @@ class UIDomainServiceTest extends AbstractServiceTest {
 
     @Test
     void updateSMLDomainData_domainNotFound() {
-        BadRequestException result =  assertThrows(BadRequestException.class, () ->
-                testInstance.updateDomainSmlIntegrationData(-1l, new DomainRO()));
-         assertEquals("Domain does not exist in database!", result.getMessage());
+        BadRequestException result = assertThrows(BadRequestException.class, () ->
+                testInstance.updateDomainSmlIntegrationData(-1L, new DomainRO()));
+        assertEquals("Domain does not exist in database!", result.getMessage());
     }
 
     @Test
@@ -114,9 +113,9 @@ class UIDomainServiceTest extends AbstractServiceTest {
         DomainRO domainRO = new DomainRO();
         domainRO.setSmlSmpId("utestRegistered03");
 
-        BadRequestException result =  assertThrows(BadRequestException.class, () ->
-                    testInstance.updateDomainSmlIntegrationData(domain.getId(), domainRO));
-         assertEquals("SMP-SML identifier must not change for registered domain [utestRegistered03]!", result.getMessage());
+        BadRequestException result = assertThrows(BadRequestException.class, () ->
+                testInstance.updateDomainSmlIntegrationData(domain.getId(), domainRO));
+        assertEquals("SMP-SML identifier must not change for registered domain [utestRegistered03]!", result.getMessage());
     }
 
     @Test
@@ -133,9 +132,9 @@ class UIDomainServiceTest extends AbstractServiceTest {
 
         Mockito.doReturn(false).when(smlIntegrationService).isDomainValid(domain);
 
-        BadRequestException result =  assertThrows(BadRequestException.class, () ->
+        BadRequestException result = assertThrows(BadRequestException.class, () ->
                 testInstance.updateDomainSmlIntegrationData(domain.getId(), domainRO));
-         assertEquals("The SML-SMP certificate for domain [utestRegistered03] is not valid!", result.getMessage());
+        assertEquals("The SML-SMP certificate for domain [utestRegistered03] is not valid!", result.getMessage());
     }
 
     @Test
@@ -165,7 +164,7 @@ class UIDomainServiceTest extends AbstractServiceTest {
     }
 
     @Test
-    void testGetDomainProperties(){
+    void testGetDomainProperties() {
         DBDomain domain = testUtilsDao.getD1();
         List<DomainPropertyRO> domainROList = testInstance.getDomainProperties(domain.getId());
 
@@ -175,7 +174,7 @@ class UIDomainServiceTest extends AbstractServiceTest {
     }
 
     @Test
-    void testUpdateDomainProperties(){
+    void testUpdateDomainProperties() {
         String newValue = "new value";
         DBDomain domain = testUtilsDao.getD1();
         List<DomainPropertyRO> domainROList = testInstance.getDomainProperties(domain.getId());
@@ -185,7 +184,7 @@ class UIDomainServiceTest extends AbstractServiceTest {
             domainPropertyRO.setValue(newValue);
             domainPropertyRO.setSystemDefault(!domainPropertyRO.isSystemDefault());
         }
-        List<DomainPropertyRO> domainROListUpdated =  testInstance.updateDomainProperties(domain.getId(), domainROList);
+        List<DomainPropertyRO> domainROListUpdated = testInstance.updateDomainProperties(domain.getId(), domainROList);
 
         List<DomainPropertyRO> domainROListUpdated2 = testInstance.getDomainProperties(domain.getId());
         assertEquals(SMPDomainPropertyEnum.values().length, domainROListUpdated2.size());
@@ -205,4 +204,18 @@ class UIDomainServiceTest extends AbstractServiceTest {
         assertNull(result);
     }
 
+    @Test
+    void deleteDomainMultipleGroups() {
+        DBDomain domain = testUtilsDao.getD1();
+        DBDomain test = domainDao.find(domain.getId());
+        testUtilsDao.createGroup("group1", VisibilityType.PUBLIC, test);
+        testUtilsDao.createGroup("group2", VisibilityType.PRIVATE, test);
+
+        testInstance.deleteDomain(domain.getId());
+
+        DBDomain result = domainDao.find(domain.getId());
+        assertNull(result);
+    }
+
+
 }
-- 
GitLab