【问题标题】:Mule - testing a flow that has an inbound/outbound endpoint with a Mule property as the file pathMule - 测试具有入站/出站端点的流,并将 Mule 属性作为文件路径
【发布时间】:2014-05-13 10:06:16
【问题描述】:

我正在尝试为 Mule 流编写一​​个测试,该测试将涉及将文件放在某个位置,等待它由我的流处理并比较输出以查看它是否已正确转换。我的流程如下所示:

<flow name="mainFlow" processingStrategy="synchronous">
    <file:inbound-endpoint name="fileIn" path="${inboundPath}">
        <file:filename-regex-filter pattern="myFile.csv" caseSensitive="true"/> 
    </file:inbound-endpoint>

    ...

    <file:outbound-endpoint path="${outboundPath}" outputPattern="out.csv"/>
</flow>

有没有一种方法可以访问我的测试类中的 inboundPathoutboundPath Mule 属性,以便我可以删除文件并在正确的位置等待输出?

我正在使用的测试类是:

public class MappingTest extends BaseFileToFileFunctionalTest {

    @Override
    protected String getConfigResources() {
        return "mappingtest.xml";
    }

    @Test
    public void testMapping() throws Exception {
        dropInputFileIntoPlace("myFile.csv");
        waitForOutputFile("out.csv", 100);

        assertEquals(getExpectedOutputFile("expected-out.csv"), getActualOutputFile("out.csv"));
    }

}

扩展了这个类:

public abstract class BaseFileToFileFunctionalTest extends FunctionalTestCase {
    private static final File INPUT_DIR = new File("/tmp/muletest/input");
    private static final File OUTPUT_DIR = new File("/tmp/muletest/output");
    private static final Charset CHARSET = Charsets.UTF_8;

    @Before
    public void setup() {
        new File("/tmp/muletest/input").mkdirs();
        new File("/tmp/muletest/output").mkdirs();

        empty(INPUT_DIR);
        empty(OUTPUT_DIR);

    }

    private void empty(File inputDir) {
        for (File file : inputDir.listFiles()) {
            file.delete();
        }
    }

    protected File waitForOutputFile(String expectedFileName, int retryAttempts) throws InterruptedException {
        boolean polling = true;
        int attemptsRemaining = retryAttempts;
        File outputFile = new File(OUTPUT_DIR, expectedFileName);

        while (polling) {
            Thread.sleep(100L);
            if (outputFile.exists()) {
                polling = false;
            }
            if (attemptsRemaining == 0) {
                VisibleAssertions.fail("Output file did not appear within expected time");
            }
            attemptsRemaining--;
        }
        outputFile.deleteOnExit();

        return outputFile;
    }

    protected void dropInputFileIntoPlace(String inputFileResourceName) throws IOException {
        File inputFile = new File(INPUT_DIR, inputFileResourceName);
        Files.copy(Resources.newInputStreamSupplier(Resources.getResource(inputFileResourceName)), inputFile);
        inputFile.deleteOnExit();
    }

    protected String getActualOutputFile(String outputFileName) throws IOException {
        File outputFile = new File(OUTPUT_DIR, outputFileName);
        return Files.toString(outputFile, CHARSET);
    }

    protected String getExpectedOutputFile(String resourceName) throws IOException {
        return Resources.toString(Resources.getResource(resourceName), CHARSET);
    }
}

如您所见,我目前正在创建临时输入/输出目录。如果可能,我想让这部分从 Mule 属性中读取?提前致谢。

【问题讨论】:

  • 加载配置文件时,您也应该加载这些属性文件。因此它们应该在您的流入站和出站端点中可用。
  • 所以我的问题是如何从 在我的 Java 类中访问这些?当我指定删除测试文件的位置以及轮询输出的位置时
  • 添加测试用例类代码示例以了解您使用什么方法来加载流配置。这将有助于提供正确的解决方案
  • @user1760178,请查看更新后的问题

标签: java testing properties mule filepath


【解决方案1】:

观察您的测试类和代码后,我可以看到您想要动态创建临时文件夹并将文件放入其中。并且流程应该从临时目录读取文件并将输出写入另一个临时目录。需要注意的一点是,Mule 的 Endpoints 是在加载配置时创建的。因此,${inbound} 和 ${outbound} 应该在提供时提供给 mule 流。

  1. 因此,一种选择是创建一个指向临时文件夹的虚拟流进行测试。 或
  2. 创建一个指向临时文件夹的测试属性文件并将其加载到您的流程配置中,以便您的流程端点将获得临时文件夹路径。

在流入站端点创建后(在配置加载时),无论如何都无法为其提供路径。

更新1: 根据您的评论,带有选项的解决方案如下所示。

  1. 将加载部分配置的属性分离到另一个配置中。 就像“mapping-core-config.xml,mappingtest.xml”一样,mapping-core-config 将有标签来加载属性文件。

  2. 现在为加载测试属性文件的 mapping-core-config.xml 文件创建一个测试配置文件。这应该在您的测试配置中使用。这样,在不修改或干扰主代码的情况下,您可以测试指向临时文件夹的流。

    "mapping-core-test-config.xml,mappingtest.xml"

注意:测试配置可以驻留在 src/test/resources 文件夹中。

希望这会有所帮助。

【讨论】:

  • 感谢您的回答。鉴于此过程将被重新用于许多流,我认为第一个选项会创建不必要数量的虚拟流 - 所以看起来第二个选项是这里要使用的选项。你能提供一个我如何加载测试属性文件的例子吗?或者它只是用return "mappingtest.xml,test.properties";替换我的MappingTest类中的return "mappingtest.xml";,然后用临时文件夹路径填充test.properties
  • 感谢@user1760178 的更新 - 效果很好!感谢您的帮助。
猜你喜欢
  • 2012-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多