mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
fix(communication): fix errors while import external xml node into input xml
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package experimental;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class ExternalNodeImportTest {
|
||||
|
||||
private static Node importExternalNode(Document targetDocument, Node externalNode) {
|
||||
Node nodeToImport = externalNode;
|
||||
if (externalNode != null && externalNode.getNodeType() == Node.DOCUMENT_NODE) {
|
||||
nodeToImport = ((Document) externalNode).getDocumentElement();
|
||||
}
|
||||
if (nodeToImport == null) {
|
||||
throw new IllegalArgumentException("nodeToImport must not be null");
|
||||
}
|
||||
if (nodeToImport.getOwnerDocument() == targetDocument) {
|
||||
return nodeToImport.cloneNode(true);
|
||||
}
|
||||
return targetDocument.importNode(nodeToImport, true);
|
||||
}
|
||||
|
||||
private static Document newDocument() throws Exception {
|
||||
return DocumentBuilderFactory.newInstance()
|
||||
.newDocumentBuilder()
|
||||
.newDocument();
|
||||
}
|
||||
|
||||
@Test
|
||||
void appendForeignElementDirectlyShouldThrow() throws Exception {
|
||||
Document targetDocument = newDocument();
|
||||
Element inputRoot = targetDocument.createElement("input");
|
||||
targetDocument.appendChild(inputRoot);
|
||||
|
||||
Document externalDocument = newDocument();
|
||||
Element externalInputs = externalDocument.createElement("inputs");
|
||||
externalDocument.appendChild(externalInputs);
|
||||
|
||||
assertThrows(DOMException.class, () -> inputRoot.appendChild(externalInputs));
|
||||
}
|
||||
|
||||
@Test
|
||||
void importForeignElementShouldWork() throws Exception {
|
||||
Document targetDocument = newDocument();
|
||||
Element inputRoot = targetDocument.createElement("input");
|
||||
targetDocument.appendChild(inputRoot);
|
||||
|
||||
Document externalDocument = newDocument();
|
||||
Element externalInputs = externalDocument.createElement("inputs");
|
||||
externalDocument.appendChild(externalInputs);
|
||||
|
||||
Node imported = importExternalNode(targetDocument, externalInputs);
|
||||
inputRoot.appendChild(imported);
|
||||
|
||||
assertEquals("inputs", inputRoot.getFirstChild().getNodeName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void importExternalDocumentShouldUseDocumentElement() throws Exception {
|
||||
Document targetDocument = newDocument();
|
||||
Element inputRoot = targetDocument.createElement("input");
|
||||
targetDocument.appendChild(inputRoot);
|
||||
|
||||
Document externalDocument = newDocument();
|
||||
Element externalInputs = externalDocument.createElement("inputs");
|
||||
externalDocument.appendChild(externalInputs);
|
||||
|
||||
Node imported = importExternalNode(targetDocument, externalDocument);
|
||||
inputRoot.appendChild(imported);
|
||||
|
||||
assertEquals("inputs", inputRoot.getFirstChild().getNodeName());
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import java.util.List;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CommunicationProducerTest {
|
||||
|
||||
@@ -32,6 +32,20 @@ class CommunicationProducerTest {
|
||||
method.invoke(producer, context, response);
|
||||
}
|
||||
|
||||
private static String invokeBuildInputXml(
|
||||
CommunicationProducer producer,
|
||||
PartnerRunningFlowContext context,
|
||||
List<?> communicationBlocks
|
||||
) throws Exception {
|
||||
Method method = CommunicationProducer.class.getDeclaredMethod(
|
||||
"buildInputXml",
|
||||
PartnerRunningFlowContext.class,
|
||||
List.class
|
||||
);
|
||||
method.setAccessible(true);
|
||||
return (String) method.invoke(producer, context, communicationBlocks);
|
||||
}
|
||||
|
||||
private static void setField(Object target, String fieldName, Object value) throws Exception {
|
||||
Field field = target.getClass().getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
@@ -72,6 +86,24 @@ class CommunicationProducerTest {
|
||||
assertEquals("[[AGENT]: self]:\n\nnormal reply", chatMessages.get(1).getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldBuildInputXmlWithoutExtraWrapper() throws Exception {
|
||||
StubCognitionCapability cognitionCapability = new StubCognitionCapability();
|
||||
CommunicationProducer producer = new CommunicationProducer();
|
||||
setField(producer, "cognitionCapability", cognitionCapability);
|
||||
|
||||
String xml = invokeBuildInputXml(
|
||||
producer,
|
||||
PartnerRunningFlowContext.fromUser("user-1", "hello"),
|
||||
List.of()
|
||||
);
|
||||
|
||||
assertTrue(xml.contains("<input>"));
|
||||
assertTrue(xml.contains("<inputs>"));
|
||||
assertTrue(xml.contains("<input interval-to-first=\"0\">hello</input>"));
|
||||
assertFalse(xml.contains("<wrapper>"));
|
||||
}
|
||||
|
||||
private static final class StubCognitionCapability implements CognitionCapability {
|
||||
private final ContextWorkspace contextWorkspace = new ContextWorkspace();
|
||||
private final List<Message> chatMessages = new ArrayList<>();
|
||||
|
||||
Reference in New Issue
Block a user