You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by kw...@apache.org on 2016/04/15 18:47:57 UTC

svn commit: r1739332 - in /qpid/java/trunk/broker-plugins/management-http/src: main/java/org/apache/qpid/server/management/plugin/servlet/query/ test/java/org/apache/qpid/server/management/plugin/servlet/query/

Author: kwall
Date: Fri Apr 15 16:47:57 2016
New Revision: 1739332

URL: http://svn.apache.org/viewvc?rev=1739332&view=rev
Log:
QPID-7177: [Java Broker] Configured Object search - add support for TO_DATE() and NOW()

* Added supporting unit test too

Added:
    qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/query/
    qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectQueryTest.java
Modified:
    qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectExpressionFactory.java
    qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectQuery.java

Modified: qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectExpressionFactory.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectExpressionFactory.java?rev=1739332&r1=1739331&r2=1739332&view=diff
==============================================================================
--- qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectExpressionFactory.java (original)
+++ qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectExpressionFactory.java Fri Apr 15 16:47:57 2016
@@ -21,14 +21,17 @@
 package org.apache.qpid.server.management.plugin.servlet.query;
 
 import java.util.Arrays;
+import java.util.Calendar;
 import java.util.Collection;
-import java.util.EnumSet;
+import java.util.Date;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import javax.xml.bind.DatatypeConverter;
+
 import org.apache.qpid.filter.Expression;
 import org.apache.qpid.server.model.ConfiguredObject;
 
@@ -59,6 +62,61 @@ public class ConfiguredObjectExpressionF
                     }
                 };
             }
+        },
+
+        NOW {
+            @Override
+            ConfiguredObjectExpression asExpression(final List<Expression> args)
+            {
+                if (args != null && !args.isEmpty())
+                {
+                    throw new IllegalArgumentException(NOW.name() + " does not accept arguments.");
+                }
+                return new ConfiguredObjectExpression()
+                {
+                    @Override
+                    public Object evaluate(final ConfiguredObject<?> object)
+                    {
+                        return new Date();
+                    }
+                };
+            }
+        },
+
+        TO_DATE
+                {
+            @Override
+            ConfiguredObjectExpression asExpression(final List<Expression> args)
+            {
+                if (args == null || args.size() != 1)
+                {
+                    throw new IllegalArgumentException(TO_DATE.name() + " requires a single argument.");
+                }
+
+                return new ConfiguredObjectExpression()
+                {
+                    @Override
+                    public Object evaluate(final ConfiguredObject<?> object)
+                    {
+                        Object dateTime = args.get(0).evaluate(object);
+                        if (!(dateTime instanceof String))
+                        {
+                            throw new IllegalArgumentException(TO_DATE.name() + " requires a string argument, not a " + dateTime.getClass());
+                        }
+                        try
+                        {
+                            final Calendar calendar = DatatypeConverter.parseDateTime((String) dateTime);
+                            return calendar.getTime();
+                        }
+                        catch (IllegalArgumentException e)
+                        {
+                            throw new IllegalArgumentException(TO_DATE
+                                                               + " requires an ISO-8601 format date or date/time.", e);
+                        }
+                    }
+                };
+            }
+
         };
 
         abstract ConfiguredObjectExpression asExpression( List<Expression> args );

Modified: qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectQuery.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectQuery.java?rev=1739332&r1=1739331&r2=1739332&view=diff
==============================================================================
--- qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectQuery.java (original)
+++ qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectQuery.java Fri Apr 15 16:47:57 2016
@@ -149,7 +149,7 @@ public final class ConfiguredObjectQuery
         }
         catch (ParseException | TokenMgrError e)
         {
-            throw new SelectorParsingException("Unable to parse where clause");
+            throw new SelectorParsingException("Unable to parse where clause", e);
         }
         return filteredObjects;
     }

