Skip to content

Commit

Permalink
Issue #424: Add CLI options for specific query execution - SNMP
Browse files Browse the repository at this point in the history
* Refactored tests to make them more concise.
* Refactored executeQuery on SNMP Extension to make it more concise.
* Reformatted the code.
* Tested the client.
  • Loading branch information
CherfaElyes committed Dec 3, 2024
1 parent de3dae5 commit ec37eba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SnmpCliTest {
SnmpCli snmpCli;
CommandLine commandLine;

static String SNMP_OID = "1.3.6.1.4.1.674.10892.5.5.1.20.130.4";
static String SNMP_OID = "1.3.6.1.4.1.64.58.5.5.1.20.10.4";
static String SNMP_VERSION = "v2c";
static String SNMP_COMMUNITY = "public";

Expand All @@ -24,48 +24,10 @@ void initCli() {
commandLine = new CommandLine(snmpCli);
}

void initSnmpGet() {
initCli();

commandLine.execute(
"hostname",
"--snmp-get",
SNMP_OID,
"--snmp",
SNMP_VERSION,
"--community",
SNMP_COMMUNITY,
"--retry",
"1000",
"--retry",
"6000"
);
}

void initSnmpGetNext() {
initCli();

void execute(String snmpMethod) {
commandLine.execute(
"hostname",
"--snmp-getnext",
SNMP_OID,
"--snmp",
SNMP_VERSION,
"--community",
SNMP_COMMUNITY,
"--retry",
"1000",
"--retry",
"6000"
);
}

void initSnmpWalk() {
initCli();

commandLine.execute(
"hostname",
"--snmp-walk",
snmpMethod,
SNMP_OID,
"--snmp",
SNMP_VERSION,
Expand All @@ -80,27 +42,29 @@ void initSnmpWalk() {

@Test
void testExecute() {
initSnmpGet();
initCli();
execute("--snmp-get");
assertEquals(SNMP_OID, snmpCli.get);
initSnmpGetNext();
execute("--snmp-getnext");
assertEquals(SNMP_OID, snmpCli.getNext);
initSnmpWalk();
execute("--snmp-walk");
assertEquals(SNMP_OID, snmpCli.walk);
}

@Test
void testGetQuery() {
initSnmpGet();
initCli();
execute("--snmp-get");
JsonNode snmpQuery = snmpCli.getQuery();
assertEquals("get", snmpQuery.get("action").asText());
assertEquals(SNMP_OID, snmpQuery.get("oid").asText());

initSnmpGetNext();
execute("--snmp-getnext");
snmpQuery = snmpCli.getQuery();
assertEquals("getNext", snmpQuery.get("action").asText());
assertEquals(SNMP_OID, snmpQuery.get("oid").asText());

initSnmpWalk();
execute("--snmp-walk");
snmpQuery = snmpCli.getQuery();
assertEquals("walk", snmpQuery.get("action").asText());
assertEquals(SNMP_OID, snmpQuery.get("oid").asText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,55 +109,34 @@ public String executeQuery(IConfiguration configuration, JsonNode query, PrintWr
final String hostname = configuration.getHostname();
String result = "Failed Executing SNMP query";
final String action = query.get("action").asText();

final String oId = query.get("oid").asText();
final String exceptionMessage = "Hostname {} - Error while executing SNMP {} query. Message: {}";

printWriter.println("Executing query from SNMP Extension");
// Printing the SNMP request
printWriter.println(String.format("Hostname %s - Executing SNMP %s query:", hostname, action));
printWriter.println(String.format("OID: %s", oId));
printWriter.flush();

switch (action) {
case GET:
try {
displayQuery(printWriter, hostname, GET, oId);
result = snmpRequestExecutor.executeSNMPGet(oId, snmpConfiguration, hostname, false);
displayQueryResult(printWriter, result);
} catch (Exception e) {
log.debug(exceptionMessage, hostname, GET, e);
}
break;
case GET_NEXT:
try {
displayQuery(printWriter, hostname, GET_NEXT, oId);
result = snmpRequestExecutor.executeSNMPGetNext(oId, snmpConfiguration, hostname, false);
displayQueryResult(printWriter, result);
} catch (Exception e) {
log.debug(exceptionMessage, hostname, GET_NEXT, e);
}
break;
case WALK:
try {
displayQuery(printWriter, hostname, WALK, oId);
result = snmpRequestExecutor.executeSNMPWalk(oId, snmpConfiguration, hostname, false);
displayQueryResult(printWriter, result);
} catch (Exception e) {
log.debug(exceptionMessage, hostname, WALK, e);
}
break;
default:
throw new IllegalArgumentException("Hostname {} - Invalid SNMP Operation");
}
return result;
}
try {
if (action.equals(GET)) {
result = snmpRequestExecutor.executeSNMPGet(oId, snmpConfiguration, hostname, false);
} else if (action.equals(GET_NEXT)) {
result = snmpRequestExecutor.executeSNMPGetNext(oId, snmpConfiguration, hostname, false);
} else if (action.equals(WALK)) {
result = snmpRequestExecutor.executeSNMPWalk(oId, snmpConfiguration, hostname, false);
} else {
throw new IllegalArgumentException(String.format("Hostname %s - Invalid SNMP Operation", hostname));
}

public void displayQuery(final PrintWriter printWriter, final String hostname, final String query, final String oId) {
printWriter.println(String.format("Hostname %s - Executing SNMP %s query:\n", hostname, query));
printWriter.println(String.format("OID: %s", oId));
printWriter.flush();
}
// Printing the result on the CLI
printWriter.print("\n\u001B[34m");
printWriter.print("Result: ");
printWriter.print("\u001B[0m");
printWriter.print(result);
printWriter.flush();
} catch (Exception e) {
log.debug("Hostname {} - Error while executing SNMP {} query. Message: {}", hostname, action, e);
}

public void displayQueryResult(final PrintWriter printWriter, final String result) {
printWriter.println(String.format("Result: %s", result));
printWriter.flush();
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
Expand Down Expand Up @@ -673,9 +672,9 @@ void testThrowsExceptionQuery() throws Exception {
snmpQueryConfiguration.set("action", new TextNode(""));
snmpQueryConfiguration.set("oid", new TextNode(SnmpExtension.SNMP_OID));

assertThrows(
Exception.class,
() -> snmpExtension.executeQuery(snmpConfiguration, snmpQueryConfiguration, new PrintWriter(new StringWriter()))
assertEquals(
"Failed Executing SNMP query",
snmpExtension.executeQuery(snmpConfiguration, snmpQueryConfiguration, new PrintWriter(new StringWriter()))
);
}
}

0 comments on commit ec37eba

Please sign in to comment.