forked from neo4j-contrib/neo4j-apoc-procedures
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
16 changed files
with
345 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package apoc.uuid; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.Base64; | ||
import java.util.UUID; | ||
|
||
public class UuidUtil { | ||
|
||
public static String fromHexToBase64(String hexUuid) { | ||
var uuid = UUID.fromString(hexUuid); | ||
return generateBase64Uuid(uuid); | ||
} | ||
|
||
public static String fromBase64ToHex(String base64Uuid) { | ||
if (base64Uuid == null) { | ||
throw new NullPointerException(); | ||
} | ||
if (base64Uuid.isBlank()) { | ||
throw new IllegalStateException("Expected not empty UUID value"); | ||
} | ||
|
||
String valueForConversion; | ||
// check if Base64 text ends with '==' already (Base64 alignment) | ||
if (base64Uuid.endsWith("==")) { | ||
if (base64Uuid.length() != 24) { | ||
throw new IllegalStateException("Invalid UUID length. Expected 24 characters"); | ||
} | ||
valueForConversion = base64Uuid; | ||
} else { | ||
if (base64Uuid.length() != 22) { | ||
throw new IllegalStateException("Invalid UUID length. Expected 22 characters"); | ||
} | ||
valueForConversion = base64Uuid + "=="; | ||
} | ||
|
||
var buffer = Base64.getDecoder().decode(valueForConversion); | ||
|
||
// Generate UUID from 16 byte buffer | ||
long msb = 0L; | ||
for (int i=0; i < 8; ++i) { | ||
msb <<= 8; | ||
msb |= (buffer[i] & 0xFF); | ||
} | ||
var lsb = 0L; | ||
for (int i=8; i < 16; ++i) { | ||
lsb <<= 8; | ||
lsb |= (buffer[i] & 0xFF); | ||
} | ||
|
||
var uuid = new UUID(msb, lsb); | ||
return uuid.toString(); | ||
} | ||
|
||
public static String generateBase64Uuid(UUID uuid) { | ||
ByteBuffer bb = ByteBuffer.wrap(new byte[16]); | ||
bb.putLong(uuid.getMostSignificantBits()); | ||
bb.putLong(uuid.getLeastSignificantBits()); | ||
var encoded = Base64.getEncoder().encodeToString(bb.array()); | ||
return encoded.substring(0, encoded.length() - 2); // skip '==' alignment | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package apoc.uuid; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode; | ||
|
||
public class UuidUtilTest { | ||
|
||
@Test | ||
public void fromHexToBase64() { | ||
var input = "290d6cba-ce94-455e-b59f-029cf1e395c5"; | ||
var output = UuidUtil.fromHexToBase64(input); | ||
assertThat(output).isEqualTo("KQ1sus6URV61nwKc8eOVxQ"); | ||
} | ||
|
||
@Test | ||
public void fromBase64ToHex() { | ||
var input = "KQ1sus6URV61nwKc8eOVxQ"; | ||
var output = UuidUtil.fromBase64ToHex(input); | ||
assertThat(output).isEqualTo("290d6cba-ce94-455e-b59f-029cf1e395c5"); | ||
} | ||
|
||
@Test | ||
public void fromBase64WithAlignmentToHex() { | ||
var input = "KQ1sus6URV61nwKc8eOVxQ=="; | ||
var output = UuidUtil.fromBase64ToHex(input); | ||
assertThat(output).isEqualTo("290d6cba-ce94-455e-b59f-029cf1e395c5"); | ||
} | ||
|
||
@Test | ||
public void shouldFailIfHexFormatIsWrong() { | ||
var input = "290d6cba-455e-b59f-029cf1e395c5"; | ||
assertThatCode(() -> UuidUtil.fromHexToBase64(input)).hasMessageStartingWith("Invalid UUID string"); | ||
} | ||
|
||
@Test | ||
public void shouldFailIfHexFormatIsEmpty() { | ||
var input = ""; | ||
assertThatCode(() -> UuidUtil.fromHexToBase64(input)).hasMessageStartingWith("Invalid UUID string"); | ||
} | ||
|
||
@Test | ||
public void shouldFailIfHexFormatIsNull() { | ||
assertThatCode(() -> UuidUtil.fromHexToBase64(null)).isInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
public void shouldFailIfBase64LengthIsWrong() { | ||
var input1 = "KQ1sus6URV61nwKc8eO=="; // wrong length | ||
assertThatCode(() -> UuidUtil.fromBase64ToHex(input1)).hasMessageStartingWith("Invalid UUID length. Expected 24 characters"); | ||
var input2 = "Q1sus6URV61nwKc8eOVxQ"; // wrong length | ||
assertThatCode(() -> UuidUtil.fromBase64ToHex(input2)).hasMessageStartingWith("Invalid UUID length. Expected 22 characters"); | ||
} | ||
|
||
@Test | ||
public void shouldFailIfBase64IsEmpty() { | ||
assertThatCode(() -> UuidUtil.fromBase64ToHex("")).hasMessageStartingWith("Expected not empty UUID value"); | ||
} | ||
|
||
@Test | ||
public void shouldFailIfBase64IsNull() { | ||
assertThatCode(() -> UuidUtil.fromBase64ToHex(null)).isInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
public void generateBase64ForSpecificUUIDs() { | ||
var uuid = UUID.fromString("00000000-0000-0000-0000-000000000000"); | ||
var uuidBase64 = UuidUtil.generateBase64Uuid(uuid); | ||
assertThat(uuidBase64).isEqualTo("AAAAAAAAAAAAAAAAAAAAAA"); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...iidoc/modules/ROOT/examples/generated-documentation/apoc.create.uuidBase64.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
¦xref::overview/apoc.create/apoc.create.uuidBase64.adoc[apoc.create.uuidBase64 icon:book[]] + | ||
|
||
- create an UUID encoded with Base64 | ||
¦label:function[] | ||
¦label:apoc-core[] |
5 changes: 5 additions & 0 deletions
5
.../modules/ROOT/examples/generated-documentation/apoc.create.uuidBase64ToHex.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
¦xref::overview/apoc.create/apoc.create.uuidBase64ToHex.adoc[apoc.create.uuidBase64ToHex icon:book[]] + | ||
|
||
- convert between an UUID encoded with Base64 to HEX format | ||
¦label:function[] | ||
¦label:apoc-core[] |
5 changes: 5 additions & 0 deletions
5
.../modules/ROOT/examples/generated-documentation/apoc.create.uuidHexToBase64.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
¦xref::overview/apoc.create/apoc.create.uuidHexToBase64.adoc[apoc.create.uuidHexToBase64 icon:book[]] + | ||
|
||
- convert an UUID in HEX format to encoded with Base64 | ||
¦label:function[] | ||
¦label:apoc-core[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
docs/asciidoc/modules/ROOT/pages/overview/apoc.create/apoc.create.uuidBase64.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//// | ||
This file is generated by DocsTest, so don't change it! | ||
//// | ||
|
||
= apoc.create.uuidBase64 | ||
:description: This section contains reference documentation for the apoc.create.uuidBase64 function. | ||
|
||
label:function[] label:apoc-core[] | ||
|
||
[.emphasis] | ||
apoc.create.uuidBase64() - create an UUID encoded with Base64 | ||
|
||
== Signature | ||
|
||
[source] | ||
---- | ||
apoc.create.uuidBase64() :: (STRING?) | ||
---- | ||
|
||
[[usage-apoc.create.uuidBase64]] | ||
== Usage Examples | ||
include::partial$usage/apoc.create.uuidBase64.adoc[] | ||
|
30 changes: 30 additions & 0 deletions
30
...ciidoc/modules/ROOT/pages/overview/apoc.create/apoc.create.uuidBase64ToHex.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//// | ||
This file is generated by DocsTest, so don't change it! | ||
//// | ||
|
||
= apoc.create.uuidBase64ToHex | ||
:description: This section contains reference documentation for the apoc.create.uuidBase64ToHex function. | ||
|
||
label:function[] label:apoc-core[] | ||
|
||
[.emphasis] | ||
apoc.create.uuidBase64ToHex() - convert between an UUID encoded with Base64 to HEX format | ||
|
||
== Signature | ||
|
||
[source] | ||
---- | ||
apoc.create.uuidBase64ToHex(base64Uuid :: STRING?) :: (STRING?) | ||
---- | ||
|
||
== Input parameters | ||
[.procedures, opts=header] | ||
|=== | ||
| Name | Type | Default | ||
|base64Uuid|STRING?|null | ||
|=== | ||
|
||
[[usage-apoc.create.uuidBase64ToHex]] | ||
== Usage Examples | ||
include::partial$usage/apoc.create.uuidBase64ToHex.adoc[] | ||
|
30 changes: 30 additions & 0 deletions
30
...ciidoc/modules/ROOT/pages/overview/apoc.create/apoc.create.uuidHexToBase64.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//// | ||
This file is generated by DocsTest, so don't change it! | ||
//// | ||
|
||
= apoc.create.uuidHexToBase64 | ||
:description: This section contains reference documentation for the apoc.create.uuidHexToBase64 function. | ||
|
||
label:function[] label:apoc-core[] | ||
|
||
[.emphasis] | ||
apoc.create.uuidHexToBase64() - convert an UUID in HEX format to encoded with Base64 | ||
|
||
== Signature | ||
|
||
[source] | ||
---- | ||
apoc.create.uuidHexToBase64(uuidHex :: STRING?) :: (STRING?) | ||
---- | ||
|
||
== Input parameters | ||
[.procedures, opts=header] | ||
|=== | ||
| Name | Type | Default | ||
|uuidHex|STRING?|null | ||
|=== | ||
|
||
[[usage-apoc.create.uuidHexToBase64]] | ||
== Usage Examples | ||
include::partial$usage/apoc.create.uuidHexToBase64.adoc[] | ||
|
13 changes: 13 additions & 0 deletions
13
docs/asciidoc/modules/ROOT/partials/usage/apoc.create.uuidBase64.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
The following generates a new UUID encoded with Base64: | ||
|
||
[source,cypher] | ||
---- | ||
RETURN apoc.create.uuidBase64() as output; | ||
---- | ||
|
||
.Results | ||
[opts="header",cols="1"] | ||
|=== | ||
| Output | ||
| "vX8dM5XoSe2ldoc/QzMEyw" | ||
|=== |
13 changes: 13 additions & 0 deletions
13
docs/asciidoc/modules/ROOT/partials/usage/apoc.create.uuidBase64ToHex.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
The following converts an UUID encoded with Base64 to HEX representation: | ||
|
||
[source,cypher] | ||
---- | ||
RETURN apoc.create.uuidBase64ToHex("vX8dM5XoSe2ldoc/QzMEyw") as output; | ||
---- | ||
|
||
.Results | ||
[opts="header",cols="1"] | ||
|=== | ||
| Output | ||
| "bd7f1d33-95e8-49ed-a576-873f433304cb" | ||
|=== |
13 changes: 13 additions & 0 deletions
13
docs/asciidoc/modules/ROOT/partials/usage/apoc.create.uuidHexToBase64.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
The following converts an UUID encoded with Base64 to HEX representation: | ||
|
||
[source,cypher] | ||
---- | ||
RETURN apoc.create.uuidHexToBase64("bd7f1d33-95e8-49ed-a576-873f433304cb") as output; | ||
---- | ||
|
||
.Results | ||
[opts="header",cols="1"] | ||
|=== | ||
| Output | ||
| "vX8dM5XoSe2ldoc/QzMEyw" | ||
|=== |
Oops, something went wrong.