changeset 286:4375a5dd5ec8

Fix bug in metadata payloadCount values. Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025403.html
author Chris Lessard <clessard@redhat.com>
date Thu, 19 Oct 2017 12:04:56 -0400
parents 701a5b3037dd
children ede4435b3682
files common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/response/MongoMetaDataGenerator.java common/mongodb/src/test/java/com/redhat/thermostat/gateway/common/mongodb/response/MongoMetaDataGeneratorTest.java
diffstat 2 files changed, 117 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/response/MongoMetaDataGenerator.java	Thu Oct 19 15:49:00 2017 +0200
+++ b/common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/response/MongoMetaDataGenerator.java	Thu Oct 19 12:04:56 2017 -0400
@@ -54,6 +54,8 @@
     private final Map<String, String> requestParamArguments;
     private final String baseURL;
 
+    private static final int UNLIMITED = 0;
+
     public MongoMetaDataGenerator(CommonQueryParams params,
                                   Map<String, String> requestParamArguments, MongoDataResultContainer execResult, String baseUrl) {
         this.limit = params.getLimit();
@@ -70,11 +72,34 @@
     public void setDocAndPayloadCount(MongoMetaDataResponseBuilder.MetaBuilder response) {
         if (!"".equals(queries)) {
             Integer docCount = (int) execResult.getGetReqCount();
-            Integer payloadCount = (limit > 1) ? 0 : docCount;
+            Integer payloadCount = generatePayloadCount(offset, limit, docCount);
+
             response.count(docCount).payloadCount(payloadCount);
         }
     }
 
+    // For testing purposes
+    int generatePayloadCount(int offset, int limit, int docCount) {
+        int payloadCount = 0;
+
+        if (offset < 0 || limit < 0) {
+            throw new IllegalArgumentException();
+        }
+
+        if (offset == 0) {
+            if (limit == UNLIMITED) {
+                return docCount;
+            }
+
+            return (docCount < limit) ? docCount : limit;
+        } else if (offset > 0 && offset < docCount) {
+            int diff = offset + limit;
+            payloadCount = (diff > docCount || diff <= docCount && limit == UNLIMITED) ? docCount - offset : limit;
+        }
+
+        return payloadCount;
+    }
+
     public void setPrev(MongoMetaDataResponseBuilder.MetaBuilder response) {
         if (!Integer.valueOf(0).equals(offset)) {
             StringBuilder prev = new StringBuilder();
--- a/common/mongodb/src/test/java/com/redhat/thermostat/gateway/common/mongodb/response/MongoMetaDataGeneratorTest.java	Thu Oct 19 15:49:00 2017 +0200
+++ b/common/mongodb/src/test/java/com/redhat/thermostat/gateway/common/mongodb/response/MongoMetaDataGeneratorTest.java	Thu Oct 19 12:04:56 2017 -0400
@@ -80,7 +80,7 @@
     public void testSetCounts() {
         fullGenerator.setDocAndPayloadCount(response);
         String output = response.build().toString();
-        String expected = "{\"payloadCount\":0,\"count\":4}";
+        String expected = "{\"payloadCount\":2,\"count\":4}";
 
         assertEquals(expected, output);
     }
@@ -123,4 +123,94 @@
 
         assertEquals(expected, output);
     }
+
+    @Test
+    public void testGeneratePayloadCountZeroLimitZeroOffset() {
+        Integer count = 10;
+        Integer limit = 0;
+        Integer offset = 0;
+
+        assertEquals(10, fullGenerator.generatePayloadCount(offset, limit, count));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testGeneratePayloadCountLimitOffsetBothNegative() {
+        Integer count = 10;
+        Integer limit = -1;
+        Integer offset = -1;
+
+        fullGenerator.generatePayloadCount(offset, limit, count);
+    }
+
+    @Test
+    public void testGeneratePayloadCountLimitLessThanCountNoOffset() {
+        Integer count = 10;
+        Integer limit = 5;
+        Integer offset = 0;
+
+        assertEquals(5, fullGenerator.generatePayloadCount(offset, limit, count));
+    }
+
+    @Test
+    public void testGeneratePayloadCountLimitEqualToCountNoOffset() {
+        Integer count = 10;
+        Integer limit = 10;
+        Integer offset = 0;
+
+        assertEquals(10, fullGenerator.generatePayloadCount(offset, limit, count));
+    }
+
+    @Test
+    public void testGeneratePayloadCountLimitGreaterThanCountNoOffset() {
+        Integer count = 10;
+        Integer limit = 15;
+        Integer offset = 0;
+
+        assertEquals(10, fullGenerator.generatePayloadCount(offset, limit, count));
+    }
+
+    @Test
+    public void testGeneratePayloadCountOffsetLessThanCountNoLimit() {
+        Integer count = 10;
+        Integer limit = 0;
+        Integer offset = 5;
+
+        assertEquals(5, fullGenerator.generatePayloadCount(offset, limit, count));
+    }
+
+    @Test
+    public void testGeneratePayloadCountOffsetEqualToCountNoLimit() {
+        Integer count = 10;
+        Integer limit = 0;
+        Integer offset = 10;
+
+        assertEquals(0, fullGenerator.generatePayloadCount(offset, limit, count));
+    }
+
+    @Test
+    public void testGeneratePayloadCountOffsetGreaterThanCountNoLimit() {
+        Integer count = 10;
+        Integer limit = 0;
+        Integer offset = 15;
+
+        assertEquals(0, fullGenerator.generatePayloadCount(offset, limit, count));
+    }
+
+    @Test
+    public void testGeneratePayloadCountOffsetLimitLessThanCountAndOffset() {
+        Integer count = 10;
+        Integer limit = 3;
+        Integer offset = 5;
+
+        assertEquals(3, fullGenerator.generatePayloadCount(offset, limit, count));
+    }
+
+    @Test
+    public void testGeneratePayloadCountOffsetLimitLessThanCountGreaterThanOffset() {
+        Integer count = 10;
+        Integer limit = 8;
+        Integer offset = 5;
+
+        assertEquals(5, fullGenerator.generatePayloadCount(offset, limit, count));
+    }
 }