From bb290c3e18761f9f111619e4d9991b3c691962c2 Mon Sep 17 00:00:00 2001 From: Clara Llorente Lemm <71637828+llorentelemmc@users.noreply.github.com> Date: Wed, 18 Oct 2023 09:28:23 +0200 Subject: [PATCH] fix: prevent renaming different resources to same name (#706) * fix: prevent renaming different resources to same name * refactor: clean up imports * refactor: rename class --------- Co-authored-by: Clara Llorente --- .../property/boundary/PropertyEditor.java | 2 +- .../control/ResourceValidationService.java | 8 +++- .../control/ResourcesScreenQueries.java | 7 +-- ...ourceValidationServiceIntegrationTest.java | 48 ++++++++++++++++--- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/property/boundary/PropertyEditor.java b/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/property/boundary/PropertyEditor.java index f22e121be..948e811d3 100644 --- a/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/property/boundary/PropertyEditor.java +++ b/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/property/boundary/PropertyEditor.java @@ -369,7 +369,7 @@ private ResourceEntity verifyAndSaveResource(Integer resourceId, ForeignableOwne private void verifyAndSetResourceName(String resourceName, ResourceEntity resource) throws AMWException { if (resourceName == null || !resourceName.equals(resource.getName())) { - resourceValidationService.validateResourceName(resourceName); + resourceValidationService.validateResourceName(resourceName, resource.getId()); resource.setName(resourceName); } } diff --git a/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/resourcegroup/control/ResourceValidationService.java b/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/resourcegroup/control/ResourceValidationService.java index 094e14b1b..96c253be8 100644 --- a/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/resourcegroup/control/ResourceValidationService.java +++ b/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/resourcegroup/control/ResourceValidationService.java @@ -40,15 +40,19 @@ public class ResourceValidationService { /** * @param resourceName - the resource name to be validated + * @param id - the id of the resource * @throws AMWException - if the name is invalid, an AMW exception is thrown */ - public void validateResourceName(String resourceName) throws AMWException { + public void validateResourceName(String resourceName, Integer id) throws AMWException { if (resourceName == null || resourceName.trim().isEmpty()) { throw new AMWException("The resource name must not be empty!"); } + if (id == null) { + throw new AMWException("The resource id must not be empty!"); + } // Check if a resource with the same name already exists... List resourceEntities = QueryUtils.fetch(ResourceEntity.class, - queries.searchResourceByName(resourceName), 0, -1); + queries.searchOtherResourcesWithName(resourceName, id), 0, -1); if (resourceEntities != null) { for (ResourceEntity resourceEntity : resourceEntities) { if (resourceEntity.getName().equalsIgnoreCase(resourceName)) { diff --git a/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/resourcegroup/control/ResourcesScreenQueries.java b/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/resourcegroup/control/ResourcesScreenQueries.java index 20478e822..4ea1b3b7c 100644 --- a/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/resourcegroup/control/ResourcesScreenQueries.java +++ b/AMW_business/src/main/java/ch/puzzle/itc/mobiliar/business/resourcegroup/control/ResourcesScreenQueries.java @@ -39,9 +39,10 @@ public Query searchResourceTypeByName(String resourceTypeName) { .setParameter("resourceTypeName", resourceTypeName); } - public Query searchResourceByName(String resourceName) { - return entityManager.createQuery("from ResourceEntity as res where res.name like :resourceName") - .setParameter("resourceName", resourceName); + public Query searchOtherResourcesWithName(String resourceName, Integer id) { + return entityManager.createQuery("from ResourceEntity as res where lower(res.name) like lower(:resourceName) and res.id != :id") + .setParameter("resourceName", resourceName) + .setParameter("id", id); } public Query searchResourceBySoftlinkIdAndHasNotResourceGroupId(String softlinkId, Integer resourceGroupId){ diff --git a/AMW_business/src/test/java/ch/puzzle/itc/mobiliar/business/resourcegroup/control/ResourceValidationServiceIntegrationTest.java b/AMW_business/src/test/java/ch/puzzle/itc/mobiliar/business/resourcegroup/control/ResourceValidationServiceIntegrationTest.java index dce710ea6..2159bc342 100644 --- a/AMW_business/src/test/java/ch/puzzle/itc/mobiliar/business/resourcegroup/control/ResourceValidationServiceIntegrationTest.java +++ b/AMW_business/src/test/java/ch/puzzle/itc/mobiliar/business/resourcegroup/control/ResourceValidationServiceIntegrationTest.java @@ -67,14 +67,29 @@ public void before() { @Test(expected = AMWException.class) public void testValidateResourceName() throws Exception { // given - String resourceName = "amw"; - ResourceEntity resourceA = new ResourceEntityBuilder().withName(resourceName).build(); + ResourceEntity resourceA = new ResourceEntityBuilder().withName("test").build(); + ResourceEntity resourceB = new ResourceEntityBuilder().withName("amw").build(); + + entityManager.persist(resourceA); + entityManager.persist(resourceB); + + assertNotNull(entityManager.find(ResourceEntity.class, resourceA.getId())); + + // when + service.validateResourceName("amw", resourceA.getId()); + } + + @Test + public void testRenameResourceNameToSame() throws Exception { + // given + ResourceEntity resourceA = new ResourceEntityBuilder().withName("test").build(); + entityManager.persist(resourceA); assertNotNull(entityManager.find(ResourceEntity.class, resourceA.getId())); // when - service.validateResourceName(resourceName); + service.validateResourceName("test", resourceA.getId()); } @Test @@ -88,11 +103,11 @@ public void shouldAllowToRenameAResourceToANameContainingUnderscore() throws Exc assertNotNull(entityManager.find(ResourceEntity.class, resourceA.getId())); // when - service.validateResourceName(newResourceName); + service.validateResourceName(newResourceName, resourceA.getId()); } @Test - public void shouldAllowToAlterCaseOfResourceName() throws Exception { + public void shouldAllowToAlterCaseOfSameResource() throws Exception { // given String resourceName = "amw"; String newResourceName = "AMW"; @@ -102,13 +117,32 @@ public void shouldAllowToAlterCaseOfResourceName() throws Exception { assertNotNull(entityManager.find(ResourceEntity.class, resourceA.getId())); // when - service.validateResourceName(newResourceName); + service.validateResourceName(newResourceName, resourceA.getId()); + } + + @Test(expected = AMWException.class) + public void shouldNotAllowToAlterCaseOfDifferentResource() throws AMWException { + // given + + ResourceEntity resourceA = new ResourceEntityBuilder().withName("myapp").build(); + entityManager.persist(resourceA); + + ResourceEntity resourceB = new ResourceEntityBuilder().withName("myapp1").build(); + entityManager.persist(resourceB); + + service.validateResourceName("myApp", resourceB.getId()); } @Test(expected = AMWException.class) public void testValidateResourceName_emptyName() throws Exception { // when - service.validateResourceName(null); + service.validateResourceName(null, null); + } + + @Test(expected = AMWException.class) + public void testValidateResourceName_emptyId() throws Exception { + // when + service.validateResourceName("test", null); } @Test(expected = AMWException.class)