From cc5f8c0ff13cf8679b6c532273d263568eaeaaea Mon Sep 17 00:00:00 2001 From: omadaan <165099174+omadaan@users.noreply.github.com> Date: Wed, 3 Apr 2024 16:59:33 +1100 Subject: [PATCH] Fix PostgresIndexQueryBuilder null pointer exception (#116) * Throw IllegalArgumentException when query string does not match regex * Unit test for exception --- .../postgres/util/PostgresIndexQueryBuilder.java | 2 ++ .../util/PostgresIndexQueryBuilderTest.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/postgres-persistence/src/main/java/com/netflix/conductor/postgres/util/PostgresIndexQueryBuilder.java b/postgres-persistence/src/main/java/com/netflix/conductor/postgres/util/PostgresIndexQueryBuilder.java index 706716a8f..9e73df686 100644 --- a/postgres-persistence/src/main/java/com/netflix/conductor/postgres/util/PostgresIndexQueryBuilder.java +++ b/postgres-persistence/src/main/java/com/netflix/conductor/postgres/util/PostgresIndexQueryBuilder.java @@ -75,6 +75,8 @@ public Condition(String query) { if (this.attribute.endsWith("_time")) { values.set(0, millisToUtc(values.get(0))); } + } else { + throw new IllegalArgumentException("Incorrectly formatted query string: " + query); } } diff --git a/postgres-persistence/src/test/java/com/netflix/conductor/postgres/util/PostgresIndexQueryBuilderTest.java b/postgres-persistence/src/test/java/com/netflix/conductor/postgres/util/PostgresIndexQueryBuilderTest.java index 2a1f8a394..dd8d3a489 100644 --- a/postgres-persistence/src/test/java/com/netflix/conductor/postgres/util/PostgresIndexQueryBuilderTest.java +++ b/postgres-persistence/src/test/java/com/netflix/conductor/postgres/util/PostgresIndexQueryBuilderTest.java @@ -24,6 +24,7 @@ import com.netflix.conductor.postgres.config.PostgresProperties; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.*; public class PostgresIndexQueryBuilderTest { @@ -288,4 +289,19 @@ void shouldAllowJsonSearch() throws SQLException { "SELECT json_data::TEXT FROM table_name WHERE json_data @> ?::JSONB LIMIT ? OFFSET ?"; assertEquals(expectedQuery, builder.getQuery()); } + + @Test() + void shouldThrowIllegalArgumentExceptionWhenQueryStringIsInvalid() { + String inputQuery = + "workflowType IN (one,two) AND status IN (COMPLETED,RUNNING) AND startTime>1675701498000 AND xyz"; + + try { + new PostgresIndexQueryBuilder( + "table_name", inputQuery, "", 0, 15, new ArrayList<>(), properties); + + fail("should have failed since xyz does not conform to expected format"); + } catch (IllegalArgumentException e) { + assertEquals("Incorrectly formatted query string: xyz", e.getMessage()); + } + } }