为什么存根的sftp Camel端点不遵守此测试中的“include”选项?

elcex8rz  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(133)

为了测试目的,我正在尝试stub一个sftp消费者端点--我还不想尝试启动一个容器。

import org.apache.camel.*;
import org.apache.camel.builder.AdviceWith;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.apache.camel.test.spring.junit5.MockEndpointsAndSkip;
import org.apache.camel.test.spring.junit5.UseAdviceWith;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import static com.routes.zipping.Resources.*;

/**
 * Tests a from() route, the scheme of which is not "direct", but is "sftp".
 */

@CamelSpringBootTest
@SpringBootTest(properties = "camel.springboot.java-routes-include-pattern=**/GetWretchedBillImagesRoute*")
@MockEndpointsAndSkip("direct:split")
@UseAdviceWith
public class ITGetWretchedBIllImagesRoute {

    @Produce
    ProducerTemplate producerTemplate;

    @Autowired
    CamelContext camelContext;

    @EndpointInject("mock:direct:split")
    MockEndpoint mockConvert;

    private String stubUrl;

    @Test
    @DisplayName("ZIPs are count")
    @DirtiesContext
    @Disabled("This gives back inconsistent results or seems to ignore the 'include' file option, so zip of any name or even a pdf is received at mocked endpoint.")
    void testGetFile_ZipMessageCountsButNotPdf() throws Exception {
        File zip = new File(TEST_PATH + ZIP + IN + "zipWith3Files.zip");
        assertTrue(zip.exists(), "The required test resource file is unavailable");

        File pdf = new File(TEST_PATH + PDF + IN + "myPdf.pdf");
        assertTrue(pdf.exists(), "The required test resource file is unavailable");

        AdviceWith.adviceWith(camelContext, "Wretched Bill Image ZIP Poller", routeBuilder -> {
            RouteDefinition rd = routeBuilder.getOriginalRoute();
            rd.setAutoStartup("true");
            var originalUrl = rd.getEndpointUrl();
            System.out.println("Advicing " + originalUrl);
            stubUrl = "stub:" + originalUrl;
            routeBuilder.replaceFromWith(stubUrl);
        });

        camelContext.start();

        mockConvert.expectedMessageCount(0);

        producerTemplate.sendBody(stubUrl, zip);
        producerTemplate.sendBody(stubUrl, zip);
        producerTemplate.sendBody(stubUrl, pdf);
        producerTemplate.sendBody(stubUrl, pdf);

        mockConvert.assertIsSatisfied();
    }
}

这是最初的路线:

import com.routes.client.props.WretchedRouteProps;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GetWretchedBillImagesRoute extends RouteBuilder {
    public static String ROUTE_ID = "Wretched Bill Image ZIP Poller";

    private final WretchedRouteProps arp;

    @Autowired
    public GetWretchedBillImagesRoute(WretchedRouteProps wretchedRouteProps) {
        this.arp = wretchedRouteProps;
    }

    //@formatter:off
    @Override
    public void configure() throws Exception {
        from(getSftpStartEndpoint())
                .log("Polled for zipped image files for Wretched")
                .id("Wretched Bill Image ZIP Poller")
                .routeId("Wretched Bill Image ZIP Poller")
                .setHeader("unzipTo", constant(arp.getFileDestination()))
                .setHeader("nameMatters", constant(arp.nameMatters()))
                .setHeader("client", constant("Wretched"))
                .autoStartup(arp.isAutoStart() || arp.shouldAutoStartThisRoute())
                .to("direct:split");
    }
    //@formatter:on

    private String getSftpStartEndpoint() {
        return new StringBuilder()
                .append("sftp://").append(arp.getSftpUser()).append("@").append(arp.getSftpHost()).append("/")
                .append(arp.getSftpPath())
                .append("?password=").append(arp.getSftpPassword())
                .append("&scheduler=spring&scheduler.cron=").append(arp.getRouteSchedule())
                .append("&readLock=changed&readLockMinAge=300000")
                .append("&streamDownload=true&stepwise=false")
                .append("&timeUnit=MINUTES")
                .append("&noop=").append(arp.shouldMoveFromFtp())
                .append("&move=").append(arp.getArchiveDestination())
                .append("&include=").append(arp.getWretchedBillZipRegex())
                .toString();
    }
}

我注意到,这将通过与多重期望:
mockConvert.expectedMessageCount(4);``mockConvert.expectedMessageCount(4);
只有当我期望的消息多于我生成的消息时,它才会失败,例如,
mockConvert.expectedMessageCount(5);
那么,1)为什么存根选项没有排除所有我通过生产者发送的文件,考虑到“include”正则表达式中的不匹配,以及2)为什么多个期望通过?顺便说一句,如果有更好的方法来测试现有的sftp消费者端点,而不使用测试容器,请告诉我,或者给我指出一些地方。

0lvr5msh

0lvr5msh1#

我只知道你第二个问题的答案
第二章
MockEndpoint.expectedMessageCount(i)只确保收到i消息。一旦达到消息计数,它就认为期望值已满足。任何无关的消息都不考虑在内。快速查看文档后,您可以使用setAssertPeriod(l)强制要求期望的消息计数为真,即使在该时间段之后也是如此。之所以5不满足“T通过的原因是端点从未接收到第五条消息。

相关问题