org.camunda.spin.xml.SpinXmlElement Java Examples

The following examples show how to use org.camunda.spin.xml.SpinXmlElement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XmlSerializationIT.java    From camunda-external-task-client-java with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldGetTypedSerializedXml() {
  // given
  engineRule.startProcessInstance(processDefinition.getId(), VARIABLE_NAME_XML, VARIABLE_VALUE_XML_OBJECT_VALUE);

  // when
  client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
    .handler(handler)
    .open();

  // then
  clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());

  ExternalTask task = handler.getHandledTasks().get(0);

  ObjectValue typedValue = task.getVariableTyped(VARIABLE_NAME_XML, false);
  assertThat(typedValue.getObjectTypeName()).isEqualTo(XmlSerializable.class.getName());
  assertThat(typedValue.getType()).isEqualTo(OBJECT);
  assertThat(typedValue.isDeserialized()).isFalse();

  SpinXmlElement serializedValue = Spin.XML(typedValue.getValueSerialized());
  assertThat(VARIABLE_VALUE_XML_DESERIALIZED.getStringProperty()).isEqualTo(serializedValue.childElement("stringProperty").textContent());
  assertThat(VARIABLE_VALUE_XML_DESERIALIZED.getBooleanProperty()).isEqualTo(Boolean.parseBoolean(serializedValue.childElement("booleanProperty").textContent()));
  assertThat(VARIABLE_VALUE_XML_DESERIALIZED.getIntProperty()).isEqualTo(Integer.parseInt(serializedValue.childElement("intProperty").textContent()));
}
 
Example #2
Source File: XmlDomXPathScriptTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
@Script(
  name = "XmlDomXPathScriptTest.canQueryElementWithNamespace",
  variables = {
    @ScriptVariable(name = "input", value = xmlWithNamespace),
    @ScriptVariable(name = "expression", value = "/root/a:child")
  }
)
public void canQueryElementWithNamespace() {
  SpinXPathQuery query = script.getVariable("query");
  SpinXmlElement child = query.element();

  assertThat(child.name()).isEqualTo("child");
  assertThat(child.namespace()).isEqualTo("http://camunda.com");
  assertThat(child.attr("id").value()).isEqualTo("child");
}
 
Example #3
Source File: SpinVariableSerializers.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public static List<TypedValueSerializer<?>> createSpinValueSerializers(DataFormats dataFormats) {
  List<TypedValueSerializer<?>> serializers = new ArrayList<TypedValueSerializer<?>>();

  if(dataFormats.getDataFormatByName(DataFormats.JSON_DATAFORMAT_NAME) != null) {
    DataFormat<SpinJsonNode> jsonDataFormat =
        (DataFormat<SpinJsonNode>) dataFormats.getDataFormatByName(DataFormats.JSON_DATAFORMAT_NAME);
    serializers.add(new JsonValueSerializer(jsonDataFormat));
  }
  if(dataFormats.getDataFormatByName(DataFormats.XML_DATAFORMAT_NAME) != null){
    DataFormat<SpinXmlElement> xmlDataFormat =
        (DataFormat<SpinXmlElement>) dataFormats.getDataFormatByName(DataFormats.XML_DATAFORMAT_NAME);
    serializers.add(new XmlValueSerializer(xmlDataFormat));
  }

  return serializers;
}
 
Example #4
Source File: XmlDomXPathScriptTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
@Script(
  name = "XmlDomXPathScriptTest.canQueryElementWithNamespace",
  variables = {
    @ScriptVariable(name = "input", value = xmlWithDefaultNamespace),
    @ScriptVariable(name = "expression", value = "/:root/a:child")
  }
)
public void canQueryElementWithDefaultNamespace() {
  SpinXPathQuery query = script.getVariable("query");
  SpinXmlElement child = query.element();

  assertThat(child.name()).isEqualTo("child");
  assertThat(child.namespace()).isEqualTo("http://camunda.com");
  assertThat(child.attr("id").value()).isEqualTo("child");
}
 
Example #5
Source File: XmlDomElementScriptTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
@Script(
  name = "XmlDomElementScriptTest.replaceChildElement",
  variables = {
    @ScriptVariable(name = "input", isNull = true),
    @ScriptVariable(name = "newChild", value = "<child/>")
  },
  execute = false
)
public void canReplaceAChildElement() {
  SpinXmlElement element = XML(exampleXmlFileAsReader());
  SpinXmlElement date = element.childElement("date");

  element = script
    .setVariable("element", element)
    .setVariable("existingChild", date)
    .execute()
    .getVariable("element");

  assertThat(element.childElement(null, "child")).isNotNull();
  try {
    element.childElement("date");
  } catch (Exception e) {
    assertThat(e).isInstanceOf(SpinXmlElementException.class);
  }
}
 
