TSK-1468: Added validation of name and type of workbasket when updating Workbasket (#1366)

* TSK-1468: Added validation of name and type of workbasket when updating Workbasket

* TSK-1468: Improvements after review
This commit is contained in:
tge20 2020-12-14 12:13:16 +01:00 committed by GitHub
parent d29021da0b
commit 218e038901
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 35 deletions

View File

@ -65,13 +65,15 @@ public interface WorkbasketService {
* *
* @param workbasket The Workbasket to update * @param workbasket The Workbasket to update
* @return the updated Workbasket * @return the updated Workbasket
* @throws InvalidWorkbasketException if workbasket name or type is invalid
* @throws NotAuthorizedException if the current user is not authorized to update the work basket * @throws NotAuthorizedException if the current user is not authorized to update the work basket
* @throws WorkbasketNotFoundException if the workbasket cannot be found. * @throws WorkbasketNotFoundException if the workbasket cannot be found.
* @throws ConcurrencyException if an attempt is made to update the workbasket and another user * @throws ConcurrencyException if an attempt is made to update the workbasket and another user
* updated it already * updated it already
*/ */
Workbasket updateWorkbasket(Workbasket workbasket) Workbasket updateWorkbasket(Workbasket workbasket)
throws NotAuthorizedException, WorkbasketNotFoundException, ConcurrencyException; throws InvalidWorkbasketException, NotAuthorizedException, WorkbasketNotFoundException,
ConcurrencyException;
/** /**
* Returns a new WorkbasketAccessItem which is not persisted. * Returns a new WorkbasketAccessItem which is not persisted.

View File

@ -173,38 +173,30 @@ public class WorkbasketServiceImpl implements WorkbasketService {
@Override @Override
public Workbasket updateWorkbasket(Workbasket workbasketToUpdate) public Workbasket updateWorkbasket(Workbasket workbasketToUpdate)
throws NotAuthorizedException, WorkbasketNotFoundException, ConcurrencyException { throws NotAuthorizedException, WorkbasketNotFoundException, ConcurrencyException,
InvalidWorkbasketException {
LOGGER.debug("entry to updateWorkbasket(Workbasket = {})", workbasketToUpdate); LOGGER.debug("entry to updateWorkbasket(Workbasket = {})", workbasketToUpdate);
taskanaEngine.getEngine().checkRoleMembership(TaskanaRole.BUSINESS_ADMIN, TaskanaRole.ADMIN); taskanaEngine.getEngine().checkRoleMembership(TaskanaRole.BUSINESS_ADMIN, TaskanaRole.ADMIN);
WorkbasketImpl workbasketImplToUpdate = (WorkbasketImpl) workbasketToUpdate;
WorkbasketImpl workbasketImplToUpdate = null; validateNameAndType(workbasketToUpdate);
try { try {
taskanaEngine.openConnection(); taskanaEngine.openConnection();
workbasketImplToUpdate = (WorkbasketImpl) workbasketToUpdate;
Workbasket oldWorkbasket = Workbasket oldWorkbasket =
this.getWorkbasket(workbasketImplToUpdate.getKey(), workbasketImplToUpdate.getDomain()); this.getWorkbasket(workbasketImplToUpdate.getKey(), workbasketImplToUpdate.getDomain());
checkModifiedHasNotChanged(oldWorkbasket, workbasketImplToUpdate); checkModifiedHasNotChanged(oldWorkbasket, workbasketImplToUpdate);
workbasketImplToUpdate.setModified(Instant.now()); workbasketImplToUpdate.setModified(Instant.now());
if (workbasketImplToUpdate.getId() == null || workbasketImplToUpdate.getId().isEmpty()) { if (workbasketImplToUpdate.getId() == null || workbasketImplToUpdate.getId().isEmpty()) {
workbasketMapper.updateByKeyAndDomain(workbasketImplToUpdate); workbasketMapper.updateByKeyAndDomain(workbasketImplToUpdate);
} else { } else {
workbasketMapper.update(workbasketImplToUpdate); workbasketMapper.update(workbasketImplToUpdate);
} }
if (HistoryEventManager.isHistoryEnabled()) { if (HistoryEventManager.isHistoryEnabled()) {
String details = String details =
ObjectAttributeChangeDetector.determineChangesInAttributes( ObjectAttributeChangeDetector.determineChangesInAttributes(
oldWorkbasket, workbasketToUpdate); oldWorkbasket, workbasketToUpdate);
@ -216,15 +208,13 @@ public class WorkbasketServiceImpl implements WorkbasketService {
taskanaEngine.getEngine().getCurrentUserContext().getUserid(), taskanaEngine.getEngine().getCurrentUserContext().getUserid(),
details)); details));
} }
LOGGER.debug( LOGGER.debug(
"Method updateWorkbasket() updated workbasket '{}'", workbasketImplToUpdate.getId()); "Method updateWorkbasket() updated workbasket '{}'", workbasketImplToUpdate.getId());
return workbasketImplToUpdate; return workbasketImplToUpdate;
} finally { } finally {
taskanaEngine.returnConnection(); taskanaEngine.returnConnection();
LOGGER.debug("exit from updateWorkbasket(). Returning result {} ", workbasketImplToUpdate); LOGGER.debug("exit from updateWorkbasket(). Returning result {} ", workbasketImplToUpdate);
} }
} }
@ -841,7 +831,7 @@ public class WorkbasketServiceImpl implements WorkbasketService {
LOGGER.debug("entry to deleteWorkbasket(workbasketId = {})", workbasketId); LOGGER.debug("entry to deleteWorkbasket(workbasketId = {})", workbasketId);
taskanaEngine.getEngine().checkRoleMembership(TaskanaRole.BUSINESS_ADMIN, TaskanaRole.ADMIN); taskanaEngine.getEngine().checkRoleMembership(TaskanaRole.BUSINESS_ADMIN, TaskanaRole.ADMIN);
validateWorkbasketId(workbasketId); validateId(workbasketId);
try { try {
taskanaEngine.openConnection(); taskanaEngine.openConnection();
@ -1104,16 +1094,6 @@ public class WorkbasketServiceImpl implements WorkbasketService {
return accessItems; return accessItems;
} }
private void validateWorkbasketId(String workbasketId) throws InvalidArgumentException {
if (workbasketId == null) {
throw new InvalidArgumentException("The WorkbasketId can´t be NULL");
}
if (workbasketId.isEmpty()) {
throw new InvalidArgumentException("The WorkbasketId can´t be EMPTY for deleteWorkbasket()");
}
}
private long getCountTasksByWorkbasketId(String workbasketId) { private long getCountTasksByWorkbasketId(String workbasketId) {
return taskanaEngine return taskanaEngine
.getEngine() .getEngine()
@ -1158,20 +1138,17 @@ public class WorkbasketServiceImpl implements WorkbasketService {
private void validateWorkbasket(Workbasket workbasket) private void validateWorkbasket(Workbasket workbasket)
throws InvalidWorkbasketException, DomainNotFoundException { throws InvalidWorkbasketException, DomainNotFoundException {
// check that required properties (database not null) are set // check that required properties (database not null) are set
validateNameAndType(workbasket);
if (workbasket.getId() == null || workbasket.getId().length() == 0) { if (workbasket.getId() == null || workbasket.getId().length() == 0) {
throw new InvalidWorkbasketException("Id must not be null for " + workbasket); throw new InvalidWorkbasketException("Id must not be null for " + workbasket);
} else if (workbasket.getKey() == null || workbasket.getKey().length() == 0) {
throw new InvalidWorkbasketException("Key must not be null for " + workbasket);
} }
if (workbasket.getName() == null || workbasket.getName().length() == 0) { if (workbasket.getKey() == null || workbasket.getKey().length() == 0) {
throw new InvalidWorkbasketException("Name must not be null for " + workbasket); throw new InvalidWorkbasketException("Key must not be null for " + workbasket);
} }
if (workbasket.getDomain() == null) { if (workbasket.getDomain() == null) {
throw new InvalidWorkbasketException("Domain must not be null for " + workbasket); throw new InvalidWorkbasketException("Domain must not be null for " + workbasket);
} }
if (workbasket.getType() == null) {
throw new InvalidWorkbasketException("Type must not be null for " + workbasket);
}
if (!taskanaEngine.domainExists(workbasket.getDomain())) { if (!taskanaEngine.domainExists(workbasket.getDomain())) {
throw new DomainNotFoundException( throw new DomainNotFoundException(
workbasket.getDomain(), workbasket.getDomain(),
@ -1179,6 +1156,27 @@ public class WorkbasketServiceImpl implements WorkbasketService {
} }
} }
private void validateId(String workbasketId) throws InvalidArgumentException {
if (workbasketId == null) {
throw new InvalidArgumentException("The WorkbasketId can´t be NULL");
}
if (workbasketId.isEmpty()) {
throw new InvalidArgumentException("The WorkbasketId can´t be EMPTY for deleteWorkbasket()");
}
}
private void validateNameAndType(Workbasket workbasket) throws InvalidWorkbasketException {
if (workbasket.getName() == null) {
throw new InvalidWorkbasketException("Name must not be NULL for " + workbasket);
}
if (workbasket.getName().length() == 0) {
throw new InvalidWorkbasketException("Name must not be EMPTY for " + workbasket);
}
if (workbasket.getType() == null) {
throw new InvalidWorkbasketException("Type must not be NULL for " + workbasket);
}
}
private List<WorkbasketPermission> getPermissionsFromWorkbasketAccessItem( private List<WorkbasketPermission> getPermissionsFromWorkbasketAccessItem(
WorkbasketAccessItem workbasketAccessItem) { WorkbasketAccessItem workbasketAccessItem) {
List<WorkbasketPermission> permissions = new ArrayList<>(); List<WorkbasketPermission> permissions = new ArrayList<>();
@ -1199,7 +1197,7 @@ public class WorkbasketServiceImpl implements WorkbasketService {
taskanaEngine.getEngine().checkRoleMembership(TaskanaRole.BUSINESS_ADMIN, TaskanaRole.ADMIN); taskanaEngine.getEngine().checkRoleMembership(TaskanaRole.BUSINESS_ADMIN, TaskanaRole.ADMIN);
try { try {
taskanaEngine.openConnection(); taskanaEngine.openConnection();
validateWorkbasketId(workbasketId); validateId(workbasketId);
WorkbasketImpl workbasket = workbasketMapper.findById(workbasketId); WorkbasketImpl workbasket = workbasketMapper.findById(workbasketId);
workbasket.setMarkedForDeletion(true); workbasket.setMarkedForDeletion(true);
workbasketMapper.update(workbasket); workbasketMapper.update(workbasket);

View File

@ -18,6 +18,7 @@ import pro.taskana.common.test.security.WithAccessId;
import pro.taskana.workbasket.api.WorkbasketCustomField; import pro.taskana.workbasket.api.WorkbasketCustomField;
import pro.taskana.workbasket.api.WorkbasketService; import pro.taskana.workbasket.api.WorkbasketService;
import pro.taskana.workbasket.api.WorkbasketType; import pro.taskana.workbasket.api.WorkbasketType;
import pro.taskana.workbasket.api.exceptions.InvalidWorkbasketException;
import pro.taskana.workbasket.api.exceptions.WorkbasketNotFoundException; import pro.taskana.workbasket.api.exceptions.WorkbasketNotFoundException;
import pro.taskana.workbasket.api.models.Workbasket; import pro.taskana.workbasket.api.models.Workbasket;
import pro.taskana.workbasket.internal.models.WorkbasketImpl; import pro.taskana.workbasket.internal.models.WorkbasketImpl;
@ -57,6 +58,35 @@ class UpdateWorkbasketAccTest extends AbstractAccTest {
assertThat(updatedWorkbasket.getModified()).isNotEqualTo(modified); assertThat(updatedWorkbasket.getModified()).isNotEqualTo(modified);
} }
@WithAccessId(user = "businessadmin")
@Test
void should_ThrowException_When_UpdatingWorkbasketWithInvalidName() throws Exception {
WorkbasketService workbasketService = taskanaEngine.getWorkbasketService();
WorkbasketImpl workbasket =
(WorkbasketImpl) workbasketService.getWorkbasket("GPK_KSC", "DOMAIN_A");
workbasket.setName(null);
assertThatThrownBy(() -> workbasketService.updateWorkbasket(workbasket))
.isInstanceOf(InvalidWorkbasketException.class);
workbasket.setName("");
assertThatThrownBy(() -> workbasketService.updateWorkbasket(workbasket))
.isInstanceOf(InvalidWorkbasketException.class);
}
@WithAccessId(user = "businessadmin")
@Test
void should_ThrowException_When_UpdatingWorkbasketWithTypeNull() throws Exception {
WorkbasketService workbasketService = taskanaEngine.getWorkbasketService();
WorkbasketImpl workbasket =
(WorkbasketImpl) workbasketService.getWorkbasket("GPK_KSC", "DOMAIN_A");
workbasket.setType(null);
assertThatThrownBy(() -> workbasketService.updateWorkbasket(workbasket))
.isInstanceOf(InvalidWorkbasketException.class);
}
@WithAccessId(user = "businessadmin") @WithAccessId(user = "businessadmin")
@Test @Test
void testUpdateWorkbasketWithConcurrentModificationShouldThrowException() throws Exception { void testUpdateWorkbasketWithConcurrentModificationShouldThrowException() throws Exception {