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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
