Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBergstrand committed Jan 8, 2025
1 parent 04a3239 commit 4856577
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 88 deletions.
28 changes: 1 addition & 27 deletions common/src/main/java/apoc/ApocConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import org.apache.commons.configuration2.CombinedConfiguration;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.EnvironmentConfiguration;
Expand Down Expand Up @@ -189,32 +188,7 @@ public void init() {
}

protected String determineNeo4jConfFolder() {
return neo4jConfig.get(configuration_directory).resolve(Config.DEFAULT_CONFIG_DIR_NAME).toString();
// String defaultPath =neo4jConfig.get(configuration_directory).toString(); //resolve(Config.DEFAULT_CONFIG_DIR_NAME).toString();
// neo4jConfig
// .get(neo4j_home)
// .resolve(Config.DEFAULT_CONFIG_DIR_NAME)
// .toString();
// String command = System.getProperty(SUN_JAVA_COMMAND);
// if (command == null) {
// log.warn(
// "system property %s is not set, assuming %s as conf dir. This might cause `apoc.conf` not getting loaded.",
// SUN_JAVA_COMMAND, defaultPath);
// return defaultPath;
// } else {
// final String neo4jConfFolder = Stream.of(command.split("--"))
// .map(String::trim)
// .filter(s -> s.startsWith(CONFIG_DIR))
// .map(s -> s.substring(CONFIG_DIR.length()))
// .findFirst()
// .orElse(defaultPath);
// if (defaultPath.equals(neo4jConfFolder)) {
// log.info("cannot determine conf folder from sys property %s, assuming %s", command, defaultPath);
// } else {
// log.info("from system properties: NEO4J_CONF=%s", neo4jConfFolder);
// }
// return neo4jConfFolder;
// }
return neo4jConfig.get(configuration_directory).toString();
}