Added: qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectQueryTest.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectQueryTest.java?rev=1739332&view=auto
==============================================================================
--- qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectQueryTest.java (added)
+++ qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/query/ConfiguredObjectQueryTest.java Fri Apr 15 16:47:57 2016
@@ -0,0 +1,240 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.qpid.server.management.plugin.servlet.query;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
+
+import javax.xml.bind.DatatypeConverter;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import org.apache.qpid.server.model.ConfiguredObject;
+import org.apache.qpid.test.utils.QpidTestCase;
+
+public class ConfiguredObjectQueryTest extends QpidTestCase
+{
+    private static final String NUMBER_ATTR = "numberAttr";
+    private static final String DATE_ATTR = "dateAttr";
+
+    private ConfiguredObjectQuery _query;
+
+    public void testSingleResultNoClauses() throws Exception
+    {
+        final List<ConfiguredObject<?>> objects = new ArrayList<>();
+
+        final UUID objectUuid = UUID.randomUUID();
+        final String objectName = "obj1";
+
+        ConfiguredObject obj1 = createCO(new HashMap<String, Object>()
+        {{
+            put(ConfiguredObject.ID, objectUuid);
+            put(ConfiguredObject.NAME, objectName);
+        }});
+
+        objects.add(obj1);
+
+        _query = new ConfiguredObjectQuery(objects, null, null);
+
+        final List<String> headers = _query.getHeaders();
+        assertEquals("Unexpected headers", Lists.newArrayList(ConfiguredObject.ID, ConfiguredObject.NAME), headers);
+
+        List<List<Object>> results = _query.getResults();
+        assertEquals("Unexpected number of results", 1, results.size());
+
+        List<Object> row = results.iterator().next();
+        assertEquals("Unexpected row", Lists.newArrayList(objectUuid, objectName), row);
+    }
+
+    public void testTwoResultNoClauses() throws Exception
+    {
+        final List<ConfiguredObject<?>> objects = new ArrayList<>();
+
+        final UUID object1Uuid = UUID.randomUUID();
+        final String object1Name = "obj1";
+
+        final UUID object2Uuid = UUID.randomUUID();
+        final String object2Name = "obj2";
+
+        ConfiguredObject obj1 = createCO(new HashMap<String, Object>()
+        {{
+            put(ConfiguredObject.ID, object1Uuid);
+            put(ConfiguredObject.NAME, object1Name);
+        }});
+
+        ConfiguredObject obj2 = createCO(new HashMap<String, Object>()
+        {{
+            put(ConfiguredObject.ID, object2Uuid);
+            put(ConfiguredObject.NAME, object2Name);
+        }});
+
+        objects.add(obj1);
+        objects.add(obj2);
+
+        _query = new ConfiguredObjectQuery(objects, null, null);
+
+        List<List<Object>> results = _query.getResults();
+        assertEquals("Unexpected number of results", 2, results.size());
+
+        final Iterator<List<Object>> iterator = results.iterator();
+        List<Object> row1 = iterator.next();
+        assertEquals("Unexpected row", Lists.newArrayList(object1Uuid, object1Name), row1);
+
+        List<Object> row2 = iterator.next();
+        assertEquals("Unexpected row", Lists.newArrayList(object2Uuid, object2Name), row2);
+    }
+
+    public void testQuery_StringEquality() throws Exception
+    {
+        final List<ConfiguredObject<?>> objects = new ArrayList<>();
+
+        final UUID objectUuid = UUID.randomUUID();
+        final String objectName = "obj2";
+
+        ConfiguredObject nonMatch = createCO(new HashMap<String, Object>()
+        {{
+            put(ConfiguredObject.ID, UUID.randomUUID());
+            put(ConfiguredObject.NAME, "obj1");
+        }});
+
+        ConfiguredObject match = createCO(new HashMap<String, Object>()
+        {{
+            put(ConfiguredObject.ID, objectUuid);
+            put(ConfiguredObject.NAME, objectName);
+        }});
+
+        objects.add(nonMatch);
+        objects.add(match);
+
+        _query = new ConfiguredObjectQuery(objects, null, String.format("name = '%s'", objectName));
+
+        final List<String> headers = _query.getHeaders();
+        assertEquals("Unexpected headers", Lists.newArrayList(ConfiguredObject.ID, ConfiguredObject.NAME), headers);
+
+        List<List<Object>> results = _query.getResults();
+        assertEquals("Unexpected number of results", 1, results.size());
+
+        final Iterator<List<Object>> iterator = results.iterator();
+        List<Object> row = iterator.next();
+        assertEquals("Unexpected row", objectUuid, row.get(0));
+    }
+
+    public void testQuery_DateInequality() throws Exception
+    {
+        final List<ConfiguredObject<?>> objects = new ArrayList<>();
+
+        final long now = System.currentTimeMillis();
+        final UUID objectUuid = UUID.randomUUID();
+        final long oneDayInMillis = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
+        final Date yesterday = new Date(now - oneDayInMillis);
+        final Date tomorrow = new Date(now + oneDayInMillis);
+
+        ConfiguredObject nonMatch = createCO(new HashMap<String, Object>()
+        {{
+            put(ConfiguredObject.ID, UUID.randomUUID());
+            put(DATE_ATTR, yesterday);
+        }});
+
+        ConfiguredObject match = createCO(new HashMap<String, Object>()
+        {{
+            put(ConfiguredObject.ID, objectUuid);
+            put(DATE_ATTR, tomorrow);
+        }});
+
+        objects.add(nonMatch);
+        objects.add(match);
+
+        _query = new ConfiguredObjectQuery(objects,
+                                           String.format("%s,%s", ConfiguredObject.ID, DATE_ATTR),
+                                           String.format("%s > NOW()", DATE_ATTR));
+
+        List<List<Object>> results = _query.getResults();
+        assertEquals("Unexpected number of results", 1, results.size());
+
+        final Iterator<List<Object>> iterator = results.iterator();
+        List<Object> row = iterator.next();
+        assertEquals("Unexpected row", objectUuid, row.get(0));
+    }
+
+    public void testQuery_DateEquality() throws Exception
+    {
+        final List<ConfiguredObject<?>> objects = new ArrayList<>();
+
+        final long now = System.currentTimeMillis();
+        final Calendar calendar = Calendar.getInstance();
+        calendar.setTimeInMillis(now);
+
+        final UUID objectUuid = UUID.randomUUID();
+
+        ConfiguredObject nonMatch = createCO(new HashMap<String, Object>()
+        {{
+            put(ConfiguredObject.ID, UUID.randomUUID());
+            put(DATE_ATTR, new Date(0));
+        }});
+
+        ConfiguredObject match = createCO(new HashMap<String, Object>()
+        {{
+            put(ConfiguredObject.ID, objectUuid);
+            put(DATE_ATTR, new Date(now));
+        }});
+
+        objects.add(nonMatch);
+        objects.add(match);
+
+        _query = new ConfiguredObjectQuery(objects,
+                                           String.format("%s,%s", ConfiguredObject.ID, DATE_ATTR),
+                                           String.format("%s = TO_DATE('%s')", DATE_ATTR, DatatypeConverter.printDateTime(calendar)));
+
+        List<List<Object>> results = _query.getResults();
+        assertEquals("Unexpected number of results", 1, results.size());
+
+        final Iterator<List<Object>> iterator = results.iterator();
+        List<Object> row = iterator.next();
+        assertEquals("Unexpected row", objectUuid, row.get(0));
+    }
+
+    private ConfiguredObject createCO(final HashMap<String, Object> map)
+    {
+        ConfiguredObject object = mock(ConfiguredObject.class);
+
+        Map<String, Object> orderedMap = Maps.newTreeMap();
+        orderedMap.putAll(map);
+
+        when(object.getAttributeNames()).thenReturn(orderedMap.keySet());
+        for(String attributeName : orderedMap.keySet())
+        {
+            when(object.getAttribute(attributeName)).thenReturn(orderedMap.get(attributeName));
+        }
+        return object;
+    }
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org