TSK-1115: Validate that Service Level is not negative (#1454)

This commit is contained in:
tge20 2021-01-25 16:49:17 +01:00 committed by GitHub
parent 92b49fc850
commit abfa07ce6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View File

@ -315,9 +315,10 @@ public class ClassificationServiceImpl implements ClassificationService {
} }
private static void validateServiceLevel(String serviceLevel) throws InvalidArgumentException { private static void validateServiceLevel(String serviceLevel) throws InvalidArgumentException {
try { Duration duration;
Duration.parse(serviceLevel);
try {
duration = Duration.parse(serviceLevel);
} catch (Exception e) { } catch (Exception e) {
throw new InvalidArgumentException( throw new InvalidArgumentException(
String.format( String.format(
@ -328,10 +329,16 @@ public class ClassificationServiceImpl implements ClassificationService {
serviceLevel), serviceLevel),
e.getCause()); e.getCause());
} }
// check that the duration is based on format PnD, i.e. it must start with a P, end with a D
String serviceLevelLower = serviceLevel.toLowerCase();
if (!serviceLevelLower.startsWith("p") || !serviceLevelLower.endsWith("d")) {
if (duration.isNegative()) {
throw new InvalidArgumentException(
String.format(
"Invalid service level %s. Taskana does not support a negative service level.",
serviceLevel));
}
// check that the duration is based on format PnD, i.e. it must start with a P, end with a D
if (!serviceLevel.toLowerCase().startsWith("p") || !serviceLevel.toLowerCase().endsWith("d")) {
throw new InvalidArgumentException( throw new InvalidArgumentException(
String.format( String.format(
"Invalid service level %s. Taskana only supports service " "Invalid service level %s. Taskana only supports service "

View File

@ -165,6 +165,17 @@ class CreateClassificationAccTest extends AbstractAccTest {
.isInstanceOf(InvalidArgumentException.class); .isInstanceOf(InvalidArgumentException.class);
} }
@WithAccessId(user = "businessadmin")
@Test
void should_ThrowException_When_TryingToCreateClassificationWithNegativeServiceLevel() {
Classification classification =
CLASSIFICATION_SERVICE.newClassification("someKey234", "DOMAIN_A", "TASK");
classification.setServiceLevel("P-1D");
assertThatThrownBy(() -> CLASSIFICATION_SERVICE.createClassification(classification))
.isInstanceOf(InvalidArgumentException.class);
}
@WithAccessId(user = "businessadmin") @WithAccessId(user = "businessadmin")
@Test @Test
void testCreateClassificationWithInvalidValues() { void testCreateClassificationWithInvalidValues() {