use of org.jivesoftware.smackx.message_fastening.element.ExternalElement in project Smack by igniterealtime.
the class MessageFasteningElementsTest method fasteningDeserializationTest.
@ParameterizedTest
@EnumSource(SmackTestUtil.XmlPullParserKind.class)
public void fasteningDeserializationTest(SmackTestUtil.XmlPullParserKind parserKind) throws XmlPullParserException, IOException, SmackParsingException {
String xml = "<apply-to xmlns='urn:xmpp:fasten:0' id='origin-id-1'>" + " <i-like-this xmlns='urn:example:like'/>" + " <external name='custom' element-namespace='urn:example:custom'/>" + " <external name='body'/>" + "</apply-to>";
FasteningElement parsed = SmackTestUtil.parse(xml, FasteningElementProvider.class, parserKind);
assertNotNull(parsed);
assertEquals(new OriginIdElement("origin-id-1"), parsed.getReferencedStanzasOriginId());
assertFalse(parsed.isRemovingElement());
assertFalse(parsed.isShellElement());
assertEquals(1, parsed.getWrappedPayloads().size());
assertEquals("i-like-this", parsed.getWrappedPayloads().get(0).getElementName());
assertEquals("urn:example:like", parsed.getWrappedPayloads().get(0).getNamespace());
assertEquals(2, parsed.getExternalPayloads().size());
ExternalElement custom = parsed.getExternalPayloads().get(0);
assertEquals("custom", custom.getName());
assertEquals("urn:example:custom", custom.getElementNamespace());
ExternalElement body = parsed.getExternalPayloads().get(1);
assertEquals("body", body.getName());
assertNull(body.getElementNamespace());
}
use of org.jivesoftware.smackx.message_fastening.element.ExternalElement in project Smack by igniterealtime.
the class MessageFasteningElementsTest method fasteningElementWithExternalElementsTest.
@Test
public void fasteningElementWithExternalElementsTest() {
String xml = "<apply-to xmlns='urn:xmpp:fasten:0' id='origin-id-2'>" + " <external name='body'/>" + " <external name='custom' element-namespace='urn:example:custom'/>" + " <edit xmlns='urn:example.edit'/>" + "</apply-to>";
FasteningElement element = FasteningElement.builder().setOriginId("origin-id-2").addExternalPayloads(Arrays.asList(new ExternalElement("body"), new ExternalElement("custom", "urn:example:custom"))).addWrappedPayload(new StandardExtensionElement("edit", "urn:example.edit")).build();
assertXmlSimilar(xml, element.toXML().toString());
}
use of org.jivesoftware.smackx.message_fastening.element.ExternalElement in project Smack by igniterealtime.
the class MessageFasteningElementsTest method shellElementMustNotContainAnyPayloads.
@Test
public void shellElementMustNotContainAnyPayloads() {
assertThrows(IllegalArgumentException.class, () -> FasteningElement.builder().setShell().addWrappedPayload(new StandardExtensionElement("edit", "urn:example.edit")).build());
assertThrows(IllegalArgumentException.class, () -> FasteningElement.builder().setShell().addExternalPayload(new ExternalElement("body")).build());
}
use of org.jivesoftware.smackx.message_fastening.element.ExternalElement in project Smack by igniterealtime.
the class FasteningElementProvider method parse.
@Override
public FasteningElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
FasteningElement.Builder builder = FasteningElement.builder();
builder.setOriginId(parser.getAttributeValue("", FasteningElement.ATTR_ID));
if (ParserUtils.getBooleanAttribute(parser, FasteningElement.ATTR_CLEAR, false)) {
builder.setClear();
}
if (ParserUtils.getBooleanAttribute(parser, FasteningElement.ATTR_SHELL, false)) {
builder.setShell();
}
outerloop: while (true) {
XmlPullParser.Event tag = parser.next();
switch(tag) {
case START_ELEMENT:
String name = parser.getName();
String namespace = parser.getNamespace();
// Parse external payload
if (MessageFasteningManager.NAMESPACE.equals(namespace) && ExternalElement.ELEMENT.equals(name)) {
ExternalElement external = new ExternalElement(parser.getAttributeValue("", ExternalElement.ATTR_NAME), parser.getAttributeValue("", ExternalElement.ATTR_ELEMENT_NAMESPACE));
builder.addExternalPayload(external);
continue;
}
// Parse wrapped payload
XmlElement wrappedPayload = PacketParserUtils.parseExtensionElement(name, namespace, parser, xmlEnvironment);
builder.addWrappedPayload(wrappedPayload);
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
break;
}
}
return builder.build();
}
Aggregations