TSK-658: Initialize task attachments correctly in resource assemblers

This commit is contained in:
julian.schallenmueller 2018-07-27 16:06:24 +02:00 committed by Holger Hagen
parent 183185ed87
commit 846a54196a
14 changed files with 359 additions and 17 deletions

View File

@ -1162,7 +1162,7 @@ public class QueryTasksAccTest extends AbstractAccTest {
@WithAccessId(
userName = "admin")
@Test
public void testQueryForOrderWithDirectionNull() throws InvalidArgumentException {
public void testQueryForOrderWithDirectionNull() {
TaskService taskService = taskanaEngine.getTaskService();
List<TaskSummary> results = taskService.createTaskQuery()
.orderByPrimaryObjectReferenceSystemInstance(null)

View File

@ -29,13 +29,14 @@ public class SampleDataGenerator {
private static final String WORKBASKET_ACCESS_LIST = SQL + TEST_DATA + "/workbasket-access-list.sql";
private static final String CLASSIFICATION = SQL + TEST_DATA + "/classification.sql";
private static final String OBJECT_REFERENCE = SQL + TEST_DATA + "/object-reference.sql";
private static final String ATTACHMENT = SQL + TEST_DATA + "/attachment.sql";
private ScriptRunner runner;
DataSource dataSource;
public SampleDataGenerator(DataSource dataSource) throws SQLException {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(dataSource.getConnection().getMetaData().toString());
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(dataSource.getConnection().getMetaData().toString());
}
this.dataSource = dataSource;
@ -69,6 +70,8 @@ public class SampleDataGenerator {
new InputStreamReader(this.getClass().getResourceAsStream(CLASSIFICATION), StandardCharsets.UTF_8)));
runner.runScript(new BufferedReader(
new InputStreamReader(this.getClass().getResourceAsStream(TASK), StandardCharsets.UTF_8)));
runner.runScript(new BufferedReader(
new InputStreamReader(this.getClass().getResourceAsStream(ATTACHMENT), StandardCharsets.UTF_8)));
runner.runScript(new BufferedReader(new InputStreamReader(
this.getClass().getResourceAsStream(WORKBASKET_ACCESS_LIST), StandardCharsets.UTF_8)));
runner.runScript(new BufferedReader(
@ -76,7 +79,7 @@ public class SampleDataGenerator {
runner.closeConnection();
LOGGER.debug(outWriter.toString());
LOGGER.trace(outWriter.toString());
if (!errorWriter.toString().trim().isEmpty()) {
LOGGER.error(errorWriter.toString());
}

View File

@ -1,6 +1,7 @@
package pro.taskana.rest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@ -18,6 +19,7 @@ import java.util.Collections;
import javax.sql.DataSource;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -290,6 +292,30 @@ public class TaskControllerIntTest {
assertNotNull(response.getBody().getLink(Link.REL_LAST));
assertNotNull(response.getBody().getLink(Link.REL_PREVIOUS));
}
@Test
public void testGetTaskWithAttachments() throws IOException {
URL url = new URL("http://127.0.0.1:" + port + "/v1/tasks/TKI:000000000000000000000000000000000002");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
assertEquals(200, con.getResponseCode());
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
con.disconnect();
String response = content.toString();
assertFalse(response.contains("\"attachments\":[]"));
int start = response.indexOf("created", response.indexOf("created") + 1);
String createdString = response.substring(start + 10, start + 30);
assertTrue(createdString.matches("\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d([+-][0-2]\\d:[0-5]\\d|Z)"));
}
@Test
public void testGetAndUpdateTask() throws IOException {

View File

@ -160,9 +160,9 @@
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>

View File

@ -0,0 +1,8 @@
package pro.taskana.rest;
/**
* Controller for all {@link pro.taskana.Attachment} related endpoints.
*/
public class AttachmentController {
}

View File

@ -0,0 +1,96 @@
package pro.taskana.rest.resource;
import java.util.HashMap;
import java.util.Map;
import org.springframework.hateoas.ResourceSupport;
import pro.taskana.ObjectReference;
/**
* Resource class for {@link pro.taskana.Attachment}.
*/
public class AttachmentResource extends ResourceSupport {
private String attachmentId;
private String taskId;
private String created;
private String modified;
private ClassificationSummaryResource classificationSummaryResource;
private ObjectReference objectReference;
private String channel;
private String received;
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getModified() {
return modified;
}
public void setModified(String modified) {
this.modified = modified;
}
public String getReceived() {
return received;
}
public void setReceived(String received) {
this.received = received;
}
private Map<String, String> customAttributes = new HashMap<String, String>();
public String getAttachmentId() {
return attachmentId;
}
public void setAttachmentId(String attachmentId) {
this.attachmentId = attachmentId;
}
public String getTaskId() {
return taskId;
}
public void setTaskId(String taskId) {
this.taskId = taskId;
}
public ClassificationSummaryResource getClassificationSummary() {
return classificationSummaryResource;
}
public void setClassificationSummary(ClassificationSummaryResource classificationSummaryResource) {
this.classificationSummaryResource = classificationSummaryResource;
}
public ObjectReference getObjectReference() {
return objectReference;
}
public void setObjectReference(ObjectReference objectReference) {
this.objectReference = objectReference;
}
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
public Map<String, String> getCustomAttributes() {
return customAttributes;
}
public void setCustomAttributes(Map<String, String> customAttributes) {
this.customAttributes = customAttributes;
}
}

View File

@ -0,0 +1,83 @@
package pro.taskana.rest.resource;
import org.springframework.hateoas.ResourceSupport;
import pro.taskana.ObjectReference;
/**
* Resource class for {@link pro.taskana.AttachmentSummary}.
*/
public class AttachmentSummaryResource extends ResourceSupport {
private String attachmentId;
private String taskId;
private String created;
private String modified;
private ClassificationSummaryResource classificationSummaryResource;
private ObjectReference objectReference;
private String channel;
private String received;
public String getAttachmentId() {
return attachmentId;
}
public void setAttachmentId(String attachmentId) {
this.attachmentId = attachmentId;
}
public String getTaskId() {
return taskId;
}
public void setTaskId(String taskId) {
this.taskId = taskId;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getModified() {
return modified;
}
public void setModified(String modified) {
this.modified = modified;
}
public ClassificationSummaryResource getClassificationSummary() {
return classificationSummaryResource;
}
public void setClassificationSummary(ClassificationSummaryResource classificationSummaryResource) {
this.classificationSummaryResource = classificationSummaryResource;
}
public ObjectReference getObjectReference() {
return objectReference;
}
public void setObjectReference(ObjectReference objectReference) {
this.objectReference = objectReference;
}
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
public String getReceived() {
return received;
}
public void setReceived(String received) {
this.received = received;
}
}

View File

@ -7,7 +7,6 @@ import java.util.Map;
import org.springframework.hateoas.ResourceSupport;
import pro.taskana.Attachment;
import pro.taskana.ObjectReference;
import pro.taskana.TaskState;
@ -39,7 +38,7 @@ public class TaskResource extends ResourceSupport {
private boolean isTransferred;
// All objects have to be serializable
private Map<String, String> customAttributes = Collections.emptyMap();
private List<Attachment> attachments = new ArrayList<>();
private List<AttachmentResource> attachments = new ArrayList<>();
private String custom1;
private String custom2;
private String custom3;
@ -233,11 +232,11 @@ public class TaskResource extends ResourceSupport {
this.customAttributes = customAttributes;
}
public List<Attachment> getAttachments() {
public List<AttachmentResource> getAttachments() {
return attachments;
}
public void setAttachments(List<Attachment> attachments) {
public void setAttachments(List<AttachmentResource> attachments) {
this.attachments = attachments;
}

View File

@ -8,7 +8,6 @@ import java.util.Map;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.hateoas.core.Relation;
import pro.taskana.AttachmentSummary;
import pro.taskana.ObjectReference;
import pro.taskana.TaskState;
@ -41,7 +40,7 @@ public class TaskSummaryResource extends ResourceSupport {
private boolean isTransferred;
// All objects have to be serializable
private Map<String, String> customAttributes = Collections.emptyMap();
private List<AttachmentSummary> attachmentSummaries = new ArrayList<>();
private List<AttachmentSummaryResource> attachmentSummaryResources = new ArrayList<>();
private String custom1;
private String custom2;
private String custom3;
@ -235,12 +234,12 @@ public class TaskSummaryResource extends ResourceSupport {
this.customAttributes = customAttributes;
}
public List<AttachmentSummary> getAttachmentSummaries() {
return attachmentSummaries;
public List<AttachmentSummaryResource> getAttachmentSummaries() {
return attachmentSummaryResources;
}
public void setAttachments(List<AttachmentSummary> attachmentSummaries) {
this.attachmentSummaries = attachmentSummaries;
public void setAttachmentSummaries(List<AttachmentSummaryResource> attachmentSummaryResources) {
this.attachmentSummaryResources = attachmentSummaryResources;
}
public String getCustom1() {

View File

@ -0,0 +1,64 @@
package pro.taskana.rest.resource.assembler;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pro.taskana.Attachment;
import pro.taskana.TaskService;
import pro.taskana.impl.AttachmentImpl;
import pro.taskana.rest.resource.AttachmentResource;
/**
* Resource assembler for {@link AttachmentResource}.
*/
@Component
public class AttachmentResourcesAssembler {
@Autowired
private TaskService taskService;
@Autowired
private ClassificationSummaryResourceAssembler classificationAssembler;
public List<AttachmentResource> toResources(List<Attachment> attachments) {
List<AttachmentResource> resourceList = new ArrayList<>();
for (Attachment a : attachments) {
AttachmentResource resource = new AttachmentResource();
BeanUtils.copyProperties(a, resource);
if (a.getCreated() != null) {
resource.setCreated(a.getCreated().toString());
}
if (a.getModified() != null) {
resource.setModified(a.getModified().toString());
}
if (a.getReceived() != null) {
resource.setReceived(a.getReceived().toString());
}
resource.setAttachmentId(a.getId());
resource.setClassificationSummary(
classificationAssembler.toResource(a.getClassificationSummary()));
resourceList.add(resource);
}
return resourceList;
}
public List<Attachment> toModel(List<AttachmentResource> resources) {
List<Attachment> attachmentList = new ArrayList<>();
for (AttachmentResource ar : resources) {
AttachmentImpl attachment = (AttachmentImpl) taskService.newAttachment();
BeanUtils.copyProperties(ar, attachment);
attachment.setId(ar.getAttachmentId());
attachment.setClassificationSummary(
classificationAssembler.toModel(ar.getClassificationSummary()));
attachmentList.add(attachment);
}
return attachmentList;
}
}

View File

@ -0,0 +1,39 @@
package pro.taskana.rest.resource.assembler;
import org.springframework.beans.BeanUtils;
import org.springframework.hateoas.mvc.ResourceAssemblerSupport;
import org.springframework.stereotype.Component;
import pro.taskana.AttachmentSummary;
import pro.taskana.rest.AttachmentController;
import pro.taskana.rest.resource.AttachmentSummaryResource;
/**
* Resource assembler for {@link AttachmentSummaryResource}.
*/
@Component
public class AttachmentSummaryResourceAssembler
extends ResourceAssemblerSupport<AttachmentSummary, AttachmentSummaryResource> {
public AttachmentSummaryResourceAssembler() {
super(AttachmentController.class, AttachmentSummaryResource.class);
}
@Override
public AttachmentSummaryResource toResource(AttachmentSummary attachmentSummary) {
AttachmentSummaryResource resource = createResourceWithId(attachmentSummary.getId(),
attachmentSummary);
BeanUtils.copyProperties(attachmentSummary, resource);
if (attachmentSummary.getCreated() != null) {
resource.setCreated(attachmentSummary.getCreated().toString());
}
if (attachmentSummary.getModified() != null) {
resource.setModified(attachmentSummary.getModified().toString());
}
if (attachmentSummary.getReceived() != null) {
resource.setReceived(attachmentSummary.getReceived().toString());
}
resource.setAttachmentId(attachmentSummary.getId());
return resource;
}
}

View File

@ -0,0 +1,18 @@
package pro.taskana.rest.resource.assembler;
import java.util.List;
import pro.taskana.AttachmentSummary;
import pro.taskana.rest.resource.AttachmentSummaryResource;
/**
* Resources assembler for {@link AttachmentSummaryResource}.
*/
public class AttachmentSummaryResourcesAssembler {
public List<AttachmentSummaryResource> toResources(
List<AttachmentSummary> attachmentSummaries) {
AttachmentSummaryResourceAssembler assembler = new AttachmentSummaryResourceAssembler();
List<AttachmentSummaryResource> resources = assembler.toResources(attachmentSummaries);
return resources;
}
}

View File

@ -31,6 +31,9 @@ public class TaskResourceAssembler
@Autowired
private WorkbasketSummaryResourceAssembler workbasketAssembler;
@Autowired
private AttachmentResourcesAssembler attachmentAssembler;
public TaskResourceAssembler() {
super(TaskController.class, TaskResource.class);
}
@ -58,6 +61,7 @@ public class TaskResourceAssembler
resource.setClassificationSummaryResource(
classificationAssembler.toResource(task.getClassificationSummary()));
resource.setWorkbasketSummaryResource(workbasketAssembler.toResource(task.getWorkbasketSummary()));
resource.setAttachments(attachmentAssembler.toResources(task.getAttachments()));
try {
if (task.getCustomAttribute("1") != null) {
resource.setCustom1(task.getCustomAttribute("1"));
@ -136,6 +140,7 @@ public class TaskResourceAssembler
}
task.setClassificationSummary(classificationAssembler.toModel(resource.getClassificationSummaryResource()));
task.setWorkbasketSummary(workbasketAssembler.toModel(resource.getWorkbasketSummaryResource()));
task.setAttachments(attachmentAssembler.toModel(resource.getAttachments()));
if (resource.getCustom1() != null) {
task.setCustom1(resource.getCustom1());
}

View File

@ -17,6 +17,7 @@ public class TaskSummaryResourceAssembler
private WorkbasketSummaryResourceAssembler workbasketAssembler = new WorkbasketSummaryResourceAssembler();
private ClassificationSummaryResourceAssembler classificationAssembler = new ClassificationSummaryResourceAssembler();
private AttachmentSummaryResourcesAssembler attachmentsAssembler = new AttachmentSummaryResourcesAssembler();
public TaskSummaryResourceAssembler() {
super(TaskController.class, TaskSummaryResource.class);
@ -44,6 +45,7 @@ public class TaskSummaryResourceAssembler
resource.setClassificationSummaryResource(
classificationAssembler.toResource(taskSummary.getClassificationSummary()));
resource.setWorkbasketSummaryResource(workbasketAssembler.toResource(taskSummary.getWorkbasketSummary()));
resource.setAttachmentSummaries(attachmentsAssembler.toResources(taskSummary.getAttachmentSummaries()));
try {
if (taskSummary.getCustomAttribute("1") != null) {
resource.setCustom1(taskSummary.getCustomAttribute("1"));