mcgilman commented on code in PR #10907:
URL: https://github.com/apache/nifi/pull/10907#discussion_r2817995993


##########
nifi-connector-mock-bundle/nifi-connector-mock-server/src/test/java/org/apache/nifi/mock/connector/server/StandardConnectorMockServerJettyTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.mock.connector.server;
+
+import org.apache.nifi.bundle.Bundle;
+import org.apache.nifi.bundle.BundleCoordinate;
+import org.apache.nifi.bundle.BundleDetails;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class StandardConnectorMockServerJettyTest {
+
+    private static final String NAR_DEPENDENCIES_PATH = 
"NAR-INF/bundled-dependencies";
+
+    @TempDir
+    private Path tempDir;
+
+    @Test
+    void testFindWarsDiscoversSingleWarFile() throws Exception {
+        final Path bundleWorkingDir = tempDir.resolve("test-bundle");
+        final Path depsDir = bundleWorkingDir.resolve(NAR_DEPENDENCIES_PATH);
+        Files.createDirectories(depsDir);
+
+        final Path warFile = depsDir.resolve("my-app.war");
+        Files.createFile(warFile);
+
+        final Bundle bundle = createBundle(bundleWorkingDir);
+        final Map<File, Bundle> wars = invokeFindWars(Set.of(bundle));
+
+        assertEquals(1, wars.size());
+        assertTrue(wars.containsKey(warFile.toFile()));
+        assertEquals(bundle, wars.get(warFile.toFile()));
+    }
+
+    @Test
+    void testFindWarsIgnoresNonWarFiles() throws Exception {
+        final Path bundleWorkingDir = tempDir.resolve("test-bundle");
+        final Path depsDir = bundleWorkingDir.resolve(NAR_DEPENDENCIES_PATH);
+        Files.createDirectories(depsDir);
+
+        Files.createFile(depsDir.resolve("some-lib.jar"));
+        Files.createFile(depsDir.resolve("config.xml"));
+
+        final Bundle bundle = createBundle(bundleWorkingDir);
+        final Map<File, Bundle> wars = invokeFindWars(Set.of(bundle));
+
+        assertTrue(wars.isEmpty());
+    }
+
+    @Test
+    void testFindWarsHandlesMissingDependenciesDirectory() throws Exception {
+        final Path bundleWorkingDir = tempDir.resolve("empty-bundle");
+        Files.createDirectories(bundleWorkingDir);
+
+        final Bundle bundle = createBundle(bundleWorkingDir);
+        final Map<File, Bundle> wars = invokeFindWars(Set.of(bundle));
+
+        assertTrue(wars.isEmpty());
+    }
+
+    @Test
+    void testFindWarsDiscoversMultipleWarFiles() throws Exception {
+        final Path bundleWorkingDir = tempDir.resolve("multi-war-bundle");
+        final Path depsDir = bundleWorkingDir.resolve(NAR_DEPENDENCIES_PATH);
+        Files.createDirectories(depsDir);
+
+        Files.createFile(depsDir.resolve("app-one.war"));
+        Files.createFile(depsDir.resolve("app-two.war"));
+        Files.createFile(depsDir.resolve("some-lib.jar"));
+
+        final Bundle bundle = createBundle(bundleWorkingDir);
+        final Map<File, Bundle> wars = invokeFindWars(Set.of(bundle));
+
+        assertEquals(2, wars.size());
+    }
+
+    @Test
+    void testFindWarsFromMultipleBundles() throws Exception {
+        final Path bundleDir1 = tempDir.resolve("bundle-1");
+        final Path depsDir1 = bundleDir1.resolve(NAR_DEPENDENCIES_PATH);
+        Files.createDirectories(depsDir1);
+        Files.createFile(depsDir1.resolve("first-app.war"));
+
+        final Path bundleDir2 = tempDir.resolve("bundle-2");
+        final Path depsDir2 = bundleDir2.resolve(NAR_DEPENDENCIES_PATH);
+        Files.createDirectories(depsDir2);
+        Files.createFile(depsDir2.resolve("second-app.war"));
+
+        final Bundle bundle1 = createBundle(bundleDir1, "test-bundle-1");
+        final Bundle bundle2 = createBundle(bundleDir2, "test-bundle-2");
+
+        final Map<File, Bundle> wars = invokeFindWars(Set.of(bundle1, 
bundle2));
+
+        assertEquals(2, wars.size());
+    }
+
+    @Test
+    void testGetHttpPortReturnsNegativeOneWhenNoServer() {
+        final StandardConnectorMockServer server = new 
StandardConnectorMockServer();
+        assertEquals(-1, server.getHttpPort());
+    }
+
+    @Test
+    void testContextPathDerivedFromWarFilename() {
+        final String warName = "my-custom-app.war";
+        final String expectedContextPath = "/my-custom-app";
+        final String warExtension = ".war";
+
+        final String contextPath = "/" + warName.substring(0, warName.length() 
- warExtension.length());
+        assertEquals(expectedContextPath, contextPath);
+    }
+
+    @Test
+    void testFindWarsReturnsEmptyForEmptyBundleSet() throws Exception {
+        final Map<File, Bundle> wars = invokeFindWars(Set.of());
+        assertTrue(wars.isEmpty());
+    }
+
+    private Bundle createBundle(final Path workingDir) {
+        return createBundle(workingDir, "test-bundle");
+    }
+
+    private Bundle createBundle(final Path workingDir, final String 
artifactId) {
+        final BundleDetails details = new BundleDetails.Builder()
+                .workingDir(workingDir.toFile())
+                .coordinate(new BundleCoordinate("org.test", artifactId, 
"1.0.0"))
+                .build();
+
+        return new Bundle(details, ClassLoader.getSystemClassLoader());
+    }
+
+    @SuppressWarnings("unchecked")
+    private Map<File, Bundle> invokeFindWars(final Set<Bundle> bundles) throws 
Exception {
+        final StandardConnectorMockServer server = new 
StandardConnectorMockServer();
+        final Method findWarsMethod = 
StandardConnectorMockServer.class.getDeclaredMethod("findWars", Set.class);
+        findWarsMethod.setAccessible(true);
+
+        try {
+            return (Map<File, Bundle>) findWarsMethod.invoke(server, bundles);
+        } catch (final InvocationTargetException e) {
+            if (e.getCause() instanceof Exception) {
+                throw (Exception) e.getCause();
+            }
+            throw e;
+        }

Review Comment:
   Should we just adjust the modifier of `findWars` to avoid use of reflection 
here?



##########
nifi-connector-mock-bundle/nifi-connector-mock-server/src/main/java/org/apache/nifi/mock/connector/server/StandardConnectorMockServer.java:
##########
@@ -401,8 +428,96 @@ public void mockControllerService(final String 
controllerServiceType, final Clas
         extensionManager.addControllerService(mockControllerServiceClass);
     }
 
+    @Override
+    public int getHttpPort() {
+        if (jettyServer == null) {
+            return -1;
+        }
+
+        final ServerConnector connector = (ServerConnector) 
jettyServer.getConnectors()[0];
+        return connector.getLocalPort();
+    }
+
     @Override
     public void close() {
         stop();
     }
+
+    private void startJettyServer() {
+        final String httpPortValue = 
nifiProperties.getProperty(NiFiProperties.WEB_HTTP_PORT);
+        if (httpPortValue == null || httpPortValue.isBlank()) {
+            logger.debug("No HTTP port configured; skipping Jetty server 
startup");
+            return;
+        }
+
+        final int httpPort = Integer.parseInt(httpPortValue);
+        final Map<File, Bundle> wars = findWars(bundles);
+        if (wars.isEmpty()) {
+            logger.debug("No WAR files found in NAR bundles; skipping Jetty 
server startup");
+            return;
+        }
+
+        jettyServer = new Server();
+
+        final ServerConnector serverConnector = new 
ServerConnector(jettyServer);
+        serverConnector.setPort(httpPort);
+        jettyServer.addConnector(serverConnector);
+
+        final List<WebAppContext> webAppContexts = new ArrayList<>();
+        final ContextHandlerCollection handlers = new 
ContextHandlerCollection();
+        for (final Map.Entry<File, Bundle> entry : wars.entrySet()) {
+            final File warFile = entry.getKey();
+            final Bundle bundle = entry.getValue();
+
+            final String warName = warFile.getName();
+            final String contextPath = "/" + warName.substring(0, 
warName.length() - WAR_EXTENSION.length());
+
+            final WebAppContext webAppContext = new 
WebAppContext(warFile.getPath(), contextPath);
+            webAppContext.setClassLoader(new 
org.eclipse.jetty.ee.webapp.WebAppClassLoader(bundle.getClassLoader(), 
webAppContext));
+
+            handlers.addHandler(webAppContext);
+            webAppContexts.add(webAppContext);
+            logger.info("Deploying WAR [{}] at context path [{}]", 
warFile.getAbsolutePath(), contextPath);
+        }
+
+        jettyServer.setHandler(handlers);
+
+        try {
+            jettyServer.start();
+            logger.info("Jetty server started on port [{}]", getHttpPort());
+        } catch (final Exception e) {
+            throw new RuntimeException("Failed to start Jetty server", e);
+        }
+
+        performInjectionForConnectorUis(webAppContexts);
+    }
+
+    private void performInjectionForConnectorUis(final List<WebAppContext> 
webAppContexts) {
+        final NiFiConnectorWebContext connectorWebContext = new 
MockNiFiConnectorWebContext(connectorRepository);
+        for (final WebAppContext webAppContext : webAppContexts) {
+            final ServletContext servletContext = 
webAppContext.getServletHandler().getServletContext();
+            servletContext.setAttribute("nifi-connector-web-context", 
connectorWebContext);
+            logger.info("Injected NiFiConnectorWebContext into WAR context 
[{}]", webAppContext.getContextPath());
+        }
+    }
+
+    private Map<File, Bundle> findWars(final Set<Bundle> bundles) {
+        final Map<File, Bundle> wars = new HashMap<>();
+
+        bundles.forEach(bundle -> {
+            final BundleDetails details = bundle.getBundleDetails();
+            final Path bundledDependencies = new 
File(details.getWorkingDirectory(), NAR_DEPENDENCIES_PATH).toPath();
+            if (Files.isDirectory(bundledDependencies)) {
+                try (final Stream<Path> dependencies = 
Files.list(bundledDependencies)) {
+                    dependencies.filter(dependency -> 
dependency.getFileName().toString().endsWith(WAR_EXTENSION))
+                            .map(Path::toFile)
+                            .forEach(dependency -> wars.put(dependency, 
bundle));

Review Comment:
   Not critical since this is just testing infrastructure but there is 
additional logic to truly identify a Connector UI war. This logic would 
incorrectly consider others wars (content viewer, etc) if they were bundled as 
part of the same NAR. Connectors NARs are found through the convention of the 
presence of the `nifi-connector` file in the META-INF.



##########
nifi-connector-mock-bundle/nifi-connector-mock-server/src/main/java/org/apache/nifi/mock/connector/server/StandardConnectorMockServer.java:
##########
@@ -401,8 +428,96 @@ public void mockControllerService(final String 
controllerServiceType, final Clas
         extensionManager.addControllerService(mockControllerServiceClass);
     }
 
+    @Override
+    public int getHttpPort() {
+        if (jettyServer == null) {
+            return -1;
+        }
+
+        final ServerConnector connector = (ServerConnector) 
jettyServer.getConnectors()[0];
+        return connector.getLocalPort();
+    }
+
     @Override
     public void close() {
         stop();
     }
+
+    private void startJettyServer() {
+        final String httpPortValue = 
nifiProperties.getProperty(NiFiProperties.WEB_HTTP_PORT);
+        if (httpPortValue == null || httpPortValue.isBlank()) {
+            logger.debug("No HTTP port configured; skipping Jetty server 
startup");
+            return;
+        }
+
+        final int httpPort = Integer.parseInt(httpPortValue);
+        final Map<File, Bundle> wars = findWars(bundles);
+        if (wars.isEmpty()) {
+            logger.debug("No WAR files found in NAR bundles; skipping Jetty 
server startup");
+            return;
+        }
+
+        jettyServer = new Server();
+
+        final ServerConnector serverConnector = new 
ServerConnector(jettyServer);
+        serverConnector.setPort(httpPort);
+        jettyServer.addConnector(serverConnector);
+
+        final List<WebAppContext> webAppContexts = new ArrayList<>();
+        final ContextHandlerCollection handlers = new 
ContextHandlerCollection();
+        for (final Map.Entry<File, Bundle> entry : wars.entrySet()) {
+            final File warFile = entry.getKey();
+            final Bundle bundle = entry.getValue();
+
+            final String warName = warFile.getName();
+            final String contextPath = "/" + warName.substring(0, 
warName.length() - WAR_EXTENSION.length());
+
+            final WebAppContext webAppContext = new 
WebAppContext(warFile.getPath(), contextPath);
+            webAppContext.setClassLoader(new 
org.eclipse.jetty.ee.webapp.WebAppClassLoader(bundle.getClassLoader(), 
webAppContext));

Review Comment:
   Could this be imported instead of the fully qualified name here?



##########
nifi-connector-mock-bundle/nifi-connector-mock-server/src/test/java/org/apache/nifi/mock/connector/server/StandardConnectorMockServerJettyTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.mock.connector.server;
+
+import org.apache.nifi.bundle.Bundle;
+import org.apache.nifi.bundle.BundleCoordinate;
+import org.apache.nifi.bundle.BundleDetails;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class StandardConnectorMockServerJettyTest {
+
+    private static final String NAR_DEPENDENCIES_PATH = 
"NAR-INF/bundled-dependencies";
+
+    @TempDir
+    private Path tempDir;
+
+    @Test
+    void testFindWarsDiscoversSingleWarFile() throws Exception {
+        final Path bundleWorkingDir = tempDir.resolve("test-bundle");
+        final Path depsDir = bundleWorkingDir.resolve(NAR_DEPENDENCIES_PATH);
+        Files.createDirectories(depsDir);
+
+        final Path warFile = depsDir.resolve("my-app.war");
+        Files.createFile(warFile);
+
+        final Bundle bundle = createBundle(bundleWorkingDir);
+        final Map<File, Bundle> wars = invokeFindWars(Set.of(bundle));
+
+        assertEquals(1, wars.size());
+        assertTrue(wars.containsKey(warFile.toFile()));
+        assertEquals(bundle, wars.get(warFile.toFile()));
+    }
+
+    @Test
+    void testFindWarsIgnoresNonWarFiles() throws Exception {
+        final Path bundleWorkingDir = tempDir.resolve("test-bundle");
+        final Path depsDir = bundleWorkingDir.resolve(NAR_DEPENDENCIES_PATH);
+        Files.createDirectories(depsDir);
+
+        Files.createFile(depsDir.resolve("some-lib.jar"));
+        Files.createFile(depsDir.resolve("config.xml"));
+
+        final Bundle bundle = createBundle(bundleWorkingDir);
+        final Map<File, Bundle> wars = invokeFindWars(Set.of(bundle));
+
+        assertTrue(wars.isEmpty());
+    }
+
+    @Test
+    void testFindWarsHandlesMissingDependenciesDirectory() throws Exception {
+        final Path bundleWorkingDir = tempDir.resolve("empty-bundle");
+        Files.createDirectories(bundleWorkingDir);
+
+        final Bundle bundle = createBundle(bundleWorkingDir);
+        final Map<File, Bundle> wars = invokeFindWars(Set.of(bundle));
+
+        assertTrue(wars.isEmpty());
+    }
+
+    @Test
+    void testFindWarsDiscoversMultipleWarFiles() throws Exception {
+        final Path bundleWorkingDir = tempDir.resolve("multi-war-bundle");
+        final Path depsDir = bundleWorkingDir.resolve(NAR_DEPENDENCIES_PATH);
+        Files.createDirectories(depsDir);
+
+        Files.createFile(depsDir.resolve("app-one.war"));
+        Files.createFile(depsDir.resolve("app-two.war"));
+        Files.createFile(depsDir.resolve("some-lib.jar"));
+
+        final Bundle bundle = createBundle(bundleWorkingDir);
+        final Map<File, Bundle> wars = invokeFindWars(Set.of(bundle));
+
+        assertEquals(2, wars.size());
+    }
+
+    @Test
+    void testFindWarsFromMultipleBundles() throws Exception {
+        final Path bundleDir1 = tempDir.resolve("bundle-1");
+        final Path depsDir1 = bundleDir1.resolve(NAR_DEPENDENCIES_PATH);
+        Files.createDirectories(depsDir1);
+        Files.createFile(depsDir1.resolve("first-app.war"));
+
+        final Path bundleDir2 = tempDir.resolve("bundle-2");
+        final Path depsDir2 = bundleDir2.resolve(NAR_DEPENDENCIES_PATH);
+        Files.createDirectories(depsDir2);
+        Files.createFile(depsDir2.resolve("second-app.war"));
+
+        final Bundle bundle1 = createBundle(bundleDir1, "test-bundle-1");
+        final Bundle bundle2 = createBundle(bundleDir2, "test-bundle-2");
+
+        final Map<File, Bundle> wars = invokeFindWars(Set.of(bundle1, 
bundle2));
+
+        assertEquals(2, wars.size());
+    }
+
+    @Test
+    void testGetHttpPortReturnsNegativeOneWhenNoServer() {
+        final StandardConnectorMockServer server = new 
StandardConnectorMockServer();
+        assertEquals(-1, server.getHttpPort());
+    }
+
+    @Test
+    void testContextPathDerivedFromWarFilename() {
+        final String warName = "my-custom-app.war";
+        final String expectedContextPath = "/my-custom-app";
+        final String warExtension = ".war";
+
+        final String contextPath = "/" + warName.substring(0, warName.length() 
- warExtension.length());
+        assertEquals(expectedContextPath, contextPath);

Review Comment:
   This doesn't appear to be testing the actual subject. Should we be invoking 
something in `StandardConnectorMockServer` to ensure that it's actually 
deriving the appropriate name?



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