TSK-1379: Verify customAttributes format in taskRepresentationModel

This commit is contained in:
Joerg Heffner 2020-08-18 09:54:49 +02:00 committed by gitgoodjhe
parent f87f90e0ff
commit 231d009d81
5 changed files with 92 additions and 6 deletions

View File

@ -20,6 +20,7 @@ import pro.taskana.TaskanaSpringBootTest;
import pro.taskana.classification.api.models.Classification;
import pro.taskana.classification.rest.assembler.ClassificationRepresentationModelAssembler;
import pro.taskana.classification.rest.models.ClassificationRepresentationModel;
import pro.taskana.common.api.exceptions.InvalidArgumentException;
import pro.taskana.common.rest.Mapping;
import pro.taskana.task.api.models.Task;
import pro.taskana.task.rest.assembler.TaskRepresentationModelAssembler;
@ -50,7 +51,7 @@ class AsyncUpdateJobIntTest {
}
@Test
void testUpdateClassificationPrioServiceLevel() {
void testUpdateClassificationPrioServiceLevel() throws InvalidArgumentException {
// 1st step: get old classification :
final Instant before = Instant.now();
@ -145,7 +146,8 @@ class AsyncUpdateJobIntTest {
}
}
private void verifyTaskIsModifiedAfterOrEquals(String taskId, Instant before) {
private void verifyTaskIsModifiedAfterOrEquals(String taskId, Instant before)
throws InvalidArgumentException {
ResponseEntity<TaskRepresentationModel> taskResponse =
TEMPLATE.exchange(

View File

@ -701,7 +701,6 @@ public class TaskController extends AbstractPagingController {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Exit from applySortingParams(), returning {}", query);
}
}
private int[] extractPriorities(String[] prioritiesInString) {

View File

@ -11,6 +11,7 @@ import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import pro.taskana.classification.rest.assembler.ClassificationSummaryRepresentationModelAssembler;
import pro.taskana.common.api.exceptions.InvalidArgumentException;
import pro.taskana.common.api.exceptions.SystemException;
import pro.taskana.task.api.TaskCustomField;
import pro.taskana.task.api.TaskService;
@ -106,7 +107,8 @@ public class TaskRepresentationModelAssembler
return repModel;
}
public Task toEntityModel(TaskRepresentationModel repModel) {
public Task toEntityModel(TaskRepresentationModel repModel) throws InvalidArgumentException {
verifyCorrectCustomAttributesFormat(repModel);
TaskImpl task =
(TaskImpl) taskService.newTask(repModel.getWorkbasketSummary().getWorkbasketId());
task.setId(repModel.getTaskId());
@ -154,7 +156,6 @@ public class TaskRepresentationModelAssembler
.collect(Collectors.toList()));
task.setCustomAttributeMap(
repModel.getCustomAttributes().stream()
.filter(e -> Objects.nonNull(e.getKey()) && !e.getKey().isEmpty())
.collect(Collectors.toMap(CustomAttribute::getKey, CustomAttribute::getValue)));
task.setCallbackInfo(
repModel.getCallbackInfo().stream()
@ -162,4 +163,19 @@ public class TaskRepresentationModelAssembler
.collect(Collectors.toMap(CustomAttribute::getKey, CustomAttribute::getValue)));
return task;
}
private void verifyCorrectCustomAttributesFormat(TaskRepresentationModel repModel)
throws InvalidArgumentException {
if (repModel.getCustomAttributes().stream()
.anyMatch(
customAttribute ->
customAttribute.getKey() == null
|| customAttribute.getKey().isEmpty()
|| customAttribute.getValue() == null)) {
throw new InvalidArgumentException(
"Format of custom attributes is not valid. Please provide the following format: "
+ "\"customAttributes\": [{\"key\": \"someKey\",\"value\": \"someValue\"},{...}])");
}
}
}

View File

@ -11,6 +11,8 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.jupiter.api.BeforeAll;
@ -35,6 +37,7 @@ import pro.taskana.sampledata.SampleDataGenerator;
import pro.taskana.task.api.TaskState;
import pro.taskana.task.api.models.ObjectReference;
import pro.taskana.task.rest.models.TaskRepresentationModel;
import pro.taskana.task.rest.models.TaskRepresentationModel.CustomAttribute;
import pro.taskana.task.rest.models.TaskSummaryRepresentationModel;
import pro.taskana.workbasket.rest.models.WorkbasketSummaryRepresentationModel;
@ -240,6 +243,71 @@ class TaskControllerIntTest {
assertThat(response.getBody().getContent()).hasSize(4);
}
@Test
void should_ReturnAllTasksByWildcardSearch_For_ProvidedSearchValu4c() {
TaskRepresentationModel taskRepresentationModel = getTaskResourceSample();
List<CustomAttribute> customAttributesWithNullKey = new ArrayList<>();
customAttributesWithNullKey.add(CustomAttribute.of(null, "value"));
taskRepresentationModel.setCustomAttributes(customAttributesWithNullKey);
ThrowingCallable httpCall =
() -> {
TEMPLATE.exchange(
restHelper.toUrl(Mapping.URL_TASKS),
HttpMethod.POST,
new HttpEntity<>(taskRepresentationModel, restHelper.getHeadersTeamlead_1()),
TASK_MODEL_TYPE);
};
assertThatThrownBy(httpCall)
.isInstanceOf(HttpClientErrorException.class)
.hasMessageContaining("Format of custom attributes is not valid")
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.BAD_REQUEST);
List<CustomAttribute> customAttributesWithEmptyKey = new ArrayList<>();
customAttributesWithEmptyKey.add(CustomAttribute.of("", "value"));
taskRepresentationModel.setCustomAttributes(customAttributesWithEmptyKey);
httpCall =
() -> {
TEMPLATE.exchange(
restHelper.toUrl(Mapping.URL_TASKS),
HttpMethod.POST,
new HttpEntity<>(taskRepresentationModel, restHelper.getHeadersTeamlead_1()),
TASK_MODEL_TYPE);
};
assertThatThrownBy(httpCall)
.isInstanceOf(HttpClientErrorException.class)
.hasMessageContaining("Format of custom attributes is not valid")
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.BAD_REQUEST);
List<CustomAttribute> customAttributesWithNullValue = new ArrayList<>();
customAttributesWithNullValue.add(CustomAttribute.of("key", null));
taskRepresentationModel.setCustomAttributes(customAttributesWithNullValue);
httpCall =
() -> {
TEMPLATE.exchange(
restHelper.toUrl(Mapping.URL_TASKS),
HttpMethod.POST,
new HttpEntity<>(taskRepresentationModel, restHelper.getHeadersTeamlead_1()),
TASK_MODEL_TYPE);
};
assertThatThrownBy(httpCall)
.isInstanceOf(HttpClientErrorException.class)
.hasMessageContaining("Format of custom attributes is not valid")
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.BAD_REQUEST);
}
@Test
void should_DeleteAllTasks_For_ProvidedParams() {
ResponseEntity<TaskanaPagedModel<TaskSummaryRepresentationModel>> response =

View File

@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import pro.taskana.classification.api.ClassificationService;
import pro.taskana.classification.api.models.ClassificationSummary;
import pro.taskana.classification.rest.models.ClassificationSummaryRepresentationModel;
import pro.taskana.common.api.exceptions.InvalidArgumentException;
import pro.taskana.common.rest.Mapping;
import pro.taskana.common.rest.TaskanaSpringBootTest;
import pro.taskana.task.api.TaskCustomField;
@ -178,7 +179,7 @@ class TaskRepresentationModelAssemberTest {
}
@Test
void should_Equal_When_ComparingEntityWithConvertedEntity() {
void should_Equal_When_ComparingEntityWithConvertedEntity() throws InvalidArgumentException {
// given
ObjectReference primaryObjRef = new ObjectReference();
primaryObjRef.setId("abc");