Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fslevoaca-ionos committed Dec 18, 2020
1 parent 349f1e7 commit 465dc00
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 62 deletions.
5 changes: 4 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog

### 1.15-SNAPSHOT
### 1.16-SNAPSHOT


### 1.15
Refactoring

### 1.14
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/io/jtest/utils/clients/database/SqlClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public SqlClient(String url, String user, String pwd, String driverClassName) {
}

public void connect() throws SQLException {
conn = DriverManager.getConnection(url, user, pwd);
LOG.debug("---- DB SETUP ----");
LOG.debug("Driver: {}", driverClassName);
LOG.debug("Database url: {}", url);
conn = DriverManager.getConnection(url, user, pwd);
}

public PreparedStatement prepareStatement(String sql) throws SQLException {
Expand All @@ -62,7 +62,7 @@ public void close() throws SQLException {
}
}

public List<Map<String, Object>> executeQueryAndGetRsAsList() {
public List<Map<String, Object>> executeQueryAndGetRsAsList() throws SQLException {
LOG.debug("---- SQL QUERY REQUEST ----");
LOG.debug("SQL query: {}", sql);
List<Map<String, Object>> tableData = new ArrayList<>();
Expand All @@ -79,8 +79,6 @@ public List<Map<String, Object>> executeQueryAndGetRsAsList() {
tableData.add(rowData);
}
return tableData;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
Expand Down
18 changes: 5 additions & 13 deletions src/main/java/io/jtest/utils/clients/jmx/JmxClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,12 @@ public JmxClient(String url, String role, String pwd) {
}
}

public <T> T getMBeanProxy(String objectName, Class<T> clazz) {
try {
return MBeanServerInvocationHandler
.newProxyInstance(mbsConnection, new ObjectName(objectName), clazz, true);
} catch (MalformedObjectNameException e) {
throw new RuntimeException(e);
}
public <T> T getMBeanProxy(String objectName, Class<T> clazz) throws MalformedObjectNameException {
return MBeanServerInvocationHandler
.newProxyInstance(mbsConnection, new ObjectName(objectName), clazz, true);
}

public void close() {
try {
this.jmxConnector.close();
} catch (IOException e) {
LOG.error(e);
}
public void close() throws IOException {
this.jmxConnector.close();
}
}
54 changes: 23 additions & 31 deletions src/main/java/io/jtest/utils/clients/jsch/JschClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,33 @@ public JschClient(String host, int port, String user, String pwd, String private
}
}

public void connect() {
try {
LOG.info("Connecting over SSH to \"{}:{}\" with user \"{}\" and privateKey \"{}\"", host, port, user, privateKey);
this.session.connect();
LOG.info("Connected");
} catch (JSchException e) {
throw new RuntimeException(e);
}
public void connect() throws JSchException {
LOG.info("Connecting over SSH to \"{}:{}\" with user \"{}\" and privateKey \"{}\"", host, port, user, privateKey);
this.session.connect();
LOG.info("Connected");
}

public String sendCommand(String cmd) {
try {
LOG.info("Execute command over SSH: \"{}\"", cmd);
Channel channel = this.session.openChannel("exec");
((ChannelExec) channel).setCommand(cmd);
InputStream commandOutput = channel.getInputStream();
InputStream commandErrOutput = ((ChannelExec) channel).getErrStream();
channel.connect();
StringBuilder outputBuffer = new StringBuilder();
int readByte = commandOutput.read();
while (readByte != 0xffffffff) {
outputBuffer.append((char) readByte);
readByte = commandOutput.read();
}
public String sendCommand(String cmd) throws IOException, JSchException {
LOG.info("Execute command over SSH: \"{}\"", cmd);
Channel channel = this.session.openChannel("exec");
((ChannelExec) channel).setCommand(cmd);
InputStream commandOutput = channel.getInputStream();
InputStream commandErrOutput = ((ChannelExec) channel).getErrStream();
channel.connect();
StringBuilder outputBuffer = new StringBuilder();
int readByte = commandOutput.read();
while (readByte != 0xffffffff) {
outputBuffer.append((char) readByte);
readByte = commandOutput.read();
}
readByte = commandErrOutput.read();
while (readByte != 0xffffffff) {
outputBuffer.append((char) readByte);
readByte = commandErrOutput.read();
while (readByte != 0xffffffff) {
outputBuffer.append((char) readByte);
readByte = commandErrOutput.read();
}
channel.disconnect();
LOG.debug("Output over SSH: {}", outputBuffer.toString());
return outputBuffer.toString();
} catch (JSchException | IOException e) {
throw new RuntimeException(e);
}
channel.disconnect();
LOG.debug("Output over SSH: {}", outputBuffer.toString());
return outputBuffer.toString();
}

public void disconnect() {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/jtest/utils/clients/shell/ShellClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public ShellClient() {
this.processBuilder = new ProcessBuilder();
}

public ProcessBuilder getProcessBuilder() {
return processBuilder;
public ShellClient(ProcessBuilder processBuilder) {
this.processBuilder = processBuilder;
}

public String execute(String... command) {
StringBuilder outputBuffer = new StringBuilder();
try {
Process p = process(command);
Process p = startProcess(command);
InputStream stdInput = p.getInputStream();
InputStream stdError = p.getErrorStream();

Expand All @@ -47,7 +47,7 @@ public String execute(String... command) {
return outputBuffer.toString();
}

public Process process(String... command) {
public Process startProcess(String... command) {
LOG.info("Executing process command \"{}\"", Arrays.toString(command));
this.processBuilder.command(command);
try {
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/io/jtest/utils/common/ResourceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ public static String read(String filePath) throws IOException {
return readFromPath(filePath);
}

public static Properties readProps(String filePath) {
public static Properties readProps(String filePath) throws IOException {
Properties props = new Properties();
try {
props.load(new StringReader(read(filePath)));
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
props.load(new StringReader(read(filePath)));
return props;
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/java/io/jtest/utils/clients/jmx/JmxClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import org.junit.Ignore;
import org.junit.Test;

import javax.management.MalformedObjectNameException;

@Ignore
public class JmxClientTest {

@Test
public void testJmxConnection() {
public void testJmxConnection() throws MalformedObjectNameException {
JmxClient jmxClient = new JmxClient("service:jmx:rmi:///jndi/rmi://domnexfrontmwqa01.mw.server.lan:11193/server",
"control", "shu8cooS");
PollerMBean proxy = jmxClient.getMBeanProxy("MigraeneDomain:name=MigraeneDomainPollerMBean", PollerMBean.class);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/io/jtest/utils/common/ResourceReadTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public void testJsonReadingFromFile() throws IOException {
}

@Test
public void testPropertiesReadFromFile() {
public void testPropertiesReadFromFile() throws IOException {
assertEquals("some values with white spaces and new lines \n ",
ResourceUtils.readProps("foobar/foo.properties").get("IAmAProperty"));
}

@Test
public void testPropertiesReadFromFileWithoutExtension() {
public void testPropertiesReadFromFileWithoutExtension() throws IOException {
assertEquals("some values with white spaces and new lines \n ",
ResourceUtils.readProps("foobar/dir/foo/foo").get("IAmAProperty"));
}
Expand Down

0 comments on commit 465dc00

Please sign in to comment.