From 0dd92343dfd295c30666f6ce0c5c840962e10d1d Mon Sep 17 00:00:00 2001 From: ChiuchiuSorin Date: Tue, 30 Jan 2024 17:57:57 +0200 Subject: [PATCH] Add functional tests #16 * Solved merge conflicts --- .../java/com/xwiki/admintools/ServerInfo.java | 2 +- .../data/identifiers/CurrentServer.java | 21 +++++++------------ .../internal/data/identifiers/TomcatInfo.java | 21 +------------------ .../data/ConfigurationDataProviderTest.java | 2 -- .../data/identifiers/CurrentServerTest.java | 17 ++++++++------- ...dentifierTest.java => TomcatInfoTest.java} | 14 ++++++------- 6 files changed, 26 insertions(+), 51 deletions(-) rename application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/identifiers/{TomcatIdentifierTest.java => TomcatInfoTest.java} (89%) diff --git a/application-admintools-api/src/main/java/com/xwiki/admintools/ServerInfo.java b/application-admintools-api/src/main/java/com/xwiki/admintools/ServerInfo.java index dae979be..733d429b 100644 --- a/application-admintools-api/src/main/java/com/xwiki/admintools/ServerInfo.java +++ b/application-admintools-api/src/main/java/com/xwiki/admintools/ServerInfo.java @@ -39,7 +39,7 @@ public interface ServerInfo * * @return {@code true} if the server is used, {@code false} otherwise. */ - boolean foundServerPath(); + boolean isUsed(); /** * Extract the hint of a component. diff --git a/application-admintools-default/src/main/java/com/xwiki/admintools/internal/data/identifiers/CurrentServer.java b/application-admintools-default/src/main/java/com/xwiki/admintools/internal/data/identifiers/CurrentServer.java index 5d1dd32e..a640c314 100644 --- a/application-admintools-default/src/main/java/com/xwiki/admintools/internal/data/identifiers/CurrentServer.java +++ b/application-admintools-default/src/main/java/com/xwiki/admintools/internal/data/identifiers/CurrentServer.java @@ -26,12 +26,12 @@ import javax.inject.Provider; import javax.inject.Singleton; -import org.xwiki.activeinstalls2.internal.data.ServletContainerPing; import org.xwiki.component.annotation.Component; import org.xwiki.component.phase.Initializable; import org.xwiki.component.phase.InitializationException; import com.xwiki.admintools.ServerInfo; +import com.xwiki.admintools.internal.wikiUsage.UsageDataProvider; /** * Manages the server identifiers and offers endpoints to retrieve info about their paths. @@ -44,11 +44,12 @@ public class CurrentServer implements Initializable { private static final String SERVER_NAME_KEY = "name"; - private static final String SERVER_VERSION_KEY = "version"; - @Inject private Provider> supportedServers; + @Inject + private UsageDataProvider usageDataProvider; + private ServerInfo currentServerInfo; @Override @@ -91,16 +92,6 @@ public List getSupportedServers() return supportedServerList; } - /** - * Access a {@link ServletContainerPing} containing the server metadata. - * - * @return the server metadata. - */ - public ServletContainerPing getServerMetadata() - { - return pingProvider.getServletPing(); - } - /** * Go through all supported servers and return the one that is used. */ @@ -108,7 +99,9 @@ public void updateCurrentServer() { this.currentServerInfo = null; for (ServerInfo serverInfo : this.supportedServers.get()) { - if (serverInfo.isUsed()) { + boolean matchingHint = usageDataProvider.getServerMetadata().get(SERVER_NAME_KEY).toLowerCase() + .contains(serverInfo.getComponentHint()); + if (matchingHint && serverInfo.isUsed()) { this.currentServerInfo = serverInfo; this.currentServerInfo.updatePossiblePaths(); break; diff --git a/application-admintools-default/src/main/java/com/xwiki/admintools/internal/data/identifiers/TomcatInfo.java b/application-admintools-default/src/main/java/com/xwiki/admintools/internal/data/identifiers/TomcatInfo.java index 74a7d10f..6cb7c3f4 100644 --- a/application-admintools-default/src/main/java/com/xwiki/admintools/internal/data/identifiers/TomcatInfo.java +++ b/application-admintools-default/src/main/java/com/xwiki/admintools/internal/data/identifiers/TomcatInfo.java @@ -42,7 +42,7 @@ public class TomcatInfo extends AbstractServerInfo /** * Component identifier. */ - public static final String HINT = "Tomcat"; + public static final String HINT = "tomcat"; @Override public boolean isUsed() @@ -100,25 +100,6 @@ public Pattern getLogsPattern() return Pattern.compile("\\d{4}-\\d{2}-\\d{2}"); } - @Override - public boolean foundServerPath() - { - this.serverPath = null; - String providedConfigServerPath = this.adminToolsConfig.getServerPath(); - if (!StringUtils.isEmpty(providedConfigServerPath)) { - return checkAndSetServerPath(providedConfigServerPath); - } else { - String catalinaBase = System.getProperty("catalina.base"); - String catalinaHome = System.getenv("CATALINA_HOME"); - if (!StringUtils.isEmpty(catalinaBase)) { - return checkAndSetServerPath(catalinaBase); - } else if (!StringUtils.isEmpty(catalinaHome)) { - return checkAndSetServerPath(catalinaHome); - } - } - return false; - } - private boolean checkAndSetServerPath(String path) { File file = new File(path + "/conf/catalina.properties"); diff --git a/application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/ConfigurationDataProviderTest.java b/application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/ConfigurationDataProviderTest.java index 3e8303e5..fd149b30 100644 --- a/application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/ConfigurationDataProviderTest.java +++ b/application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/ConfigurationDataProviderTest.java @@ -31,9 +31,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.mockito.Mock; -import org.xwiki.activeinstalls2.internal.data.DatabasePing; import org.xwiki.activeinstalls2.internal.data.ServletContainerPing; -import org.xwiki.component.util.ReflectionUtils; import org.xwiki.script.ScriptContextManager; import org.xwiki.template.TemplateManager; import org.xwiki.test.LogLevel; diff --git a/application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/identifiers/CurrentServerTest.java b/application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/identifiers/CurrentServerTest.java index 064625d6..24a5892c 100644 --- a/application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/identifiers/CurrentServerTest.java +++ b/application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/identifiers/CurrentServerTest.java @@ -21,13 +21,12 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; import javax.inject.Named; import javax.inject.Provider; import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.xwiki.activeinstalls2.internal.data.ServletContainerPing; import org.xwiki.component.phase.InitializationException; import org.xwiki.test.annotation.BeforeComponent; import org.xwiki.test.junit5.mockito.ComponentTest; @@ -36,7 +35,7 @@ import com.xwiki.admintools.ServerInfo; import com.xwiki.admintools.configuration.AdminToolsConfiguration; -import com.xwiki.admintools.internal.PingProvider; +import com.xwiki.admintools.internal.wikiUsage.UsageDataProvider; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -63,15 +62,19 @@ class CurrentServerTest @Named("default") private AdminToolsConfiguration adminToolsConfig; + @MockComponent + private UsageDataProvider usageDataProvider; + @BeforeComponent void setUp() { // Mock the list of supported servers. - List mockServerInfos = new ArrayList<>(); - mockServerInfos.add(serverInfo); - when(supportedServers.get()).thenReturn(mockServerInfos); + List mockServerInfo = new ArrayList<>(); + mockServerInfo.add(serverInfo); + when(supportedServers.get()).thenReturn(mockServerInfo); when(serverInfo.isUsed()).thenReturn(true); - + when(usageDataProvider.getServerMetadata()).thenReturn(Map.of("name", "Tomcat")); + when(serverInfo.getComponentHint()).thenReturn("tomcat"); // Mock the behavior of adminToolsConfig. when(adminToolsConfig.getServerPath()).thenReturn("exampleServerPath"); } diff --git a/application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/identifiers/TomcatIdentifierTest.java b/application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/identifiers/TomcatInfoTest.java similarity index 89% rename from application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/identifiers/TomcatIdentifierTest.java rename to application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/identifiers/TomcatInfoTest.java index 23040098..a496f635 100644 --- a/application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/identifiers/TomcatIdentifierTest.java +++ b/application-admintools-default/src/test/java/com/xwiki/admintools/internal/data/identifiers/TomcatInfoTest.java @@ -43,7 +43,7 @@ * @version $Id$ */ @ComponentTest -class TomcatIdentifierTest +class TomcatInfoTest { @InjectMockComponents private TomcatInfo tomcatIdentifier; @@ -56,7 +56,7 @@ class TomcatIdentifierTest private File tmpDir; @Test - void foundServerPathFound() throws IOException + void isUsedPathFound() throws IOException { when(adminToolsConfig.getServerPath()).thenReturn(tmpDir.getAbsolutePath()); @@ -69,11 +69,11 @@ void foundServerPathFound() throws IOException assertTrue(testFile.exists()); // Test with a valid providedConfigServerPath - assertTrue(tomcatIdentifier.foundServerPath()); + assertTrue(tomcatIdentifier.isUsed()); } @Test - void foundServerPathWithValidCatalinaBase() throws IOException + void isUsedWithValidCatalinaBase() throws IOException { File configDirectory = new File(tmpDir, "conf"); configDirectory.mkdir(); @@ -85,18 +85,18 @@ void foundServerPathWithValidCatalinaBase() throws IOException when(adminToolsConfig.getServerPath()).thenReturn(null); System.setProperty("catalina.base", tmpDir.getAbsolutePath()); - assertTrue(tomcatIdentifier.foundServerPath()); + assertTrue(tomcatIdentifier.isUsed()); System.clearProperty("catalina.base"); } @Test - void foundServerPathValidSystemPathMissingCatalinaProperties() + void isUsedValidSystemPathMissingCatalinaProperties() { File configDirectory = new File(tmpDir, "conf"); configDirectory.mkdir(); when(adminToolsConfig.getServerPath()).thenReturn(null); System.setProperty("catalina.base", tmpDir.getAbsolutePath()); - assertFalse(tomcatIdentifier.foundServerPath()); + assertFalse(tomcatIdentifier.isUsed()); System.clearProperty("catalina.base"); configDirectory.delete(); }