代码之家  ›  专栏  ›  技术社区  ›  sceaj

基本流导致无效负载

  •  0
  • sceaj  · 技术社区  · 7 年前

    我正在使用Mule 3.9和Anypoint Studio 6.4.1。在第1章中,他们描述了我创建的一个非常基本的product_注册流程,如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
        xmlns:spring="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-current.xsd
    http://www.mulesoft.org/schema/mule/core 
    http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/http 
    http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
    http://www.mulesoft.org/schema/mule/jms 
    http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd">
        <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8880" basePath="products" doc:name="HTTP Listener Configuration"/>
        <jms:activemq-connector name="Active_MQ" username="admin" password="admin" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
        <flow name="product_registrationFlow">
            <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
            <logger level="INFO" doc:name="Logger Before"/>
            <byte-array-to-string-transformer doc:name="Byte Array to String"/>
            <logger level="INFO" doc:name="Logger After"/>
            <jms:outbound-endpoint doc:name="JMS" queue="products"/>
        </flow>
    </mule>
    

    以及伴随的功能测试:

    @Test
    public void testCanRegisterProducts() throws Exception {
    
        LocalMuleClient client = muleContext.getClient();
    
        String productAsJson = "{ \"name\":\"Widget\", \"price\": 9.99, \"weight\": 1.0, \"sku\": \"abcd-56789\" }";
    
        MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
        client.dispatch("http://localhost:8880/products", source);
    
        MuleMessage result = client.request("jms://products", RECEIVE_TIMEOUT);
    
        assertNotNull(result);
        assertFalse(result.getPayload() instanceof NullPayload);
        assertEquals(productAsJson, result.getPayloadAsString());
    }
    

    当我运行测试时,它在最后一次断言时失败,因为实际有效负载是:

    {NullPayload}
    

    如果我直接在ActiveMQ中查看,我会看到该负载。如果我手动发布到Mule(使用类似于Chrome中的Poster的工具,只设置标题内容类型:application/json),负载是有效的json,我可以让测试通过(因为它正在从Poster发布的队列中获取挂起的消息,并且它创建的消息位于负载为{NullPayload}的队列的末尾。

    有人能解释一下为什么从JUnit测试调用流时失败,但使用Poster之类的工具调用流时似乎有效吗?

    更新:在Pierre B的帮助下,我成功了。FunctionalTestCase中多路消息的初始化更新如下:

        MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
        source.setProperty("Content-Type", "application/json", PropertyScope.INBOUND);
        source.setProperty("Content-Length", Integer.valueOf(productAsJson.length()), PropertyScope.INBOUND);
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Pierre B.    7 年前

    在Chrome中使用Poster之类的工具,只设置标题内容类型:application/json

    尝试将相同的标题添加到 MuleMessage 例如:

    MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
    
    # define the headers
    Map<String, Object> headers = new HashMap<String, Object>(1);
    headers.put("Content-Type", "application/json");
    headers.put("Content-Length", sourceLength);
    
    # add the headers as function parameter
    client.dispatch("http://localhost:8880/products", source, headers);
    

    编辑:正如@sceaj所指出的,内容类型和内容长度标题都是必需的。