Example #6
Source File: XmlDomElementScriptTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
@Script(
  name = "XmlDomElementScriptTest.replaceElement",
  variables = {
    @ScriptVariable(name = "newElement", value = "<root/>")
  },
  execute = false
)
public void canReplaceRootElement() {
  SpinXmlElement element = XML(exampleXmlFileAsReader());
  element = script
    .setVariable("oldElement", element)
    .execute()
    .getVariable("element");

  assertThat(element.name()).isEqualTo("root");
  assertThat(element.childElements()).isEmpty();
}
 
Example #7
Source File: DomXmlElementIterable.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
public Iterator<SpinXmlElement> iterator() {
  return new DomXmlNodeIterator<SpinXmlElement>() {

    private NodeList childs = nodeList;

    protected int getLength() {
      return childs.getLength();
    }

    protected SpinXmlElement getCurrent() {
      if (childs != null) {
        Node item = childs.item(index);
        if (item != null && item instanceof Element) {
          SpinXmlElement current = dataFormat.createElementWrapper((Element) item);
          if (!validating || (current.hasNamespace(namespace) && name.equals(current.name()))) {
              return current;
          }
        }
      }
      return null;
    }
  };

}
 
Example #8
Source File: XmlDomElementScriptTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
@Script(
  name = "XmlDomElementScriptTest.replaceChildElement",
  variables = {
    @ScriptVariable(name = "input", isNull = true),
    @ScriptVariable(name = "newChild", isNull = true)
  },
  execute = false
)
public void cannotReplaceByANullChildElement() throws Throwable {
  SpinXmlElement element = XML(exampleXmlFileAsReader());
  SpinXmlElement date = element.childElement("date");

  script
    .setVariable("element", element)
    .setVariable("existingChild", date);
  failingWithException();
}
 
Example #9
Source File: DomXmlElement.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
public SpinXmlElement appendBefore(SpinXmlElement childElement, SpinXmlElement existingChildElement) {
  ensureNotNull("childElement", childElement);
  ensureNotNull("existingChildElement", existingChildElement);
  DomXmlElement childDomElement = ensureParamInstanceOf("childElement", childElement, DomXmlElement.class);
  DomXmlElement existingChildDomElement = ensureParamInstanceOf("existingChildElement", existingChildElement, DomXmlElement.class);
  ensureChildElement(this, existingChildDomElement);

  adoptElement(childDomElement);

  try {
    domElement.insertBefore(childDomElement.domElement, existingChildDomElement.domElement);
  }
  catch (DOMException e) {
    throw LOG.unableToInsertElementInImplementation(this, childElement, e);
  }

  return this;
}
 
Example #10
Source File: XmlDomElementTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
public void canRemoveAChildElement() {
  SpinXmlElement child = XML("<child/>");
  element.append(child);

  assertThat(element.childElement(null, "child")).isNotNull();

  element.remove(child);

  try {
    assertThat(element.childElement(null, "child"));
    fail("Child element should be removed");
  } catch (Exception e) {
    assertThat(e).isInstanceOf(SpinXmlElementException.class);
  }
}
 
Example #11
Source File: DomXmlElement.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
public SpinXmlElement appendAfter(SpinXmlElement childElement, SpinXmlElement existingChildElement) {
  ensureNotNull("childElement", childElement);
  ensureNotNull("existingChildElement", existingChildElement);
  DomXmlElement childDomElement = ensureParamInstanceOf("childElement", childElement, DomXmlElement.class);
  DomXmlElement existingChildDomElement = ensureParamInstanceOf("existingChildElement", existingChildElement, DomXmlElement.class);
  ensureChildElement(this, existingChildDomElement);

  adoptElement(childDomElement);

  Node nextSibling = existingChildDomElement.domElement.getNextSibling();

  try {
    if (nextSibling != null) {
      domElement.insertBefore(childDomElement.domElement, nextSibling);
    } else {
      domElement.appendChild(childDomElement.domElement);
    }
  }
  catch (DOMException e) {
    throw LOG.unableToInsertElementInImplementation(this, childElement, e);
  }

  return this;
}
 
Example #12
Source File: SpinValueMapperTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldMapCamundaSpinXMLObjectWithAttributes() {
  // given
  Map<String, Val> xmlInnerMap = new HashMap();
  xmlInnerMap.put("@name", new ValString("Kermit"));
  xmlInnerMap.put("@language", new ValString("en"));
  Map<String, Val> xmlContextMap = new HashMap();
  xmlContextMap.put("customer", valueMapper.toVal(xmlInnerMap));

  ValContext context = (ValContext) valueMapper.toVal(xmlContextMap);
  SpinXmlElement xml = Spin.XML(" <customer name=\"Kermit\" language=\"en\" /> ");

  // when
  Val value = valueMapper.toVal(xml);

  // then
  assertThat(value).isEqualTo(context);
}
 
