Search in sources :

Example 1 with JaxbContent

use of org.jbpm.services.task.impl.model.xml.JaxbContent in project jbpm by kiegroup.

the class AbstractTaskSerializationTest method jaxbContentTest.

@Test
public void jaxbContentTest() throws Exception {
    Assume.assumeFalse(getType().equals(TestType.YAML));
    ContentImpl content = new ContentImpl();
    content.setId(23);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("life", new Integer(23));
    map.put("sick", new Integer(45));
    byte[] bytes = ContentMarshallerHelper.marshallContent(null, map, null);
    content.setContent(bytes);
    JaxbContent jaxbContent = new JaxbContent(content);
    JaxbContent copyJaxbContent = testRoundTrip(jaxbContent);
    Assertions.assertThat(jaxbContent).isEqualToComparingFieldByFieldRecursively(copyJaxbContent);
}
Also used : HashMap(java.util.HashMap) ContentImpl(org.jbpm.services.task.impl.model.ContentImpl) JaxbContent(org.jbpm.services.task.impl.model.xml.JaxbContent) Test(org.junit.Test)

Example 2 with JaxbContent

use of org.jbpm.services.task.impl.model.xml.JaxbContent in project jbpm by kiegroup.

the class HumanTaskServicesBaseTest method xmlRoundTripContent.

protected JaxbContent xmlRoundTripContent(Content content) {
    JaxbContent xmlContent = new JaxbContent(content);
    JaxbContent xmlCopy = null;
    try {
        Marshaller marshaller = JAXBContext.newInstance(JaxbContent.class).createMarshaller();
        // marshal
        StringWriter stringWriter = new StringWriter();
        marshaller.marshal(xmlContent, stringWriter);
        // unmarshal
        Unmarshaller unmarshaller = JAXBContext.newInstance(JaxbContent.class).createUnmarshaller();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(stringWriter.toString().getBytes());
        xmlCopy = (JaxbContent) unmarshaller.unmarshal(inputStream);
        for (Field field : JaxbContent.class.getDeclaredFields()) {
            field.setAccessible(true);
            Object orig = field.get(xmlContent);
            Object roundTrip = field.get(xmlCopy);
            if (orig instanceof byte[]) {
                Assert.assertTrue(Arrays.equals((byte[]) orig, (byte[]) roundTrip));
            } else {
                Assert.assertEquals(field.getName(), orig, roundTrip);
            }
        }
    } catch (Exception e) {
        logger.error("Unable to complete round trip: " + e.getMessage(), e);
        Assert.fail("Unable to complete round trip: " + e.getMessage());
    }
    Object orig = ContentMarshallerHelper.unmarshall(content.getContent(), null);
    assertNotNull("Round tripped JaxbContent is null!", xmlCopy);
    Object roundTrip = ContentMarshallerHelper.unmarshall(xmlCopy.getContent(), null);
    Assert.assertEquals(orig, roundTrip);
    return xmlCopy;
}
Also used : Field(java.lang.reflect.Field) Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) Unmarshaller(javax.xml.bind.Unmarshaller) JaxbContent(org.jbpm.services.task.impl.model.xml.JaxbContent) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 3 with JaxbContent

use of org.jbpm.services.task.impl.model.xml.JaxbContent in project jbpm by kiegroup.

the class AddAttachmentCommand method execute.

