From 55b0ed34072c038bc364bc712ea4b4dc82403133 Mon Sep 17 00:00:00 2001 From: diaohancai <550630588@qq.com> Date: Thu, 25 Jan 2024 23:37:20 +0800 Subject: [PATCH] chore: add IdUtilTest unit test --- .../hugegraph/computer/core/util/IdUtil.java | 9 +++-- .../computer/core/graph/id/IdFactoryTest.java | 8 ++++ .../computer/core/util/IdUtilTest.java | 38 +++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 computer-test/src/main/java/org/apache/hugegraph/computer/core/util/IdUtilTest.java diff --git a/computer-api/src/main/java/org/apache/hugegraph/computer/core/util/IdUtil.java b/computer-api/src/main/java/org/apache/hugegraph/computer/core/util/IdUtil.java index 3a6e442f2..49445695e 100644 --- a/computer-api/src/main/java/org/apache/hugegraph/computer/core/util/IdUtil.java +++ b/computer-api/src/main/java/org/apache/hugegraph/computer/core/util/IdUtil.java @@ -17,6 +17,7 @@ package org.apache.hugegraph.computer.core.util; +import java.util.UUID; import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; @@ -41,9 +42,9 @@ public static Id parseId(String idStr) { } if (StringUtils.isNumeric(idStr)) { - return IdFactory.parseId(IdType.LONG, idStr); + return IdFactory.parseId(IdType.LONG, Long.valueOf(idStr)); } else if (P.matcher(idStr).matches()) { - return IdFactory.parseId(IdType.UUID, idStr); + return IdFactory.parseId(IdType.UUID, UUID.fromString(idStr)); } else { return IdFactory.parseId(IdType.UTF8, idStr); } @@ -61,9 +62,9 @@ public static Id parseId(IdCategory idCategory, String idStr) { switch (idCategory) { case NUMBER: - return IdFactory.parseId(IdType.LONG, idStr); + return IdFactory.parseId(IdType.LONG, Long.valueOf(idStr)); case UUID: - return IdFactory.parseId(IdType.UUID, idStr); + return IdFactory.parseId(IdType.UUID, UUID.fromString(idStr)); default: return IdFactory.parseId(IdType.UTF8, idStr); } diff --git a/computer-test/src/main/java/org/apache/hugegraph/computer/core/graph/id/IdFactoryTest.java b/computer-test/src/main/java/org/apache/hugegraph/computer/core/graph/id/IdFactoryTest.java index c95ea8bf9..6fa03fc1b 100644 --- a/computer-test/src/main/java/org/apache/hugegraph/computer/core/graph/id/IdFactoryTest.java +++ b/computer-test/src/main/java/org/apache/hugegraph/computer/core/graph/id/IdFactoryTest.java @@ -51,4 +51,12 @@ public void testCreateIdFromType() { Assert.assertEquals(BytesId.of(new UUID(0L, 0L)), IdFactory.createId(IdType.UUID)); } + + @Test + public void testParseId() { + UUID uuid = UUID.fromString("3b676b77-c484-4ba6-b627-8c040bc42863"); + Assert.assertEquals(IdType.LONG, IdFactory.parseId(IdType.LONG, 222).idType()); + Assert.assertEquals(IdType.UTF8, IdFactory.parseId(IdType.UTF8, "aaa222").idType()); + Assert.assertEquals(IdType.UUID, IdFactory.parseId(IdType.UUID, uuid).idType()); + } } diff --git a/computer-test/src/main/java/org/apache/hugegraph/computer/core/util/IdUtilTest.java b/computer-test/src/main/java/org/apache/hugegraph/computer/core/util/IdUtilTest.java new file mode 100644 index 000000000..4de0e2beb --- /dev/null +++ b/computer-test/src/main/java/org/apache/hugegraph/computer/core/util/IdUtilTest.java @@ -0,0 +1,38 @@ +/* + * 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.hugegraph.computer.core.util; + +import org.apache.hugegraph.computer.core.graph.id.IdCategory; +import org.apache.hugegraph.computer.core.graph.id.IdType; +import org.apache.hugegraph.testutil.Assert; +import org.junit.Test; + +public class IdUtilTest { + + @Test + public void testParseId() { + String uuid = "3b676b77-c484-4ba6-b627-8c040bc42863"; + Assert.assertEquals(IdType.LONG, IdUtil.parseId("222").idType()); + Assert.assertEquals(IdType.UTF8, IdUtil.parseId("aaa222").idType()); + Assert.assertEquals(IdType.UUID, IdUtil.parseId(uuid).idType()); + + Assert.assertEquals(IdType.LONG, IdUtil.parseId(IdCategory.NUMBER, "222").idType()); + Assert.assertEquals(IdType.UTF8, IdUtil.parseId(IdCategory.STRING, "aaa222").idType()); + Assert.assertEquals(IdType.UUID, IdUtil.parseId(IdCategory.UUID, uuid).idType()); + } +}