当前位置:   article > 正文

用于在Synology NAS上测试Spring Boot Web应用程序的JUnit模拟文件

springboot读写群晖nas系统文件

对于将在Synology RS815 + NAS上检查备份文件的Spring Boot应用程序,我们希望能够轻松测试此NAS上存储的文件,而不必复制存储在其上的7TB。

理想情况下,我们希望创建相同的文件结构以在Spring开发配置文件中使用Web应用程序,并在JUnit测试中使用这些文件结构。

介绍FileStructureCreator

我们首先创建一个新的类FileStructureCreator ,如下所示:

  1. @Getter
  2. @Setter
  3. public class FileStructureCreator implements Closeable {
  4. public static final Path baseTestPath = Paths.get("testFiles");
  5. private Path fileStructureBasePath;
  6. public static FileStructureCreator create(Path file) {
  7. return createStructure(file, false);
  8. }
  9. public static FileStructureCreator createTempDirectory(Path file) {
  10. return createStructure(file, true);
  11. }
  12. @SneakyThrows
  13. private static FileStructureCreator createStructure(Path file, boolean createTempDirectory) {
  14. FileStructureCreator fileStructureCreator = new FileStructureCreator();
  15. if (!Files.exists(baseTestPath)) {
  16. Files.createDirectory(baseTestPath);
  17. }
  18. String path = baseTestPath.toString() + (createTempDirectory ? "/" + UUID.randomUUID().toString() : "")
  19. + "/";
  20. Path basePath = Paths.get(path);
  21. fileStructureCreator.setFileStructureBasePath(basePath);
  22. FileUtils.forceMkdir(basePath.toFile());
  23. try (Stream<String> stream = Files.lines(file)) {
  24. stream.forEach(line -> {
  25. Metadata fileMetaData = Metadata.from(line);
  26. Path fileEntry = Paths.get(path + fileMetaData.getWindowsSafeFilename());
  27. try {
  28. FileUtils.forceMkdir(fileEntry.getParent().toFile());
  29. if (!Files.exists(fileEntry)) {
  30. Files.write(fileEntry, line.getBytes());
  31. Files.setLastModifiedTime(fileEntry, FileTime.from(fileMetaData.getModificationTime()));
  32. }
  33. } catch (IOException ignore) {
  34. throw new RuntimeException("Exception creating directory: " + fileEntry.getParent());
  35. }
  36. });
  37. }
  38. return fileStructureCreator;
  39. }
  40. @Override
  41. @SneakyThrows
  42. public void close() {
  43. if (fileStructureBasePath != null) {
  44. FileUtils.deleteDirectory(fileStructureBasePath.toFile());
  45. }
  46. }
  47. }

基本上,这将创建整个目录结构和必要的文件。 我们只需要传递一个包含文件结构元数据的基本文件即可。

元数据保存时间戳,文件大小和该文件的路径。 看起来像这样:

  1. 2016-04-05T10:30:15.012345678   5120
  2. backupftp/@eaDir/sharesnap_share_configuration/SYNO@.quota
  3. 2018-02-26T00:00:09.012345678  169
  4. backupftp/@eaDir/sharesnap_share_configuration/share_configuration

然后,在Synology NAS上,我们可以通过执行以下命令轻松生成具有(特定)目录的整个树结构的文件:

  1. find backupftp -type f -printf
  2. "%TY-%Tm-%TdT%TH:%TM:%.12TS\t%s\t%p\n">test/backupftp.files.txt

将生成的文件从您的Synology NAS复制到您的项目。

在JUnit测试中,我们使用FileStructureCreator类,如下面的示例所示。 请注意, FileStructureCreator实现了AutoCloseable ,因此我们可以在测试完成后使用try / catch块来清理文件。

  1. @Value("classpath:/TestDiskConsistencyPolicy-notEnoughFileSets.txt")
  2. private Path notEnoughFileSets;
  3. @Test(expected = RuntimeException.class)
  4. public void backupSetWithNoFileSetsThrowException() {
  5. try( FileStructureCreator creator = FileStructureCreator.createTempDirectory(notEnoughFileSets) ) {
  6. BackupSet backupSet = BackupSet.builder().uri(creator.getFileStructureBasePath().toString()).build();
  7. new DiskConsistencyPolicy(backupSet).execute();
  8. assertTrue( "Expecting a RuntimeException here", false);
  9. }
  10. }

对于Spring Boot应用程序,我们只定义一个@Configuration类,该类将为Synology NAS上定义的文件共享创建数据结构。

  1. @Configuration
  2. @Profile("dev")
  3. public class TestFilesInstaller {
  4. @Bean
  5. public FileStructureCreator ftpFiles(@Value("classpath:/backupftp.files.txt") Path file) {
  6. return FileStructureCreator.create(file);
  7. }
  8. @Bean
  9. public FileStructureCreator nfsFiles(@Value("classpath:/backupnfs.files.txt") Path file) {
  10. return FileStructureCreator.create(file);
  11. }
  12. }

因为它们被定义为@Bean ,所以在应用程序关闭时将自动调用close()方法,并在Spring Boot应用程序停止时从磁盘上删除所有文件。
只是……不要在生产环境中运行开发人员资料; 我让你知道会发生什么。 ;-) 将来,我们将向您展示如何构建备份检查器以监视和验证NAS上的备份。

翻译自: https://www.javacodegeeks.com/2018/04/mocking-files-for-junit-testing-a-spring-boot-web-application-on-synology-nas.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/224875
推荐阅读
相关标签
  

闽ICP备14008679号