Skip to content

Commit

Permalink
Add functional tests xwikisas#16
Browse files Browse the repository at this point in the history
* Fixed functional test errors regarding the server initialization
* Created AdminToolsViewPage class to access the info from the WebHome
* Refactored the server identification code to fix a bug where the incorect server is detected
* Adapted the tests
  • Loading branch information
ChiuchiuSorin committed Nov 8, 2023
1 parent 14d2bba commit d07aae7
Show file tree
Hide file tree
Showing 20 changed files with 233 additions and 84 deletions.
2 changes: 1 addition & 1 deletion application-admintools-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>com.xwiki.admintools</groupId>
<artifactId>application-admintools</artifactId>
<version>1.0.139-SNAPSHOT</version>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>application-admintools-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
*/
package com.xwiki.admintools;

import java.util.Map;
import java.util.regex.Pattern;

import org.xwiki.component.annotation.Role;

import java.util.regex.Pattern;

/**
* Exposes methods for accessing server specific information, like configurations, logs or other XWiki and server
* files.
*
* @version $Id$
*/
@Role
Expand All @@ -40,7 +39,7 @@ public interface ServerIdentifier
*
* @return {@code true} if the server is used, {@code false} otherwise.
*/
boolean isUsed();
boolean foundServerPath();

/**
* Extract the hint of a component.
Expand Down Expand Up @@ -68,13 +67,6 @@ public interface ServerIdentifier
*/
void updatePossiblePaths();

/**
* Access a JSON containing the server metadata.
*
* @return the server metadata.
*/
Map<String, String> getServerMetadata();

/**
* Get path to server.
*
Expand Down
2 changes: 1 addition & 1 deletion application-admintools-default/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>com.xwiki.admintools</groupId>
<artifactId>application-admintools</artifactId>
<version>1.0.139-SNAPSHOT</version>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>application-admintools-default</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;

import org.xwiki.bridge.event.DocumentDeletedEvent;
Expand Down Expand Up @@ -63,7 +64,7 @@ public class AdminToolsEventListener extends AbstractEventListener
private WikiDescriptorManager wikiManager;

@Inject
private CurrentServer currentServer;
private Provider<CurrentServer> currentServerProvider;

/**
* Creates an event-listener filtering for DocumentUpdatedEvent and DocumentDeletedEvent.
Expand All @@ -79,7 +80,7 @@ public void onEvent(Event event, Object source, Object data)
if (event instanceof DocumentUpdatedEvent || event instanceof DocumentDeletedEvent) {
XWikiDocument document = (XWikiDocument) source;
if (document != null && isAdminToolsConfigObject(document)) {
this.currentServer.updateCurrentServer();
this.currentServerProvider.get().updateCurrentServer();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Map<String, String> getDataAsJSON() throws Exception
systemInfo.put("xwikiCfgPath", getCurrentServer().getXwikiCfgFolderPath());
systemInfo.put("tomcatConfPath", this.getCurrentServer().getServerCfgPath());
systemInfo.put("javaVersion", this.getJavaVersion());
Map<String, String> serverMetadata = this.getCurrentServer().getServerMetadata();
Map<String, String> serverMetadata = this.currentServer.getServerMetadata();
systemInfo.put("usedServerName", serverMetadata.get(METADATA_NAME));
systemInfo.put("usedServerVersion", serverMetadata.get(METADATA_VERSION));
systemInfo.put("xwikiVersion", getXWikiVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
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.ServerIdentifier;
import com.xwiki.admintools.internal.PingProvider;

/**
* Manages the server identifiers and offers endpoints to retrieve info about their paths.
Expand All @@ -43,11 +45,18 @@
@Singleton
public class CurrentServer implements Initializable
{
private static final String SERVER_NAME_KEY = "name";

private static final String SERVER_VERSION_KEY = "version";

@Inject
private Provider<List<ServerIdentifier>> supportedServers;

private ServerIdentifier currentServerIdentifier;

@Inject
private PingProvider pingProvider;

@Override
public void initialize() throws InitializationException
{
Expand Down Expand Up @@ -94,14 +103,29 @@ public List<String> getSupportedServers()
return supportedServerList;
}

/**
* Access a JSON containing the server metadata.
*
* @return the server metadata.
*/
public Map<String, String> getServerMetadata()
{
ServletContainerPing servletPing = pingProvider.getServletPing();
String serverName = servletPing.getName();
String serverVersion = servletPing.getVersion();
return Map.of(SERVER_NAME_KEY, serverName, SERVER_VERSION_KEY, serverVersion);
}

