You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "JiriOndrusek (via GitHub)" <gi...@apache.org> on 2023/04/20 15:21:06 UTC

[GitHub] [camel-quarkus] JiriOndrusek opened a new pull request, #4813: Snmp: Extend test coverage #4797

JiriOndrusek opened a new pull request, #4813:
URL: https://github.com/apache/camel-quarkus/pull/4813

   fixes https://github.com/apache/camel-quarkus/issues/4797
   
   <!-- Uncomment and fill this section if your PR is not trivial
   [ ] An issue should be filed for the change unless this is a trivial change (fixing a typo or similar). One issue should ideally be fixed by not more than one commit and the other way round, each commit should fix just one issue, without pulling in other changes.
   [ ] Each commit in the pull request should have a meaningful and properly spelled subject line and body. Copying the title of the associated issue is typically enough. Please include the issue number in the commit message prefixed by #.
   [ ] The pull request description should explain what the pull request does, how, and why. If the info is available in the associated issue or some other external document, a link is enough.
   [ ] Phrases like Fix #<issueNumber> or Fixes #<issueNumber> will auto-close the named issue upon merging the pull request. Using them is typically a good idea.
   [ ] Please run mvn process-resources -Pformat (and amend the changes if necessary) before sending the pull request.
   [ ] Contributor guide is your good friend: https://camel.apache.org/camel-quarkus/latest/contributor-guide.html
   -->


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [camel-quarkus] JiriOndrusek commented on a diff in pull request #4813: Snmp: Extend test coverage #4797

Posted by "JiriOndrusek (via GitHub)" <gi...@apache.org>.
JiriOndrusek commented on code in PR #4813:
URL: https://github.com/apache/camel-quarkus/pull/4813#discussion_r1173362211


##########
integration-tests-jvm/snmp/src/test/java/org/apache/camel/quarkus/component/snmp/it/SnmpTest.java:
##########
@@ -16,19 +16,80 @@
  */
 package org.apache.camel.quarkus.component.snmp.it;
 
+import java.util.concurrent.TimeUnit;
+
+import io.quarkus.test.common.QuarkusTestResource;
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
+import org.hamcrest.Matchers;
 import org.junit.jupiter.api.Test;
 
+import static org.awaitility.Awaitility.await;
+
+/**
+ * There is a responder defined in the test resource. Which returns 2 responses without a delay and the third one
+ * with delay longer then default timeout. This means following behavior:
+ * - send PDU will receive 1 response
+ * - get_next will receive 2 responses (the third one reaches timeout)
+ * - poll returns unending stream of responses
+ */

Review Comment:
   @jamesnetherton here is a simple explanation, why different test cases asserts different values



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [camel-quarkus] JiriOndrusek commented on a diff in pull request #4813: Snmp: Extend test coverage #4797

Posted by "JiriOndrusek (via GitHub)" <gi...@apache.org>.
JiriOndrusek commented on code in PR #4813:
URL: https://github.com/apache/camel-quarkus/pull/4813#discussion_r1173360219


##########
integration-tests-jvm/snmp/src/test/java/org/apache/camel/quarkus/component/snmp/it/SnmpTest.java:
##########
@@ -16,19 +16,80 @@
  */
 package org.apache.camel.quarkus.component.snmp.it;
 
+import java.util.concurrent.TimeUnit;
+
+import io.quarkus.test.common.QuarkusTestResource;
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
+import org.hamcrest.Matchers;
 import org.junit.jupiter.api.Test;
 
+import static org.awaitility.Awaitility.await;
+
+/**
+ * There is a responder defined in the test resource. Which returns 2 responses without a delay and the third one
+ * with delay longer then default timeout. This means following behavior:
+ * - send PDU will receive 1 response
+ * - get_next will receive 2 responses (the third one reaches timeout)
+ * - poll returns unending stream of responses
+ */
 @QuarkusTest
