【问题标题】:Writing Junit Test case for merging files in sftp server编写用于在 sftp 服务器中合并文件的 Junit 测试用例
【发布时间】:2022-11-18 04:15:50
【问题描述】:

我正在使用 apache commons VFS 连接到 sftp 服务器,并将 /input 目录中的文件内容写入 /output 目录中的单个文件。输入目录中的文件名作为 List 提供。我正在努力为其编写Junit测试用例。我的意图是一旦代码被执行,我将比较/input中的文件内容与/output中的文件内容

public void exportFile(List<String> fileNamesList){
for (String file : fileNamesList){
            try(FileObject fileObject= //getsFileObject
                OutputStream fileOutputStream= fileObject.resolveFile("/output/"+"exportfile.txt").getContent().getOutputStream(true);
            )
                                            fileObject.resolveFile("/input/"+file).getContent().getInputStream().transferTo(fileOutputStream);
    
            }
            }

我想为上面写 Junit 测试用例。下面是我的测试用例设置

            @BeforeAll
                  static void setUpSftpServer() throws IOException {
                System.out.println("inside setup ssh");
                sshd= SshServer.setUpDefaultServer();
                    sshd.setPort(1234);
                    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
                    sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE);
                    sshd.setSubsystemFactories(Arrays.asList(new SftpSubsystemFactory()));
                    sshd.start();
            }
        
         @Test
            void exportFileTest() throws IOException, URISyntaxException {
        
                System.out.println("Inside exportFile test");
                FileObject fileObject=getFileObject();
                when(sftpConfiguration.connect()).thenReturn(fileObject);
                myobject.exportFile(Arrays.asList("a.txt"));
               String actualContent=fileObject.resolveFile("/input/a.txt").getContentContent().getString("UTF-8");
 String expectedContent=fileObject.resolveFile("/output/exportFile.txt").getContentContent().getString("UTF-8");
                assertTrue(actualContent.equals(expectedContent));
        
        
            }
    static FileObject getFileObject() throws URISyntaxException, FileSystemException {
            String userInfo = "uname" + ":" + "pwd";
            SftpFileSystemConfigBuilder sftpConfigBuilder = SftpFileSystemConfigBuilder.getInstance();
            FileSystemOptions options = new FileSystemOptions();
            IdentityProvider identityInfo = new IdentityInfo(new File("/fake/path/to/key"), "test".getBytes());
            sftpConfigBuilder.setIdentityProvider(options, identityInfo);
            URI uri=  new URI("sftp", userInfo, "127.0.0.1", Objects.requireNonNullElse(1234, -1), null, null, null);
           FileObject fileObject= VFS.getManager().resolveFile(uri.toString(),options);
           System.out.println("creating file object complete");
           fileObject.resolveFile("/input").createFolder(); //create a folder in the path
           fileObject.resolveFile("/output").createFolder(); 
           //code  to create a file called a.txt inside /input and write the string "abc" to the file
            return  fileObject;
        }

但是我遇到了如下异常

org.apache.commons.vfs2.FileSystemException: Unknown message with code "Could not get the user id of the current user (error code: -1)".

我正在排队的这个例外

FileObject fileObject= VFS.getManager().resolveFile(uri.toString(),options);

如何为这种情况正确编写单元测试?

【问题讨论】:

    标签: java junit mockito junit5 apache-commons-vfs


    【解决方案1】:

    这是由于 SftpFileSystem 未能运行命令 id -u 导致的,该命令在某些 SSH 连接(例如 Windows OpenSSH)上不存在。它在尝试检测 exec 通道时运行此命令。通过添加以下 SFTP 配置来解决此问题:

    sftpConfigBuilder.setDisableDetectExecChannel(options, true);
    

    参见here。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多