From ea130581a354cf4e6b6a90036b6a01af43c8d98d Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Thu, 4 Jan 2024 12:38:40 +0100 Subject: [PATCH] unit test and improve PostgresArray --- .../hsadminng/mapper/PostgresArray.java | 5 +- .../mapper/PostgresArrayIntegrationTest.java | 88 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/hostsharing/hsadminng/mapper/PostgresArrayIntegrationTest.java diff --git a/src/main/java/net/hostsharing/hsadminng/mapper/PostgresArray.java b/src/main/java/net/hostsharing/hsadminng/mapper/PostgresArray.java index b5031402..e1e1d056 100644 --- a/src/main/java/net/hostsharing/hsadminng/mapper/PostgresArray.java +++ b/src/main/java/net/hostsharing/hsadminng/mapper/PostgresArray.java @@ -42,7 +42,10 @@ public class PostgresArray { tokenizer.remove("\"", "\""); final T[] array = newGenericArray(elementClass, tokenizer.getSize()); // Create a new array of the specified type and length for ( int n = 0; n < tokenizer.getSize(); ++n ) { - array[n] = itemParser.apply(tokenizer.getToken(n).trim().replace("\\\"", "\"")); + final String token = tokenizer.getToken(n); + if ( !"NULL".equals(token) ) { + array[n] = itemParser.apply(token.trim().replace("\\\"", "\"")); + } } return array; } diff --git a/src/test/java/net/hostsharing/hsadminng/mapper/PostgresArrayIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/mapper/PostgresArrayIntegrationTest.java new file mode 100644 index 00000000..c76141b1 --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/mapper/PostgresArrayIntegrationTest.java @@ -0,0 +1,88 @@ +package net.hostsharing.hsadminng.mapper; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; + +import jakarta.persistence.EntityManager; + +import java.util.UUID; +import java.util.function.Function; + +import static org.assertj.core.api.Assertions.assertThat; + +@DataJpaTest +class PostgresArrayIntegrationTest { + + @Autowired + EntityManager em; + + @Test + void shouldCreateEmptyArray() { + em.createNativeQuery(""" + create or replace function returnEmptyArray() + returns text[] + stable leakproof + language plpgsql as $$ + declare + emptyArray text[] = '{}'; + begin + return emptyArray; + end; $$; + """).executeUpdate(); + final byte[] pgArray = (byte[]) em.createNativeQuery("SELECT returnEmptyArray()", String[].class).getSingleResult(); + + final String[] result = PostgresArray.fromPostgresArray(pgArray, String.class, Function.identity()); + + assertThat(result).isEmpty(); + } + + @Test + void shouldCreateStringArray() { + em.createNativeQuery(""" + create or replace function returnStringArray() + returns varchar(63)[] + stable leakproof + language plpgsql as $$ + declare + text1 text = 'one'; + text2 text = 'two, three'; + text3 text = 'four; five'; + text4 text = 'say "Hello" to me'; + begin + return array[text1, text2, text3, null, text4]; + end; $$; + """).executeUpdate(); + final byte[] pgArray = (byte[]) em.createNativeQuery("SELECT returnStringArray()", String[].class).getSingleResult(); + + final String[] result = PostgresArray.fromPostgresArray(pgArray, String.class, Function.identity()); + + assertThat(result).containsExactly("one", "two, three", "four; five", null, "say \"Hello\" to me"); + } + + @Test + void shouldCreateUUidArray() { + em.createNativeQuery(""" + create or replace function returnUuidArray() + returns uuid[] + stable leakproof + language plpgsql as $$ + declare + uuid1 UUID = 'f47ac10b-58cc-4372-a567-0e02b2c3d479'; + uuid2 UUID = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; + uuid3 UUID = '01234567-89ab-cdef-0123-456789abcdef'; + begin + return ARRAY[uuid1, uuid2, null, uuid3]; + end; $$; + """).executeUpdate(); + final byte[] pgArray = (byte[]) em.createNativeQuery("SELECT returnUuidArray()", UUID[].class).getSingleResult(); + + final UUID[] result = PostgresArray.fromPostgresArray(pgArray, UUID.class, UUID::fromString); + + assertThat(result).containsExactly( + UUID.fromString("f47ac10b-58cc-4372-a567-0e02b2c3d479"), + UUID.fromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8"), + null, + UUID.fromString("01234567-89ab-cdef-0123-456789abcdef")); + } +}