Skip to content
Snippets Groups Projects
Commit 68acc930 authored by Lukas Johannes Horn's avatar Lukas Johannes Horn Committed by Ellen Seifert
Browse files

Resolve "Add rmdir command"

parent 002aed2a
No related branches found
No related tags found
1 merge request!29Resolve "Add rmdir command"
......@@ -70,4 +70,11 @@ public interface SSHFacade {
*/
public DeletionResult delete(String remoteFilePath);
/**
* Removes a directory on remote server. Watch out directory has to be empty! If necessary clean it with rm method first
* @param remoteFilePath path on remote containing the directory to get deleted at the end
* @return RemoveDirectoryResult containing the most important information about the deletion
*/
public RemoveDirectoryResult rmdir(String remoteFilePath);
}
......@@ -10,10 +10,7 @@ import de.uniwue.swp.dragdropssh.ssh.connection.SSHSession;
import de.uniwue.swp.dragdropssh.ssh.connection.ServerData;
import de.uniwue.swp.dragdropssh.ssh.connection.UserLogin;
import de.uniwue.swp.dragdropssh.ssh.results.*;
import de.uniwue.swp.dragdropssh.ssh.tasks.console.CreateDirectory;
import de.uniwue.swp.dragdropssh.ssh.tasks.console.Delete;
import de.uniwue.swp.dragdropssh.ssh.tasks.console.ListStructure;
import de.uniwue.swp.dragdropssh.ssh.tasks.console.WorkingDirectoryHandler;
import de.uniwue.swp.dragdropssh.ssh.tasks.console.*;
import de.uniwue.swp.dragdropssh.ssh.tasks.transfer.DataTransferHandler;
import de.uniwue.swp.dragdropssh.ssh.tasks.transfer.Download;
import de.uniwue.swp.dragdropssh.ssh.tasks.transfer.Upload;
......@@ -159,4 +156,23 @@ public class SSHManagement implements SSHFacade {
}
}
/**
* Removes a directory on remote server. Watch out directory has to be empty! If necessary clean it with rm method first
*
* @param remoteFilePath path on remote containing the directory to get deleted at the end
* @return RemoveDirectoryResult containing the most important information about the deletion
*/
@Override
public RemoveDirectoryResult rmdir(String remoteFilePath) {
String name = remoteFilePath.substring(remoteFilePath.lastIndexOf("/")+1);
String path = remoteFilePath.substring(0,remoteFilePath.lastIndexOf("/")+1);
logger.info("Deleting directory "+name+" in "+path);
try{
RemoveDirectory rm = new RemoveDirectory(path,name);
return rm.rmdir();
}catch(JSchException jsche){
return new RemoveDirectoryResult(Status.FAILED, name, jsche.getMessage(), jsche.getClass().getSimpleName());
}
}
}
package de.uniwue.swp.dragdropssh.ssh.results;
/**
* @author Lukas Johannes Horn
**/
public class RemoveDirectoryResult extends Result{
private String name;
/**
* Constructs a new RemoveDirectoryResult for a successful deletion
*
*/
public RemoveDirectoryResult(String name) {
super(Status.WORKED);
this.name = name;
}
/**
* Constructs a new result for a failed deletion which contains the exception message and class
*
* @param status of the deletion
* @param exceptionMessage message of the most significant exception
* @param exceptionName class name of the most significant exception
*/
public RemoveDirectoryResult(Status status, String name, String exceptionMessage, String exceptionName) {
super(status, exceptionMessage, exceptionName);
this.name = name;
}
}
package de.uniwue.swp.dragdropssh.ssh.tasks.console;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import de.uniwue.swp.dragdropssh.ssh.results.RemoveDirectoryResult;
import de.uniwue.swp.dragdropssh.ssh.results.Status;
/**
* @author Lukas Johannes Horn
**/
public class RemoveDirectory extends ConsoleAccess{
private WorkingDirectoryHandler handler;
private String name;
/**
* Creates a RemoveDirectory objects which holds an sftp channel and already connects this channel
*
* @throws JSchException
*/
public RemoveDirectory(String path, String name) throws JSchException {
this.handler = new WorkingDirectoryHandler(path, channel);
this.name = name;
}
/**
* Deletes a directory with a specific name on remote. First changes into this directory and creates afterwards
* @return
*/
public RemoveDirectoryResult rmdir(){
try{
handler.switchDirectory();
channel.rmdir(name);
logger.info("Successfully deleted directory "+name);
}catch(SftpException sftpe){
logger.severe(sftpe.getMessage());
logger.severe("Probably the directory is not empty");
return new RemoveDirectoryResult(Status.FAILED,name,sftpe.getMessage(), sftpe.getClass().getSimpleName());
}finally {
closeAll();
handler.closeAll();
}
return new RemoveDirectoryResult(name);
}
}
......@@ -46,7 +46,7 @@ public class MakeDirectoryTest {
@BeforeAll
public static void initializeKnownHostsPath(){
knownHostsPath = UploadTest.class.getClassLoader().getResource("known_hosts").getPath().replace("%20"," ");
knownHostsPath = MakeDirectoryTest.class.getClassLoader().getResource("known_hosts").getPath().replace("%20"," ");
}
......
package de.uniwue.swp.dragdropssh.ssh.tasks.console;
import com.jcraft.jsch.*;
import de.uniwue.swp.dragdropssh.ssh.connection.SSHSession;
import de.uniwue.swp.dragdropssh.ssh.connection.SSHSessionAccessor;
import de.uniwue.swp.dragdropssh.ssh.connection.ServerData;
import de.uniwue.swp.dragdropssh.ssh.connection.UserLogin;
import de.uniwue.swp.dragdropssh.ssh.results.RemoveDirectoryResult;
import de.uniwue.swp.dragdropssh.ssh.results.Status;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Vector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;
import static org.junit.jupiter.api.Assertions.assertFalse;
/**
* @author Lukas Johannes Horn
**/
@ExtendWith(MockitoExtension.class)
public class RemoveDirectoryTest {
private Session session;
@Mock(answer = Answers.CALLS_REAL_METHODS)
private UserLogin user;
@Mock
private ServerData server;
private static String knownHostsPath;
@BeforeAll
public static void initializeKnownHostsPath(){
knownHostsPath = RemoveDirectoryTest.class.getClassLoader().getResource("known_hosts").getPath().replace("%20"," ");
}
@BeforeEach
public void setupUserAndServer() throws JSchException {
when(user.getUsername()).thenReturn("root");
when(user.getPassword()).thenReturn("root");
lenient().when(user.authenticateHost(anyString())).thenReturn(true);
when(server.getPort()).thenReturn(2222);
when(server.getHostName()).thenReturn("localhost");
JSch jsch = new JSch();
jsch.setKnownHosts(knownHostsPath);
session = jsch.getSession(user.getUsername(),server.getHostName(),server.getPort());
session.setUserInfo(user);
session.connect();
SSHSessionAccessor.instantiateSSHSession(session,user,server,knownHostsPath);
}
@AfterEach
public void closeSession() throws NoSuchFieldException, IllegalAccessException {
Field f = SSHSession.class.getDeclaredField("instance");
f.setAccessible(true);
f.set(null,null);
}
@Test
public void removeDirectories() throws JSchException, SftpException {
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
sftp.connect();
List<String> directories = IntStream.rangeClosed(0,10).mapToObj(i -> "test"+i).collect(Collectors.toList());
directories.forEach(d-> {
try {
sftp.mkdir(d);
} catch (SftpException e) {
e.printStackTrace();
}
});
directories.forEach(d-> {
try {
RemoveDirectory directory = new RemoveDirectory("/root/",d);
RemoveDirectoryResult res = directory.rmdir();
assertEquals(res.getStatus(), Status.WORKED);
} catch (JSchException e) {
e.printStackTrace();
}
});
Vector<ChannelSftp.LsEntry> ls = sftp.ls(".");
List<String> list = ls.stream().map(ChannelSftp.LsEntry::getFilename).collect(Collectors.toList());
directories.forEach(d->assertFalse(list.contains(d)));
sftp.disconnect();
}
@Test
public void testNoConnection() throws JSchException {
session.disconnect();
assertThrows(JSchException.class,() ->new RemoveDirectory("/root/","test"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment