|
| 1 | +package apoc.dv; |
| 2 | + |
| 3 | +import apoc.util.Neo4jContainerExtension; |
| 4 | +import apoc.util.TestContainerUtil; |
| 5 | +import apoc.util.TestcontainersCausalCluster; |
| 6 | +import org.apache.commons.io.FileUtils; |
| 7 | +import org.junit.AfterClass; |
| 8 | +import org.junit.BeforeClass; |
| 9 | +import org.junit.Test; |
| 10 | +import org.neo4j.driver.Driver; |
| 11 | +import org.neo4j.driver.Result; |
| 12 | +import org.neo4j.driver.Session; |
| 13 | +import org.neo4j.driver.SessionConfig; |
| 14 | +import org.neo4j.driver.types.Node; |
| 15 | +import org.neo4j.driver.types.Path; |
| 16 | +import org.neo4j.driver.types.Relationship; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.net.URI; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +import static apoc.dv.DataVirtualizationCatalogTestUtil.*; |
| 26 | +import static apoc.util.ExtendedTestContainerUtil.dbIsWriter; |
| 27 | +import static apoc.util.ExtendedTestContainerUtil.getBoltAddress; |
| 28 | +import static apoc.util.ExtendedTestContainerUtil.getDriverIfNotReplica; |
| 29 | +import static apoc.util.MapUtil.map; |
| 30 | +import static apoc.util.SystemDbUtil.PROCEDURE_NOT_ROUTED_ERROR; |
| 31 | +import static apoc.util.TestContainerUtil.importFolder; |
| 32 | +import static apoc.util.TestContainerUtil.testCall; |
| 33 | +import static apoc.util.TestContainerUtil.testCallEmpty; |
| 34 | +import static org.junit.Assert.assertEquals; |
| 35 | +import static org.junit.Assert.assertTrue; |
| 36 | +import static org.junit.Assert.fail; |
| 37 | +import static org.neo4j.configuration.GraphDatabaseSettings.SYSTEM_DATABASE_NAME; |
| 38 | + |
| 39 | + |
| 40 | +public class DataVirtualizationCatalogClusterRoutingTest { |
| 41 | + private static final int NUM_CORES = 3; |
| 42 | + private static TestcontainersCausalCluster cluster; |
| 43 | + private static Session clusterSession; |
| 44 | + private static List<Neo4jContainerExtension> members; |
| 45 | + |
| 46 | + @BeforeClass |
| 47 | + public static void setupCluster() throws Exception { |
| 48 | + cluster = TestContainerUtil |
| 49 | + .createEnterpriseCluster(List.of(TestContainerUtil.ApocPackage.EXTENDED, TestContainerUtil.ApocPackage.CORE), NUM_CORES, 0, |
| 50 | + Collections.emptyMap(), |
| 51 | + Map.of("NEO4J_dbms_routing_enabled", "true") |
| 52 | + ); |
| 53 | + clusterSession = cluster.getSession(); |
| 54 | + members = cluster.getClusterMembers(); |
| 55 | + FileUtils.copyFileToDirectory(new File(new URI(FILE_URL).toURL().getPath()), importFolder); |
| 56 | + assertEquals(NUM_CORES, members.size()); |
| 57 | + } |
| 58 | + |
| 59 | + @AfterClass |
| 60 | + public static void bringDownCluster() { |
| 61 | + cluster.close(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testVirtualizeCSV() { |
| 66 | + dvInSysLeaderMemberCommon(PROCEDURE_NOT_ROUTED_ERROR, SYSTEM_DATABASE_NAME, |
| 67 | + (session) -> testCall(session, APOC_DV_INSTALL_QUERY, |
| 68 | + APOC_DV_INSTALL_PARAMS, |
| 69 | + (row) -> assertCatalogContent(row, CSV_TEST_FILE)), APOC_DV_INSTALL_PARAMS |
| 70 | + ); |
| 71 | + |
| 72 | + clusterSession.executeRead(tx -> { |
| 73 | + final Result result = tx.run(APOC_DV_QUERY, |
| 74 | + Map.of(NAME_KEY, CSV_NAME_VALUE, |
| 75 | + APOC_DV_QUERY_PARAMS_KEY, APOC_DV_QUERY_PARAMS, |
| 76 | + CONFIG_KEY, CONFIG_VALUE) |
| 77 | + ); |
| 78 | + |
| 79 | + Node node = result.single().get(NODE_KEY).asNode(); |
| 80 | + assertEquals(NAME_VALUE, node.get(NAME_KEY).asString()); |
| 81 | + assertEquals(AGE_VALUE, node.get(AGE_KEY).asString()); |
| 82 | + assertEquals(List.of(LABELS_VALUE), node.labels()); |
| 83 | + |
| 84 | + return result.consume(); |
| 85 | + } |
| 86 | + ); |
| 87 | + |
| 88 | + clusterSession.executeWrite(tx -> tx.run(CREATE_HOOK_QUERY, CREATE_HOOK_PARAMS).consume()); |
| 89 | + |
| 90 | + clusterSession.executeRead(tx -> { |
| 91 | + final Result result = tx.run(APOC_DV_QUERY_AND_LINK_QUERY, |
| 92 | + map(NAME_KEY, CSV_NAME_VALUE, APOC_DV_QUERY_PARAMS_KEY, APOC_DV_QUERY_PARAMS, RELTYPE_KEY, RELTYPE_VALUE, CONFIG_KEY, CONFIG_VALUE) |
| 93 | + ); |
| 94 | + |
| 95 | + Path path = result.single().get("path").asPath(); |
| 96 | + Node node = path.end(); |
| 97 | + assertEquals(NAME_VALUE, node.get(NAME_KEY).asString()); |
| 98 | + assertEquals(AGE_VALUE, node.get(AGE_KEY).asString()); |
| 99 | + assertEquals(List.of(LABELS_VALUE), node.labels()); |
| 100 | + |
| 101 | + Node hook = path.start(); |
| 102 | + assertEquals(HOOK_NODE_NAME_VALUE, hook.get(NAME_KEY).asString()); |
| 103 | + assertEquals(List.of("Hook"), hook.labels()); |
| 104 | + |
| 105 | + Relationship relationship = path.relationships().iterator().next(); |
| 106 | + assertEquals(hook.elementId(), relationship.startNodeElementId()); |
| 107 | + assertEquals(node.elementId(), relationship.endNodeElementId()); |
| 108 | + assertEquals(RELTYPE_VALUE, relationship.type()); |
| 109 | + |
| 110 | + return result.consume(); |
| 111 | + } |
| 112 | + ); |
| 113 | + |
| 114 | + dvInSysLeaderMemberCommon(PROCEDURE_NOT_ROUTED_ERROR, SYSTEM_DATABASE_NAME, |
| 115 | + (session) -> testCallEmpty(session, APOC_DV_DROP_QUERY, |
| 116 | + APOC_DV_DROP_PARAMS), APOC_DV_DROP_PARAMS |
| 117 | + ); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + private static void dvInSysLeaderMemberCommon(String uuidNotRoutedError, String dbName, Consumer<Session> testDv, Map<String, Object> params) { |
| 122 | + dvInSysLeaderMemberCommon(uuidNotRoutedError, dbName, testDv, false, params); |
| 123 | + } |
| 124 | + |
| 125 | + private static void dvInSysLeaderMemberCommon(String uuidNotRoutedError, String dbName, Consumer<Session> testDv, boolean readOnlyOperation, Map<String, Object> params) { |
| 126 | + final List<Neo4jContainerExtension> members = cluster.getClusterMembers(); |
| 127 | + assertEquals(NUM_CORES, members.size()); |
| 128 | + boolean writeExecuted = false; |
| 129 | + for (Neo4jContainerExtension container: members) { |
| 130 | + // we skip READ_REPLICA members with write operations |
| 131 | + // instead, we consider all members with a read only operations |
| 132 | + final Driver driver = readOnlyOperation |
| 133 | + ? container.getDriver() |
| 134 | + : getDriverIfNotReplica(container); |
| 135 | + if (driver == null) { |
| 136 | + continue; |
| 137 | + } |
| 138 | + Session session = driver.session(SessionConfig.forDatabase(dbName)); |
| 139 | + boolean isWriter = dbIsWriter(dbName, session, getBoltAddress(container)); |
| 140 | + if (isWriter) { |
| 141 | + testDv.accept(session); |
| 142 | + writeExecuted = true; |
| 143 | + } else { |
| 144 | + try { |
| 145 | + testDv.accept(session); |
| 146 | + fail("Should fail because of non leader Data Virtualization addition"); |
| 147 | + } catch (Exception e) { |
| 148 | + String errorMsg = e.getMessage(); |
| 149 | + assertTrue("The actual message is: " + errorMsg, errorMsg.contains(uuidNotRoutedError)); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + assertTrue(writeExecuted); |
| 154 | + } |
| 155 | +} |
0 commit comments