You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2023/07/27 06:15:47 UTC
[camel-quarkus] branch main updated: SNMPv3 test coverage
This is an automated email from the ASF dual-hosted git repository.
jamesnetherton pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
The following commit(s) were added to refs/heads/main by this push:
new 86f75e1a1d SNMPv3 test coverage
86f75e1a1d is described below
commit 86f75e1a1d96208939f20de5122a2927c8bcc23b
Author: JiriOndrusek <on...@gmail.com>
AuthorDate: Wed Jul 26 13:43:16 2023 +0200
SNMPv3 test coverage
---
.../quarkus/component/snmp/it/SnmpResource.java | 69 +++++++++++++---------
.../camel/quarkus/component/snmp/it/SnmpRoute.java | 9 +++
.../camel/quarkus/component/snmp/it/SnmpTest.java | 24 ++++----
.../component/snmp/it/SnmpTestResource.java | 2 +-
4 files changed, 63 insertions(+), 41 deletions(-)
diff --git a/integration-tests-jvm/snmp/src/main/java/org/apache/camel/quarkus/component/snmp/it/SnmpResource.java b/integration-tests-jvm/snmp/src/main/java/org/apache/camel/quarkus/component/snmp/it/SnmpResource.java
index 3a0475e6c7..cc9958615b 100644
--- a/integration-tests-jvm/snmp/src/main/java/org/apache/camel/quarkus/component/snmp/it/SnmpResource.java
+++ b/integration-tests-jvm/snmp/src/main/java/org/apache/camel/quarkus/component/snmp/it/SnmpResource.java
@@ -38,6 +38,7 @@ import org.apache.camel.component.snmp.SnmpMessage;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.snmp4j.PDU;
import org.snmp4j.PDUv1;
+import org.snmp4j.ScopedPDU;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
@@ -55,6 +56,9 @@ public class SnmpResource {
@ConfigProperty(name = SnmpRoute.TRAP_V1_PORT)
int trap1Port;
+ @ConfigProperty(name = SnmpRoute.TRAP_V3_PORT)
+ int trap3Port;
+
@ConfigProperty(name = "snmpListenAddress")
String snmpListenAddress;
@@ -71,8 +75,14 @@ public class SnmpResource {
@Path("/producePDU/{version}")
@POST
@Produces(MediaType.TEXT_PLAIN)
- public Response producePDU(@PathParam("version") int version, String payload) {
+ public Response producePDU(@PathParam("version") int version,
+ @QueryParam("urlAppend") String urlAppend,
+ String payload) {
String url = String.format("snmp://%s?retries=1&snmpVersion=%d", snmpListenAddress, version);
+ if (urlAppend != null) {
+ url = url + urlAppend;
+ }
+
SnmpMessage pdu = producerTemplate.requestBody(url, version, SnmpMessage.class);
String response = pdu.getSnmpMessage().getVariableBindings().stream()
@@ -85,9 +95,15 @@ public class SnmpResource {
@Path("/getNext/{version}")
@POST
@Produces(MediaType.TEXT_PLAIN)
- public Response getNext(String payload, @PathParam("version") int version) {
+ public Response getNext(String payload,
+ @QueryParam("urlAppend") String urlAppend,
+ @PathParam("version") int version) {
String url = String.format("snmp://%s?type=GET_NEXT&retries=1&protocol=udp&oids=%s&snmpVersion=%d", snmpListenAddress,
payload, version);
+ if (urlAppend != null) {
+ url = url + urlAppend;
+ }
+
@SuppressWarnings("unchecked")
List<SnmpMessage> pdu = producerTemplate.requestBody(url, "", List.class);
@@ -105,9 +121,14 @@ public class SnmpResource {
@Path("/produceTrap/{version}")
@POST
@Produces(MediaType.TEXT_PLAIN)
- public Response produceTrap(String payload, @PathParam("version") int version) {
- int port = new int[] { trap0Port, trap1Port, -1, -1 }[version];
+ public Response produceTrap(String payload,
+ @QueryParam("urlAppend") String urlAppend,
+ @PathParam("version") int version) {
+ int port = new int[] { trap0Port, trap1Port, -1, trap3Port }[version];
String url = "snmp:127.0.0.1:" + port + "?protocol=udp&type=TRAP&snmpVersion=" + version;
+ if (urlAppend != null) {
+ url = url + urlAppend;
+ }
PDU trap = createTrap(payload, version);
producerTemplate.sendBody(url, trap);
@@ -119,15 +140,11 @@ public class SnmpResource {
@POST
@Produces(MediaType.TEXT_PLAIN)
public Response poll(@PathParam("version") int version,
- @QueryParam("user") String user,
- @QueryParam("securityLevel") String securityLevel,
+ @QueryParam("urlAppend") String urlAppend,
String oid) {
String url = String.format("snmp:%s?protocol=udp&snmpVersion=%d&type=POLL&oids=%s", snmpListenAddress, version, oid);
- if (user != null) {
- url = url + "&securityName=" + user;
- }
- if (securityLevel != null) {
- url = url + "&securityLevel=" + securityLevel;
+ if (urlAppend != null) {
+ url = url + urlAppend;
}
//Even if routeBuilder is preferred instead of consumerTemplate, consumerTemplete can be used in a case, when the component uses polling consumers by default.
@@ -157,31 +174,29 @@ public class SnmpResource {
public PDU createTrap(String payload, int version) {
OID oid = new OID("1.2.3.4.5");
Variable var = new OctetString(payload);
+ PDU pdu;
switch (version) {
case 0:
PDUv1 trap0 = new PDUv1();
trap0.setGenericTrap(PDUv1.ENTERPRISE_SPECIFIC);
trap0.setSpecificTrap(1);
-
- trap0.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OctetString(payload)));
- trap0.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000))); // put your uptime here
- trap0.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));
trap0.setEnterprise(oid);
- trap0.add(new VariableBinding(oid, var));
- return trap0;
+ pdu = trap0;
+ break;
case 1:
- PDU trap1 = new PDU();
-
- trap1.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OctetString(payload)));
- trap1.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000))); // put your uptime here
- trap1.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));
-
- //Add Payload
- trap1.add(new VariableBinding(oid, var));
- return trap1;
+ pdu = new PDU();
+ break;
default:
- return null;
+ pdu = new ScopedPDU();
+ break;
}
+
+ pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OctetString(payload)));
+ pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000))); // put your uptime here
+ pdu.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));
+
+ pdu.add(new VariableBinding(oid, var));
+ return pdu;
}
}
diff --git a/integration-tests-jvm/snmp/src/main/java/org/apache/camel/quarkus/component/snmp/it/SnmpRoute.java b/integration-tests-jvm/snmp/src/main/java/org/apache/camel/quarkus/component/snmp/it/SnmpRoute.java
index 8fe4149ece..1805ce8d43 100644
--- a/integration-tests-jvm/snmp/src/main/java/org/apache/camel/quarkus/component/snmp/it/SnmpRoute.java
+++ b/integration-tests-jvm/snmp/src/main/java/org/apache/camel/quarkus/component/snmp/it/SnmpRoute.java
@@ -34,6 +34,7 @@ public class SnmpRoute extends RouteBuilder {
public static final String TRAP_V0_PORT = "SnmpRoute_trap_v0";
public static final String TRAP_V1_PORT = "SnmpRoute_trap_v1";
+ public static final String TRAP_V3_PORT = "SnmpRoute_trap_v3";
@ConfigProperty(name = TRAP_V0_PORT)
int trap0Port;
@@ -41,6 +42,9 @@ public class SnmpRoute extends RouteBuilder {
@ConfigProperty(name = TRAP_V1_PORT)
int trap1Port;
+ @ConfigProperty(name = TRAP_V3_PORT)
+ int trap3Port;
+
@Inject
@Named("snmpTrapResults")
Map<String, Deque<SnmpMessage>> snmpResults;
@@ -54,6 +58,10 @@ public class SnmpRoute extends RouteBuilder {
//TRAP consumer snmpVersion=1
from("snmp:0.0.0.0:" + trap1Port + "?protocol=udp&type=TRAP&snmpVersion=1")
.process(e -> snmpResults.get("v1_trap").add(e.getIn().getBody(SnmpMessage.class)));
+
+ //TRAP consumer snmpVersion=3
+ from("snmp:0.0.0.0:" + trap3Port + "?protocol=udp&type=TRAP&snmpVersion=3")
+ .process(e -> snmpResults.get("v3_trap").add(e.getIn().getBody(SnmpMessage.class)));
}
static class Producers {
@@ -64,6 +72,7 @@ public class SnmpRoute extends RouteBuilder {
Map<String, Deque<SnmpMessage>> map = new ConcurrentHashMap<>();
map.put("v0_trap", new ConcurrentLinkedDeque<>());
map.put("v1_trap", new ConcurrentLinkedDeque<>());
+ map.put("v3_trap", new ConcurrentLinkedDeque<>());
return map;
}
}
diff --git a/integration-tests-jvm/snmp/src/test/java/org/apache/camel/quarkus/component/snmp/it/SnmpTest.java b/integration-tests-jvm/snmp/src/test/java/org/apache/camel/quarkus/component/snmp/it/SnmpTest.java
index 1220e8aa44..48dd9d1972 100644
--- a/integration-tests-jvm/snmp/src/test/java/org/apache/camel/quarkus/component/snmp/it/SnmpTest.java
+++ b/integration-tests-jvm/snmp/src/test/java/org/apache/camel/quarkus/component/snmp/it/SnmpTest.java
@@ -22,11 +22,9 @@ import java.util.stream.Stream;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
-import io.restassured.specification.RequestSpecification;
import org.hamcrest.Matchers;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
-import org.junit.jupiter.params.provider.ValueSource;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
@@ -51,7 +49,7 @@ class SnmpTest {
public static final OID DOT_OID = new OID(new int[] { 1, 3, 6, 1, 4, 1, 6527, 3, 1, 2, 21, 2, 1, 50 });
static Stream<Integer> supportedVersions() {
- return Stream.of(0, 1/*, 3 not supported because of https://issues.apache.org/jira/browse/CAMEL-19298 */);
+ return Stream.of(0, 1, 3);
}
@ParameterizedTest
@@ -61,6 +59,7 @@ class SnmpTest {
RestAssured.given()
.body("TEXT")
+ .queryParam("urlAppend", version == 3 ? "&securityName=test&securityLevel=1" : null)
.post("/snmp/produceTrap/" + version)
.then()
.statusCode(200);
@@ -78,17 +77,12 @@ class SnmpTest {
}
@ParameterizedTest
- @ValueSource(ints = { 0, 1, 3 })
+ @MethodSource("supportedVersions")
public void testPoll(int version) throws Exception {
- RequestSpecification rs = RestAssured.given()
- .body(POLL_OID.toString());
-
- if (version == 3) {
- rs.queryParam("user", "test")
- .queryParam("securityLevel", 1);
- }
-
- rs.post("/snmp/poll/" + version)
+ RestAssured.given()
+ .body(POLL_OID.toString())
+ .queryParam("urlAppend", version == 3 ? "&securityName=test&securityLevel=1" : null)
+ .post("/snmp/poll/" + version)
.then()
.statusCode(200)
.body(Matchers.equalTo("My POLL Printer - response #1"));
@@ -99,6 +93,7 @@ class SnmpTest {
public void testPollWith2OIDs(int version) throws Exception {
RestAssured.given()
.body(TWO_OIDS_A + "," + TWO_OIDS_B)
+ .queryParam("urlAppend", version == 3 ? "&securityName=test&securityLevel=1" : null)
.post("/snmp/poll/" + version)
.then()
.statusCode(200)
@@ -112,6 +107,7 @@ class SnmpTest {
public void testPollStartingDot(int version) throws Exception {
RestAssured.given()
.body("." + DOT_OID)
+ .queryParam("urlAppend", version == 3 ? "&securityName=test&securityLevel=1" : null)
.post("/snmp/poll/" + version)
.then()
.statusCode(200)
@@ -123,6 +119,7 @@ class SnmpTest {
public void testProducePDU(int version) {
RestAssured.given()
.body(PRODUCE_PDU_OID.toString())
+ .queryParam("urlAppend", version == 3 ? "&securityName=test&securityLevel=1" : null)
.post("/snmp/producePDU/" + version)
.then()
.statusCode(200)
@@ -135,6 +132,7 @@ class SnmpTest {
RestAssured.given()
.body(GET_NEXT_OID.toString())
+ .queryParam("urlAppend", version == 3 ? "&securityName=test&securityLevel=1" : null)
.post("/snmp/getNext/" + version)
.then()
.statusCode(200)
diff --git a/integration-tests-jvm/snmp/src/test/java/org/apache/camel/quarkus/component/snmp/it/SnmpTestResource.java b/integration-tests-jvm/snmp/src/test/java/org/apache/camel/quarkus/component/snmp/it/SnmpTestResource.java
index c487d4acdc..9f8d962376 100644
--- a/integration-tests-jvm/snmp/src/test/java/org/apache/camel/quarkus/component/snmp/it/SnmpTestResource.java
+++ b/integration-tests-jvm/snmp/src/test/java/org/apache/camel/quarkus/component/snmp/it/SnmpTestResource.java
@@ -88,7 +88,7 @@ public class SnmpTestResource implements QuarkusTestResourceLifecycleManager {
}
Map<String, String> ports = AvailablePortFinder.reserveNetworkPorts(Objects::toString, SnmpRoute.TRAP_V0_PORT,
- SnmpRoute.TRAP_V1_PORT);
+ SnmpRoute.TRAP_V1_PORT, SnmpRoute.TRAP_V3_PORT);
Map<String, String> m = CollectionHelper.mergeMaps(
ports,
CollectionHelper.mapOf(LISTEN_ADDRESS,