diff --git a/extended-it/src/test/java/apoc/vectordb/QdrantTest.java b/extended-it/src/test/java/apoc/vectordb/QdrantTest.java index 3bce11f137..9e6a7965f6 100644 --- a/extended-it/src/test/java/apoc/vectordb/QdrantTest.java +++ b/extended-it/src/test/java/apoc/vectordb/QdrantTest.java @@ -22,6 +22,7 @@ import static apoc.ml.Prompt.API_KEY_CONF; import static apoc.ml.RestAPIConfig.HEADERS_KEY; import static apoc.util.ExtendedTestUtil.assertFails; +import static apoc.util.ExtendedTestUtil.testResultEventually; import static apoc.util.MapUtil.map; import static apoc.util.TestUtil.testCall; import static apoc.util.TestUtil.testResult; @@ -59,6 +60,7 @@ public class QdrantTest { private static final Map ADMIN_AUTHORIZATION = getAuthHeader(ADMIN_KEY); private static final Map READONLY_AUTHORIZATION = getAuthHeader(READONLY_KEY); private static final Map ADMIN_HEADER_CONF = map(HEADERS_KEY, ADMIN_AUTHORIZATION); + public static final long TIMEOUT = 10L; private static String HOST; @@ -244,7 +246,7 @@ WITH collect(node) as paths @Test public void queryVectors() { - testResult(db, "CALL apoc.vectordb.qdrant.query($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf)", + testResultEventually(db, "CALL apoc.vectordb.qdrant.query($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf)", map("host", HOST, "conf", map(ALL_RESULTS_KEY, true, HEADERS_KEY, ADMIN_AUTHORIZATION)), r -> { Map row = r.next(); @@ -256,12 +258,13 @@ public void queryVectors() { assertLondonResult(row, FALSE); assertNotNull(row.get("score")); assertNotNull(row.get("vector")); - }); + }, + TIMEOUT); } @Test public void queryVectorsWithoutVectorResult() { - testResult(db, "CALL apoc.vectordb.qdrant.query($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf)", + testResultEventually(db, "CALL apoc.vectordb.qdrant.query($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf)", map("host", HOST, "conf", map(HEADERS_KEY, ADMIN_AUTHORIZATION)), r -> { Map row = r.next(); @@ -275,24 +278,26 @@ public void queryVectorsWithoutVectorResult() { assertNotNull(row.get("score")); assertNull(row.get("vector")); assertNull(row.get("id")); - }); + }, + TIMEOUT); } @Test public void queryVectorsWithYield() { - testResult(db, "CALL apoc.vectordb.qdrant.query($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf) YIELD metadata, id", + testResultEventually(db, "CALL apoc.vectordb.qdrant.query($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf) YIELD metadata, id", map("host", HOST, "conf", map(ALL_RESULTS_KEY, true, HEADERS_KEY, ADMIN_AUTHORIZATION) ), r -> { assertBerlinResult(r.next(), FALSE); assertLondonResult(r.next(), FALSE); - }); + }, + TIMEOUT); } @Test public void queryVectorsWithFilter() { - testResult(db, """ + testResultEventually(db, """ CALL apoc.vectordb.qdrant.query($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], { must: [ { key: "city", match: { value: "London" } } ] @@ -303,19 +308,21 @@ public void queryVectorsWithFilter() { ), r -> { assertLondonResult(r.next(), FALSE); - }); + }, + TIMEOUT); } @Test public void queryVectorsWithLimit() { - testResult(db, """ + testResultEventually(db, """ CALL apoc.vectordb.qdrant.query($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 1, $conf) YIELD metadata, id""", map("host", HOST, "conf", map(ALL_RESULTS_KEY, true, HEADERS_KEY, ADMIN_AUTHORIZATION) ), r -> { assertBerlinResult(r.next(), FALSE); - }); + }, + TIMEOUT); } @Test @@ -331,7 +338,7 @@ MAPPING_KEY, map( MODE_KEY, MappingMode.CREATE_IF_MISSING.toString() ) ); - testResult(db, "CALL apoc.vectordb.qdrant.queryAndUpdate($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf)", + testResultEventually(db, "CALL apoc.vectordb.qdrant.queryAndUpdate($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf)", map("host", HOST, "conf", conf), r -> { Map row = r.next(); @@ -343,14 +350,15 @@ MAPPING_KEY, map( assertLondonResult(row, NODE); assertNotNull(row.get("score")); assertNotNull(row.get("vector")); - }); + }, + TIMEOUT); assertNodesCreated(db); testResult(db, "MATCH (n:Test) RETURN properties(n) AS props ORDER BY n.myId", VectorDbTestUtil::vectorEntityAssertions); - testResult(db, "CALL apoc.vectordb.qdrant.queryAndUpdate($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf)", + testResultEventually(db, "CALL apoc.vectordb.qdrant.queryAndUpdate($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf)", map("host", HOST, "conf", conf), r -> { Map row = r.next(); @@ -362,7 +370,8 @@ MAPPING_KEY, map( assertLondonResult(row, NODE); assertNotNull(row.get("score")); assertNotNull(row.get("vector")); - }); + }, + TIMEOUT); assertNodesCreated(db); } @@ -424,8 +433,8 @@ MAPPING_KEY, map(EMBEDDING_KEY, "vect", NODE_LABEL, "Test", ENTITY_KEY, "myId", METADATA_KEY, "foo")); - - testResult(db, "CALL apoc.vectordb.qdrant.queryAndUpdate($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf)", + + testResultEventually(db, "CALL apoc.vectordb.qdrant.queryAndUpdate($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf)", map("host", HOST, "conf", conf), r -> { Map row = r.next(); @@ -437,7 +446,8 @@ MAPPING_KEY, map(EMBEDDING_KEY, "vect", assertLondonResult(row, NODE); assertNotNull(row.get("score")); assertNotNull(row.get("vector")); - }); + }, + TIMEOUT); assertNodesCreated(db); } @@ -455,7 +465,7 @@ MAPPING_KEY, map( ENTITY_KEY, "myId", METADATA_KEY, "foo") ); - testResult(db, "CALL apoc.vectordb.qdrant.queryAndUpdate($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf)", + testResultEventually(db, "CALL apoc.vectordb.qdrant.queryAndUpdate($host, 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, $conf)", map("host", HOST, "conf", conf), r -> { Map row = r.next(); @@ -467,7 +477,8 @@ MAPPING_KEY, map( assertLondonResult(row, REL); assertNotNull(row.get("score")); assertNotNull(row.get("vector")); - }); + }, + TIMEOUT); assertRelsCreated(db); } diff --git a/extended/src/test/java/apoc/gephi/GephiMock.java b/extended/src/test/java/apoc/gephi/GephiMock.java index f3026d536a..b40c91c65f 100644 --- a/extended/src/test/java/apoc/gephi/GephiMock.java +++ b/extended/src/test/java/apoc/gephi/GephiMock.java @@ -3,6 +3,7 @@ import org.mockserver.client.MockServerClient; import org.mockserver.integration.ClientAndServer; import org.mockserver.model.RegexBody; +import org.mockserver.socket.PortFactory; import java.util.Arrays; import java.util.HashSet; @@ -26,9 +27,13 @@ */ public class GephiMock { private final ClientAndServer server; + private final int PORT; + public final String HOST; public GephiMock() { - this.server = ClientAndServer.startClientAndServer(8080); + PORT = PortFactory.findFreePort(); + HOST = "http://localhost:" + PORT; + this.server = ClientAndServer.startClientAndServer(PORT); } public void clearAllExpectations() { @@ -40,7 +45,7 @@ public void shutdown() { } public void mockSuccess(String workspace, GephiEntity... entities) { - new MockServerClient("localhost", 8080) + new MockServerClient("localhost", PORT) .when(request() .withMethod("POST") .withPath("/" + workspace) diff --git a/extended/src/test/java/apoc/gephi/GephiTest.java b/extended/src/test/java/apoc/gephi/GephiTest.java index eb2fa7cf61..87a9d0b13e 100644 --- a/extended/src/test/java/apoc/gephi/GephiTest.java +++ b/extended/src/test/java/apoc/gephi/GephiTest.java @@ -51,8 +51,9 @@ public void testAdd() throws Exception { node(0, "Foo"), node(1, "Bar"), relationship(0, "KNOWS",0, 1)); - testCall(db, "MATCH p = (:Foo)-->() WITH p CALL apoc.gephi.add(null,$workspace,p) yield nodes, relationships, format return *", - map("workspace", GEPHI_WORKSPACE), + testCall(db, "MATCH p = (:Foo)-->() WITH p CALL apoc.gephi.add($host,$workspace,p) yield nodes, relationships, format return *", + map("host", gephiMock.HOST, + "workspace", GEPHI_WORKSPACE), r -> { assertEquals(2L, r.get("nodes")); assertEquals(1L, r.get("relationships")); @@ -67,8 +68,9 @@ public void testWeightParameter() throws Exception { node(0, "Foo"), node(1, "Bar"), relationship(0, "KNOWS",0, 1, "7.2")); - testCall(db, "MATCH p = (:Foo)-->() WITH p CALL apoc.gephi.add(null,$workspace,p,'weight') yield nodes, relationships, format return *", - map("workspace", GEPHI_WORKSPACE), + testCall(db, "MATCH p = (:Foo)-->() WITH p CALL apoc.gephi.add($host,$workspace,p,'weight') yield nodes, relationships, format return *", + map("host", gephiMock.HOST, + "workspace", GEPHI_WORKSPACE), r -> { assertEquals(2L, r.get("nodes")); assertEquals(1L, r.get("relationships")); @@ -83,8 +85,9 @@ public void testWrongWeightParameter() throws Exception { node(0, "Foo"), node(1, "Bar"), relationship(0, "KNOWS",0, 1)); - testCall(db, "MATCH p = (:Foo)-->() WITH p CALL apoc.gephi.add(null,$workspace,p,'test') yield nodes, relationships, format return *", - map("workspace", GEPHI_WORKSPACE), + testCall(db, "MATCH p = (:Foo)-->() WITH p CALL apoc.gephi.add($host,$workspace,p,'test') yield nodes, relationships, format return *", + map("host", gephiMock.HOST, + "workspace", GEPHI_WORKSPACE), r -> { assertEquals(2L, r.get("nodes")); assertEquals(1L, r.get("relationships")); @@ -99,8 +102,9 @@ public void testRightExportParameter() throws Exception { node(0, "Foo"), node(1, "Bar"), relationship(0, "KNOWS",0, 1, "7.2", Set.of("foo"))); - testCall(db, "MATCH p = (:Foo)-->() WITH p CALL apoc.gephi.add(null,$workspace,p,'weight',['foo']) yield nodes, relationships, format return *", - map("workspace", GEPHI_WORKSPACE), + testCall(db, "MATCH p = (:Foo)-->() WITH p CALL apoc.gephi.add($host,$workspace,p,'weight',['foo']) yield nodes, relationships, format return *", + map("host", gephiMock.HOST, + "workspace", GEPHI_WORKSPACE), r -> { assertEquals(2L, r.get("nodes")); assertEquals(1L, r.get("relationships")); @@ -115,8 +119,9 @@ public void testWrongExportParameter() throws Exception { node(0, "Foo"), node(1, "Bar"), relationship(0, "KNOWS",0, 1, "7.2")); - testCall(db, "MATCH p = (:Foo)-->() WITH p CALL apoc.gephi.add(null,$workspace,p,'weight',['faa','fee']) yield nodes, relationships, format return *", - map("workspace", GEPHI_WORKSPACE), + testCall(db, "MATCH p = (:Foo)-->() WITH p CALL apoc.gephi.add($host,$workspace,p,'weight',['faa','fee']) yield nodes, relationships, format return *", + map("host", gephiMock.HOST, + "workspace", GEPHI_WORKSPACE), r -> { assertEquals(2L, r.get("nodes")); assertEquals(1L, r.get("relationships")); @@ -131,8 +136,9 @@ public void reservedExportParameter() throws Exception { node(0, "Foo"), node(1, "Bar"), relationship(0, "KNOWS",0, 1, "7.2")); - testCall(db, "MATCH p = (:Foo)-->() WITH p CALL apoc.gephi.add(null,$workspace,p,'weight',['directed','label']) yield nodes, relationships, format return *", - map("workspace", GEPHI_WORKSPACE), + testCall(db, "MATCH p = (:Foo)-->() WITH p CALL apoc.gephi.add($host,$workspace,p,'weight',['directed','label']) yield nodes, relationships, format return *", + map("host", gephiMock.HOST, + "workspace", GEPHI_WORKSPACE), r -> { assertEquals(2L, r.get("nodes")); assertEquals(1L, r.get("relationships")); diff --git a/extended/src/test/java/apoc/load/LoadCsvTest.java b/extended/src/test/java/apoc/load/LoadCsvTest.java index c8e77d04fb..c33f15907f 100644 --- a/extended/src/test/java/apoc/load/LoadCsvTest.java +++ b/extended/src/test/java/apoc/load/LoadCsvTest.java @@ -10,6 +10,7 @@ import org.mockserver.client.MockServerClient; import org.mockserver.integration.ClientAndServer; import org.mockserver.model.Header; +import org.mockserver.socket.PortFactory; import org.neo4j.configuration.GraphDatabaseSettings; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.QueryExecutionException; @@ -47,7 +48,8 @@ public class LoadCsvTest { private static ClientAndServer mockServer; - + private static int PORT; + private static final List> RESPONSE_BODY = List.of( Map.of("headFoo", "one", "headBar", "two"), Map.of("headFoo", "three", "headBar", "four"), @@ -56,7 +58,8 @@ public class LoadCsvTest { @BeforeClass public static void startServer() { - mockServer = startClientAndServer(1080); + PORT = PortFactory.findFreePort(); + mockServer = startClientAndServer(PORT); } @AfterClass @@ -492,7 +495,7 @@ public void testLoadCsvWithUserPassInUrl() throws JsonProcessingException { String userPass = "user:password"; String token = Util.encodeUserColonPassToBase64(userPass); - new MockServerClient("localhost", 1080) + new MockServerClient("localhost", PORT) .when( request() .withPath("/docs/csv") @@ -509,7 +512,7 @@ public void testLoadCsvWithUserPassInUrl() throws JsonProcessingException { ); testResult(db, "CALL apoc.load.csv($url, {results:['map']}) YIELD map", - map("url", "http://" + userPass + "@localhost:1080/docs/csv"), + map("url", "http://" + userPass + "@localhost:" + PORT + "/docs/csv"), (row) -> assertEquals(RESPONSE_BODY, row.stream().map(i->i.get("map")).collect(Collectors.toList())) ); } @@ -519,7 +522,7 @@ public void testLoadCsvParamsWithUserPassInUrl() throws JsonProcessingException String userPass = "user:password"; String token = Util.encodeUserColonPassToBase64(userPass); - new MockServerClient("localhost", 1080) + new MockServerClient("localhost", PORT) .when( request() .withMethod("POST") @@ -537,7 +540,7 @@ public void testLoadCsvParamsWithUserPassInUrl() throws JsonProcessingException ); testResult(db, "CALL apoc.load.csvParams($url, $header, $payload, {results:['map','list','stringMap','strings']})", - map("url", "http://" + userPass + "@localhost:1080/docs/csv", + map("url", "http://" + userPass + "@localhost:" + PORT +"/docs/csv", "header", map("method", "POST"), "payload", "{\"query\":\"pagecache\",\"version\":\"3.5\"}"), (row) -> assertEquals(RESPONSE_BODY, row.stream().map(i->i.get("map")).collect(Collectors.toList())) @@ -549,7 +552,7 @@ public void testLoadCsvParamsWithBasicAuth() throws JsonProcessingException { String userPass = "user:password"; String token = Util.encodeUserColonPassToBase64(userPass); - new MockServerClient("localhost", 1080) + new MockServerClient("localhost", PORT) .when( request() .withMethod("POST") @@ -568,7 +571,7 @@ public void testLoadCsvParamsWithBasicAuth() throws JsonProcessingException { ); testResult(db, "CALL apoc.load.csvParams($url, $header, $payload, {results:['map','list','stringMap','strings']})", - map("url", "http://localhost:1080/docs/csv", + map("url", "http://localhost:" + PORT + "/docs/csv", "header", map("method", "POST", "Authorization", "Basic " + token, "Content-Type", "application/json"), diff --git a/extended/src/test/java/apoc/ml/WatsonTest.java b/extended/src/test/java/apoc/ml/WatsonTest.java index 111e0188d8..003830a312 100644 --- a/extended/src/test/java/apoc/ml/WatsonTest.java +++ b/extended/src/test/java/apoc/ml/WatsonTest.java @@ -9,6 +9,7 @@ import org.junit.Test; import org.mockserver.integration.ClientAndServer; import org.mockserver.model.Header; +import org.mockserver.socket.PortFactory; import org.neo4j.test.rule.DbmsRule; import org.neo4j.test.rule.ImpermanentDbmsRule; @@ -42,17 +43,18 @@ public class WatsonTest { @BeforeClass public static void startServer() throws Exception { + int port = PortFactory.findFreePort(); TestUtil.registerProcedure(db, Watson.class); String path = "/generation/text"; - apocConfig().setProperty(APOC_ML_WATSON_URL, "http://localhost:1080" + path); + apocConfig().setProperty(APOC_ML_WATSON_URL, "http://localhost:" + port + path); apocConfig().setProperty(APOC_IMPORT_FILE_ENABLED, true); apocConfig().setProperty(APOC_ML_WATSON_PROJECT_ID, "fakeProjectId"); File urlFileName = new File(getUrlFileName("watson.json").getFile()); String body = FileUtils.readFileToString(urlFileName, UTF_8); - mockServer = startClientAndServer(1080); + mockServer = startClientAndServer(port); mockServer.when( request() .withMethod("POST") diff --git a/extended/src/test/java/apoc/util/ExtendedTestUtil.java b/extended/src/test/java/apoc/util/ExtendedTestUtil.java index c19a04f859..9aeb6747a5 100644 --- a/extended/src/test/java/apoc/util/ExtendedTestUtil.java +++ b/extended/src/test/java/apoc/util/ExtendedTestUtil.java @@ -126,9 +126,13 @@ public static void testRetryCallEventually(GraphDatabaseService db, String call, * but with multiple results */ public static void testResultEventually(GraphDatabaseService db, String call, Consumer resultConsumer, long timeout) { + testResultEventually(db, call, Map.of(), resultConsumer, timeout); + } + + public static void testResultEventually(GraphDatabaseService db, String call, Map params, Consumer resultConsumer, long timeout) { assertEventually(() -> { try { - return db.executeTransactionally(call, Map.of(), r -> { + return db.executeTransactionally(call, params, r -> { resultConsumer.accept(r); return true; });