exceptionfactory commented on code in PR #6606:
URL: https://github.com/apache/nifi/pull/6606#discussion_r1009903767


##########
nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/factory/core/SNMPTtest.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.nifi.snmp.factory.core;
+
+import org.apache.nifi.remote.io.socket.NetworkUtils;
+import org.apache.nifi.snmp.configuration.SNMPConfiguration;
+import org.snmp4j.Snmp;
+import org.snmp4j.Target;
+import org.snmp4j.security.SecurityLevel;
+
+import java.net.BindException;
+import java.util.function.Function;
+
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.SECURITY_NAME;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPConfigurationFactory.LOCALHOST;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.AUTH_PASSPHRASE;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.AUTH_PROTOCOL;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.PRIV_PASSPHRASE;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.PRIV_PROTOCOL;
+
+public class SNMPTtest {
+
+    protected static final int RETRIES = 3;
+
+    protected SNMPConfiguration getSnmpConfiguration(int managerPort, String 
targetPort) {
+        return new SNMPConfiguration.Builder()
+                .setRetries(RETRIES)
+                .setManagerPort(managerPort)
+                .setTargetHost(LOCALHOST)
+                .setTargetPort(targetPort)
+                .setSecurityLevel(SecurityLevel.authPriv.name())
+                .setSecurityName(SECURITY_NAME)
+                .setAuthProtocol(AUTH_PROTOCOL)
+                .setAuthPassphrase(AUTH_PASSPHRASE)
+                .setPrivacyProtocol(PRIV_PROTOCOL)
+                .setPrivacyPassphrase(PRIV_PASSPHRASE)
+                .build();
+    }
+
+    protected Snmp createSnmpManagerInstance(final Function<SNMPConfiguration, 
Snmp> runnable, final int retries) throws BindException {
+        int attempts = 0;
+        while(attempts < retries) {
+            try {
+                return 
runnable.apply(getSnmpConfiguration(NetworkUtils.getAvailableUdpPort(), 
String.valueOf(NetworkUtils.getAvailableUdpPort())));
+            } catch (Exception e) {
+                if (e instanceof BindException) {
+                    attempts++;
+                }
+            }
+        }
+        throw new BindException();

Review Comment:
   Instead of throwing an empty `BindException`, the method should throw the 
last `BindException` to provide applicable detail.s



##########
nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/factory/core/SNMPTtest.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.nifi.snmp.factory.core;
+
+import org.apache.nifi.remote.io.socket.NetworkUtils;
+import org.apache.nifi.snmp.configuration.SNMPConfiguration;
+import org.snmp4j.Snmp;
+import org.snmp4j.Target;
+import org.snmp4j.security.SecurityLevel;
+
+import java.net.BindException;
+import java.util.function.Function;
+
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.SECURITY_NAME;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPConfigurationFactory.LOCALHOST;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.AUTH_PASSPHRASE;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.AUTH_PROTOCOL;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.PRIV_PASSPHRASE;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.PRIV_PROTOCOL;
+
+public class SNMPTtest {
+
+    protected static final int RETRIES = 3;
+
+    protected SNMPConfiguration getSnmpConfiguration(int managerPort, String 
targetPort) {
+        return new SNMPConfiguration.Builder()
+                .setRetries(RETRIES)
+                .setManagerPort(managerPort)
+                .setTargetHost(LOCALHOST)
+                .setTargetPort(targetPort)
+                .setSecurityLevel(SecurityLevel.authPriv.name())
+                .setSecurityName(SECURITY_NAME)
+                .setAuthProtocol(AUTH_PROTOCOL)
+                .setAuthPassphrase(AUTH_PASSPHRASE)
+                .setPrivacyProtocol(PRIV_PROTOCOL)
+                .setPrivacyPassphrase(PRIV_PASSPHRASE)
+                .build();
+    }
+
+    protected Snmp createSnmpManagerInstance(final Function<SNMPConfiguration, 
Snmp> runnable, final int retries) throws BindException {
+        int attempts = 0;
+        while(attempts < retries) {

Review Comment:
   ```suggestion
           while (attempts < retries) {
   ```



##########
nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/factory/core/V1V2cSNMPFactoryTest.java:
##########
@@ -19,53 +19,45 @@
 import org.apache.nifi.remote.io.socket.NetworkUtils;
 import org.apache.nifi.snmp.configuration.SNMPConfiguration;
 import org.apache.nifi.util.StringUtils;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.snmp4j.CommunityTarget;
 import org.snmp4j.Snmp;
 import org.snmp4j.Target;
 import org.snmp4j.security.SecurityLevel;
 
-import java.util.regex.Pattern;
+import java.net.BindException;
 
 import static 
org.apache.nifi.snmp.helper.configurations.SNMPConfigurationFactory.LOCALHOST;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.core.IsInstanceOf.instanceOf;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 
-class V1V2cSNMPFactoryTest {
+class V1V2cSNMPFactoryTest extends SNMPTtest {
 
     private static final int RETRIES = 3;
 
     @Test
-    void testFactoryCreatesV1V2Configuration() {
+    void testFactoryCreatesV1V2Configuration() throws BindException {
         final V1V2cSNMPFactory snmpFactory = new V1V2cSNMPFactory();
-        final int managerPort = NetworkUtils.getAvailableUdpPort();
-        final String targetPort = 
String.valueOf(NetworkUtils.getAvailableUdpPort());
-        final SNMPConfiguration snmpConfiguration = 
getSnmpConfiguration(managerPort, targetPort);
-
-        final Target target = 
snmpFactory.createTargetInstance(snmpConfiguration);
+        final Target target = 
createTargetInstance(snmpFactory::createTargetInstance, 5);
 
         assertThat(target, instanceOf(CommunityTarget.class));
-        assertEquals(LOCALHOST + "/" + targetPort, 
target.getAddress().toString());
+        assertNotNull(target.getAddress().toString());
         assertEquals(RETRIES, target.getRetries());
         assertEquals(1, target.getSecurityLevel());
         assertEquals(StringUtils.EMPTY, target.getSecurityName().toString());
     }
 
     @Test
-    void testFactoryCreatesSnmpManager() {
+    void testFactoryCreatesSnmpManager() throws BindException {
         final V1V2cSNMPFactory snmpFactory = new V1V2cSNMPFactory();
-        final int managerPort = NetworkUtils.getAvailableUdpPort();
-        final String targetPort = 
String.valueOf(NetworkUtils.getAvailableUdpPort());
-        final SNMPConfiguration snmpConfiguration = 
getSnmpConfiguration(managerPort, targetPort);
-
-        final Snmp snmpManager = 
snmpFactory.createSnmpManagerInstance(snmpConfiguration);
-
+        final Snmp snmpManager = 
createSnmpManagerInstance(snmpFactory::createSnmpManagerInstance, 5);
         final String address = 
snmpManager.getMessageDispatcher().getTransportMappings().iterator().next().getListenAddress().toString();
-        assertTrue(Pattern.compile("0.+?0/" + 
managerPort).matcher(address).matches());
+        Assertions.assertNotNull(address);

Review Comment:
   ```suggestion
           assertNotNull(address);
   
   ```



##########
nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/factory/core/SNMPTtest.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.nifi.snmp.factory.core;
+
+import org.apache.nifi.remote.io.socket.NetworkUtils;
+import org.apache.nifi.snmp.configuration.SNMPConfiguration;
+import org.snmp4j.Snmp;
+import org.snmp4j.Target;
+import org.snmp4j.security.SecurityLevel;
+
+import java.net.BindException;
+import java.util.function.Function;
+
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.SECURITY_NAME;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPConfigurationFactory.LOCALHOST;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.AUTH_PASSPHRASE;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.AUTH_PROTOCOL;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.PRIV_PASSPHRASE;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.PRIV_PROTOCOL;
+
+public class SNMPTtest {

Review Comment:
   This should be changed some something else, perhaps `SnmpSocketSupport`?



##########
nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/factory/core/SNMPTtest.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.nifi.snmp.factory.core;
+
+import org.apache.nifi.remote.io.socket.NetworkUtils;
+import org.apache.nifi.snmp.configuration.SNMPConfiguration;
+import org.snmp4j.Snmp;
+import org.snmp4j.Target;
+import org.snmp4j.security.SecurityLevel;
+
+import java.net.BindException;
+import java.util.function.Function;
+
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.SECURITY_NAME;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPConfigurationFactory.LOCALHOST;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.AUTH_PASSPHRASE;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.AUTH_PROTOCOL;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.PRIV_PASSPHRASE;
+import static 
org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.PRIV_PROTOCOL;
+
+public class SNMPTtest {
+
+    protected static final int RETRIES = 3;
+
+    protected SNMPConfiguration getSnmpConfiguration(int managerPort, String 
targetPort) {
+        return new SNMPConfiguration.Builder()
+                .setRetries(RETRIES)
+                .setManagerPort(managerPort)
+                .setTargetHost(LOCALHOST)
+                .setTargetPort(targetPort)
+                .setSecurityLevel(SecurityLevel.authPriv.name())
+                .setSecurityName(SECURITY_NAME)
+                .setAuthProtocol(AUTH_PROTOCOL)
+                .setAuthPassphrase(AUTH_PASSPHRASE)
+                .setPrivacyProtocol(PRIV_PROTOCOL)
+                .setPrivacyPassphrase(PRIV_PASSPHRASE)
+                .build();
+    }
+
+    protected Snmp createSnmpManagerInstance(final Function<SNMPConfiguration, 
Snmp> runnable, final int retries) throws BindException {
+        int attempts = 0;
+        while(attempts < retries) {
+            try {
+                return 
runnable.apply(getSnmpConfiguration(NetworkUtils.getAvailableUdpPort(), 
String.valueOf(NetworkUtils.getAvailableUdpPort())));
+            } catch (Exception e) {
+                if (e instanceof BindException) {
+                    attempts++;
+                }
+            }
+        }
+        throw new BindException();
+    }
+
+    protected Target createTargetInstance(final Function<SNMPConfiguration, 
Target> runnable, final int retries) throws BindException {
+        int attempts = 0;
+        while(attempts < retries) {

Review Comment:
   ```suggestion
           while (attempts < retries) {
   ```



-- 
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]

Reply via email to