### Question:
FtpManager extends FileSender { @Override public boolean isAvailable() { return validSettings(preferenceHelper.getFtpServerName(), preferenceHelper.getFtpUsername(), preferenceHelper.getFtpPassword(), preferenceHelper.getFtpPort(), preferenceHelper.shouldFtpUseFtps(), preferenceHelper.getFtpProtocol(), preferenceHelper.isFtpImplicit()); } FtpManager(PreferenceHelper preferenceHelper); void testFtp(final String servername, final String username, final String password, final String directory, final int port, final boolean useFtps, final String protocol, final boolean implicit); @Override void uploadFile(List<File> files); @Override boolean isAvailable(); @Override boolean hasUserAllowedAutoSending(); void uploadFile(final File f); @Override boolean accept(File file, String s); boolean validSettings(String servername, String username, String password, Integer port, boolean useFtps,
                                 String sslTls, boolean implicit);  }### Answer:
@Test public void IsAvailable_WhenAllValuesPresent_ReturnsTrue(){ PreferenceHelper pm = mock(PreferenceHelper.class); when(pm.getFtpServerName()).thenReturn("example.com"); when(pm.getFtpUsername()).thenReturn("aaa"); when(pm.getFtpPassword()).thenReturn("BBBB"); when(pm.getFtpPort()).thenReturn(9001); when(pm.shouldFtpUseFtps()).thenReturn(true); when(pm.getFtpProtocol()).thenReturn("SSL"); when(pm.isFtpImplicit()).thenReturn(false); FtpManager aem = new FtpManager(pm); assertThat("ALL FTP values indicate valid", aem.isAvailable(), is(true)); }
@Test public void IsAvailable_ServerNameAndPort_ReturnsTrue(){ PreferenceHelper pm = mock(PreferenceHelper.class); when(pm.getFtpServerName()).thenReturn("example.com"); FtpManager aem = new FtpManager(pm); assertThat("Without port, not available", aem.isAvailable(), is(false)); when(pm.getFtpPort()).thenReturn(9001); assertThat("Username with port, available", aem.isAvailable(), is(true)); }
@Test public void IsAvailable_WhenFTPS_ProtocolRequired(){ PreferenceHelper pm = mock(PreferenceHelper.class); when(pm.getFtpServerName()).thenReturn("example.com"); when(pm.getFtpPort()).thenReturn(9001); when(pm.shouldFtpUseFtps()).thenReturn(true); FtpManager aem = new FtpManager(pm); assertThat("If FTPS specified, a protocol is required", aem.isAvailable(), is(false)); when(pm.getFtpProtocol()).thenReturn("TLS"); assertThat("If FTPS specified, a protocol is required", aem.isAvailable(), is(true)); }
@Test public void IsAvailable_AutoSendUnchecked_IsAvailable(){ PreferenceHelper pm = mock(PreferenceHelper.class); when(pm.getFtpServerName()).thenReturn("example.com"); when(pm.isFtpAutoSendEnabled()).thenReturn(false); when(pm.getFtpUsername()).thenReturn("aaa"); when(pm.getFtpPassword()).thenReturn("BBBB"); when(pm.getFtpPort()).thenReturn(9001); when(pm.shouldFtpUseFtps()).thenReturn(true); when(pm.getFtpProtocol()).thenReturn("SSL"); when(pm.isFtpImplicit()).thenReturn(false); FtpManager aem = new FtpManager(pm); assertThat("IsAvailable even if user unchecked autosend", aem.isAvailable(), is(true)); when(pm.getFtpProtocol()).thenReturn("TLS"); assertThat("If FTPS specified, a protocol is required", aem.isAvailable(), is(true)); }