Example #13
Source File: SpinValueMapperTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldMapCamundaSpinXMLObjectWithPrefix() {
  // given
  SpinXmlElement xml = Spin.XML("<data xmlns:p=\"http://www.example.org\">" +
                                        "<p:customer p:name=\"Kermit\" language=\"en\" />" +
                                      "</data>");

  Map<String, Val> xmlAttrMap = new HashMap();
  xmlAttrMap.put("@p$name", new ValString("Kermit"));
  xmlAttrMap.put("@language", new ValString("en"));

  Map<String, Val> xmlInnerMap = new HashMap();
  xmlInnerMap.put("p$customer", valueMapper.toVal(xmlAttrMap));
  xmlInnerMap.put("@xmlns$p", new ValString("http://www.example.org"));

  Map<String, Val> xmlContextMap = new HashMap();
  xmlContextMap.put("data", valueMapper.toVal(xmlInnerMap));

  ValContext context = (ValContext) valueMapper.toVal(xmlContextMap);

  // when
  Val value = valueMapper.toVal(xml);

  // then
  assertThat(value).isEqualTo(context);
}
 
Example #14
Source File: XmlDomCreateTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBeIdempotent() {
  SpinXmlElement xml = XML(EXAMPLE_XML);
  assertThat(xml).isEqualTo(XML(xml));
  assertThat(xml).isEqualTo(S(xml, xml()));
  assertThat(xml).isEqualTo(S(xml, DataFormats.XML_DATAFORMAT_NAME));
  assertThat(xml).isEqualTo(S(xml));
}
 
Example #15
Source File: XmlDomElementTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void canReplaceRootElement() {
  SpinXmlElement root = XML("<root/>");
  assertThat(element.name()).isEqualTo("customers");
  assertThat(element.childElements()).isNotEmpty();
  element = element.replace(root);
  assertThat(element.name()).isEqualTo("root");
  assertThat(element.childElements()).isEmpty();
}
 
Example #16
Source File: XmlDomElementTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void canReplaceAChildElement() {
  SpinXmlElement child = XML("<child/>");
  SpinXmlElement date = element.childElement("date");
  assertThat(date).isNotNull();

  element.replaceChild(date, child);

  assertThat(element.childElement(null, "child")).isNotNull();
  try {
    element.childElement("date");
  } catch (Exception e) {
    assertThat(e).isInstanceOf(SpinXmlElementException.class);
  }
}
 
Example #17
Source File: XmlDomElementTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void canReplaceAElement() {
  SpinXmlElement child = XML("<child/>");
  SpinXmlElement date = element.childElement("date");
  assertThat(date).isNotNull();

  date.replace(child);

  assertThat(element.childElement(null, "child")).isNotNull();
  try {
    element.childElement("date");
  } catch (Exception e) {
    assertThat(e).isInstanceOf(SpinXmlElementException.class);
  }
}
 
Example #18
Source File: XmlDomElementScriptTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
@Script(
  name = "XmlDomElementScriptTest.removeChildElement",
  variables = {
    @ScriptVariable(name = "input", isNull = true),
    @ScriptVariable(name = "child2", isNull = true)
  },
  execute = false
)
public void canRemoveChildElementCollection() {
  SpinXmlElement element = XML(exampleXmlFileAsReader());
  element.append(XML("<child/>"), XML("<child/>"), XML("<child/>"));

  element = script
    .setVariable("element", element)
    .setVariable("child", element.childElements(null, "child"))
    .execute()
    .getVariable("element");

  try {
    assertThat(element.childElement(null, "child"));
    fail("Child elements should be removed");
  }
  catch (Exception e) {
    assertThat(e).isInstanceOf(SpinXmlElementException.class);
  }
}
 
Example #19
Source File: XmlDomAttributeTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void remove() {
  String namespace = attribute.namespace();
  String name = attribute.name();

  SpinXmlElement element = attribute.remove();
  assertThat(element.hasAttrNs(namespace, name)).isFalse();
}
 
Example #20
Source File: XmlDomElementTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void canAppendChildElementAfterLastChildElement() {
  SpinXmlElement child = XML("<child/>");
  int childCount = element.childElements().size();
  SpinXmlElement lastChildElement = element.childElements().get(childCount - 1);
  element.appendAfter(child, lastChildElement);
  SpinXmlElement insertedElement = element.childElements().get(childCount);
  assertThat(insertedElement.name()).isEqualTo("child");
}
 
Example #21
Source File: XmlDomElementTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void canAppendChildElementAfterExistingElement() {
  SpinXmlElement child = XML("<child/>");
  SpinXmlElement date = element.childElement("date");
  element.appendAfter(child, date);
  SpinXmlElement insertedElement = element.childElements().get(1);
  assertThat(insertedElement.name()).isEqualTo("child");
}
 
Example #22
Source File: XmlDomElementTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void canAppendChildElementBeforeExistingElement() {
  SpinXmlElement child = XML("<child/>");
  SpinXmlElement date = element.childElement("date");
  element.appendBefore(child, date);
  SpinXmlElement insertedElement = element.childElements().get(0);
  assertThat(insertedElement.name()).isEqualTo("child");
}
 
Example #23
Source File: XmlDomElementScriptTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
@Script(
  name = "XmlDomElementScriptTest.removeAttributeByName",
  variables = {
    @ScriptVariable(name = "input", file = EXAMPLE_XML_FILE_NAME),
    @ScriptVariable(name = "name",  value = "order")
  }
)
public void canRemoveAttributeByName() {
  SpinXmlElement element = script.getVariable("element");
  assertThat(element.hasAttr("order")).isFalse();
}
 
Example #24
Source File: XmlDomElementTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void canAppendChildElementCollection() {
  Collection<SpinXmlElement> childElements = new ArrayList<SpinXmlElement>();
  childElements.add(XML("<child/>"));
  childElements.add(XML("<child/>"));
  childElements.add(XML("<child/>"));

  element = element.append(childElements);

  SpinList<SpinXmlElement> childs = element.childElements(null, "child");
  assertThat(childs).hasSize(3);
}
 
Example #25
Source File: XmlDomElementScriptTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
@Script(
  name = "XmlDomElementScriptTest.removeAttributeByNamespaceAndName",
  variables = {
    @ScriptVariable(name = "input", file = EXAMPLE_XML_FILE_NAME),
    @ScriptVariable(name = "namespace",  value = EXAMPLE_NAMESPACE),
    @ScriptVariable(name = "name",  value = "order")
  }
)
public void canRemoveAttributeByNamespaceAndName() {
  SpinXmlElement element = script.getVariable("element");
  assertThat(element.hasAttrNs(EXAMPLE_NAMESPACE, "order")).isFalse();
}
 
Example #26
Source File: XmlDomXPathTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void canQueryElement() {
  SpinXmlElement child = element.xPath("/root/child").element();
  assertThat(child.name()).isEqualTo("child");
  assertThat(child.attr("id").value()).isEqualTo("child");

  SpinXmlElement b = child.xPath("b").element();
  assertThat(b.name()).isEqualTo("b");
  assertThat(b.attr("id").value()).isEqualTo("b");
}
 
Example #27
Source File: XmlDomElementTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void canAppendChildElementWithNamespace() {
  SpinXmlElement child = XML("<child xmlns=\"" + EXAMPLE_NAMESPACE + "\"/>");
  element = element.append(child);

  child.attr("id", "child");
  child = element.childElement(EXAMPLE_NAMESPACE, "child");

  assertThat(child).isNotNull();
  assertThat(child.attr("id").value()).isEqualTo("child");
}
 
Example #28
Source File: XmlDomXPathTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void canQueryElementWithNamespace() {
  SpinXmlElement child = elementWithNamespace.xPath("/root/a:child")
    .ns("a", "http://camunda.com")
    .element();

  assertThat(child.name()).isEqualTo("child");
  assertThat(child.namespace()).isEqualTo("http://camunda.com");
  assertThat(child.attr("id").value()).isEqualTo("child");
}
 
Example #29
Source File: XmlDomCreateTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateForReader() {
  SpinXmlElement xml = XML(stringAsReader(EXAMPLE_XML));
  assertThat(xml).isNotNull();

  xml = S(stringAsReader(EXAMPLE_XML), xml());
  assertThat(xml).isNotNull();

  xml = S(stringAsReader(EXAMPLE_XML), DataFormats.XML_DATAFORMAT_NAME);
  assertThat(xml).isNotNull();

  xml = S(stringAsReader(EXAMPLE_XML));
  assertThat(xml).isNotNull();
}
 
Example #30
Source File: XmlDomXPathTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void canQueryElementWithNamespaceMap() {
  Map<String, String> namespaces = new HashMap<String, String>();
  namespaces.put("a", "http://camunda.com");
  namespaces.put("b", "http://camunda.org");

  SpinXmlElement child = elementWithNamespace.xPath("/root/a:child/b:a")
    .ns(namespaces)
    .element();

  assertThat(child.name()).isEqualTo("a");
  assertThat(child.namespace()).isEqualTo("http://camunda.org");
  assertThat(child.attr("id").value()).isEqualTo("a");
}