public Long execute(Context cntxt) {
    TaskContext context = (TaskContext) cntxt;
    Attachment attachmentImpl = attachment;
    if (attachmentImpl == null) {
        attachmentImpl = jaxbAttachment;
    }
    Content contentImpl = content;
    if (contentImpl == null) {
        contentImpl = jaxbContent;
    }
    if (rawContent != null && contentImpl == null) {
        Task task = context.getPersistenceContext().findTask(taskId);
        contentImpl = TaskModelProvider.getFactory().newContent();
        ContentMarshallerContext ctx = TaskContentRegistry.get().getMarshallerContext(task.getTaskData().getDeploymentId());
        ((InternalContent) contentImpl).setContent(ContentMarshallerHelper.marshallContent(task, rawContent, ctx.getEnvironment()));
        ((InternalAttachment) attachmentImpl).setSize(contentImpl.getContent().length);
    }
    doCallbackOperationForAttachment(attachmentImpl, context);
    return context.getTaskAttachmentService().addAttachment(taskId, attachmentImpl, contentImpl);
}
Also used : Task(org.kie.api.task.model.Task) InternalAttachment(org.kie.internal.task.api.model.InternalAttachment) Content(org.kie.api.task.model.Content) InternalContent(org.kie.internal.task.api.model.InternalContent) JaxbContent(org.jbpm.services.task.impl.model.xml.JaxbContent) JaxbAttachment(org.jbpm.services.task.impl.model.xml.JaxbAttachment) Attachment(org.kie.api.task.model.Attachment) InternalAttachment(org.kie.internal.task.api.model.InternalAttachment) InternalContent(org.kie.internal.task.api.model.InternalContent) ContentMarshallerContext(org.kie.internal.task.api.ContentMarshallerContext)

Example 4 with JaxbContent

use of org.jbpm.services.task.impl.model.xml.JaxbContent in project jbpm by kiegroup.

the class LifeCycleBaseTest method testNewTaskWithMapContent.

@Test
public void testNewTaskWithMapContent() {
    String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { } ), ";
    str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [new User('Bobba Fet') ],businessAdministrators = [ new User('Administrator') ], }),";
    str += "name =  'This is my task name' })";
    Map<String, Object> variablesMap = new HashMap<String, Object>();
    variablesMap.put("key1", "value1");
    variablesMap.put("key2", null);
    variablesMap.put("key3", "value3");
    ContentData data = ContentMarshallerHelper.marshal(null, variablesMap, null);
    Task task = TaskFactory.evalTask(new StringReader(str));
    taskService.addTask(task, data);
    long taskId = task.getId();
    // Task should be assigned to the single potential owner and state set to Reserved
    Task task1 = taskService.getTaskById(taskId);
    assertEquals(AccessType.Inline, ((InternalTaskData) task1.getTaskData()).getDocumentAccessType());
    assertEquals("java.util.HashMap", task1.getTaskData().getDocumentType());
    long contentId = task1.getTaskData().getDocumentContentId();
    assertTrue(contentId != -1);
    // content
    Content content = taskService.getContentById(contentId);
    Object unmarshalledObject = ContentMarshallerHelper.unmarshall(content.getContent(), null);
    if (!(unmarshalledObject instanceof Map)) {
        fail("The variables should be a Map");
    }
    Map<String, Object> unmarshalledvars = (Map<String, Object>) unmarshalledObject;
    JaxbContent jaxbContent = xmlRoundTripContent(content);
    assertNotNull("Jaxb Content map not filled", jaxbContent.getContentMap());
    assertEquals("value1", unmarshalledvars.get("key1"));
    assertNull(unmarshalledvars.get("key2"));
    assertEquals("value3", unmarshalledvars.get("key3"));
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) ContentData(org.kie.internal.task.api.model.ContentData) HashMap(java.util.HashMap) Content(org.kie.api.task.model.Content) JaxbContent(org.jbpm.services.task.impl.model.xml.JaxbContent) StringReader(java.io.StringReader) HashMap(java.util.HashMap) Map(java.util.Map) JaxbContent(org.jbpm.services.task.impl.model.xml.JaxbContent) Test(org.junit.Test)

Aggregations

JaxbContent (org.jbpm.services.task.impl.model.xml.JaxbContent)4 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Content (org.kie.api.task.model.Content)2 Task (org.kie.api.task.model.Task)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Field (java.lang.reflect.Field)1 ParseException (java.text.ParseException)1 Map (java.util.Map)1 Marshaller (javax.xml.bind.Marshaller)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 ContentImpl (org.jbpm.services.task.impl.model.ContentImpl)1 JaxbAttachment (org.jbpm.services.task.impl.model.xml.JaxbAttachment)1 Attachment (org.kie.api.task.model.Attachment)1 ContentMarshallerContext (org.kie.internal.task.api.ContentMarshallerContext)1 ContentData (org.kie.internal.task.api.model.ContentData)1 InternalAttachment (org.kie.internal.task.api.model.InternalAttachment)1