/**
* Go through all supported servers and return the one that is used.
*/
public void updateCurrentServer()
{
this.currentServerIdentifier = null;
for (ServerIdentifier serverIdentifier : this.supportedServers.get()) {
if (serverIdentifier.isUsed()) {
boolean matchingHint =
getServerMetadata().get(SERVER_NAME_KEY).toLowerCase().contains(serverIdentifier.getComponentHint());
if (matchingHint && serverIdentifier.foundServerPath()) {
this.currentServerIdentifier = serverIdentifier;
this.currentServerIdentifier.updatePossiblePaths();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@
package com.xwiki.admintools.internal.data.identifiers;

import java.io.File;
import java.util.Map;
import java.util.regex.Pattern;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.xwiki.activeinstalls2.internal.data.ServletContainerPing;
import org.xwiki.component.annotation.Component;

import com.xwiki.admintools.ServerIdentifier;
import com.xwiki.admintools.internal.PingProvider;

/**
* {@link ServerIdentifier} implementation used for identifying a Tomcat server and retrieving its info.
Expand All @@ -46,29 +42,7 @@ public class TomcatIdentifier extends AbstractServerIdentifier
/**
* Component identifier.
*/
public static final String HINT = "Tomcat";

@Inject
private PingProvider pingProvider;

@Override
public boolean isUsed()
{
this.serverPath = null;
String providedConfigServerPath = this.adminToolsConfig.getServerPath();
if (providedConfigServerPath != null && !providedConfigServerPath.isEmpty()) {
return checkAndSetServerPath(providedConfigServerPath);
} else {
String catalinaBase = System.getProperty("catalina.base");
String catalinaHome = System.getenv("CATALINA_HOME");
if (catalinaBase != null && !catalinaBase.isEmpty()) {
return checkAndSetServerPath(catalinaBase);
} else if (catalinaHome != null && !catalinaHome.isEmpty()) {
return checkAndSetServerPath(catalinaHome);
}
}
return false;
}
public static final String HINT = "tomcat";

@Override
public String getComponentHint()
Expand All @@ -89,15 +63,6 @@ public void updatePossiblePaths()
String.format("%s/webapps/xwiki/WEB-INF/", this.serverPath) };
}

@Override
public Map<String, String> getServerMetadata()
{
ServletContainerPing servletPing = pingProvider.getServletPing();
String serverName = servletPing.getName();
String serverVersion = servletPing.getVersion();
return Map.of("name", serverName, "version", serverVersion);
}

@Override
public String getLogsFolderPath()
{
Expand All @@ -116,6 +81,25 @@ 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 (providedConfigServerPath != null && !providedConfigServerPath.isEmpty()) {
return checkAndSetServerPath(providedConfigServerPath);
} else {
String catalinaBase = System.getProperty("catalina.base");
String catalinaHome = System.getenv("CATALINA_HOME");
if (catalinaBase != null && !catalinaBase.isEmpty()) {
return checkAndSetServerPath(catalinaBase);
} else if (catalinaHome != null && !catalinaHome.isEmpty()) {
return checkAndSetServerPath(catalinaHome);
}
}
return false;
}

private boolean checkAndSetServerPath(String path)
{
File file = new File(path + "/conf/catalina.properties");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void beforeEach()
when(currentServer.getCurrentServer()).thenReturn(serverIdentifier);
when(serverIdentifier.getXwikiCfgFolderPath()).thenReturn("xwiki_config_folder_path");
when(serverIdentifier.getServerCfgPath()).thenReturn("server_config_folder_path");
when(serverIdentifier.getServerMetadata()).thenReturn(
when(currentServer.getServerMetadata()).thenReturn(
Map.of("name", "test_server_name", "version", "test_server_version"));
when(pingProvider.getDatabasePing()).thenReturn(databasePing);
when(databasePing.getName()).thenReturn("MySQL");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
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;
Expand All @@ -34,6 +36,7 @@

import com.xwiki.admintools.ServerIdentifier;
import com.xwiki.admintools.configuration.AdminToolsConfiguration;
import com.xwiki.admintools.internal.PingProvider;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -60,17 +63,28 @@ public class CurrentServerTest
@Named("default")
private AdminToolsConfiguration adminToolsConfig;

@MockComponent
private PingProvider pingProvider;

private ServletContainerPing containerPing;

@BeforeComponent
void setUp()
{
// Mock the list of supported servers.
List<ServerIdentifier> mockServerIdentifiers = new ArrayList<>();
mockServerIdentifiers.add(serverIdentifier);
when(supportedServers.get()).thenReturn(mockServerIdentifiers);
when(serverIdentifier.isUsed()).thenReturn(true);
when(serverIdentifier.foundServerPath()).thenReturn(true);
when(serverIdentifier.getComponentHint()).thenReturn("tomcat");

// Mock the behavior of adminToolsConfig.
when(adminToolsConfig.getServerPath()).thenReturn("exampleServerPath");

containerPing = new ServletContainerPing();
containerPing.setName("tomcat");
containerPing.setVersion("x.y.z");
when(pingProvider.getServletPing()).thenReturn(containerPing);
}

@Test
Expand All @@ -86,7 +100,7 @@ void initializeFound() throws InitializationException
@Test
void initializeWithServerNotFound() throws InitializationException
{
when(serverIdentifier.isUsed()).thenReturn(false);
when(serverIdentifier.foundServerPath()).thenReturn(false);
currentServer.initialize();

assertNull(currentServer.getCurrentServer());
Expand All @@ -95,11 +109,11 @@ void initializeWithServerNotFound() throws InitializationException
@Test
void updateCurrentServer()
{
when(serverIdentifier.isUsed()).thenReturn(false);
when(serverIdentifier.foundServerPath()).thenReturn(false);
currentServer.updateCurrentServer();
assertNull(currentServer.getCurrentServer());

when(serverIdentifier.isUsed()).thenReturn(true);
when(serverIdentifier.foundServerPath()).thenReturn(true);
currentServer.updateCurrentServer();
assertEquals(serverIdentifier, currentServer.getCurrentServer());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class TomcatIdentifierTest
private File tmpDir;

@Test
void isUsedFound() throws IOException
void foundServerPathFound() throws IOException
{
when(adminToolsConfig.getServerPath()).thenReturn(tmpDir.getAbsolutePath());

Expand All @@ -69,11 +69,11 @@ void isUsedFound() throws IOException
assertTrue(testFile.exists());

// Test with a valid providedConfigServerPath
assertTrue(tomcatIdentifier.isUsed());
assertTrue(tomcatIdentifier.foundServerPath());
}

@Test
void isUsedWithValidCatalinaBase() throws IOException
void foundServerPathWithValidCatalinaBase() throws IOException
{
File configDirectory = new File(tmpDir, "conf");
configDirectory.mkdir();
Expand All @@ -85,25 +85,25 @@ void isUsedWithValidCatalinaBase() throws IOException

when(adminToolsConfig.getServerPath()).thenReturn(null);
System.setProperty("catalina.base", tmpDir.getAbsolutePath());
assertTrue(tomcatIdentifier.isUsed());
assertTrue(tomcatIdentifier.foundServerPath());
System.clearProperty("catalina.base");
}

@Test
void isUsedValidSystemPathMissingCatalinaProperties()
void foundServerPathValidSystemPathMissingCatalinaProperties()
{
File configDirectory = new File(tmpDir, "conf");
configDirectory.mkdir();
when(adminToolsConfig.getServerPath()).thenReturn(null);
System.setProperty("catalina.base", tmpDir.getAbsolutePath());
assertFalse(tomcatIdentifier.isUsed());
assertFalse(tomcatIdentifier.foundServerPath());
System.clearProperty("catalina.base");
configDirectory.delete();
}

@Test
void getIdentifier()
{
assertEquals("Tomcat", tomcatIdentifier.getComponentHint());
assertEquals("tomcat", tomcatIdentifier.getComponentHint());
}
}
Loading

0 comments on commit d07aae7

Please sign in to comment.