
com.github.gun88.fitnesse.fixture.ssh.session.DummySession Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fitnesse-ssh-fixture Show documentation
Show all versions of fitnesse-ssh-fixture Show documentation
SSH and SFTP fixture for FitNesse
The newest version!
package com.github.gun88.fitnesse.fixture.ssh.session;
import com.github.gun88.fitnesse.fixture.ssh.endpoint.Endpoint;
import com.github.gun88.fitnesse.fixture.ssh.option.Option;
import com.github.gun88.fitnesse.fixture.ssh.option.Options;
import com.github.gun88.fitnesse.fixture.ssh.result.ExecutionResult;
import com.github.gun88.fitnesse.fixture.ssh.util.SshClientUtils;
import lombok.AllArgsConstructor;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class DummySession implements SshSession {
final Map streams = new HashMap<>();
private Endpoint endpoint;
private Options options;
private boolean connectionOpen;
@Override
public void open(Endpoint endpoint, Options options) throws IOException {
if (connectionOpen)
throw new IOException("Connection already opened");
this.endpoint = endpoint;
this.options = options;
this.connectionOpen = true;
}
@Override
public void close() throws IOException {
if (!connectionOpen)
throw new IOException("Connection already closed");
this.connectionOpen = false;
}
@Override
public ExecutionResult execute(String command) throws IOException {
if (!connectionOpen)
throw new IOException("Connection closed");
resetWriter("1");
resetWriter("2");
List commands = SshClientUtils.splitQuoted(command, ';')
.stream()
.map(String::trim)
.filter(string -> !string.isEmpty())
.map(this::createCommand)
.collect(Collectors.toList());
int exitCode = commands.stream().mapToInt(this::execute).reduce((a, b) -> b).orElse(0);
ExecutionResult result = new ExecutionResult();
result.setOutput(streams.get("1").toString());
result.setError(streams.get("2").toString());
result.setExitCode(exitCode);
return result;
}
@Override
public ExecutionResult download(String source, String destination) throws IOException {
if (!connectionOpen)
throw new IOException("Connection closed");
ExecutionResult result = new ExecutionResult();
if (this.streams.containsKey(source)) {
Files.write(Paths.get(destination), streams.get(source).toString().getBytes());
result.setOutput("Downloaded at: " + destination);
result.setExitCode(0);
} else {
result.setError("No such file");
result.setExitCode(1);
}
return result;
}
@Override
public ExecutionResult upload(String source, String destination) throws IOException {
if (!connectionOpen)
throw new IOException("Connection closed");
ExecutionResult result = new ExecutionResult();
if (Paths.get(source).toFile().exists()) {
StringWriter stringWriter = streams.computeIfAbsent(destination, x -> new StringWriter());
stringWriter.append(new String(Files.readAllBytes(Paths.get(source))));
result.setOutput("Uploaded at: " + destination);
result.setExitCode(0);
} else {
result.setError("No such file");
result.setExitCode(1);
}
return result;
}
private void resetWriter(String key) throws IOException {
StringWriter old = this.streams.put(key, new StringWriter());
if (old != null)
old.close();
}
private int execute(Command command) {
DummyStreams streams;
try {
streams = buildStreams(command.streamParameter);
} catch (Exception e) {
this.streams.get("2").append(e.getMessage());
return 1;
}
try {
switch (command.name.toLowerCase()) {
case "echo":
return echo(command.parameters, streams);
case "exit":
return exit(command.parameters, streams);
case "dump-endpoint":
return dumpEndpoint(command.parameters, streams);
case "cat":
return cat(command.parameters, streams);
case "rm":
return rm(command.parameters);
case "dump-options":
return dumpOptions(command.parameters, streams);
default:
return unknownCommand(command.name, streams);
}
} catch (Exception e) {
streams.err.append(e.getMessage());
return 1;
}
}
@SuppressWarnings("SameReturnValue")
private int dumpEndpoint(List parameters, DummyStreams streams) {
List options = extractOptions(parameters);
parameters.removeAll(options);
streams.out.append(parameters.isEmpty() ? endpoint.toString() : parameters.stream()
.map(this::fromEndpoint)
.collect(Collectors.joining(" ")));
return 0;
}
private String fromEndpoint(String part) {
switch (part) {
case "username":
return endpoint.getUsername();
case "password":
return endpoint.getPassword();
case "host":
return endpoint.getHost();
case "port":
return String.valueOf(endpoint.getPort());
default:
return "unknown part: " + part;
}
}
@SuppressWarnings("SameReturnValue")
private int dumpOptions(List parameters, DummyStreams streams) {
List options = extractOptions(parameters);
parameters.removeAll(options);
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy