TSK-597: fixed validation of parent id and key for classifications during creation.

This commit is contained in:
Holger Hagen 2018-06-26 16:59:24 +02:00 committed by Martin Rojas Miguel Angel
parent b84ee12f45
commit 104d5ed60e
9 changed files with 126 additions and 56 deletions

View File

@ -82,15 +82,13 @@ public interface ClassificationService {
* when the classification does already exists at the given domain.
* @throws NotAuthorizedException
* if the current user is not member of role BUSINESS_ADMIN or ADMIN
* @throws ClassificationNotFoundException
* if the current parentId is not NULL/EMPTY and can not be found.
* @throws DomainNotFoundException
* if the domain does not exist in the configuration
* @throws InvalidArgumentException
* if the ServiceLevel property does not comply with the ISO 8601 specification
*/
Classification createClassification(Classification classification)
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
throws ClassificationAlreadyExistException, NotAuthorizedException,
DomainNotFoundException, InvalidArgumentException;
/**

View File

@ -54,7 +54,7 @@ public class ClassificationServiceImpl implements ClassificationService {
@Override
public Classification createClassification(Classification classification)
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
throws ClassificationAlreadyExistException, NotAuthorizedException,
DomainNotFoundException, InvalidArgumentException {
LOGGER.debug("entry to createClassification(classification = {})", classification);
taskanaEngine.checkRoleMembership(TaskanaRole.BUSINESS_ADMIN, TaskanaRole.ADMIN);
@ -76,12 +76,8 @@ public class ClassificationServiceImpl implements ClassificationService {
classificationImpl.setModified(classificationImpl.getCreated());
this.initDefaultClassificationValues(classificationImpl);
if (classificationImpl.getParentId() != null && !classificationImpl.getParentId().isEmpty()) {
this.getClassification(classificationImpl.getParentId());
}
if (classificationImpl.getParentKey() != null && !classificationImpl.getParentKey().isEmpty()) {
this.getClassification(classificationImpl.getParentKey(), classificationImpl.getDomain());
}
validateAndPopulateParentInformation(classificationImpl);
classificationMapper.insert(classificationImpl);
LOGGER.debug("Method createClassification created classification {}.", classificationImpl);
@ -95,6 +91,32 @@ public class ClassificationServiceImpl implements ClassificationService {
return classificationImpl;
}
private void validateAndPopulateParentInformation(ClassificationImpl classificationImpl)
throws InvalidArgumentException {
try {
if (classificationImpl.getParentId() != null && !classificationImpl.getParentId().isEmpty()) {
Classification parentClassification = this.getClassification(classificationImpl.getParentId());
if (classificationImpl.getParentKey() != null && !classificationImpl.getParentKey().isEmpty()) {
if (!classificationImpl.getParentKey().equals(parentClassification.getKey())) {
throw new InvalidArgumentException(
"Given parent key of classification does not match key of parent id.");
}
classificationImpl.setParentKey(parentClassification.getKey());
}
}
if (classificationImpl.getParentKey() != null && !classificationImpl.getParentKey().isEmpty()) {
Classification parentClassification = this.getClassification(classificationImpl.getParentKey(),
classificationImpl.getDomain());
classificationImpl.setParentId(parentClassification.getId());
}
} catch (ClassificationNotFoundException e) {
throw new InvalidArgumentException("Parent classification could not be found.", e);
}
}
private void checkClassificationId(ClassificationImpl classificationImpl) throws InvalidArgumentException {
if (classificationImpl.getId() != null && !"".equals(classificationImpl.getId())) {
throw new InvalidArgumentException("ClassificationId should be null on creation");

View File

@ -101,8 +101,7 @@ public class CreateClassificationAccTest extends AbstractAccTest {
groupNames = {"group_1", "businessadmin"})
@Test
public void testCreateClassificationWithInvalidValues()
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
DomainNotFoundException {
throws ClassificationAlreadyExistException, NotAuthorizedException, DomainNotFoundException {
long amountOfClassificationsBefore = classificationService.createClassificationQuery().count();
// Check key NULL
@ -128,8 +127,8 @@ public class CreateClassificationAccTest extends AbstractAccTest {
groupNames = {"group_1", "businessadmin"})
@Test(expected = ClassificationAlreadyExistException.class)
public void testCreateClassificationAlreadyExisting()
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
DomainNotFoundException, InvalidArgumentException {
throws ClassificationAlreadyExistException, NotAuthorizedException, DomainNotFoundException,
InvalidArgumentException {
Classification classification = classificationService.newClassification("Key3", "", "TASK");
classification = classificationService.createClassification(classification);
classification = classificationService.createClassification(classification);
@ -140,8 +139,8 @@ public class CreateClassificationAccTest extends AbstractAccTest {
groupNames = {"group_1", "businessadmin"})
@Test(expected = DomainNotFoundException.class)
public void testCreateClassificationInUnknownDomain()
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
DomainNotFoundException, InvalidArgumentException {
throws ClassificationAlreadyExistException, NotAuthorizedException, DomainNotFoundException,
InvalidArgumentException {
Classification classification = classificationService.newClassification("Key3", "UNKNOWN_DOMAIN", "TASK");
classification = classificationService.createClassification(classification);
}
@ -151,8 +150,8 @@ public class CreateClassificationAccTest extends AbstractAccTest {
groupNames = {"group_1", "businessadmin"})
@Test(expected = InvalidArgumentException.class)
public void testCreateClassificationOfUnknownType()
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
DomainNotFoundException, InvalidArgumentException {
throws ClassificationAlreadyExistException, NotAuthorizedException, DomainNotFoundException,
InvalidArgumentException {
Classification classification = classificationService.newClassification("Key3", "DOMAIN_A", "UNKNOWN_TYPE");
classification = classificationService.createClassification(classification);
}
@ -162,8 +161,8 @@ public class CreateClassificationAccTest extends AbstractAccTest {
groupNames = {"group_1", "businessadmin"})
@Test(expected = InvalidArgumentException.class)
public void testCreateClassificationOfUnknownCategory()
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
DomainNotFoundException, InvalidArgumentException {
throws ClassificationAlreadyExistException, NotAuthorizedException, DomainNotFoundException,
InvalidArgumentException {
Classification classification = classificationService.newClassification("Key4", "DOMAIN_A", "TASK");
classification.setCategory("UNKNOWN_CATEGORY");
classification = classificationService.createClassification(classification);
@ -172,10 +171,10 @@ public class CreateClassificationAccTest extends AbstractAccTest {
@WithAccessId(
userName = "teamlead_1",
groupNames = {"group_1", "businessadmin"})
@Test(expected = ClassificationNotFoundException.class)
@Test(expected = InvalidArgumentException.class)
public void testCreateClassificationWithInvalidParentId()
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
DomainNotFoundException, InvalidArgumentException {
throws ClassificationAlreadyExistException, NotAuthorizedException, DomainNotFoundException,
InvalidArgumentException {
Classification classification = classificationService.newClassification("Key5", "", "TASK");
classification.setParentId("ID WHICH CANT BE FOUND");
classification = classificationService.createClassification(classification);
@ -184,10 +183,10 @@ public class CreateClassificationAccTest extends AbstractAccTest {
@WithAccessId(
userName = "teamlead_1",
groupNames = {"group_1", "businessadmin"})
@Test(expected = ClassificationNotFoundException.class)
@Test(expected = InvalidArgumentException.class)
public void testCreateClassificationWithInvalidParentKey()
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
DomainNotFoundException, InvalidArgumentException {
throws ClassificationAlreadyExistException, NotAuthorizedException, DomainNotFoundException,
InvalidArgumentException {
Classification classification = classificationService.newClassification("Key5", "", "TASK");
classification.setParentKey("KEY WHICH CANT BE FOUND");
classification = classificationService.createClassification(classification);

View File

@ -63,7 +63,7 @@ public class ClassificationServiceImplTest {
@Test(expected = ClassificationAlreadyExistException.class)
public void testCreateClassificationAlreadyExisting()
throws ClassificationAlreadyExistException, ClassificationNotFoundException, NotAuthorizedException,
throws ClassificationAlreadyExistException, NotAuthorizedException,
DomainNotFoundException, InvalidArgumentException {
Classification classification = createDummyClassification();
doReturn(classification).when(classificationMapperMock).findByKeyAndDomain(classification.getKey(),
@ -84,7 +84,7 @@ public class ClassificationServiceImplTest {
}
}
@Test(expected = ClassificationNotFoundException.class)
@Test(expected = InvalidArgumentException.class)
public void testCreateClassificationParentIdNotExisting()
throws ClassificationAlreadyExistException, ClassificationNotFoundException, NotAuthorizedException,
DomainNotFoundException, InvalidArgumentException {
@ -97,7 +97,7 @@ public class ClassificationServiceImplTest {
try {
cutSpy.createClassification(classification);
} catch (ClassificationNotFoundException e) {
} catch (InvalidArgumentException e) {
verify(taskanaEngineImplMock, times(1)).checkRoleMembership(any());
verify(taskanaEngineImplMock, times(2)).openConnection();
verify(classificationMapperMock, times(1)).findByKeyAndDomain(classification.getKey(),
@ -111,7 +111,7 @@ public class ClassificationServiceImplTest {
}
}
@Test(expected = ClassificationNotFoundException.class)
@Test(expected = InvalidArgumentException.class)
public void testCreateClassificationParentKeyNotExisting()
throws ClassificationAlreadyExistException, ClassificationNotFoundException, NotAuthorizedException,
DomainNotFoundException, InvalidArgumentException {
@ -125,7 +125,7 @@ public class ClassificationServiceImplTest {
try {
cutSpy.createClassification(classification);
} catch (ClassificationNotFoundException e) {
} catch (InvalidArgumentException e) {
verify(taskanaEngineImplMock, times(1)).checkRoleMembership(any());
verify(taskanaEngineImplMock, times(2)).openConnection();
verify(classificationMapperMock, times(1)).findByKeyAndDomain(classification.getKey(),
@ -143,7 +143,7 @@ public class ClassificationServiceImplTest {
@Test
public void testCreateClassificationInOwnDomainButExistingInRoot()
throws ClassificationAlreadyExistException, ClassificationNotFoundException, InterruptedException,
throws ClassificationAlreadyExistException, InterruptedException,
NotAuthorizedException, DomainNotFoundException, InvalidArgumentException {
Instant beforeTimestamp = Instant.now();
Thread.sleep(10L);
@ -176,8 +176,8 @@ public class ClassificationServiceImplTest {
@Test
public void testCreateClassificationInOwnDomainAndCopyInRootDomain()
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
DomainNotFoundException, InvalidArgumentException {
throws ClassificationAlreadyExistException, NotAuthorizedException, DomainNotFoundException,
InvalidArgumentException {
Classification classification = createDummyClassification("");
String domain = classification.getDomain();
String key = classification.getKey();
@ -203,7 +203,7 @@ public class ClassificationServiceImplTest {
@Test
public void testCreateClassificationIntoRootDomain()
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
throws ClassificationAlreadyExistException, NotAuthorizedException,
DomainNotFoundException, InvalidArgumentException {
ClassificationImpl classification = (ClassificationImpl) createDummyClassification(null);
classification.setDomain("");
@ -364,7 +364,7 @@ public class ClassificationServiceImplTest {
@Test(expected = InvalidArgumentException.class)
public void testThrowExceptionIdIfClassificationIsCreatedWithAnExplicitId()
throws ClassificationNotFoundException, DomainNotFoundException, InvalidArgumentException,
throws DomainNotFoundException, InvalidArgumentException,
NotAuthorizedException, ClassificationAlreadyExistException {
try {
Classification classification = createDummyClassification();

View File

@ -153,8 +153,8 @@ public class ClassificationServiceImplIntAutoCommitTest {
@Test
public void testFindAllClassifications()
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
DomainNotFoundException, InvalidArgumentException {
throws ClassificationAlreadyExistException, NotAuthorizedException, DomainNotFoundException,
InvalidArgumentException {
Classification classification0 = this.createDummyClassificationWithUniqueKey("", "TASK");
classificationService.createClassification(classification0);
Classification classification1 = this.createDummyClassificationWithUniqueKey("", "TASK");
@ -183,8 +183,8 @@ public class ClassificationServiceImplIntAutoCommitTest {
@Test
public void testInsertAndClassificationMapper()
throws NotAuthorizedException, ClassificationAlreadyExistException, ClassificationNotFoundException,
InvalidArgumentException, DomainNotFoundException {
throws NotAuthorizedException, ClassificationAlreadyExistException, InvalidArgumentException,
DomainNotFoundException {
Classification classification = this.createDummyClassificationWithUniqueKey("DOMAIN_A", "TASK");
classification = classificationService.createClassification(classification);
@ -221,8 +221,8 @@ public class ClassificationServiceImplIntAutoCommitTest {
@Test
public void testFindWithClassificationMapperDomainAndCategory()
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
DomainNotFoundException, InvalidArgumentException {
throws ClassificationAlreadyExistException, NotAuthorizedException, DomainNotFoundException,
InvalidArgumentException {
Classification classification1 = this.createDummyClassificationWithUniqueKey("DOMAIN_A", "TASK");
classification1.setCategory("EXTERNAL");
classificationService.createClassification(classification1);
@ -244,8 +244,8 @@ public class ClassificationServiceImplIntAutoCommitTest {
@Test
public void testFindWithClassificationMapperCustomAndCategory()
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
DomainNotFoundException, InvalidArgumentException {
throws ClassificationAlreadyExistException, NotAuthorizedException, DomainNotFoundException,
InvalidArgumentException {
Classification classification1 = this.createDummyClassificationWithUniqueKey("", "TASK");
classification1.setDescription("DESC1");
classification1.setCategory("EXTERNAL");
@ -283,7 +283,7 @@ public class ClassificationServiceImplIntAutoCommitTest {
@Test
public void testFindWithClassificationMapperPriorityTypeAndParent()
throws ClassificationAlreadyExistException, NumberFormatException, NotAuthorizedException,
ClassificationNotFoundException, DomainNotFoundException, InvalidArgumentException {
DomainNotFoundException, InvalidArgumentException {
Classification classification = this.createDummyClassificationWithUniqueKey("", "TASK");
classification.setPriority(Integer.decode("5"));
classificationService.createClassification(classification);
@ -317,8 +317,8 @@ public class ClassificationServiceImplIntAutoCommitTest {
@Test
public void testFindWithClassificationMapperServiceLevelNameAndDescription()
throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException,
DomainNotFoundException, InvalidArgumentException {
throws ClassificationAlreadyExistException, NotAuthorizedException, DomainNotFoundException,
InvalidArgumentException {
int all = 0;
Classification classification = this.createDummyClassificationWithUniqueKey("", "TASK");
classification.setServiceLevel("P1D");

View File

@ -166,7 +166,7 @@ public class ClassificationServiceImplIntExplicitTest {
@Test
public void testFindAllClassifications()
throws SQLException, ClassificationAlreadyExistException, NotAuthorizedException,
ClassificationNotFoundException, DomainNotFoundException, InvalidArgumentException {
DomainNotFoundException, InvalidArgumentException {
Connection connection = dataSource.getConnection();
taskanaEngineImpl.setConnection(connection);
Classification classification0 = this.createNewClassificationWithUniqueKey("", "TASK");
@ -204,7 +204,7 @@ public class ClassificationServiceImplIntExplicitTest {
@Test
public void testInsertAndClassificationQuery()
throws SQLException, ClassificationAlreadyExistException, NotAuthorizedException,
ClassificationNotFoundException, DomainNotFoundException, InvalidArgumentException {
DomainNotFoundException, InvalidArgumentException {
Connection connection = dataSource.getConnection();
taskanaEngineImpl.setConnection(connection);
Classification classification = this.createNewClassificationWithUniqueKey("DOMAIN_A", "TASK");
@ -249,7 +249,7 @@ public class ClassificationServiceImplIntExplicitTest {
@Test
public void testFindWithClassificationMapperDomainAndCategory()
throws SQLException, ClassificationAlreadyExistException, NotAuthorizedException,
ClassificationNotFoundException, DomainNotFoundException, InvalidArgumentException {
DomainNotFoundException, InvalidArgumentException {
Connection connection = dataSource.getConnection();
taskanaEngineImpl.setConnection(connection);
Classification classification1 = this.createNewClassificationWithUniqueKey("DOMAIN_A", "TASK");
@ -276,7 +276,7 @@ public class ClassificationServiceImplIntExplicitTest {
@Test
public void testFindWithClassificationMapperCustomAndCategory()
throws SQLException, ClassificationAlreadyExistException, NotAuthorizedException,
ClassificationNotFoundException, DomainNotFoundException, InvalidArgumentException {
DomainNotFoundException, InvalidArgumentException {
Connection connection = dataSource.getConnection();
taskanaEngineImpl.setConnection(connection);
Classification classification1 = this.createNewClassificationWithUniqueKey("", "TASK");
@ -317,7 +317,7 @@ public class ClassificationServiceImplIntExplicitTest {
@Test
public void testFindWithClassificationMapperPriorityTypeAndParent()
throws SQLException, ClassificationAlreadyExistException, NotAuthorizedException,
ClassificationNotFoundException, DomainNotFoundException, InvalidArgumentException {
DomainNotFoundException, InvalidArgumentException {
Connection connection = dataSource.getConnection();
taskanaEngineImpl.setConnection(connection);
Classification classification = this.createNewClassificationWithUniqueKey("", "TASK");
@ -354,7 +354,7 @@ public class ClassificationServiceImplIntExplicitTest {
@Test
public void testFindWithClassificationMapperServiceLevelNameAndDescription()
throws NotAuthorizedException, SQLException, ClassificationAlreadyExistException,
ClassificationNotFoundException, DomainNotFoundException, InvalidArgumentException {
DomainNotFoundException, InvalidArgumentException {
Connection connection = dataSource.getConnection();
taskanaEngineImpl.setConnection(connection);
int all = 0;

View File

@ -459,8 +459,8 @@ public class TaskServiceImplIntExplicitTest {
}
}
private Task generateDummyTask() throws ClassificationAlreadyExistException, ClassificationNotFoundException,
InvalidWorkbasketException, NotAuthorizedException,
private Task generateDummyTask()
throws ClassificationAlreadyExistException, InvalidWorkbasketException, NotAuthorizedException,
WorkbasketAlreadyExistException, DomainNotFoundException, InvalidArgumentException {
WorkbasketImpl workbasket = (WorkbasketImpl) workbasketService.newWorkbasket("wb", "DOMAIN_A");
workbasket.setName("wb");

View File

@ -183,6 +183,57 @@ public class ClassificationControllerIntTest {
con.disconnect();
}
@Test
public void testCreateClassificationWithParentId() throws IOException {
String newClassification = "{\"classificationId\":\"\",\"category\":\"MANUAL\",\"domain\":\"DOMAIN_B\",\"key\":\"NEW_CLASS_P1\",\"name\":\"new classification\",\"type\":\"TASK\",\"parentId\":\"CLI:200000000000000000000000000000000015\"}";
URL url = new URL(server + port + "/v1/classifications");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x");
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
out.write(newClassification);
out.flush();
out.close();
assertEquals(201, con.getResponseCode());
con.disconnect();
}
@Test
public void testCreateClassificationWithParentKey() throws IOException {
String newClassification = "{\"classificationId\":\"\",\"category\":\"MANUAL\",\"domain\":\"DOMAIN_B\",\"key\":\"NEW_CLASS_P2\",\"name\":\"new classification\",\"type\":\"TASK\",\"parentKey\":\"T2100\"}";
URL url = new URL(server + port + "/v1/classifications");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x");
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
out.write(newClassification);
out.flush();
out.close();
assertEquals(201, con.getResponseCode());
con.disconnect();
}
@Test
public void testReturn400IfCreateClassificationWithIncompatibleParentIdAndKey() throws IOException {
String newClassification = "{\"classificationId\":\"\",\"category\":\"MANUAL\",\"domain\":\"DOMAIN_B\",\"key\":\"NEW_CLASS_P3\",\"name\":\"new classification\",\"type\":\"TASK\",\"parentId\":\"CLI:200000000000000000000000000000000015\",\"parentKey\":\"T2000\"}";
URL url = new URL(server + port + "/v1/classifications");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x");
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
out.write(newClassification);
out.flush();
out.close();
assertEquals(400, con.getResponseCode());
con.disconnect();
}
@Test
public void testCreateClassificationWithClassificationIdReturnsError400() throws IOException {
String newClassification = "{\"classificationId\":\"someId\",\"category\":\"MANUAL\",\"domain\":\"DOMAIN_A\",\"key\":\"NEW_CLASS\",\"name\":\"new classification\",\"type\":\"TASK\"}";

View File

@ -60,7 +60,7 @@ public class ClassificationResource extends ResourceSupport {
}
public String getParentKey() {
return parentId;
return parentKey;
}
public void setParentKey(String parentKey) {