/**
Expand Down
37 changes: 7 additions & 30 deletions common/src/test/java/apoc/ApocConfigCommandExpansionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package apoc;

import static apoc.ApocConfig.APOC_MAX_DECOMPRESSION_RATIO;
import static apoc.ApocConfig.SUN_JAVA_COMMAND;
import static java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE;
import static java.nio.file.attribute.PosixFilePermission.GROUP_READ;
import static java.nio.file.attribute.PosixFilePermission.GROUP_WRITE;
Expand All @@ -46,7 +45,6 @@
import java.util.Collections;
import java.util.Set;
import org.assertj.core.api.Assertions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.neo4j.configuration.Config;
Expand Down Expand Up @@ -75,46 +73,31 @@ public void setup() throws Exception {
when(neo4jConfig.get(any())).thenReturn(null);
when(neo4jConfig.get(GraphDatabaseSettings.allow_file_urls)).thenReturn(false);
when(neo4jConfig.expandCommands()).thenReturn(true);
// when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0"));

apocConfigCommandExpansionFile = new File(getClass()
.getClassLoader()
.getResource("apoc.conf")
.getResource("apoc-config-command-expansion/apoc.conf")
.toURI());
Files.setPosixFilePermissions(
apocConfigCommandExpansionFile.toPath(), permittedFilePermissionsForCommandExpansion);

when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of(apocConfigCommandExpansionFile.getParent()));
when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory))
.thenReturn(Path.of(apocConfigCommandExpansionFile.getParent()));

GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class);
DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class);
apocConfig =
new ApocConfig(neo4jConfig, new SimpleLogService(logProvider), registry, databaseManagementService);
}

@After
public void cleanup() {
System.clearProperty(SUN_JAVA_COMMAND);
System.clearProperty(APOC_MAX_DECOMPRESSION_RATIO);
System.clearProperty("command.expansion");
}

private void setApocConfigFilePermissions(Set<PosixFilePermission> forbidden) throws Exception {
Files.setPosixFilePermissions(
apocConfigCommandExpansionFile.toPath(),
Sets.union(permittedFilePermissionsForCommandExpansion, forbidden));
}

// private void setApocConfigSystemProperty() {
// System.setProperty(
// SUN_JAVA_COMMAND,
// "com.neo4j.server.enterprise.CommercialEntryPoint --home-dir=/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02 --config-dir="
// + apocConfigCommandExpansionFile.getParent());
// }

@Test
public void testApocConfWithExpandCommands() {
// setApocConfigSystemProperty();
apocConfig.init();

assertEquals("expanded value", apocConfig.getConfig().getString("command.expansion"));
Expand All @@ -124,7 +107,6 @@ public void testApocConfWithExpandCommands() {
public void testApocConfWithInvalidExpandCommands() throws Exception {
String invalidExpandLine = "command.expansion.3=$(fakeCommand 3 + 3)";
addLineToApocConfig(invalidExpandLine);
// setApocConfigSystemProperty();

RuntimeException e = assertThrows(RuntimeException.class, apocConfig::init);
String expectedMessage =
Expand All @@ -138,7 +120,6 @@ public void testApocConfWithInvalidExpandCommands() throws Exception {
public void testApocConfWithWrongFilePermissions() throws Exception {
for (PosixFilePermission filePermission : forbiddenFilePermissionsForCommandExpansion) {
setApocConfigFilePermissions(Set.of(filePermission));
// setApocConfigSystemProperty();

RuntimeException e = assertThrows(RuntimeException.class, apocConfig::init);
String expectedMessage = "does not have the correct file permissions to evaluate commands.";
Expand All @@ -156,17 +137,14 @@ public void testApocConfWithoutExpandCommands() {
when(neo4jConfig.getDeclaredSettings()).thenReturn(Collections.emptyMap());
when(neo4jConfig.get(any())).thenReturn(null);
when(neo4jConfig.expandCommands()).thenReturn(false);
// when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0"));
// when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0"));
when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of(apocConfigCommandExpansionFile.getParent()));
when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory))
.thenReturn(Path.of(apocConfigCommandExpansionFile.getParent()));

GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class);
DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class);
ApocConfig apocConfig =
new ApocConfig(neo4jConfig, new SimpleLogService(logProvider), registry, databaseManagementService);

// setApocConfigSystemProperty();

RuntimeException e = assertThrows(RuntimeException.class, apocConfig::init);
String expectedMessage =
"$(echo \"expanded value\") is a command, but config is not explicitly told to expand it. (Missing --expand-commands argument?)";
Expand All @@ -182,9 +160,8 @@ public void testMaxDecompressionRatioValidation() {
when(neo4jConfig.getDeclaredSettings()).thenReturn(Collections.emptyMap());
when(neo4jConfig.get(any())).thenReturn(null);
when(neo4jConfig.expandCommands()).thenReturn(false);
when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0"));
// when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0"));
when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of(apocConfigCommandExpansionFile.getParent()));
when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory))
.thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0/conf"));

GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class);
DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class);
Expand Down
34 changes: 3 additions & 31 deletions common/src/test/java/apoc/ApocConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package apoc;

import static apoc.ApocConfig.SUN_JAVA_COMMAND;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -50,54 +49,27 @@ public void setup() throws Exception {
when(neo4jConfig.getDeclaredSettings()).thenReturn(Collections.emptyMap());
when(neo4jConfig.get(any())).thenReturn(null);
when(neo4jConfig.get(GraphDatabaseSettings.allow_file_urls)).thenReturn(false);
when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0"));
when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0"));

apocConfigFile =
new File(getClass().getClassLoader().getResource("apoc.conf").toURI());
when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory))
.thenReturn(Path.of(apocConfigFile.getParent()));

GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class);
DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class);
apocConfig =
new ApocConfig(neo4jConfig, new SimpleLogService(logProvider), registry, databaseManagementService);
}

private void setApocConfigSystemProperty() {
System.setProperty(
SUN_JAVA_COMMAND,
"com.neo4j.server.enterprise.CommercialEntryPoint --home-dir=/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02 --config-dir="
+ apocConfigFile.getParent());
}

@Test
public void testDetermineNeo4jConfFolderDefault() {
System.setProperty(SUN_JAVA_COMMAND, "");
assertEquals("C:/neo4j/neo4j-enterprise-5.x.0/conf", apocConfig.determineNeo4jConfFolder());
}

@Test
public void testDetermineNeo4jConfFolder() {
System.setProperty(
SUN_JAVA_COMMAND,
"com.neo4j.server.enterprise.CommercialEntryPoint --home-dir=/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02 --config-dir=/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02/conf");

assertEquals("/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02/conf", apocConfig.determineNeo4jConfFolder());
assertEquals(apocConfigFile.getParent(), apocConfig.determineNeo4jConfFolder());
}

@Test
public void testApocConfFileBeingLoaded() {
setApocConfigSystemProperty();
apocConfig.init();

assertEquals("bar", apocConfig.getConfig().getString("foo"));
}

@Test
public void testDetermineNeo4jConfFolderWithWhitespaces() {
System.setProperty(
SUN_JAVA_COMMAND,
"com.neo4j.server.enterprise.CommercialEntryPoint --config-dir=/home/stefan/neo4j enterprise-4.0.0-alpha09mr02/conf --home-dir=/home/stefan/neo4j enterprise-4.0.0-alpha09mr02");

assertEquals("/home/stefan/neo4j enterprise-4.0.0-alpha09mr02/conf", apocConfig.determineNeo4jConfFolder());
}
}
File renamed without changes.

0 comments on commit 4856577

Please sign in to comment.