实际上,我正在创建一个POC,用于运行与Kubernetes的Spring集成,为此,我创建了一个集成流程,该流程将读取XML文件,如果它是有效的xml文件,则将其移至Processed Dir,否则将其移至Error Dir
package com.stackoverflow.questions.config;
import static java.util.Arrays.asList;
import com.stackoverflow.questions.dto.WriteResult;
import com.stackoverflow.questions.handler.FileReaderHandler;
import com.stackoverflow.questions.handler.StudentErrorHandler;
import com.stackoverflow.questions.handler.StudentWriterHandler;
import com.stackoverflow.questions.service.DirectoryManagerService;
import com.stackoverflow.questions.transformer.FileToStudentTransformer;
import java.io.File;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.MessageChannels;
import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.file.DirectoryScanner;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.file.RecursiveDirectoryScanner;
import org.springframework.integration.file.filters.AcceptOnceFileListFilter;
import org.springframework.integration.file.filters.CompositeFileListFilter;
import org.springframework.integration.file.filters.RegexPatternFileListFilter;
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.messaging.MessageChannel;
@Configuration
@EnableIntegration
@RequiredArgsConstructor
public class MainIntegrationFlow {
@Value("${regex.filename.pattern}")
private String regexFileNamePattern;
@Value("${root.file.dir}")
private String rootFileDir;
@Value("${default.polling.rate}")
private Long defaultPollingRate;
private final DirectoryManagerService directoryManagerService;
private final StudentErrorHandler studentErrorHandler;
private final FileReaderHandler fileReaderHandler;
private final StudentWriterHandler studentWriterHandler;
private final FileToStudentTransformer fileToStudentTransformer;
@Bean("mainStudentIntegrationFlow")
public IntegrationFlow mainStudentIntegrationFlow(
@Qualifier("mainFileReadingSourceMessage") MessageSource<File> mainFileReadingSourceMessage,
@Qualifier("fileReaderChannel") MessageChannel fileReaderChannel) {
return IntegrationFlows.from(mainFileReadingSourceMessage)
.channel(fileReaderChannel)
.handle(fileReaderHandler)
.transform(fileToStudentTransformer)
.handle(studentWriterHandler)
.<WriteResult, Boolean>route(WriteResult::isWriten,
mapping -> mapping
.subFlowMapping(true, moveToProcessedDirFlow())
.subFlowMapping(false, moveToErrorDirFlow()))
.get();
}
public IntegrationFlow moveToProcessedDirFlow() {
return flow -> flow.handle(message ->
directoryManagerService
.moveToProcessedDir(((WriteResult) message.getPayload()).getFilename()));
}
public IntegrationFlow moveToErrorDirFlow() {
return flow -> flow.channel("studentErrorChannel")
.handle(message ->
directoryManagerService
.moveToErrorDir(((WriteResult) message.getPayload()).getFilename()));
}
@Bean(name = "errorHandlerMainFlow")
public IntegrationFlow errorHandlerMainFlow() {
return IntegrationFlows.from("errorChannel")
.handle(studentErrorHandler)
.get();
}
@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata mainPollerMetadata() {
return Pollers.fixedRate(defaultPollingRate, TimeUnit.SECONDS)
.maxMessagesPerPoll(0)
.get();
}
@Bean(name = "fileReaderChannel")
public MessageChannel fileReaderChannel() {
return MessageChannels.queue("fileReaderChannel").get();
}
@Bean("mainDirectoryScanner")
public DirectoryScanner mainDirectoryScanner() {
DirectoryScanner recursiveDirectoryScanner = new RecursiveDirectoryScanner();
CompositeFileListFilter<File> compositeFileListFilter = new CompositeFileListFilter<>(
asList(new AcceptOnceFileListFilter<>(),
new RegexPatternFileListFilter(regexFileNamePattern)));
recursiveDirectoryScanner.setFilter(compositeFileListFilter);
return recursiveDirectoryScanner;
}
@Bean("mainFileReadingSourceMessage")
public MessageSource<File> mainFileReadingSourceMessage(
@Qualifier("mainDirectoryScanner") DirectoryScanner mainDirectoryScanner) {
FileReadingMessageSource fileReadingMessageSource = new FileReadingMessageSource();
fileReadingMessageSource.setDirectory(new File(rootFileDir));
fileReadingMessageSource.setScanner(mainDirectoryScanner);
return fileReadingMessageSource;
}
}
我正在尝试测试整个流程,为此,我创建了一个测试类:
@SpringBootTest
@SpringIntegrationTest(noAutoStartup = "fileReadingEndpoint")
public class MainFlowIntegratoinTests {
@Autowired
private MockIntegrationContext mockIntegrationContext;
@Autowired
private SourcePollingChannelAdapter fileReadingEndpoint;
@Test
public void readingInvalidFileAndMoveItToErrorDir() throws IOException {
File file = new ClassPathResource("valid01-student-01.xml").getFile();
MessageSource<File> mockInvalidStudentFile = () -> MessageBuilder.withPayload(file).build();
mockIntegrationContext.substituteMessageSourceFor("fileReadingEndpoint", mockInvalidStudentFile);
// start the file adapter manually
fileReadingEndpoint.start();
}
}
我正在其中测试集成流程,但是以某种方式测试未到达编写器端点,我可以从阅读器和转换器端点看到日志,而从编写器看不到日志。
我试图阅读文档https://docs.spring.io/spring-integration/reference/html/testing.html-但无法弄清楚。
请您给我们提供一个示例或更多有关如何测试整个集成流程的详细信息。
工作测试:
package com.stackoverflow.questions;
import static org.apache.commons.io.FileUtils.forceDelete;
import static org.awaitility.Awaitility.await;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD;
import com.stackoverflow.questions.service.DirectoryManagerService;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.util.ReflectionTestUtils;
@SpringBootTest
@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD)
public class MainFlowIntegrationTests {
private static final String MOCK_FILE_DIR = "intFiles/";
private static final String VALID_XML_MOCK_FILE = "valid01-student-01.xml";
private static final String INVALID_XML_MOCK_FILE = "invalid02-student-02.xml";
@Autowired
private MessageChannel fileReaderChannel;
@Autowired
private DirectoryManagerService directoryManagerService;
private File queueDir;
private File processed;
private File error;
@BeforeEach
public void setup() throws IOException {
createRequiredDirectories();
moveFilesToQueueDir();
injectProperties();
}
@AfterEach
public void tearDown() throws IOException {
deleteRequiredDirectories();
}
@Test
public void readingFileAndMoveItToProcessedDir() throws IOException, InterruptedException {
// When: the fileReaderChannel receives a valid XML file
fileReaderChannel
.send(MessageBuilder.withPayload(new File(queueDir, VALID_XML_MOCK_FILE)).build());
// Then: the valid XML file should be sent to the processedDir
await().until(() -> processed.list().length == 1);
}
@Test
public void readingInvalidFileAndMoveItToErrorDir() throws IOException, InterruptedException {
// When: the fileReaderChannel receives a invalid XML file
fileReaderChannel
.send(MessageBuilder.withPayload(new File(queueDir, INVALID_XML_MOCK_FILE)).build());
// Then: the invalid XML file should be sent to the errorDir
await().until(() -> error.list().length == 1);
}
private void injectProperties() {
ReflectionTestUtils
.setField(directoryManagerService, "errorDir", error.getAbsolutePath().concat("/"));
ReflectionTestUtils
.setField(directoryManagerService, "processedDir", processed.getAbsolutePath().concat("/"));
}
private void moveFilesToQueueDir() throws IOException {
File intFiles = new ClassPathResource(MOCK_FILE_DIR).getFile();
for (String filename : intFiles.list()) {
FileUtils.copyFile(new File(intFiles, filename), new File(queueDir, filename));
}
}
private void createRequiredDirectories() throws IOException {
queueDir = Files.createTempDirectory("queueDir").toFile();
processed = Files.createTempDirectory("processedDir").toFile();
error = Files.createTempDirectory("errorDir").toFile();
}
private void deleteRequiredDirectories() throws IOException {
forceDelete(queueDir);
forceDelete(processed);
forceDelete(error);
}
}
答案 0 :(得分:0)
看起来完全没有测试或验证readingInvalidFileAndMoveItToErrorDir()
中的任何内容。 fileReadingEndpoint.start();
是测试方法的最后一行。
请考虑研究什么是JUnit,我们应该如何编写测试方法,以及如何真正验证类似于您的集成流程之类的异步解决方案:https://junit.org/junit5/docs/current/user-guide/
到目前为止,我看到的不是很好。消息源是关于一段时间轮询的。您的mockInvalidStudentFile
一直在生产相同的文件。您的解决方案中确实有期望吗?您可能只是考虑将文件直接发送到配置了消息源的通道。
您不需要fileReadingEndpoint.start();
,因为substituteMessageSourceFor()
会默认自动启动。
以文件作为有效载荷发送到通道后,您的消息将在流中传播。您可能应该看到如何在测试中验证逻辑的正确性。因为您说过将文件放在流末尾的某个目录中,所以可能您应该在发送后检查该目录。甚至可能使用某些循环或等待时间:https://github.com/awaitility/awaitility。
另一种方法是使用substituteMessageHandlerFor()
,以便放置MockMessageHandler
而不是文件编写器来验证文件。注意:如果您直接向通道发送原始消息,而不是作为模拟MessageSource
的原始消息,并且流中没有线程转移,则此方法将起作用。因此,所有流程都将在与测试方法相同的线程中执行。如果流程中有一些异步传递,则MockMessageHandler
应该做一些CountDonwLatch
来阻止测试方法。
说所有这些,我的意思是实践和经验会带来很多与测试理解的细微差别。可能无法为您做一些对其他人有用的样本,因为那里的解决方案可能有所不同,并且需要其他测试方法。
因此,我的建议是:尽您所能,您将知道如何测试自己的解决方案。