TSK-1150 - added taskId as pathvariable to updateTaskComment and createTaskComment

-Added tests
This commit is contained in:
Jörg Heffner 2020-03-19 12:30:52 +01:00 committed by Holger Hagen
parent 4eba81bd57
commit c5bb6fb32c
4 changed files with 366 additions and 87 deletions

View File

@ -209,7 +209,7 @@ class TaskCommentServiceImpl {
throw new TaskCommentNotFoundException(
taskCommentId,
String.format(
"TaskComment for taskIid '%s' and taskCommentId '%s' was not found",
"TaskComment for taskId '%s' and taskCommentId '%s' was not found",
taskId, taskCommentId));
}

View File

@ -113,15 +113,24 @@ public class TaskCommentController {
@PutMapping(path = Mapping.URL_TASK_COMMENT)
@Transactional(readOnly = true, rollbackFor = Exception.class)
public ResponseEntity<TaskCommentResource> updateTaskComment(
@PathVariable String taskCommentId, @RequestBody TaskCommentResource taskCommentResource)
@PathVariable String taskId,
@PathVariable String taskCommentId,
@RequestBody TaskCommentResource taskCommentResource)
throws NotAuthorizedException, TaskNotFoundException, TaskCommentNotFoundException,
InvalidArgumentException, ConcurrencyException {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Entry to updateTaskComment(taskCommentId= {})", taskCommentId);
LOGGER.debug(
"Entry to updateTaskComment(taskId= {}, taskCommentId= {}, taskCommentResource= {})",
taskId,
taskCommentId,
taskCommentResource);
}
ResponseEntity<TaskCommentResource> result;
if (taskCommentId.equals(taskCommentResource.getTaskCommentId())) {
if ((taskCommentId.equals(taskCommentResource.getTaskCommentId())
&& (taskId.equals(taskCommentResource.getTaskId())))) {
TaskComment taskComment = taskCommentResourceAssembler.toModel(taskCommentResource);
taskComment = taskService.updateTaskComment(taskComment);
@ -129,8 +138,8 @@ public class TaskCommentController {
} else {
throw new InvalidArgumentException(
String.format(
"TaskCommentId ('%s') is not identical with the taskCommentId of "
+ "object in the payload which should be updated. ID=('%s')",
"TaskCommentId ('%s') or TaskId ('%s') are not identical with the ids"
+ " of object in the payload which should be updated",
taskCommentId, taskCommentResource.getTaskId()));
}
@ -144,19 +153,34 @@ public class TaskCommentController {
@PostMapping(path = Mapping.URL_TASK_COMMENTS)
@Transactional(rollbackFor = Exception.class)
public ResponseEntity<TaskCommentResource> createTaskComment(
@RequestBody TaskCommentResource taskCommentResource)
@PathVariable String taskId, @RequestBody TaskCommentResource taskCommentResource)
throws NotAuthorizedException, InvalidArgumentException, TaskNotFoundException {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Entry to createTaskComment(taskCommentResource= {})", taskCommentResource);
LOGGER.debug(
"Entry to createTaskComment(taskId = {}, taskCommentResource= {})",
taskId,
taskCommentResource);
}
TaskComment taskCommentFromResource = taskCommentResourceAssembler.toModel(taskCommentResource);
TaskComment createdTaskComment = taskService.createTaskComment(taskCommentFromResource);
ResponseEntity<TaskCommentResource> result;
ResponseEntity<TaskCommentResource> result =
ResponseEntity.status(HttpStatus.CREATED)
.body(taskCommentResourceAssembler.toResource(createdTaskComment));
if (taskId.equals(taskCommentResource.getTaskId())) {
TaskComment taskCommentFromResource =
taskCommentResourceAssembler.toModel(taskCommentResource);
TaskComment createdTaskComment = taskService.createTaskComment(taskCommentFromResource);
result =
ResponseEntity.status(HttpStatus.CREATED)
.body(taskCommentResourceAssembler.toResource(createdTaskComment));
} else {
throw new InvalidArgumentException(
String.format(
"TaskId ('%s') is not identical with the taskId of "
+ "object in the payload which should be updated.",
taskCommentResource.getTaskId()));
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Exit from createTaskComment(), returning {}", result);
}

View File

@ -143,9 +143,17 @@ public class TaskanaRestExceptionHandler extends ResponseEntityExceptionHandler
Exception ex, WebRequest req, HttpStatus status, boolean logExceptionOnError) {
TaskanaErrorData errorData = new TaskanaErrorData(status, ex, req);
if (logExceptionOnError) {
LOGGER.error("Error occurred during processing of rest request: " + errorData.toString(), ex);
LOGGER.error(
String.format(
"Error occurred during processing of rest request: %s", errorData.toString()),
ex);
} else {
LOGGER.debug("Error occurred during processing of rest request: " + errorData.toString(), ex);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(
String.format(
"Error occurred during processing of rest request: %s", errorData.toString()),
ex);
}
}
return ResponseEntity.status(status).body(errorData);
}

