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 a793184d1a Snmp: Extend coverage of some smaller features #4850 a793184d1a is described below commit a793184d1a419bd0cfdf451b1c95c735fc41751c Author: JiriOndrusek <ondrusek.j...@gmail.com> AuthorDate: Fri Apr 28 11:46:45 2023 +0200 Snmp: Extend coverage of some smaller features #4850 --- .../quarkus/component/snmp/it/SnmpResource.java | 14 +++--- .../camel/quarkus/component/snmp/it/SnmpRoute.java | 46 +++++++++++++---- .../camel/quarkus/component/snmp/it/SnmpTest.java | 57 +++++++++++++++++++--- .../component/snmp/it/SnmpTestResource.java | 35 +++++++------ 4 files changed, 113 insertions(+), 39 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 bb32f3cf73..eee1ae9cd2 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 @@ -47,6 +47,8 @@ import org.snmp4j.smi.VariableBinding; @ApplicationScoped public class SnmpResource { + public static OID TRAP_OID = new OID("1.2.3.4.5"); + @ConfigProperty(name = SnmpRoute.TRAP_V0_PORT) int trap0Port; @@ -108,12 +110,11 @@ public class SnmpResource { return Response.ok().build(); } - @Path("/results") + @Path("/results/{from}") @POST @Produces(MediaType.TEXT_PLAIN) - public Response results(String from) throws Exception { - OID oid = from.startsWith("trap") ? new OID("1.2.3.4.5") : SnmpConstants.sysDescr; - String result = snmpResults.get(from).stream().map(m -> m.getSnmpMessage().getVariable(oid).toString()) + public Response results(@PathParam("from") String from, String oid) throws Exception { + String result = snmpResults.get(from).stream().map(m -> m.getSnmpMessage().getVariable(new OID(oid)).toString()) .collect(Collectors.joining(",")); return Response.ok(result).build(); @@ -128,18 +129,17 @@ public class SnmpResource { trap0.setGenericTrap(PDUv1.ENTERPRISE_SPECIFIC); trap0.setSpecificTrap(1); - trap0.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid)); + 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); - //Add Payload trap0.add(new VariableBinding(oid, var)); return trap0; case 1: PDU trap1 = new PDU(); - trap1.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid)); + 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"))); 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 134ecf6be4..ea4db233a5 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 @@ -28,6 +28,7 @@ import jakarta.inject.Singleton; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.snmp.SnmpMessage; import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.snmp4j.mp.SnmpConstants; @ApplicationScoped public class SnmpRoute extends RouteBuilder { @@ -52,18 +53,38 @@ public class SnmpRoute extends RouteBuilder { public void configure() { //TRAP consumer snmpVersion=0 from("snmp:0.0.0.0:" + trap0Port + "?protocol=udp&type=TRAP&snmpVersion=0") - .process(e -> snmpResults.get("trap0").add(e.getIn().getBody(SnmpMessage.class))); + .process(e -> snmpResults.get("v0_trap").add(e.getIn().getBody(SnmpMessage.class))); //TRAP consumer snmpVersion=1 from("snmp:0.0.0.0:" + trap1Port + "?protocol=udp&type=TRAP&snmpVersion=1") - .process(e -> snmpResults.get("trap1").add(e.getIn().getBody(SnmpMessage.class))); + .process(e -> snmpResults.get("v1_trap").add(e.getIn().getBody(SnmpMessage.class))); + + //POLL consumer 2 oidps, snmpVersion=0 + from("snmp://" + snmpListenAddress + "?protocol=udp&snmpVersion=0&type=POLL&oids=" + + SnmpConstants.sysName + "," + SnmpConstants.sysContact) + .process(e -> snmpResults.get("v0_poll2oids").add(e.getIn().getBody(SnmpMessage.class))); + //POLL consumer 2 oidps, snmpVersion=1 + from("snmp://" + snmpListenAddress + "?protocol=udp&snmpVersion=1&type=POLL&oids=" + + SnmpConstants.sysName + "," + SnmpConstants.sysContact) + .process(e -> snmpResults.get("v1_poll2oids").add(e.getIn().getBody(SnmpMessage.class))); + + //POLL consumer starting with dot snmpVersion=0 + from("snmp://" + snmpListenAddress + + "?protocol=udp&snmpVersion=0&type=POLL&oids=.1.3.6.1.4.1.6527.3.1.2.21.2.1.50") + .process(e -> snmpResults.get("v0_pollStartingDot").add(e.getIn().getBody(SnmpMessage.class))); + //POLL consumer startingWith dot snmpVersion=1 + from("snmp://" + snmpListenAddress + + "?protocol=udp&snmpVersion=1&type=POLL&oids=.1.3.6.1.4.1.6527.3.1.2.21.2.1.50") + .process(e -> snmpResults.get("v1_pollStartingDot").add(e.getIn().getBody(SnmpMessage.class))); //POLL consumer snmpVersion=0 - from("snmp://" + snmpListenAddress + "?protocol=udp&snmpVersion=0&securityName=aaa&type=POLL&oids=1.3.6.1.2.1.1.5.0") - .process(e -> snmpResults.get("poll0").add(e.getIn().getBody(SnmpMessage.class))); + from("snmp://" + snmpListenAddress + "?protocol=udp&snmpVersion=0&type=POLL&oids=" + + SnmpConstants.sysName) + .process(e -> snmpResults.get("v0_poll").add(e.getIn().getBody(SnmpMessage.class))); //POLL consumer snmpVersion=1 - from("snmp://" + snmpListenAddress + "?protocol=udp&snmpVersion=1&securityName=aaa&type=POLL&oids=1.3.6.1.2.1.1.5.0") - .process(e -> snmpResults.get("poll1").add(e.getIn().getBody(SnmpMessage.class))); + from("snmp://" + snmpListenAddress + "?protocol=udp&snmpVersion=1&type=POLL&oids=" + + SnmpConstants.sysName) + .process(e -> snmpResults.get("v1_poll").add(e.getIn().getBody(SnmpMessage.class))); } static class Producers { @@ -72,10 +93,15 @@ public class SnmpRoute extends RouteBuilder { @Named("snmpTrapResults") Map<String, Deque<SnmpMessage>> snmpResults() { Map<String, Deque<SnmpMessage>> map = new ConcurrentHashMap<>(); - map.put("trap0", new ConcurrentLinkedDeque()); - map.put("trap1", new ConcurrentLinkedDeque()); - map.put("poll0", new ConcurrentLinkedDeque()); - map.put("poll1", new ConcurrentLinkedDeque()); + map.put("v0_trap", new ConcurrentLinkedDeque()); + map.put("v1_trap", new ConcurrentLinkedDeque()); + map.put("v0_poll", new ConcurrentLinkedDeque()); + map.put("v1_poll", new ConcurrentLinkedDeque()); + map.put("v3_poll", new ConcurrentLinkedDeque()); + map.put("v0_pollStartingDot", new ConcurrentLinkedDeque()); + map.put("v1_pollStartingDot", new ConcurrentLinkedDeque()); + map.put("v0_poll2oids", new ConcurrentLinkedDeque()); + map.put("v1_poll2oids", 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 200fe0e948..47b19ecca1 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 @@ -25,6 +25,7 @@ import io.restassured.RestAssured; import org.hamcrest.Matchers; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; +import org.snmp4j.mp.SnmpConstants; import static org.awaitility.Awaitility.await; @@ -46,6 +47,7 @@ class SnmpTest { @ParameterizedTest @MethodSource("supportedVersions") public void testSendReceiveTrap(int version) throws Exception { + String resultsName = "v" + version + "_trap"; RestAssured.given() .body("TEXT") @@ -55,8 +57,8 @@ class SnmpTest { await().atMost(10L, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> { String result = RestAssured.given() - .body("trap" + version) - .post("/snmp/results") + .body(SnmpConstants.snmpTrapOID.toString()) + .post("/snmp/results/" + resultsName) .then() .statusCode(200) .extract().body().asString(); @@ -68,10 +70,12 @@ class SnmpTest { @ParameterizedTest @MethodSource("supportedVersions") public void testPoll(int version) throws Exception { - await().atMost(10L, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> { + String resultsName = "v" + version + "_poll"; + + await().atMost(20L, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> { String result = RestAssured.given() - .body("poll" + version) - .post("/snmp/results") + .body(SnmpConstants.sysName.toString()) + .post("/snmp/results/" + resultsName) .then() .statusCode(200) .extract().body().asString(); @@ -82,8 +86,24 @@ class SnmpTest { @ParameterizedTest @MethodSource("supportedVersions") - public void testProducePDU(int version) { + public void testPollStartingDot(int version) throws Exception { + String resultsName = "v" + version + "_pollStartingDot"; + + await().atMost(20L, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> { + String result = RestAssured.given() + .body("1.3.6.1.4.1.6527.3.1.2.21.2.1.50") + .post("/snmp/results/" + resultsName) + .then() + .statusCode(200) + .extract().body().asString(); + return result.startsWith("Response from the test #1,Response from the test #2,Response from the test #3"); + }); + } + + @ParameterizedTest + @MethodSource("supportedVersions") + public void testProducePDU(int version) { RestAssured .get("/snmp/producePDU/" + version) .then() @@ -102,4 +122,29 @@ class SnmpTest { .statusCode(200) .body(Matchers.equalTo("Response from the test #1,Response from the test #2")); } + + @ParameterizedTest + @MethodSource("supportedVersions") + public void testPollWith2OIDs(int version) throws Exception { + String resultsName = "v" + version + "_poll2oids"; + + await().atMost(20L, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> { + String resultOid1 = RestAssured.given() + .body(SnmpConstants.sysName.toString()) + .post("/snmp/results/" + resultsName) + .then() + .statusCode(200) + .extract().body().asString(); + String resultOid2 = RestAssured.given() + .body(SnmpConstants.sysContact.toString()) + .post("/snmp/results/" + resultsName) + .then() + .statusCode(200) + .extract().body().asString(); + + return resultOid1.startsWith("Response from the test #1,Response from the test #2,Response from the test #3") && + resultOid2.startsWith("Response from the test #1,Response from the test #2,Response from the test #3"); + }); + + } } 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 9653a3dfb4..59e87e38a5 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 @@ -120,29 +120,32 @@ public class SnmpTestResource implements QuarkusTestResourceLifecycleManager { } } - PDU response = makeResponse(++numberOfSent, SnmpConstants.version1); - if (response != null) { - try { - response.setRequestID(pdu.getRequestID()); - commandResponder.getMessageDispatcher().returnResponsePdu( - event.getMessageProcessingModel(), event.getSecurityModel(), - event.getSecurityName(), event.getSecurityLevel(), - response, event.getMaxSizeResponsePDU(), - event.getStateReference(), new StatusInformation()); - } catch (MessageException e) { - Assertions.assertNull(e); - } - counts.put(key, numberOfSent); + try { + PDU response = makeResponse(++numberOfSent, SnmpConstants.version1, vbs); + response.setRequestID(pdu.getRequestID()); + commandResponder.getMessageDispatcher().returnResponsePdu( + event.getMessageProcessingModel(), event.getSecurityModel(), + event.getSecurityName(), event.getSecurityLevel(), + response, event.getMaxSizeResponsePDU(), + event.getStateReference(), new StatusInformation()); + } catch (MessageException e) { + Assertions.assertNull(e); } + counts.put(key, numberOfSent); } - private PDU makeResponse(int counter, int version) { + private PDU makeResponse(int counter, int version, Vector<? extends VariableBinding> vbs) { PDU responsePDU = new PDU(); responsePDU.setType(PDU.RESPONSE); responsePDU.setErrorStatus(PDU.noError); responsePDU.setErrorIndex(0); - responsePDU.add(new VariableBinding(new OID(SnmpConstants.sysDescr), - new OctetString("Response from the test #" + counter))); + if (vbs.isEmpty()) { + responsePDU.add(new VariableBinding(new OID(SnmpConstants.sysDescr), + new OctetString("Response from the test #" + counter))); + } else { + vbs.stream().forEach(vb -> responsePDU.add(new VariableBinding(vb.getOid(), + new OctetString("Response from the test #" + counter)))); + } return responsePDU; } }