davidzollo commented on code in PR #10002:
URL: https://github.com/apache/seatunnel/pull/10002#discussion_r2503806207


##########
seatunnel-e2e/seatunnel-connector-v2-e2e/connector-hugegraph-e2e/src/test/java/org/apache/seatunnel/e2e/connector/hugegraph/HugeGraphIT.java:
##########
@@ -0,0 +1,688 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.seatunnel.e2e.connector.hugegraph;
+
+import org.apache.seatunnel.api.table.type.RowKind;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import 
org.apache.seatunnel.connectors.seatunnel.hugegraph.config.HugeGraphSinkConfig;
+import 
org.apache.seatunnel.connectors.seatunnel.hugegraph.config.MappingConfig;
+import org.apache.seatunnel.connectors.seatunnel.hugegraph.config.SchemaConfig;
+import 
org.apache.seatunnel.connectors.seatunnel.hugegraph.config.SchemaConfig.SourceTargetConfig;
+import 
org.apache.seatunnel.connectors.seatunnel.hugegraph.sink.HugeGraphSinkWriter;
+
+import org.apache.hugegraph.driver.HugeClient;
+import org.apache.hugegraph.exception.ServerException;
+import org.apache.hugegraph.structure.constant.IdStrategy;
+import org.apache.hugegraph.structure.graph.Edge;
+import org.apache.hugegraph.structure.graph.Vertex;
+
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.wait.strategy.Wait;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+import org.testcontainers.utility.DockerImageName;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+@Testcontainers
+public class HugeGraphIT {
+
+    private static final String HUGE_GRAPH_IMAGE = 
"hugegraph/hugegraph:latest";
+    private static final String GRAPH_NAME = "hugegraph";
+    private static final String VERTEX_LABEL_PERSON = "person_for_test";
+    private static final String VERTEX_LABEL_ALL_TYPES = 
"vertex_all_types_for_test";
+    private static final SeaTunnelRowType SEATUNNEL_ROW_TYPE =
+            new SeaTunnelRowType(
+                    new String[] {"name", "age"},
+                    new SeaTunnelDataType<?>[] {
+                        
org.apache.seatunnel.api.table.type.BasicType.STRING_TYPE,
+                        org.apache.seatunnel.api.table.type.BasicType.INT_TYPE
+                    });
+    private static final DateTimeFormatter formatter =
+            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
+    private static HugeClient hugeClient;
+
+    @Container
+    private static final GenericContainer<?> HUGE_GRAPH_CONTAINER =
+            new GenericContainer<>(DockerImageName.parse(HUGE_GRAPH_IMAGE))
+                    .withExposedPorts(8080, 8182)
+                    
.waitingFor(Wait.forHttp("/graphs").forPort(8080).forStatusCode(200))
+                    .withStartupTimeout(Duration.ofMinutes(3));
+
+    @BeforeAll
+    public static void setup() {
+        String host = HUGE_GRAPH_CONTAINER.getHost();
+        Integer port = HUGE_GRAPH_CONTAINER.getMappedPort(8080);
+        String url = String.format("http://%s:%d";, host, port);
+        hugeClient = HugeClient.builder(url, GRAPH_NAME).build();
+        setupSchema();
+    }
+
+    @AfterAll
+    public static void cleanup() {
+        if (hugeClient != null) {
+            hugeClient.close();
+        }
+    }
+
+    @BeforeEach
+    public void clearGraph() {
+        // Clear all vertices and edges before each test using 
GraphsManager.clearGraph()
+        try {
+            hugeClient.graphs().clearGraph(GRAPH_NAME, "I'm sure to delete all 
data");
+            // After clearing, need to recreate schema
+            setupSchema();
+        } catch (Exception e) {
+            // Ignore errors during clear
+        }
+    }
+
+    private static void setupSchema() {
+        hugeClient.schema().propertyKey("name").asText().ifNotExist().create();
+        hugeClient.schema().propertyKey("age").asInt().ifNotExist().create();
+        hugeClient
+                .schema()
+                .vertexLabel(VERTEX_LABEL_PERSON)
+                .idStrategy(IdStrategy.PRIMARY_KEY)
+                .primaryKeys("name")
+                .properties("name", "age")
+                .ifNotExist()
+                .create();
+
+        
hugeClient.schema().propertyKey("duration").asFloat().ifNotExist().create();
+        hugeClient
+                .schema()
+                .edgeLabel("knows")
+                .sourceLabel(VERTEX_LABEL_PERSON)
+                .targetLabel(VERTEX_LABEL_PERSON)
+                .properties("duration")
+                .ifNotExist()
+                .create();
+
+        // New schema for all types vertex
+        
hugeClient.schema().propertyKey("id_field").asText().ifNotExist().create();
+        
hugeClient.schema().propertyKey("prop_string").asText().ifNotExist().create();
+        
hugeClient.schema().propertyKey("prop_long").asLong().ifNotExist().create();
+        
hugeClient.schema().propertyKey("prop_double").asDouble().ifNotExist().create();
+        
hugeClient.schema().propertyKey("prop_boolean").asBoolean().ifNotExist().create();
+        
hugeClient.schema().propertyKey("prop_date").asDate().ifNotExist().create();
+
+        hugeClient
+                .schema()
+                .vertexLabel(VERTEX_LABEL_ALL_TYPES)
+                .idStrategy(IdStrategy.CUSTOMIZE_STRING)
+                .properties(
+                        "id_field",
+                        "prop_string",
+                        "prop_long",
+                        "prop_double",
+                        "prop_boolean",
+                        "prop_date")
+                .ifNotExist()
+                .create();
+
+        hugeClient.schema().propertyKey("lang").asText().ifNotExist().create();
+
+        hugeClient
+                .schema()
+                .vertexLabel("person_pk_for_edge")
+                .idStrategy(IdStrategy.PRIMARY_KEY)
+                .primaryKeys("name")
+                .properties("name")
+                .ifNotExist()
+                .create();
+
+        hugeClient
+                .schema()
+                .vertexLabel("software_cs_for_edge")
+                .idStrategy(IdStrategy.CUSTOMIZE_STRING)
+                .properties("lang")
+                .ifNotExist()
+                .create();
+
+        hugeClient
+                .schema()
+                .edgeLabel("transfer")
+                .sourceLabel("person_pk_for_edge")
+                .targetLabel("software_cs_for_edge")
+                .properties("prop_string", "prop_long", "prop_double", 
"prop_boolean", "prop_date")
+                .ifNotExist()
+                .create();
+    }
+
+    private HugeGraphSinkWriter createSinkWriter(
+            SchemaConfig schemaConfig, SeaTunnelRowType rowType) throws 
IOException {
+        HugeGraphSinkConfig config = new HugeGraphSinkConfig();
+        config.setHost(HUGE_GRAPH_CONTAINER.getHost());
+        config.setPort(HUGE_GRAPH_CONTAINER.getMappedPort(8080));
+        config.setGraphName(GRAPH_NAME);
+        config.setSchemaConfig(schemaConfig);
+        return new HugeGraphSinkWriter(config, rowType);
+    }
+
+    @Test
+    public void testInsert() throws IOException {
+        SchemaConfig schemaConfig = new SchemaConfig();
+        schemaConfig.setType(SchemaConfig.LabelType.VERTEX);
+        schemaConfig.setLabel(VERTEX_LABEL_PERSON);
+        schemaConfig.setIdStrategy(IdStrategy.PRIMARY_KEY);
+        schemaConfig.setIdFields(Collections.singletonList("name"));
+
+        try {
+            HugeGraphSinkWriter writer = createSinkWriter(schemaConfig, 
SEATUNNEL_ROW_TYPE);
+            SeaTunnelRow row = new SeaTunnelRow(new Object[] {"marko", 29});
+            row.setRowKind(RowKind.INSERT);
+            writer.write(row);
+            writer.close();
+        } finally {
+
+        }
+
+        // Verify using REST API
+        Map<String, Object> properties = new HashMap<>();
+        properties.put("name", "marko");
+        List<Vertex> vertices =
+                hugeClient.graph().listVertices(VERTEX_LABEL_PERSON, 
properties, 10);
+        assertEquals(1, vertices.size());
+        assertEquals(29, vertices.get(0).property("age"));
+    }
+
+    @Test
+    public void testEdgeInsert() throws IOException {
+        // 1. Insert source and target vertices
+        Vertex marko =
+                new Vertex(VERTEX_LABEL_PERSON).property("name", 
"marko").property("age", 29);
+        Vertex david =
+                new Vertex(VERTEX_LABEL_PERSON).property("name", 
"david").property("age", 30);
+        hugeClient.graph().addVertex(marko);
+        hugeClient.graph().addVertex(david);
+
+        // 2. Define edge row type
+        SeaTunnelRowType edgeRowType =
+                new SeaTunnelRowType(
+                        new String[] {"src_name", "tgt_name", "duration"},
+                        new SeaTunnelDataType<?>[] {
+                            
org.apache.seatunnel.api.table.type.BasicType.STRING_TYPE,
+                            
org.apache.seatunnel.api.table.type.BasicType.STRING_TYPE,
+                            
org.apache.seatunnel.api.table.type.BasicType.FLOAT_TYPE
+                        });
+
+        // 3. Configure SchemaConfig for edge
+        SchemaConfig schemaConfig = new SchemaConfig();
+        schemaConfig.setType(SchemaConfig.LabelType.EDGE);
+        schemaConfig.setLabel("knows");
+
+        SourceTargetConfig sourceConfig = new SourceTargetConfig();
+        sourceConfig.setLabel(VERTEX_LABEL_PERSON);
+        sourceConfig.setIdFields(Collections.singletonList("name"));
+
+        SourceTargetConfig targetConfig = new SourceTargetConfig();
+        targetConfig.setLabel(VERTEX_LABEL_PERSON);
+        targetConfig.setIdFields(Collections.singletonList("name"));
+
+        schemaConfig.setSourceConfig(sourceConfig);
+        schemaConfig.setTargetConfig(targetConfig);
+
+        MappingConfig mappingConfig = new MappingConfig();
+        Map<String, String> map = new HashMap<>();
+        map.put("duration", "duration");
+        Map<String, String> sourceMap = new HashMap<>();
+        sourceMap.put("src_name", "name");
+        Map<String, String> targetMap = new HashMap<>();
+        targetMap.put("tgt_name", "name");
+        mappingConfig.setFieldMapping(map);
+        mappingConfig.setSourceIdMapping(sourceMap);
+        mappingConfig.setTargetIdMapping(targetMap);
+
+        schemaConfig.setMapping(mappingConfig);
+
+        try {
+            // 4. Create writer with new row type
+            HugeGraphSinkWriter writer = createSinkWriter(schemaConfig, 
edgeRowType);
+            // 5. Create and write row
+            SeaTunnelRow row = new SeaTunnelRow(new Object[] {"marko", 
"david", 1.5});
+            row.setRowKind(RowKind.INSERT);
+            writer.write(row);
+            writer.close();
+        } finally {
+        }
+
+        // 6. Verify edge creation
+        List<Edge> edges = hugeClient.graph().listEdges("knows");
+        assertEquals(1, edges.size());
+        Edge createdEdge = edges.get(0);
+        assertEquals(1.5, createdEdge.property("duration"));
+
+        // Also verify source and target
+        Vertex sourceVertex = 
hugeClient.graph().getVertex(createdEdge.sourceId());
+        Vertex targetVertex = 
hugeClient.graph().getVertex(createdEdge.targetId());
+        assertEquals("marko", sourceVertex.property("name"));
+        assertEquals("david", targetVertex.property("name"));
+    }
+
+    @Test
+    public void testUpdate() throws IOException {
+        // First, insert a vertex using REST API
+        Vertex vadas = new Vertex(VERTEX_LABEL_PERSON);
+        vadas.property("name", "vadas");
+        vadas.property("age", 27);
+        hugeClient.graph().addVertex(vadas);
+
+        MappingConfig mappingConfig = new MappingConfig();
+        Map<String, String> map = new HashMap<>();
+        map.put("name", "name");
+        map.put("age", "age");
+        mappingConfig.setFieldMapping(map);
+        SchemaConfig schemaConfig = new SchemaConfig();
+        schemaConfig.setType(SchemaConfig.LabelType.VERTEX);
+        schemaConfig.setLabel(VERTEX_LABEL_PERSON);
+        schemaConfig.setIdStrategy(IdStrategy.PRIMARY_KEY);
+        schemaConfig.setIdFields(Collections.singletonList("name"));
+        schemaConfig.setMapping(mappingConfig);
+
+        try {
+            HugeGraphSinkWriter writer = createSinkWriter(schemaConfig, 
SEATUNNEL_ROW_TYPE);
+            SeaTunnelRow row = new SeaTunnelRow(new Object[] {"vadas", 28});
+            row.setRowKind(RowKind.UPDATE_AFTER);
+            writer.write(row);
+            writer.close();
+        } finally {
+        }
+
+        // Verify using REST API
+        Map<String, Object> properties = new HashMap<>();
+        properties.put("name", "vadas");
+        List<Vertex> vertices =
+                hugeClient.graph().listVertices(VERTEX_LABEL_PERSON, 
properties, 10);
+        assertEquals(1, vertices.size());
+        assertEquals(28, vertices.get(0).property("age"));
+    }
+
+    @Test
+    public void testEdgeDelete() throws IOException {
+        // 1. Insert vertices and an edge to be deleted
+        Vertex marko =
+                new Vertex(VERTEX_LABEL_PERSON).property("name", 
"marko").property("age", 29);
+        Vertex david =
+                new Vertex(VERTEX_LABEL_PERSON).property("name", 
"david").property("age", 30);
+        marko = hugeClient.graph().addVertex(marko);
+        david = hugeClient.graph().addVertex(david);
+
+        Edge edge = new 
Edge("knows").source(marko).target(david).property("duration", 12.3);
+        hugeClient.graph().addEdge(edge);
+        Edge edge_1 = new 
Edge("knows").source(marko).target(david).property("duration", 22.0);
+        hugeClient.graph().addEdge(edge_1);
+
+        // Verify it exists first
+        assertEquals(1, hugeClient.graph().listEdges("knows").size());
+

Review Comment:
   I'm not familiar with graphs. Shouldn't this be 2 when `knows` adds two 
edges?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to