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


##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/test/java/org/apache/nifi/mongodb/MongoDBControllerServiceTest.java:
##########
@@ -0,0 +1,214 @@
+/*
+ * 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.mongodb;
+
+import com.mongodb.MongoClientSettings;
+import com.mongodb.MongoCredential;
+import com.mongodb.client.MongoClient;
+import com.mongodb.client.internal.MongoClientImpl;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.util.MockConfigurationContext;
+import org.apache.nifi.util.MockControllerServiceLookup;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class MongoDBControllerServiceTest {
+
+    private static final String IDENTIFIER = "mongodb-client";
+
+    private TestRunner runner;
+    private MongoDBControllerService service;
+
+    @BeforeEach
+    public void setUp() throws Exception {
+        runner = 
TestRunners.newTestRunner(TestControllerServiceProcessor.class);
+        service = new MongoDBControllerService();
+        runner.addControllerService(IDENTIFIER, service);
+    }
+
+    @AfterEach
+    public void tearDown() {
+        try {
+            if (service != null) {
+                service.onDisable();
+            }
+        } catch (final Exception e) {
+            // Ignore during cleanup
+            System.err.println("Error during service cleanup: " + 
e.getMessage());

Review Comment:
   System.err and System.out should be avoided.  The `e` can be changed to 
`ignored` or the exception should just be thrown from the `tearDown` method.



##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java:
##########
@@ -111,41 +111,57 @@ protected MongoClient createClient(ConfigurationContext 
context, MongoClient exi
             final String authMechanism;
             if (authMechanismMatcher.find()) {
                 authMechanism = authMechanismMatcher.group(1);
-                uri = authMechanismMatcher.replaceFirst(uri.contains("?") ? 
"?" : "");
-                // If trailing & or ? is left, clean up
-                uri = uri.replaceAll("[&?]+$", "");
             } else {
                 authMechanism = null;
             }
 
-            final ConnectionString cs = new ConnectionString(uri);
+            // When properties specify the user, and the URI includes an 
authMechanism but no user in the URI,
+            // the Mongo driver would attempt to build credentials from the 
URI and fail. In that case, remove the
+            // authMechanism from the URI to allow property-based credentials. 
If the URI already includes user info,
+            // keep the mechanism so the URI remains valid; property 
credentials will override later.
+            final boolean hasUserInfoInUri = 
uri.matches("(?i)^mongodb(?:\\+srv)?://[^/]*@.*");

Review Comment:
   Similar to the `AUTH_MECHANISM_PATTERN`, recommend declaring this pattern 
statically and then matching.



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