From 01afb572bc518eb3304f69fab529304d656f7c76 Mon Sep 17 00:00:00 2001 From: Justin Bertram Date: Thu, 26 Sep 2024 15:41:34 -0500 Subject: [PATCH] ARTEMIS-5072 support non-paged management results --- .../impl/view/ActiveMQAbstractView.java | 11 +++- .../core/management/impl/view/ViewTest.java | 53 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/view/ActiveMQAbstractView.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/view/ActiveMQAbstractView.java index 9cadd78363..2355aba60b 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/view/ActiveMQAbstractView.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/view/ActiveMQAbstractView.java @@ -88,8 +88,15 @@ public abstract class ActiveMQAbstractView { public List getPagedResult(int page, int pageSize) { List builder = new ArrayList<>(); - int start = (page - 1) * pageSize; - int end = Math.min(page * pageSize, collection.size()); + final int start; + final int end; + if (page == -1 || pageSize == -1) { + start = 0; + end = collection.size(); + } else { + start = (page - 1) * pageSize; + end = Math.min(page * pageSize, collection.size()); + } int i = 0; for (T e : collection.stream().sorted(getComparator()).collect(Collectors.toList())) { if (i >= start && i < end) { diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/management/impl/view/ViewTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/management/impl/view/ViewTest.java index 997bdab071..032e9fc385 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/core/management/impl/view/ViewTest.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/management/impl/view/ViewTest.java @@ -16,7 +16,11 @@ */ package org.apache.activemq.artemis.core.management.impl.view; +import java.util.ArrayList; +import java.util.List; + import org.apache.activemq.artemis.core.server.ActiveMQServer; +import org.apache.activemq.artemis.json.JsonObjectBuilder; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -156,4 +160,53 @@ public class ViewTest { view.setOptions("{\"field\":\"user\",\"operation\":\"EQUALS\",\"value\":\"123\",\"sortColumn\":\"user\",\"sortOrder\":\"asc\"}"); assertEquals("user", view.getSortField()); } + + @Test + public void testPageSizeAndPageNumber() { + ActiveMQAbstractView myView = new ActiveMQAbstractView() { + @Override + Object getField(Object o, String fieldName) { + return null; + } + + @Override + public Class getClassT() { + return null; + } + + @Override + public JsonObjectBuilder toJson(Object obj) { + return null; + } + + @Override + public String getDefaultOrderColumn() { + return ""; + } + }; + + List list = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + list.add(i); + } + + myView.setCollection(list); + + // one or more inputs is -1 + assertEquals(list.size(), myView.getPagedResult(-1, -1).size()); + assertEquals(list.size(), myView.getPagedResult(123, -1).size()); + assertEquals(list.size(), myView.getPagedResult(-1, 123).size()); + + // page 0 - not really valid but still "works" + assertEquals(0, myView.getPagedResult(0, 123).size()); + + + assertEquals(123, myView.getPagedResult(1, 123).size()); + + // last page + assertEquals(100, myView.getPagedResult(10, 100).size()); + + // past the last page + assertEquals(0, myView.getPagedResult(11, 100).size()); + } }