|
| 1 | +package com.alibaba.datax.example.neo4j; |
| 2 | + |
| 3 | +import com.alibaba.datax.example.ExampleContainer; |
| 4 | +import com.alibaba.datax.example.util.PathUtil; |
| 5 | +import org.junit.After; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.neo4j.driver.*; |
| 10 | +import org.neo4j.driver.types.Node; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | +import org.testcontainers.containers.GenericContainer; |
| 14 | +import org.testcontainers.containers.Network; |
| 15 | +import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 16 | +import org.testcontainers.lifecycle.Startables; |
| 17 | +import org.testcontainers.shaded.org.awaitility.Awaitility; |
| 18 | +import org.testcontainers.utility.DockerImageName; |
| 19 | +import org.testcontainers.utility.DockerLoggerFactory; |
| 20 | + |
| 21 | +import java.net.URI; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import java.util.stream.Stream; |
| 25 | + |
| 26 | +/** |
| 27 | + * {@code Author} FuYouJ |
| 28 | + * {@code Date} 2023/8/19 21:48 |
| 29 | + */ |
| 30 | + |
| 31 | +public class StreamReader2Neo4jWriterTest { |
| 32 | + private static final Logger LOGGER = LoggerFactory.getLogger(StreamReader2Neo4jWriterTest.class); |
| 33 | + private static final String CONTAINER_IMAGE = "neo4j:5.9.0"; |
| 34 | + |
| 35 | + private static final String CONTAINER_HOST = "neo4j-host"; |
| 36 | + private static final int HTTP_PORT = 7474; |
| 37 | + private static final int BOLT_PORT = 7687; |
| 38 | + private static final String CONTAINER_NEO4J_USERNAME = "neo4j"; |
| 39 | + private static final String CONTAINER_NEO4J_PASSWORD = "Test@12343"; |
| 40 | + private static final URI CONTAINER_URI = URI.create("neo4j://localhost:" + BOLT_PORT); |
| 41 | + |
| 42 | + protected static final Network NETWORK = Network.newNetwork(); |
| 43 | + |
| 44 | + private GenericContainer<?> container; |
| 45 | + protected Driver neo4jDriver; |
| 46 | + protected Session neo4jSession; |
| 47 | + private static final int CHANNEL = 5; |
| 48 | + private static final int READER_NUM = 10; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void init() { |
| 52 | + DockerImageName imageName = DockerImageName.parse(CONTAINER_IMAGE); |
| 53 | + container = |
| 54 | + new GenericContainer<>(imageName) |
| 55 | + .withNetwork(NETWORK) |
| 56 | + .withNetworkAliases(CONTAINER_HOST) |
| 57 | + .withExposedPorts(HTTP_PORT, BOLT_PORT) |
| 58 | + .withEnv( |
| 59 | + "NEO4J_AUTH", |
| 60 | + CONTAINER_NEO4J_USERNAME + "/" + CONTAINER_NEO4J_PASSWORD) |
| 61 | + .withEnv("apoc.export.file.enabled", "true") |
| 62 | + .withEnv("apoc.import.file.enabled", "true") |
| 63 | + .withEnv("apoc.import.file.use_neo4j_config", "true") |
| 64 | + .withEnv("NEO4J_PLUGINS", "[\"apoc\"]") |
| 65 | + .withLogConsumer( |
| 66 | + new Slf4jLogConsumer( |
| 67 | + DockerLoggerFactory.getLogger(CONTAINER_IMAGE))); |
| 68 | + container.setPortBindings( |
| 69 | + Arrays.asList( |
| 70 | + String.format("%s:%s", HTTP_PORT, HTTP_PORT), |
| 71 | + String.format("%s:%s", BOLT_PORT, BOLT_PORT))); |
| 72 | + Startables.deepStart(Stream.of(container)).join(); |
| 73 | + LOGGER.info("container started"); |
| 74 | + Awaitility.given() |
| 75 | + .ignoreExceptions() |
| 76 | + .await() |
| 77 | + .atMost(30, TimeUnit.SECONDS) |
| 78 | + .untilAsserted(this::initConnection); |
| 79 | + } |
| 80 | + |
| 81 | + //在neo4jWriter模块使用Example测试整个job,方便发现整个流程的代码问题 |
| 82 | + @Test |
| 83 | + public void streamReader2Neo4j() { |
| 84 | + |
| 85 | + deleteHistoryIfExist(); |
| 86 | + |
| 87 | + String path = "/streamreader2neo4j.json"; |
| 88 | + String jobPath = PathUtil.getAbsolutePathFromClassPath(path); |
| 89 | + |
| 90 | + ExampleContainer.start(jobPath); |
| 91 | + |
| 92 | + //根据channel和reader的mock数据,校验结果集是否符合预期 |
| 93 | + verifyWriteResult(); |
| 94 | + } |
| 95 | + |
| 96 | + private void deleteHistoryIfExist() { |
| 97 | + String query = "match (n:StreamReader) return n limit 1"; |
| 98 | + String delete = "match (n:StreamReader) delete n"; |
| 99 | + if (neo4jSession.run(query).hasNext()) { |
| 100 | + neo4jSession.run(delete); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void verifyWriteResult() { |
| 105 | + int total = CHANNEL * READER_NUM; |
| 106 | + String query = "match (n:StreamReader) return n"; |
| 107 | + Result run = neo4jSession.run(query); |
| 108 | + int count = 0; |
| 109 | + while (run.hasNext()) { |
| 110 | + Record record = run.next(); |
| 111 | + Node node = record.get("n").asNode(); |
| 112 | + if (node.hasLabel("StreamReader")) { |
| 113 | + count++; |
| 114 | + } |
| 115 | + } |
| 116 | + Assert.assertEquals(count, total); |
| 117 | + } |
| 118 | + @After |
| 119 | + public void destroy() { |
| 120 | + if (neo4jSession != null) { |
| 121 | + neo4jSession.close(); |
| 122 | + } |
| 123 | + if (neo4jDriver != null) { |
| 124 | + neo4jDriver.close(); |
| 125 | + } |
| 126 | + if (container != null) { |
| 127 | + container.close(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private void initConnection() { |
| 132 | + neo4jDriver = |
| 133 | + GraphDatabase.driver( |
| 134 | + CONTAINER_URI, |
| 135 | + AuthTokens.basic(CONTAINER_NEO4J_USERNAME, CONTAINER_NEO4J_PASSWORD)); |
| 136 | + neo4jSession = neo4jDriver.session(SessionConfig.forDatabase("neo4j")); |
| 137 | + } |
| 138 | +} |
0 commit comments