Skip to content

PowerMock and FakeSftpServerRule Server wont start #18

@stefanwendelmann

Description

@stefanwendelmann

I have a worst case scenario application i have to write unit test for.
Much static & legacy code.
I managed to get it running by powermock static methods.
One Unit test has to run against a SFTP Server.
I tried your rule in a clean env. and it works as designed.
But when i add it to our code, the server wont start. (Tired with FileZilla to connect to the random port).

package de.itout.spinterface.interfaces.fileDistributor;

import com.github.stefanbirkner.fakesftpserver.rule.*;
import de.gsq.system.*;
import de.itout.spinterfaces.*;
import de.itout.spinterfaces.interfaces.*;
import java.io.*;
import java.net.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.util.*;
import org.junit.*;
import static org.junit.Assert.*;
import org.junit.runner.*;
import static org.mockito.Mockito.*;
import org.powermock.api.mockito.*;
import org.powermock.core.classloader.annotations.*;
import org.powermock.modules.junit4.*;

/**
 * Unit Test der Schnittstelle FileDistributor.
 *
 * Testcase: File SFTP Upload.
 *
 * @author swendelmann
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest(VO.class)
@PowerMockIgnore(
  {
    "javax.management.*", "javax.script.*", "org.apache.sshd.*", "com.github.*"
  })
public class FileDistributorSFTPUploadTest
{

  /**
   * Interface to Test.
   */
  private SPInterface inter;

  @Rule
  public final FakeSftpServerRule sftpServer = new FakeSftpServerRule().addUser("username", "password");

  private final String sftpDir = "/data";
  private final String sftpUser = "username";
  private final String sftpPass = "password";
  private File testFile;

  public FileDistributorSFTPUploadTest()
  {
  }

  @Before
  public void setUp() throws URISyntaxException, IOException
  {
    Environment.getInstance().setApplicationType(Environment.APPLICATION_TYPE_CLI);

    PowerMockito.mockStatic(VO.class);
    PowerMockito.when(VO.getVO(anyString())).thenReturn("mocked");
    PowerMockito.when(VO.getVO(anyString(), any(Object[].class))).thenReturn("mocked");
    PowerMockito.when(VO.getVO(anyString(), any(Object[].class), anyString())).thenReturn("mocked");
    PowerMockito.when(VO.getVO(anyString(), any(Class.class))).thenReturn("mocked");
    PowerMockito.when(VO.getVOEntry(anyString(), anyString(), anyString(), any(Object[].class), anyString(), anyString())).thenReturn("mocked");

    // Interface Setup
    inter = new FileDistributor();
    inter.setLaufID("FD00002");
    inter.setSchnittstelle("FileDist");
    inter.setVersion("FD02");

    testFile = new File(getClass().getClassLoader().getResource("order.csv").toURI());

    sftpServer.createDirectory(sftpDir);

  }

  @After
  public void tearDown()
  {
  }

  @Test
  public void ftpUloadFileFromFilesystem() throws URISyntaxException, IOException
  {
    // Init Interface Parameter
    HashMap<String, SPInterface.ParameterHeader> header = new HashMap<>();
    LinkedHashMap<String, SPParameter> parameter = new LinkedHashMap<>();
    header.put("SAVE_DIC", inter.new ParameterHeader("A", 1, "Sicherungsverzeichnis"));
    header.put("IN_DIR", inter.new ParameterHeader("A", 1, "Input Verzeichnis"));
    header.put("FILESEL", inter.new ParameterHeader("A", 1, "Datei Selection"));
    header.put("SFTP", inter.new ParameterHeader("A", 1, "SFTP übertragen?"));
    header.put("SFTPDIR", inter.new ParameterHeader("A", 1, "SFTP Verz"));
    header.put("SFTPHOST", inter.new ParameterHeader("A", 1, "SFTP host"));
    header.put("SFTPPORT", inter.new ParameterHeader("A", 1, "SFTP Port"));
    header.put("SFTPUSER", inter.new ParameterHeader("A", 1, "SFTP User"));
    header.put("SFTPPWD", inter.new ParameterHeader("A", 1, "SFTP PWD"));

    parameter.put("SAVE_DIC_1", new SPParameter("A", "test-save-dic", 0, "", "Sicherungsverzeichnis"));
    parameter.put("IN_DIR_1", new SPParameter("A", testFile.getParent(), 0, "", "Input Verzeichnis"));
    parameter.put("FILESEL_1", new SPParameter("A", testFile.getName(), 0, "", "Datei Selection"));
    parameter.put("SFTP_1", new SPParameter("A", "JA", 0, "", "SFTP übertragen?"));
    parameter.put("SFTPDIR_1", new SPParameter("A", sftpDir, 0, "", "SFTP Verz"));
    parameter.put("SFTPHOST_1", new SPParameter("A", "localhost", 0, "", "SFTP host"));
    parameter.put("SFTPPORT_1", new SPParameter("A", "", sftpServer.getPort(), "", "SFTP Port"));
    parameter.put("SFTPUSER_1", new SPParameter("A", sftpUser, 0, "", "SFTP User"));
    parameter.put("SFTPPWD_1", new SPParameter("A", sftpPass, 0, "", "SFTP PWD"));
    inter.loadTestParameter(header, parameter);

    inter.startTest();

    // Assertions
    // Assert Interface Values (status, appended files, logging, etc.)
    assertEquals(SPInterface.OK, inter.getLaufStatus());
    assertTrue(inter.getLaufDateien().stream().filter(o -> o.getDatei().equals(testFile.getName())).findFirst().isPresent());
    assertTrue(sftpServer.existsFile(sftpDir + "/order.csv"));
    assertEquals("A;B;C", sftpServer.getFileContent(sftpDir + "/order.csv", UTF_8));

    //TODO: Weitere Assertions definieren.
  }

}

The Output

Running de.itout.spinterface.interfaces.fileDistributor.FileDistributorSFTPUploadTest
ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [level]
ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [logger]
ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [msg]
ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [n]
ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.
ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2
ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [level]
ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [logger]
ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [msg]
ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [n]
ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions