Skip to content

Commit

Permalink
added test - changed version handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 committed Apr 27, 2022
1 parent 90cd88e commit a91ab9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
30 changes: 10 additions & 20 deletions core/src/main/java/apoc/cypher/CypherInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class CypherInitializer implements AvailabilityListener {
* indicates the status of the initializer, to be used for tests to ensure initializer operations are already done
*/
private boolean finished = false;
private boolean checkedVersion = false;

public CypherInitializer(GraphDatabaseAPI db, Log userLog) {
this.db = db;
Expand Down Expand Up @@ -65,19 +64,13 @@ public void available() {
}

if (defaultDb.equals(db.databaseName())) {
try {
final String apocFullVersion = Version.class.getPackage().getImplementationVersion();
final String apocVersion = getMajorMinVersion(apocFullVersion);
final List<String> versions = db.executeTransactionally("CALL dbms.components", Collections.emptyMap(),
r -> (List<String>) r.next().get("versions"));
final boolean versionDifferent = versions.stream().noneMatch(kernelVersion -> getMajorMinVersion(kernelVersion).equals(apocVersion));
if (versionDifferent) {
userLog.warn("The apoc version (%s) and the Neo4j DBMS versions %s are incompatible. \n" +
"See the compatibility matrix in https://neo4j.com/labs/apoc/4.4/installation/ to see the correct version",
apocFullVersion, versions.toString());
}
} catch (Exception ignored) {
// with embedded testdb, "call dbms.components" is not recognized here
final List<String> versions = db.executeTransactionally("CALL dbms.components", Collections.emptyMap(),
r -> (List<String>) r.next().get("versions"));
final String apocFullVersion = Version.class.getPackage().getImplementationVersion();
if (isVersionDifferent(versions, apocFullVersion)) {
userLog.warn("The apoc version (%s) and the Neo4j DBMS versions %s are incompatible. \n" +
"See the compatibility matrix in https://neo4j.com/labs/apoc/4.4/installation/ to see the correct version",
apocFullVersion, versions.toString());
}
}
Configuration config = dependencyResolver.resolveDependency(ApocConfig.class).getConfig();
Expand All @@ -98,12 +91,9 @@ public void available() {
}).start();
}

private String getMajorMinVersion(String completeVersion) {
if (completeVersion == null) {
return null;
}
final String[] split = completeVersion.split("\\.");
return split[0] + "." + split[1];
// only for testing purpose
public static boolean isVersionDifferent(List<String> versions, String apocFullVersion) {
return versions.stream().noneMatch(apocFullVersion::startsWith);
}

private Collection<String> collectInitializers(boolean isSystemDatabase, Configuration config) {
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/java/apoc/cypher/CypherInitializerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@
import org.neo4j.kernel.availability.AvailabilityListener;
import org.neo4j.kernel.availability.DatabaseAvailabilityGuard;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.logging.NullLog;
import org.neo4j.procedure.Procedure;
import org.neo4j.test.ReflectionUtil;
import org.neo4j.test.rule.DbmsRule;
import org.neo4j.test.rule.ImpermanentDbmsRule;

import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import static apoc.ApocConfig.APOC_CONFIG_INITIALIZER;
import static apoc.ApocConfig.APOC_CONFIG_INITIALIZER_CYPHER;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME;
import static org.neo4j.configuration.GraphDatabaseSettings.SYSTEM_DATABASE_NAME;

Expand Down Expand Up @@ -76,6 +82,16 @@ private CypherInitializer getInitializer(String dbName) {
}

}

@Test
@Env
public void shouldReturnFalseOnlyWithCompatibleVersion() {
assertTrue(CypherInitializer.isVersionDifferent(List.of("3.5"), "4.4.0.2"));
assertTrue(CypherInitializer.isVersionDifferent(List.of("5_0"), "4.4.0.2"));

// we expect that APOC versioning is always consistent to Neo4j versioning
assertFalse(CypherInitializer.isVersionDifferent(List.of("5_0"), "5_0_0_1"));
}

@Test
@Env
Expand Down Expand Up @@ -130,4 +146,5 @@ public void databaseSpecificInitializersForSystem() {
private void expectNodeCount(long i) {
assertEquals(i, TestUtil.count(dbmsRule, "match (n) return n"));
}

}

0 comments on commit a91ab9c

Please sign in to comment.