+@QuarkusTestResource(SnmpTestResource.class)
 class SnmpTest {
 
     @Test
-    public void loadComponentSnmp() {
-        /* A simple autogenerated test */
-        RestAssured.get("/snmp/load/component/snmp")
+    public void testSendReceiveTrap() throws Exception {
+
+        RestAssured.given()
+                .body("TEXT")
+                .post("/snmp/produceTrap")
                 .then()
                 .statusCode(200);
+
+        await().atMost(10L, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> {
+            String result = RestAssured.given()
+                    .body("trap")
+                    .post("/snmp/results")
+                    .then()
+                    .statusCode(200)
+                    .extract().body().asString();
+
+            return result.contains("TEXT");
+        });
+    }
+
+    @Test
+    public void testPoll() throws Exception {
+        await().atMost(10L, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> {
+            String result = RestAssured.given()
+                    .body("poll")
+                    .post("/snmp/results")
+                    .then()
+                    .statusCode(200)
+                    .extract().body().asString();
+
+            return result.startsWith("Response from the test #1,Response from the test #2,Response from the test #3");
+        });
     }
 
+    @Test
+    public void testProducePDU() {
+
+        RestAssured
+                .get("/snmp/producePDU")
+                .then()
+                .statusCode(200)
+                .body(Matchers.equalTo("Response from the test #1"));
+    }
+
+    @Test
+    public void testGetNext() {
+
+        RestAssured.given()
+                .body("TEXT")
+                .post("/snmp/getNext")
+                .then()
+                .statusCode(200)
+                .body(Matchers.equalTo("Response from the test #1,Response from the test #2"));

Review Comment:
   No, tests uses different OID and the test resource returns for each of oids a freshly counted responses.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [camel-quarkus] jamesnetherton commented on a diff in pull request #4813: Snmp: Extend test coverage #4797

Posted by "jamesnetherton (via GitHub)" <gi...@apache.org>.
jamesnetherton commented on code in PR #4813:
URL: https://github.com/apache/camel-quarkus/pull/4813#discussion_r1173353813


##########
integration-tests-jvm/snmp/src/test/java/org/apache/camel/quarkus/component/snmp/it/SnmpTest.java:
##########
@@ -16,19 +16,80 @@
  */
 package org.apache.camel.quarkus.component.snmp.it;
 
+import java.util.concurrent.TimeUnit;
+
+import io.quarkus.test.common.QuarkusTestResource;
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
+import org.hamcrest.Matchers;
 import org.junit.jupiter.api.Test;
 
+import static org.awaitility.Awaitility.await;
+
+/**
+ * There is a responder defined in the test resource. Which returns 2 responses without a delay and the third one
+ * with delay longer then default timeout. This means following behavior:
+ * - send PDU will receive 1 response
+ * - get_next will receive 2 responses (the third one reaches timeout)
+ * - poll returns unending stream of responses
+ */
 @QuarkusTest
+@QuarkusTestResource(SnmpTestResource.class)
 class SnmpTest {
 
     @Test
-    public void loadComponentSnmp() {
-        /* A simple autogenerated test */
-        RestAssured.get("/snmp/load/component/snmp")
+    public void testSendReceiveTrap() throws Exception {
+
+        RestAssured.given()
+                .body("TEXT")
+                .post("/snmp/produceTrap")
                 .then()
                 .statusCode(200);
+
+        await().atMost(10L, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> {
+            String result = RestAssured.given()
+                    .body("trap")
+                    .post("/snmp/results")
+                    .then()
+                    .statusCode(200)
+                    .extract().body().asString();
+
+            return result.contains("TEXT");
+        });
+    }
+
+    @Test
+    public void testPoll() throws Exception {
+        await().atMost(10L, TimeUnit.SECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> {
+            String result = RestAssured.given()
+                    .body("poll")
+                    .post("/snmp/results")
+                    .then()
+                    .statusCode(200)
+                    .extract().body().asString();
+
+            return result.startsWith("Response from the test #1,Response from the test #2,Response from the test #3");
+        });
     }
 
+    @Test
+    public void testProducePDU() {
+
+        RestAssured
+                .get("/snmp/producePDU")
+                .then()
+                .statusCode(200)
+                .body(Matchers.equalTo("Response from the test #1"));
+    }
+
+    @Test
+    public void testGetNext() {
+
+        RestAssured.given()
+                .body("TEXT")
+                .post("/snmp/getNext")
+                .then()
+                .statusCode(200)
+                .body(Matchers.equalTo("Response from the test #1,Response from the test #2"));

Review Comment:
   Does this test depend on `testProducePDU` being executed first?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [camel-quarkus] jamesnetherton merged pull request #4813: Snmp: Extend test coverage #4797

Posted by "jamesnetherton (via GitHub)" <gi...@apache.org>.
jamesnetherton merged PR #4813:
URL: https://github.com/apache/camel-quarkus/pull/4813


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [camel-quarkus] JiriOndrusek commented on a diff in pull request #4813: Snmp: Extend test coverage #4797

Posted by "JiriOndrusek (via GitHub)" <gi...@apache.org>.
JiriOndrusek commented on code in PR #4813:
URL: https://github.com/apache/camel-quarkus/pull/4813#discussion_r1173360986


##########
integration-tests-jvm/snmp/src/test/java/org/apache/camel/quarkus/component/snmp/it/SnmpTestResource.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.quarkus.component.snmp.it;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Vector;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
+
+import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
+import org.apache.camel.util.CollectionHelper;
+import org.junit.jupiter.api.Assertions;
+import org.snmp4j.CommandResponder;
+import org.snmp4j.CommandResponderEvent;
+import org.snmp4j.MessageException;
+import org.snmp4j.PDU;
+import org.snmp4j.Snmp;
+import org.snmp4j.mp.SnmpConstants;
+import org.snmp4j.mp.StatusInformation;
+import org.snmp4j.smi.OID;
+import org.snmp4j.smi.OctetString;
+import org.snmp4j.smi.UdpAddress;
+import org.snmp4j.smi.VariableBinding;
+import org.snmp4j.transport.DefaultUdpTransportMapping;
+
+public class SnmpTestResource implements QuarkusTestResourceLifecycleManager {
+
+    public static final String LISTEN_ADDRESS = "snmpListenAddress";
+    public static final String LOCAL_ADDRESS = "127.0.0.1/0";
+
+    Snmp snmpResponder;
+
+    @Override
+    public Map<String, String> start() {
+        DefaultUdpTransportMapping udpTransportMapping;
+        try {
+            udpTransportMapping = new DefaultUdpTransportMapping(new UdpAddress(LOCAL_ADDRESS));
+            snmpResponder = new Snmp(udpTransportMapping);
+
+            TestCommandResponder responder = new TestCommandResponder(snmpResponder);
+            snmpResponder.addCommandResponder(responder);
+
+            snmpResponder.listen();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+        return CollectionHelper.mapOf(LISTEN_ADDRESS, udpTransportMapping.getListenAddress().toString().replaceFirst("/", ":"));
+    }
+
+    @Override
+    public void stop() {
+        if (snmpResponder != null) {
+            try {
+                snmpResponder.close();
+            } catch (IOException e) {
+                //do nothing
+            }
+        }
+    }
+
+    static class TestCommandResponder implements CommandResponder {
+
+        private final Snmp commandResponder;
+        private final Map<String, Integer> counts = new ConcurrentHashMap<>();
+
+        public TestCommandResponder(Snmp commandResponder) {
+            this.commandResponder = commandResponder;
+        }
+
+        @Override
+        public synchronized void processPdu(CommandResponderEvent event) {
+            PDU pdu = event.getPDU();
+            Vector<? extends VariableBinding> vbs = Optional.ofNullable(pdu.getVariableBindings()).orElse(new Vector<>(0));
+            String key = vbs.stream().sequential().map(vb -> vb.getOid().toString()).collect(Collectors.joining(","));

Review Comment:
   @jamesnetherton  there is a differencerfor  tests based on variable bindings



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org