View File

@ -1,13 +1,18 @@
package pro.taskana.rest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.Instant;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.hateoas.Link;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
@ -45,13 +50,15 @@ class TaskCommentControllerIntTest {
"TKI:000000000000000000000000000000000000",
"Non existing task comment Id");
assertThatThrownBy(
() ->
template.exchange(
urlToNonExistingTaskComment,
HttpMethod.GET,
new HttpEntity<String>(restHelper.getHeadersAdmin()),
ParameterizedTypeReference.forType(TaskCommentResource.class)))
ThrowingCallable httpCall =
() -> {
template.exchange(
urlToNonExistingTaskComment,
HttpMethod.GET,
new HttpEntity<String>(restHelper.getHeadersAdmin()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.NOT_FOUND);
}
@ -61,13 +68,15 @@ class TaskCommentControllerIntTest {
String urlToNonExistingTask = restHelper.toUrl(Mapping.URL_TASK_COMMENTS, "nonExistingTaskId");
assertThatThrownBy(
() ->
template.exchange(
urlToNonExistingTask,
HttpMethod.GET,
new HttpEntity<String>(restHelper.getHeadersAdmin()),
ParameterizedTypeReference.forType(TaskCommentListResource.class)))
ThrowingCallable httpCall =
() -> {
template.exchange(
urlToNonExistingTask,
HttpMethod.GET,
new HttpEntity<String>(restHelper.getHeadersAdmin()),
ParameterizedTypeReference.forType(TaskCommentListResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.NOT_FOUND);
}
@ -79,12 +88,17 @@ class TaskCommentControllerIntTest {
String urlToNotVisibleTask =
restHelper.toUrl(Mapping.URL_TASK_COMMENTS, "TKI:000000000000000000000000000000000004");
ResponseEntity<TaskCommentListResource> response =
template.exchange(
urlToNotVisibleTask,
HttpMethod.GET,
new HttpEntity<String>(restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentListResource.class));
ThrowingCallable httpCall =
() -> {
template.exchange(
urlToNotVisibleTask,
HttpMethod.GET,
new HttpEntity<String>(restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentListResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.FORBIDDEN);
}
@Disabled("Disabled until Authorization check is up!")
@ -97,12 +111,17 @@ class TaskCommentControllerIntTest {
"TKI:000000000000000000000000000000000004",
"TCI:000000000000000000000000000000000013");
ResponseEntity<TaskCommentResource> response =
template.exchange(
urlToNotVisibleTask,
HttpMethod.GET,
new HttpEntity<String>(restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
ThrowingCallable httpCall =
() -> {
template.exchange(
urlToNotVisibleTask,
HttpMethod.GET,
new HttpEntity<String>(restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.FORBIDDEN);
}
@Disabled("Disabled until Authorization check is up!")
@ -113,14 +132,16 @@ class TaskCommentControllerIntTest {
taskCommentResourceToCreate.setTaskId("TKI:000000000000000000000000000000000004");
taskCommentResourceToCreate.setTextField("newly created task comment");
assertThatThrownBy(
() ->
template.exchange(
restHelper.toUrl(
Mapping.URL_TASK_COMMENTS, "TKI:000000000000000000000000000000000004"),
HttpMethod.POST,
new HttpEntity<>(taskCommentResourceToCreate, restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentResource.class)))
ThrowingCallable httpCall =
() -> {
template.exchange(
restHelper.toUrl(
Mapping.URL_TASK_COMMENTS, "TKI:000000000000000000000000000000000004"),
HttpMethod.POST,
new HttpEntity<>(taskCommentResourceToCreate, restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.FORBIDDEN);
}
@ -132,51 +153,277 @@ class TaskCommentControllerIntTest {
taskCommentResourceToCreate.setTaskId("DefinatelyNotExistingId");
taskCommentResourceToCreate.setTextField("newly created task comment");
assertThatThrownBy(
() ->
template.exchange(
restHelper.toUrl(Mapping.URL_TASK_COMMENTS, "NotExistingTaskId"),
HttpMethod.POST,
new HttpEntity<>(taskCommentResourceToCreate, restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentResource.class)))
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.NOT_FOUND);
TaskCommentResource taskCommentResourceToCreate2 = new TaskCommentResource();
taskCommentResourceToCreate.setTaskId(null);
taskCommentResourceToCreate.setTextField("newly created task comment");
assertThatThrownBy(
() ->
template.exchange(
restHelper.toUrl(Mapping.URL_TASK_COMMENTS, "NotExistingTaskId"),
HttpMethod.POST,
new HttpEntity<>(taskCommentResourceToCreate2, restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentResource.class)))
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.NOT_FOUND);
TaskCommentResource taskCommentResourceToCreate3 = new TaskCommentResource();
taskCommentResourceToCreate.setTaskId("");
taskCommentResourceToCreate.setTextField("newly created task comment");
assertThatThrownBy(
() ->
template.exchange(
restHelper.toUrl(Mapping.URL_TASK_COMMENTS, "NotExistingTaskId"),
HttpMethod.POST,
new HttpEntity<>(taskCommentResourceToCreate3, restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentResource.class)))
ThrowingCallable httpCall =
() -> {
template.exchange(
restHelper.toUrl(Mapping.URL_TASK_COMMENTS, "DefinatelyNotExistingId"),
HttpMethod.POST,
new HttpEntity<>(taskCommentResourceToCreate, restHelper.getHeadersAdmin()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.NOT_FOUND);
}
@Test
void testUpdateTaskCommentWithConcurrentModificationShouldFail() {}
void testCreateTaskCommentWithDifferentTaskIdInResourceShouldFail() {
TaskCommentResource taskCommentResourceToCreate = new TaskCommentResource();
taskCommentResourceToCreate.setTaskId("TKI:000000000000000000000000000000000000");
taskCommentResourceToCreate.setTextField("newly created task comment");
ThrowingCallable httpCall =
() -> {
template.exchange(
restHelper.toUrl(Mapping.URL_TASK_COMMENTS, "DifferentTaskId"),
HttpMethod.POST,
new HttpEntity<>(taskCommentResourceToCreate, restHelper.getHeadersAdmin()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.BAD_REQUEST);
}
@Test
void testUpdateTaskCommentWithNoAuthorizationShouldFail() {}
void testUpdateTaskCommentWithConcurrentModificationShouldFail() {
final ObjectMapper mapper = new ObjectMapper();
String url =
restHelper.toUrl(
Mapping.URL_TASK_COMMENT,
"TKI:000000000000000000000000000000000000",
"TCI:000000000000000000000000000000000000");
ResponseEntity<TaskCommentResource> getTaskCommentResponse =
template.exchange(
url,
HttpMethod.GET,
new HttpEntity<String>(restHelper.getHeadersAdmin()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
assertThat(getTaskCommentResponse.getBody().getLink(Link.REL_SELF)).isNotNull();
assertThat(getTaskCommentResponse.getBody().getCreator()).isEqualTo("user_1_1");
assertThat(getTaskCommentResponse.getBody().getTextField()).isEqualTo("some text in textfield");
TaskCommentResource taskCommentResourceToUpdate = getTaskCommentResponse.getBody();
taskCommentResourceToUpdate.setModified(Instant.now().toString());
ThrowingCallable httpCall =
() -> {
template.exchange(
url,
HttpMethod.PUT,
new HttpEntity<>(
mapper.writeValueAsString(taskCommentResourceToUpdate),
restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.CONFLICT);
}
@Disabled("Disabled until Authorization check is up!")
@Test
void testUpdateTaskCommentWithNoAuthorizationShouldFail() {
final ObjectMapper mapper = new ObjectMapper();
String url =
restHelper.toUrl(
Mapping.URL_TASK_COMMENT,
"TKI:000000000000000000000000000000000000",
"TCI:000000000000000000000000000000000000");
ResponseEntity<TaskCommentResource> getTaskCommentResponse =
template.exchange(
url,
HttpMethod.GET,
new HttpEntity<String>(restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
assertThat(getTaskCommentResponse.getBody().getLink(Link.REL_SELF)).isNotNull();
assertThat(getTaskCommentResponse.getBody().getCreator()).isEqualTo("user_1_1");
assertThat(getTaskCommentResponse.getBody().getTextField()).isEqualTo("some text in textfield");
TaskCommentResource taskCommentResourceToUpdate = getTaskCommentResponse.getBody();
taskCommentResourceToUpdate.setTextField("updated textfield");
ThrowingCallable httpCall =
() -> {
template.exchange(
url,
HttpMethod.PUT,
new HttpEntity<>(
mapper.writeValueAsString(taskCommentResourceToUpdate),
restHelper.getHeadersUser_1_2()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.FORBIDDEN);
}
@Test
void testUpdateTaskCommentOfNotExistingTaskShouldFail() {}
void testUpdateTaskCommentOfNotExistingTaskShouldFail() {
final ObjectMapper mapper = new ObjectMapper();
TaskCommentResource taskCommentResourceToUpdate = new TaskCommentResource();
taskCommentResourceToUpdate.setTaskId("TKI:000000000000000000000000000000009999");
taskCommentResourceToUpdate.setTaskCommentId("TCI:000000000000000000000000000000000000");
taskCommentResourceToUpdate.setTextField("updated text");
String url =
restHelper.toUrl(
Mapping.URL_TASK_COMMENT,
"TKI:000000000000000000000000000000009999",
"TCI:000000000000000000000000000000000000");
ThrowingCallable httpCall =
() -> {
template.exchange(
url,
HttpMethod.PUT,
new HttpEntity<>(
mapper.writeValueAsString(taskCommentResourceToUpdate),
restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.NOT_FOUND);
}
@Test
void testUpdateTaskCommentWithDifferentIdsInResourceShouldFail() {
final ObjectMapper mapper = new ObjectMapper();
String url =
restHelper.toUrl(
Mapping.URL_TASK_COMMENT,
"TKI:000000000000000000000000000000000000",
"TCI:000000000000000000000000000000000000");
ResponseEntity<TaskCommentResource> getTaskCommentResponse =
template.exchange(
url,
HttpMethod.GET,
new HttpEntity<String>(restHelper.getHeadersAdmin()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
assertThat(getTaskCommentResponse.getBody().getLink(Link.REL_SELF)).isNotNull();
assertThat(getTaskCommentResponse.getBody().getCreator()).isEqualTo("user_1_1");
assertThat(getTaskCommentResponse.getBody().getTextField()).isEqualTo("some text in textfield");
TaskCommentResource taskCommentResourceToUpdate = getTaskCommentResponse.getBody();
taskCommentResourceToUpdate.setTextField("updated text");
taskCommentResourceToUpdate.setTaskId("DifferentTaskid");
ThrowingCallable httpCall =
() -> {
template.exchange(
url,
HttpMethod.PUT,
new HttpEntity<>(
mapper.writeValueAsString(taskCommentResourceToUpdate),
restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.BAD_REQUEST);
taskCommentResourceToUpdate.setTaskId("TKI:000000000000000000000000000000000000");
taskCommentResourceToUpdate.setTaskCommentId("DifferentTaskCommentId");
ThrowingCallable httpCall2 =
() -> {
template.exchange(
url,
HttpMethod.PUT,
new HttpEntity<>(
mapper.writeValueAsString(taskCommentResourceToUpdate),
restHelper.getHeadersUser_1_1()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
};
assertThatThrownBy(httpCall2)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.BAD_REQUEST);
}
@Disabled("Disabled until Authorization check is up!")
@Test
void testDeleteTaskCommentWithDifferentUserThanCreatorShouldFail() {
ResponseEntity<TaskCommentListResource> getTaskCommentsBeforeDeleteionResponse =
template.exchange(
restHelper.toUrl(Mapping.URL_TASK_COMMENTS, "TKI:000000000000000000000000000000000001"),
HttpMethod.GET,
new HttpEntity<String>(restHelper.getHeadersAdmin()),
ParameterizedTypeReference.forType(TaskCommentListResource.class));
assertThat(getTaskCommentsBeforeDeleteionResponse.getBody().getContent()).hasSize(2);
String url =
restHelper.toUrl(
Mapping.URL_TASK_COMMENT,
"TKI:000000000000000000000000000000000001",
"TCI:000000000000000000000000000000000004");
ThrowingCallable httpCall =
() -> {
template.exchange(
url,
HttpMethod.DELETE,
new HttpEntity<String>(restHelper.getHeadersUser_1_2()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.FORBIDDEN);
}
@Test
void testDeleteNonExistingTaskCommentShouldFail() {
String url =
restHelper.toUrl(
Mapping.URL_TASK_COMMENT,
"TKI:000000000000000000000000000000000001",
"NotExistingTaskComment");
ThrowingCallable httpCall =
() -> {
template.exchange(
url,
HttpMethod.DELETE,
new HttpEntity<String>(restHelper.getHeadersAdmin()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.NOT_FOUND);
}
@Test
void testDeleteTaskCommentOfNotExistingTaskShouldFail() {
String url =
restHelper.toUrl(
Mapping.URL_TASK_COMMENT,
"NotExistingTaskId",
"TCI:000000000000000000000000000000000004");
ThrowingCallable httpCall =
() -> {
template.exchange(
url,
HttpMethod.DELETE,
new HttpEntity<String>(restHelper.getHeadersAdmin()),
ParameterizedTypeReference.forType(TaskCommentResource.class));
};
assertThatThrownBy(httpCall)
.extracting(ex -> ((HttpClientErrorException) ex).getStatusCode())
.isEqualTo(HttpStatus.NOT_FOUND);
}
}