Skip to content

Commit

Permalink
Issue #424: Add CLI options for specific query execution
Browse files Browse the repository at this point in the history
* Reformatted the code.
  • Loading branch information
CherfaElyes committed Dec 2, 2024
1 parent dcb4b64 commit de3dae5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;

import lombok.Data;

import java.io.PrintWriter;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.stream.Stream;
import lombok.Data;
import org.fusesource.jansi.AnsiConsole;
import org.sentrysoftware.metricshub.cli.service.CliExtensionManager;
import org.sentrysoftware.metricshub.cli.service.PrintExceptionMessageHandlerService;
Expand Down Expand Up @@ -100,18 +98,21 @@ void validate() throws ParameterException {
throw new ParameterException(spec.commandLine(), "SNMP protocol must be configured: --snmp.");
}

Stream.of(get, getNext, walk)
Stream
.of(get, getNext, walk)
.filter(Objects::nonNull)
.reduce((a, b) -> {
throw new ParameterException(
spec.commandLine(),
"Only one SNMP query can be specified at a time: --snmp-get, --snmp-getnext, --snmp-walk."
);
})
.orElseThrow(() -> new ParameterException(
spec.commandLine(),
"At least one SNMP query must be specified: --snmp-get, --snmp-getnext, --snmp-walk."
));
.orElseThrow(() ->
new ParameterException(
spec.commandLine(),
"At least one SNMP query must be specified: --snmp-get, --snmp-getnext, --snmp-walk."
)
);
}

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.api.Test;
import org.sentrysoftware.metricshub.cli.service.protocol.SnmpConfigCli;

import com.fasterxml.jackson.databind.JsonNode;
import picocli.CommandLine;
import picocli.CommandLine.ParameterException;

Expand Down Expand Up @@ -78,6 +77,7 @@ void initSnmpWalk() {
"6000"
);
}

@Test
void testExecute() {
initSnmpGet();
Expand Down Expand Up @@ -113,11 +113,17 @@ void testValidate() {
assertEquals("SNMP protocol must be configured: --snmp.", snmpConfigException.getMessage());
snmpCli.setSnmpConfigCli(new SnmpConfigCli());
ParameterException noQueriesException = assertThrows(ParameterException.class, () -> snmpCli.validate());
assertEquals("At least one SNMP query must be specified: --snmp-get, --snmp-getnext, --snmp-walk.", noQueriesException.getMessage());
assertEquals(
"At least one SNMP query must be specified: --snmp-get, --snmp-getnext, --snmp-walk.",
noQueriesException.getMessage()
);
snmpCli.setGet(SNMP_OID);
assertDoesNotThrow(() -> snmpCli.validate());
snmpCli.setGetNext(SNMP_OID);
ParameterException manyQueriesException = assertThrows(ParameterException.class, () -> snmpCli.validate());
assertEquals("Only one SNMP query can be specified at a time: --snmp-get, --snmp-getnext, --snmp-walk.", manyQueriesException.getMessage());
assertEquals(
"Only one SNMP query can be specified at a time: --snmp-get, --snmp-getnext, --snmp-walk.",
manyQueriesException.getMessage()
);
}
}

0 comments on commit de3dae5

Please sign in to comment.