Skip to content

Commit

Permalink
fix: prevent renaming different resources to same name (#706)
Browse files Browse the repository at this point in the history
* fix: prevent renaming different resources to same name

* refactor: clean up imports

* refactor: rename class

---------

Co-authored-by: Clara Llorente <[email protected]>
  • Loading branch information
llorentelemmc and llorentelemmc authored Oct 18, 2023
1 parent 8c9198e commit bb290c3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResourceEntity> 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
Expand All @@ -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)
Expand Down

0 comments on commit bb290c3

Please sign in to comment.