You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2021/06/24 13:51:15 UTC

[sling-org-apache-sling-graphql-core] branch master updated: SLING-10541 - add test for start cursor with empty result set, adapt code accordingly and use assertThrows

This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-graphql-core.git


The following commit(s) were added to refs/heads/master by this push:
     new fcb5b60  SLING-10541 - add test for start cursor with empty result set, adapt code accordingly and use assertThrows
fcb5b60 is described below

commit fcb5b60033806a9809eba0749859c1323463f912
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Thu Jun 24 15:49:45 2021 +0200

    SLING-10541 - add test for start cursor with empty result set, adapt code accordingly and use assertThrows
---
 .../sling/graphql/helpers/GenericConnection.java   |  3 +-
 .../core/pagination/GenericConnectionTest.java     | 39 ++++++++++------------
 2 files changed, 19 insertions(+), 23 deletions(-)

diff --git a/src/main/java/org/apache/sling/graphql/helpers/GenericConnection.java b/src/main/java/org/apache/sling/graphql/helpers/GenericConnection.java
index 7b3d1be..91408eb 100644
--- a/src/main/java/org/apache/sling/graphql/helpers/GenericConnection.java
+++ b/src/main/java/org/apache/sling/graphql/helpers/GenericConnection.java
@@ -74,7 +74,6 @@ public final class GenericConnection<T> implements Connection<T>, PageInfo {
             throw new IllegalStateException("Already initialized");
         }
         initialized = true;
-        final boolean anyData = dataIterator.hasNext();
 
         // Need to visit the stream first to setup the PageInfo, which graphql-java
         // apparently uses before visiting all the edges
@@ -112,7 +111,7 @@ public final class GenericConnection<T> implements Connection<T>, PageInfo {
             }
         }
 
-        if(anyData && !inRange && limit > 0) {
+        if(!inRange && limit > 0 && startAfter != null) {
             throw new SlingGraphQLException("Start cursor not found in supplied data:" + startAfter);
         }
         if(hasPreviousPage == null) {
diff --git a/src/test/java/org/apache/sling/graphql/core/pagination/GenericConnectionTest.java b/src/test/java/org/apache/sling/graphql/core/pagination/GenericConnectionTest.java
index a531ebf..64d28ee 100644
--- a/src/test/java/org/apache/sling/graphql/core/pagination/GenericConnectionTest.java
+++ b/src/test/java/org/apache/sling/graphql/core/pagination/GenericConnectionTest.java
@@ -20,6 +20,7 @@ package org.apache.sling.graphql.core.pagination;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -147,24 +148,16 @@ public class GenericConnectionTest {
 
     @Test
     public void startCursorNotFound() {
-        try {
-            new GenericConnection.Builder<>(data.iterator(), cursorize)
-                .withLimit(2)
-                .withStartAfter(new Cursor(cursorize.apply(HIGH_LIMIT)))
-                .build();
-            fail("Expecting a RuntimeException");
-        } catch(RuntimeException rex) {
-            assertTrue(rex.getMessage().contains("Start cursor not found"));
-        }
+        final GenericConnection.Builder<Integer> b = new GenericConnection.Builder<>(data.iterator(), cursorize)
+            .withLimit(2)
+            .withStartAfter(new Cursor(cursorize.apply(HIGH_LIMIT)))
+        ;
+        final Throwable rex = assertThrows(RuntimeException.class, () -> b.build() );
+        assertTrue(rex.getMessage().contains("Start cursor not found"));
     }
 
     private static void assertSupplierException(Supplier<?> s) {
-        try {
-            s.get();
-            fail("Expected an Exception");
-        } catch(IllegalArgumentException iarg) {
-            // as expcted
-        }
+        assertThrows(IllegalArgumentException.class, () -> s.get());
     }
 
     @Test
@@ -180,12 +173,7 @@ public class GenericConnectionTest {
         b.withLimit(0);
         b.withLimit(42);
         b.withLimit(100);
-        try {
-            b.withLimit(101);
-            fail("Expecting an exception, over limit");
-        } catch(IllegalArgumentException iex) {
-            assertTrue(iex.getMessage().contains("aximum"));
-        }
+        assertThrows(IllegalArgumentException.class, () -> b.withLimit(101));
     }
 
     @Test
@@ -193,4 +181,13 @@ public class GenericConnectionTest {
         final Connection<String> empty = new GenericConnection.Builder<String>(Collections.emptyIterator(), s -> s).build();
         assertFalse("Expecting no data", empty.getEdges().iterator().hasNext());
     }
+
+    @Test
+    public void testEmptyResultSetWithCursor() {
+        final GenericConnection.Builder<String> b = new GenericConnection.Builder<String>(Collections.emptyIterator(), s -> s)
+            .withStartAfter(new Cursor("won't be used"))
+        ;
+        final Throwable rex = assertThrows(RuntimeException.class, () -> b.build() );
+        assertTrue(rex.getMessage().contains("Start cursor not found"));
+    }
 }