This is an automated email from the ASF dual-hosted git repository.
tpalfy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/main by this push:
new 328d38facb NIFI-10194: Simplified SendTrapSNMP error handling
328d38facb is described below
commit 328d38facb25936f8050cb4364757b7994ed3bde
Author: Lehel <[email protected]>
AuthorDate: Tue Jul 5 11:27:02 2022 +0200
NIFI-10194: Simplified SendTrapSNMP error handling
This closes #6178.
Signed-off-by: Tamas Palfy <[email protected]>
---
.../nifi/snmp/operations/SendTrapSNMPHandler.java | 10 +------
.../snmp/operations/SendTrapSNMPHandlerTest.java | 34 ----------------------
2 files changed, 1 insertion(+), 43 deletions(-)
diff --git
a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/operations/SendTrapSNMPHandler.java
b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/operations/SendTrapSNMPHandler.java
index 6b2a3bfc2a..f9f608d670 100644
---
a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/operations/SendTrapSNMPHandler.java
+++
b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/operations/SendTrapSNMPHandler.java
@@ -25,8 +25,6 @@ import org.apache.nifi.snmp.utils.SNMPUtils;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
-import org.snmp4j.event.ResponseEvent;
-import org.snmp4j.smi.TransportIpAddress;
import java.io.IOException;
import java.time.Instant;
@@ -64,13 +62,7 @@ public class SendTrapSNMPHandler {
logger.debug("No optional SNMP specific variables found in
flowfile.");
}
- final ResponseEvent response = snmpManager.send(pdu, target);
- if (response == null) {
- final int port = ((TransportIpAddress)
target.getAddress()).getPort();
- throw new IOException(String.format("The sent PDU has not been
confirmed, the target port [%d] may be unavailable.", port));
- } else if (response.getError() != null) {
- throw new IOException("An error occurred while sending trap.",
response.getError());
- }
+ snmpManager.send(pdu, target);
}
V1TrapPDUFactory createV1TrapPduFactory(final Instant startTime) {
diff --git
a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/operations/SendTrapSNMPHandlerTest.java
b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/operations/SendTrapSNMPHandlerTest.java
index b7bed1d682..9a28b9b499 100644
---
a/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/operations/SendTrapSNMPHandlerTest.java
+++
b/nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/operations/SendTrapSNMPHandlerTest.java
@@ -18,7 +18,6 @@ package org.apache.nifi.snmp.operations;
import org.apache.nifi.snmp.configuration.V1TrapConfiguration;
import org.apache.nifi.snmp.configuration.V2TrapConfiguration;
-import org.apache.nifi.snmp.exception.SNMPException;
import org.apache.nifi.snmp.factory.trap.V1TrapPDUFactory;
import org.apache.nifi.snmp.factory.trap.V2TrapPDUFactory;
import org.apache.nifi.util.MockComponentLog;
@@ -29,15 +28,12 @@ import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.event.ResponseEvent;
-import org.snmp4j.smi.TransportIpAddress;
import java.io.IOException;
import java.time.Instant;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThrows;
-import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -115,34 +111,4 @@ public class SendTrapSNMPHandlerTest {
final String expectedDebugLog = "{} No optional SNMP specific
variables found in flowfile.";
assertEquals(expectedDebugLog,
mockComponentLog.getDebugMessages().get(0).getMsg());
}
-
- @Test
- public void testSendTrapToInvalidPort() throws IOException {
- when(mockSnmpManager.send(mockPdu, mockTarget)).thenReturn(null);
- final TransportIpAddress mockAddress = mock(TransportIpAddress.class);
- final int port = 55555;
- when(mockAddress.getPort()).thenReturn(port);
- when(mockTarget.getAddress()).thenReturn(mockAddress);
-
- final String flowFileOid = "1.3.6.1.2.1.1.1.0";
- Exception e = assertThrows(IOException.class,
- () ->
sendTrapSNMPHandler.sendTrap(Collections.singletonMap("snmp$" + flowFileOid,
"OID value"), mockV2TrapConfiguration)
- );
-
- assertTrue(e.getMessage().contains(String.valueOf(port)));
- }
-
- @Test
- public void testSendTrapInvalidResponse() throws IOException {
- final String trapSnmpError = "Test Trap SNMP Error";
- when(mockResponseEvent.getError()).thenReturn(new
SNMPException(trapSnmpError));
- when(mockSnmpManager.send(mockPdu,
mockTarget)).thenReturn(mockResponseEvent);
-
- final String flowFileOid = "1.3.6.1.2.1.1.1.0";
- Exception e = assertThrows(IOException.class,
- () ->
sendTrapSNMPHandler.sendTrap(Collections.singletonMap("snmp$" + flowFileOid,
"OID value"), mockV2TrapConfiguration)
- );
-
- assertTrue(e.getCause().getMessage().contains(trapSnmpError));
- }
}