ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jspei...@apache.org
Subject svn commit: r1440067 [2/2] - in /incubator/ambari/trunk: ./ ambari-server/src/main/java/org/apache/ambari/server/api/handlers/ ambari-server/src/main/java/org/apache/ambari/server/api/predicate/ ambari-server/src/main/java/org/apache/ambari/server/api/...
Date Tue, 29 Jan 2013 19:19:11 GMT
Added: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/NotOperator.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/NotOperator.java?rev=1440067&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/NotOperator.java (added)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/NotOperator.java Tue Jan 29 19:19:10 2013
@@ -0,0 +1,63 @@
+/**
+ * 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.ambari.server.api.predicate.operators;
+
+import org.apache.ambari.server.controller.predicate.BasePredicate;
+import org.apache.ambari.server.controller.predicate.NotPredicate;
+import org.apache.ambari.server.controller.spi.Predicate;
+
+/**
+ * Not unary operator implementation.
+ */
+public class NotOperator extends AbstractOperator implements LogicalOperator {
+
+  /**
+   * Constructor.
+   *
+   * @param ctxPrecedence  the precedence value of the current context
+   */
+  public NotOperator(int ctxPrecedence) {
+    super(ctxPrecedence);
+  }
+
+  @Override
+  public TYPE getType() {
+    return TYPE.NOT;
+  }
+
+  @Override
+  public String getName() {
+    return "NotOperator";
+  }
+
+  @Override
+  public int getBasePrecedence() {
+    return 3;
+  }
+
+  @Override
+  public Predicate toPredicate(Predicate left, Predicate right) {
+    return new NotPredicate((BasePredicate) right);
+  }
+
+  @Override
+  public String toString() {
+    return getName() + "[precedence=" + getPrecedence() + "]";
+  }
+}

Added: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/Operator.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/Operator.java?rev=1440067&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/Operator.java (added)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/Operator.java Tue Jan 29 19:19:10 2013
@@ -0,0 +1,61 @@
+/**
+ * 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.ambari.server.api.predicate.operators;
+
+/**
+ * Operator representation.
+ */
+public interface Operator {
+
+  /**
+   * Operator types.
+   */
+  public enum TYPE {
+    LESS,
+    LESS_EQUAL,
+    GREATER,
+    GREATER_EQUAL,
+    EQUAL,
+    NOT_EQUAL,
+    AND,
+    OR,
+    NOT
+  }
+
+  /**
+   * The highest base operator precedence level.
+   */
+  public static final int MAX_OP_PRECEDENCE = 3;
+
+  /**
+   * Get the operator type.
+   *
+   * @return the operator type
+   */
+  public TYPE getType();
+
+  /**
+   * Obtain the precedence of the operator.
+   * This value is calculated based on the operators base precedence and the context of the
+   * surrounding expressions.  Higher precedence values have higher precedence.
+   *
+   * @return  the precedence of this operator in it's current context
+   */
+  public int getPrecedence();
+}

Added: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/OrOperator.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/OrOperator.java?rev=1440067&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/OrOperator.java (added)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/OrOperator.java Tue Jan 29 19:19:10 2013
@@ -0,0 +1,64 @@
+/**
+ * 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.ambari.server.api.predicate.operators;
+
+import org.apache.ambari.server.controller.predicate.BasePredicate;
+import org.apache.ambari.server.controller.predicate.OrPredicate;
+import org.apache.ambari.server.controller.spi.Predicate;
+
+/**
+ * Or operator implementation.
+ */
+public class OrOperator extends AbstractOperator implements LogicalOperator {
+
+  /**
+   * Constructor.
+   *
+   * @param ctxPrecedence  precedence value for the current context
+   */
+  public OrOperator(int ctxPrecedence) {
+    super(ctxPrecedence);
+  }
+
+  @Override
+  public TYPE getType() {
+    return TYPE.OR;
+  }
+
+  @Override
+  public String getName() {
+    return "OrOperator";
+  }
+
+  @Override
+  public int getBasePrecedence() {
+    return 1;
+  }
+
+  @Override
+  public Predicate toPredicate(Predicate left, Predicate right) {
+    //todo: refactor to remove down casts
+    return new OrPredicate((BasePredicate) left, (BasePredicate) right);
+  }
+
+  @Override
+  public String toString() {
+    return getName() + "[precedence=" + getPrecedence() + "]";
+  }
+}

Added: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/RelationalOperator.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/RelationalOperator.java?rev=1440067&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/RelationalOperator.java (added)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/RelationalOperator.java Tue Jan 29 19:19:10 2013
@@ -0,0 +1,35 @@
+/**
+ * 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.ambari.server.api.predicate.operators;
+
+import org.apache.ambari.server.controller.spi.Predicate;
+
+/**
+ * Relational operator external representation.
+ */
+public interface RelationalOperator extends Operator {
+  /**
+   * Create a predicate for this relational operator.
+   *
+   * @param prop  left operand
+   * @param val   right operand
+   * @return  a predicate instance for this operator.
+   */
+  public Predicate toPredicate(String prop, String val);
+}

Added: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/RelationalOperatorFactory.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/RelationalOperatorFactory.java?rev=1440067&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/RelationalOperatorFactory.java (added)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/predicate/operators/RelationalOperatorFactory.java Tue Jan 29 19:19:10 2013
@@ -0,0 +1,52 @@
+/**
+ * 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.ambari.server.api.predicate.operators;
+
+import org.apache.ambari.server.api.predicate.InvalidQueryException;
+
+/**
+ * Factory of relational operators.
+ */
+public class RelationalOperatorFactory {
+  /**
+   * Create a relational operator based on the string representation
+   * of the operator.
+   *
+   * @param operator  the string representation of the operator
+   * @return relationl operator for the given string
+   * @throws InvalidQueryException if an invalid operator is passed in
+   */
+  public static RelationalOperator createOperator(String operator) throws InvalidQueryException {
+    if ("!=".equals(operator)) {
+      return new NotEqualsOperator();
+    } else if ("=".equals(operator)) {
+      return new EqualsOperator();
+    } if ("<=".equals(operator)) {
+      return new LessEqualsOperator();
+    } if ("<".equals(operator)) {
+      return new LessOperator();
+    }else if (">=".equals(operator)) {
+      return new GreaterEqualsOperator();
+    } if (">".equals(operator)) {
+      return new GreaterOperator();
+    } else {
+      throw new RuntimeException("Invalid Operator Type: " + operator);
+    }
+  }
+}

Modified: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/services/BaseRequest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/services/BaseRequest.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/services/BaseRequest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/services/BaseRequest.java Tue Jan 29 19:19:10 2013
@@ -18,13 +18,14 @@
 
 package org.apache.ambari.server.api.services;
 
+import org.apache.ambari.server.api.predicate.InvalidQueryException;
+import org.apache.ambari.server.api.predicate.PredicateCompiler;
 import org.apache.ambari.server.api.resources.*;
 import org.apache.ambari.server.api.services.parsers.JsonPropertyParser;
 import org.apache.ambari.server.api.services.parsers.RequestBodyParser;
 import org.apache.ambari.server.api.services.serializers.JsonSerializer;
 import org.apache.ambari.server.api.services.serializers.ResultSerializer;
 import org.apache.ambari.server.controller.internal.TemporalInfoImpl;
-import org.apache.ambari.server.controller.predicate.*;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.TemporalInfo;
 
@@ -58,11 +59,6 @@ public abstract class BaseRequest implem
 
 
   /**
-   * Predicate operators.
-   */
-  private Pattern m_pattern = Pattern.compile("!=|>=|<=|=|>|<");
-
-  /**
    * Associated resource definition
    */
   private ResourceInstance m_resource;
@@ -104,37 +100,12 @@ public abstract class BaseRequest implem
   }
 
   @Override
-  public Predicate getQueryPredicate() {
-    //todo: parse during init
-    //not using getQueryParameters because it assumes '=' operator
-    String uri = getURI();
-    int qsBegin = uri.indexOf("?");
-
-    if (qsBegin == -1) return null;
-
-    String[] tokens = uri.substring(qsBegin + 1).split("&");
-
-    Set<BasePredicate> setPredicates = new HashSet<BasePredicate>();
-    for (String outerToken : tokens) {
-      if (outerToken.startsWith("_=")) {
-        // NOTE: This is to enable UI to pass a _= parameter for unique query 
-        // string even though the backend doesnt need it.
-        continue;
-      }
-      
-      if (outerToken != null &&  !outerToken.startsWith("fields")) {
-        setPredicates.add(outerToken.contains("|") ?
-            handleOrPredicate(outerToken) : createPredicate(outerToken));
-      }
-    }
+  public Predicate getQueryPredicate() throws InvalidQueryException {
+    String uri     = getURI();
+    int    qsBegin = uri.indexOf("?");
 
-    if (setPredicates.size() == 1) {
-      return setPredicates.iterator().next();
-    } else if (setPredicates.size() > 1) {
-      return new AndPredicate(setPredicates.toArray(new BasePredicate[setPredicates.size()]));
-    } else {
-      return null;
-    }
+    return (qsBegin == -1) ? null :
+        getPredicateCompiler().compile(uri.substring(qsBegin + 1));
   }
 
   @Override
@@ -205,43 +176,11 @@ public abstract class BaseRequest implem
     return new ResultPostProcessorImpl(this);
   }
 
-  private BasePredicate createPredicate(String token) {
-
-    Matcher m = m_pattern.matcher(token);
-    m.find();
-
-    String propertyId = token.substring(0, m.start());
-    String     value      = token.substring(m.end());
-    String     operator   = m.group();
-
-    if (operator.equals("=")) {
-      return new EqualsPredicate<String>(propertyId, value);
-    } else if (operator.equals("!=")) {
-      return new NotPredicate(new EqualsPredicate<String>(propertyId, value));
-    } else if (operator.equals("<")) {
-      return new LessPredicate<String>(propertyId, value);
-    } else if (operator.equals(">"))  {
-      return new GreaterPredicate<String>(propertyId, value);
-    } else if (operator.equals("<=")) {
-      return new LessEqualsPredicate<String>(propertyId, value);
-    } else if (operator.equals(">=")) {
-      return new GreaterEqualsPredicate<String>(propertyId, value);
-    } else {
-      throw new RuntimeException("Unknown operator provided in predicate: " + operator);
-    }
-  }
-
-  private BasePredicate handleOrPredicate(String predicate) {
-    Set<BasePredicate> setPredicates = new HashSet<BasePredicate>();
-    String[] tokens = predicate.split("\\|");
-    for (String tok : tokens) {
-      setPredicates.add(createPredicate(tok));
-    }
-
-    return new OrPredicate(setPredicates.toArray(new BasePredicate[setPredicates.size()]));
-  }
-
   protected RequestBodyParser getHttpBodyParser() {
     return new JsonPropertyParser();
   }
+
+  protected PredicateCompiler getPredicateCompiler() {
+    return new PredicateCompiler();
+  }
 }

Modified: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/services/Request.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/services/Request.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/services/Request.java (original)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/api/services/Request.java Tue Jan 29 19:19:10 2013
@@ -18,6 +18,7 @@
 
 package org.apache.ambari.server.api.services;
 
+import org.apache.ambari.server.api.predicate.InvalidQueryException;
 import org.apache.ambari.server.api.resources.ResourceDefinition;
 import org.apache.ambari.server.api.resources.ResourceInstance;
 import org.apache.ambari.server.api.services.serializers.ResultSerializer;
@@ -79,8 +80,9 @@ public interface Request {
    * such as 'AND'.
    *
    * @return the user defined predicate
+   * @throws InvalidQueryException if the query syntax is invalid
    */
-  public Predicate getQueryPredicate();
+  public Predicate getQueryPredicate() throws InvalidQueryException;
 
   /**
    * Obtain the partial response fields and associated temporal information which were provided

Modified: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java (original)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java Tue Jan 29 19:19:10 2013
@@ -1151,7 +1151,12 @@ public class AmbariManagementControllerI
       hosts = clusters.getHosts();
     } else {
       hosts = new ArrayList<Host>();
-      hosts.add(clusters.getHost(request.getHostname()));
+      try {
+        hosts.add(clusters.getHost(request.getHostname()));
+      } catch (HostNotFoundException e) {
+        // add cluster name
+        throw new HostNotFoundException(clusterName, hostName);
+      }
     }
 
     for (Host h : hosts) {
@@ -1161,7 +1166,7 @@ public class AmbariManagementControllerI
           r.setClusterName(clusterName);
           response.add(r);
         } else if (hostName != null) {
-          throw new HostNotFoundException(hostName);
+          throw new HostNotFoundException(clusterName, hostName);
         }
       } else {
         HostResponse r = h.convertToResponse();

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/TestSuite.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/TestSuite.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/TestSuite.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/TestSuite.java Tue Jan 29 19:19:10 2013
@@ -19,10 +19,12 @@ package org.apache.ambari.server.api;
  */
 
 /**
- * All unit tests.
+ * All api unit tests.
  */
 
 import org.apache.ambari.server.api.handlers.*;
+import org.apache.ambari.server.api.predicate.QueryLexerTest;
+import org.apache.ambari.server.api.predicate.QueryParserTest;
 import org.apache.ambari.server.api.query.QueryImplTest;
 import org.apache.ambari.server.api.resources.ResourceInstanceImplTest;
 import org.apache.ambari.server.api.services.*;
@@ -36,6 +38,7 @@ import org.junit.runners.Suite;
     ComponentServiceTest.class, HostComponentServiceTest.class, ReadHandlerTest.class, QueryImplTest.class,
     JsonPropertyParserTest.class, CreateHandlerTest.class, UpdateHandlerTest.class, DeleteHandlerTest.class,
     PersistenceManagerImplTest.class, GetRequestTest.class, PutRequestTest.class, PostRequestTest.class,
-    DeleteRequestTest.class, JsonSerializerTest.class, QueryCreateHandlerTest.class, ResourceInstanceImplTest.class})
+    DeleteRequestTest.class, JsonSerializerTest.class, QueryCreateHandlerTest.class, ResourceInstanceImplTest.class,
+    QueryLexerTest.class, QueryParserTest.class})
 public class TestSuite {
 }

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/CreateHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/CreateHandlerTest.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/CreateHandlerTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/CreateHandlerTest.java Tue Jan 29 19:19:10 2013
@@ -18,6 +18,7 @@
 
 package org.apache.ambari.server.api.handlers;
 
+import org.apache.ambari.server.api.predicate.InvalidQueryException;
 import org.apache.ambari.server.api.resources.ResourceInstance;
 import org.apache.ambari.server.api.services.ResultStatus;
 import org.apache.ambari.server.api.services.persistence.PersistenceManager;
@@ -162,4 +163,21 @@ public class CreateHandlerTest {
       return m_testPm;
     }
   }
+
+  @Test
+  public void testHandleRequest__InvalidQuery() throws Exception {
+    Request request = createNiceMock(Request.class);
+    ResourceInstance resource = createNiceMock(ResourceInstance.class);
+    Exception e = new InvalidQueryException("test exception");
+
+    expect(request.getResource()).andReturn(resource);
+    expect(request.getQueryPredicate()).andThrow(e);
+    replay(request, resource);
+
+    Result result = new CreateHandler().handleRequest(request);
+    assertEquals(ResultStatus.STATUS.BAD_REQUEST, result.getStatus().getStatus());
+    assertTrue(result.getStatus().getMessage().contains(e.getMessage()));
+
+    verify(request, resource);
+  }
 }

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/DeleteHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/DeleteHandlerTest.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/DeleteHandlerTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/DeleteHandlerTest.java Tue Jan 29 19:19:10 2013
@@ -18,6 +18,7 @@ package org.apache.ambari.server.api.han
  * limitations under the License.
  */
 
+import org.apache.ambari.server.api.predicate.InvalidQueryException;
 import org.apache.ambari.server.api.query.Query;
 import org.apache.ambari.server.api.resources.ResourceInstance;
 import org.apache.ambari.server.api.services.ResultStatus;
@@ -28,7 +29,6 @@ import org.apache.ambari.server.api.util
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.RequestStatus;
 import org.apache.ambari.server.controller.spi.Resource;
-import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.junit.Test;
 
 import java.util.*;
@@ -171,4 +171,21 @@ public class DeleteHandlerTest {
       return m_testPm;
     }
   }
+
+  @Test
+  public void testHandleRequest__InvalidQuery() throws Exception {
+    Request request = createNiceMock(Request.class);
+    ResourceInstance resource = createNiceMock(ResourceInstance.class);
+    Exception e = new InvalidQueryException("test exception");
+
+    expect(request.getResource()).andReturn(resource);
+    expect(request.getQueryPredicate()).andThrow(e);
+    replay(request, resource);
+
+    Result result = new DeleteHandler().handleRequest(request);
+    assertEquals(ResultStatus.STATUS.BAD_REQUEST, result.getStatus().getStatus());
+    assertTrue(result.getStatus().getMessage().contains(e.getMessage()));
+
+    verify(request, resource);
+  }
 }
\ No newline at end of file

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/QueryCreateHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/QueryCreateHandlerTest.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/QueryCreateHandlerTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/QueryCreateHandlerTest.java Tue Jan 29 19:19:10 2013
@@ -19,6 +19,7 @@
 
 package org.apache.ambari.server.api.handlers;
 
+import org.apache.ambari.server.api.predicate.InvalidQueryException;
 import org.apache.ambari.server.api.query.Query;
 import org.apache.ambari.server.api.resources.ResourceDefinition;
 import org.apache.ambari.server.api.resources.ResourceInstance;
@@ -224,4 +225,28 @@ public class QueryCreateHandlerTest {
     }
   }
 
+  @Test
+  public void testHandleRequest__InvalidQueryException() throws Exception {
+    Request request = createStrictMock(Request.class);
+    ResourceInstance resource = createStrictMock(ResourceInstance.class);
+    Query query = createMock(Query.class);
+    InvalidQueryException exception = new InvalidQueryException("test");
+
+    expect(request.getResource()).andReturn(resource);
+    expect(resource.getQuery()).andReturn(query);
+
+    expect(request.getFields()).andReturn(Collections.<String, TemporalInfo>emptyMap());
+
+    expect(request.getQueryPredicate()).andThrow(exception);
+    replay(request, resource, query);
+
+    //test
+    QueryCreateHandler handler = new QueryCreateHandler();
+    Result result = handler.handleRequest(request);
+
+    assertEquals(ResultStatus.STATUS.BAD_REQUEST, result.getStatus().getStatus());
+    assertTrue(result.getStatus().getMessage().contains(exception.getMessage()));
+    verify(request, resource, query);
+  }
+
 }

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/ReadHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/ReadHandlerTest.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/ReadHandlerTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/ReadHandlerTest.java Tue Jan 29 19:19:10 2013
@@ -18,6 +18,7 @@
 
 package org.apache.ambari.server.api.handlers;
 
+import org.apache.ambari.server.api.predicate.InvalidQueryException;
 import org.apache.ambari.server.api.query.Query;
 import org.apache.ambari.server.api.resources.ResourceInstance;
 import org.apache.ambari.server.api.services.Request;
@@ -35,6 +36,7 @@ import java.util.Map;
 import static org.easymock.EasyMock.*;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
 
 /**
  * Unit tests for ReadHandler.
@@ -246,6 +248,30 @@ public class ReadHandlerTest {
     verify(request, resource, query);
   }
 
+  @Test
+  public void testHandleRequest__InvalidQueryException() throws Exception {
+    Request request = createStrictMock(Request.class);
+    ResourceInstance resource = createStrictMock(ResourceInstance.class);
+    Query query = createMock(Query.class);
+    InvalidQueryException exception = new InvalidQueryException("test");
+
+    expect(request.getResource()).andReturn(resource);
+    expect(resource.getQuery()).andReturn(query);
+
+    expect(request.getFields()).andReturn(Collections.<String, TemporalInfo>emptyMap());
+
+    expect(request.getQueryPredicate()).andThrow(exception);
+    replay(request, resource, query);
+
+    //test
+    ReadHandler handler = new ReadHandler();
+    Result result = handler.handleRequest(request);
+
+    assertEquals(ResultStatus.STATUS.BAD_REQUEST, result.getStatus().getStatus());
+    assertTrue(result.getStatus().getMessage().contains(exception.getMessage()));
+    verify(request, resource, query);
+  }
+
   //todo: reverted to just logging the exception and re-throwing it
 //  @Test
 //  public void testHandleRequest__RuntimeException() throws Exception {

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/UpdateHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/UpdateHandlerTest.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/UpdateHandlerTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/handlers/UpdateHandlerTest.java Tue Jan 29 19:19:10 2013
@@ -18,6 +18,7 @@
 
 package org.apache.ambari.server.api.handlers;
 
+import org.apache.ambari.server.api.predicate.InvalidQueryException;
 import org.apache.ambari.server.api.query.Query;
 import org.apache.ambari.server.api.resources.ResourceInstance;
 import org.apache.ambari.server.api.services.ResultStatus;
@@ -28,7 +29,6 @@ import org.apache.ambari.server.api.util
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.RequestStatus;
 import org.apache.ambari.server.controller.spi.Resource;
-import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.junit.Test;
 
 import java.util.*;
@@ -174,4 +174,21 @@ public class UpdateHandlerTest {
       return m_testPm;
     }
   }
+
+  @Test
+  public void testHandleRequest__InvalidQuery() throws Exception {
+    Request request = createNiceMock(Request.class);
+    ResourceInstance resource = createNiceMock(ResourceInstance.class);
+    Exception e = new InvalidQueryException("test exception");
+
+    expect(request.getResource()).andReturn(resource);
+    expect(request.getQueryPredicate()).andThrow(e);
+    replay(request, resource);
+
+    Result result = new UpdateHandler().handleRequest(request);
+    assertEquals(ResultStatus.STATUS.BAD_REQUEST, result.getStatus().getStatus());
+    assertTrue(result.getStatus().getMessage().contains(e.getMessage()));
+
+    verify(request, resource);
+  }
 }
\ No newline at end of file

Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/predicate/QueryLexerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/predicate/QueryLexerTest.java?rev=1440067&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/predicate/QueryLexerTest.java (added)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/predicate/QueryLexerTest.java Tue Jan 29 19:19:10 2013
@@ -0,0 +1,200 @@
+/**
+ * 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.ambari.server.api.predicate;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+import static org.junit.Assert.*;
+
+/**
+ * QueryLexer unit tests
+ */
+public class QueryLexerTest {
+
+  @Test
+  public void testTokens_simple() throws InvalidQueryException {
+    List<Token> listTokens = new ArrayList<Token>();
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "a"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "1"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "&"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_OPEN, "("));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "<="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "b"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "2"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "|"));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, ">"));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "c"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "3"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_CLOSE, ")"));
+
+    QueryLexer lexer = new QueryLexer();
+    Token[] tokens = lexer.tokens("a=1&(b<=2|c>3)");
+
+    assertArrayEquals(listTokens.toArray(new Token[listTokens.size()]), tokens);
+  }
+
+  @Test
+  public void testTokens_multipleBrackets() throws InvalidQueryException {
+    List<Token> listTokens = new ArrayList<Token>();
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "<"));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "a"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "1"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "&"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_OPEN, "("));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "<="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "b"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "2"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "&"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_OPEN, "("));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, ">="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "c"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "3"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "|"));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "!="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "d"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "4"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_CLOSE, ")"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_CLOSE, ")"));
+
+    QueryLexer lexer = new QueryLexer();
+    Token[] tokens = lexer.tokens("a<1&(b<=2&(c>=3|d!=4))");
+
+    assertArrayEquals(listTokens.toArray(new Token[listTokens.size()]), tokens);
+  }
+
+  @Test
+  public void testUnaryNot() throws Exception {
+    List<Token> listTokens = new ArrayList<Token>();
+    listTokens.add(new Token(Token.TYPE.LOGICAL_UNARY_OPERATOR, "!"));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "<"));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "foo"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "5"));
+
+    QueryLexer lexer = new QueryLexer();
+    Token[] tokens = lexer.tokens("!foo<5");
+
+    assertArrayEquals(listTokens.toArray(new Token[listTokens.size()]), tokens);
+  }
+
+  @Test
+  public void testTokens_ignoreFieldsSyntax___noPredicate() throws InvalidQueryException {
+
+    QueryLexer lexer = new QueryLexer();
+    Token[] tokens = lexer.tokens("fields=foo,bar");
+    assertEquals(0, tokens.length);
+  }
+
+  @Test
+  public void testTokens_ignoreFieldsSyntax___fieldsFirst() throws InvalidQueryException {
+
+    List<Token> listTokens = new ArrayList<Token>();
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "foo"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "1"));
+
+    QueryLexer lexer = new QueryLexer();
+    Token[] tokens = lexer.tokens("fields=foo,bar&foo=1");
+
+    assertArrayEquals(listTokens.toArray(new Token[listTokens.size()]), tokens);
+  }
+
+  @Test
+  public void testTokens_ignoreFieldsSyntax___fieldsLast() throws InvalidQueryException {
+
+    List<Token> listTokens = new ArrayList<Token>();
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "foo"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "1"));
+
+    QueryLexer lexer = new QueryLexer();
+    Token[] tokens = lexer.tokens("foo=1&fields=foo,bar");
+
+    assertArrayEquals(listTokens.toArray(new Token[listTokens.size()]), tokens);
+  }
+
+  @Test
+  public void testTokens_ignoreUnderscoreSyntax___noPredicate() throws InvalidQueryException {
+
+    QueryLexer lexer = new QueryLexer();
+    Token[] tokens = lexer.tokens("_=1");
+    assertEquals(0, tokens.length);
+  }
+
+  @Test
+  public void testTokens_ignoreUnderscoreSyntax___fieldsFirst() throws InvalidQueryException {
+
+    List<Token> listTokens = new ArrayList<Token>();
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "foo"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "1"));
+
+    QueryLexer lexer = new QueryLexer();
+    Token[] tokens = lexer.tokens("_=111111&foo=1");
+
+    assertArrayEquals(listTokens.toArray(new Token[listTokens.size()]), tokens);
+  }
+
+  @Test
+  public void testTokens_ignoreUnderscoreSyntax___fieldsLast() throws InvalidQueryException {
+
+    List<Token> listTokens = new ArrayList<Token>();
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "foo"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "1"));
+
+    QueryLexer lexer = new QueryLexer();
+    Token[] tokens = lexer.tokens("foo=1&_=11111");
+
+    assertArrayEquals(listTokens.toArray(new Token[listTokens.size()]), tokens);
+  }
+
+  @Test
+  public void testTokens_invalidRelationalOp() {
+    try {
+      new QueryLexer().tokens("foo=1&bar|5");
+      fail("Expected InvalidQueryException due to invalid relational op");
+    } catch (InvalidQueryException e) {
+      //expected
+    }
+  }
+
+  @Test
+  public void testTokens_invalidLogicalOp() {
+    try {
+      new QueryLexer().tokens("foo=1<5=2");
+      fail("Expected InvalidQueryException due to invalid logical op");
+    } catch (InvalidQueryException e) {
+      //expected
+    }
+  }
+
+  @Test
+  public void testTokens_invalidLogicalOp2() {
+    try {
+      new QueryLexer().tokens("foo=1&&5=2");
+      fail("Expected InvalidQueryException due to invalid logical op");
+    } catch (InvalidQueryException e) {
+      //expected
+    }
+  }
+}

Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/predicate/QueryParserTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/predicate/QueryParserTest.java?rev=1440067&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/predicate/QueryParserTest.java (added)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/predicate/QueryParserTest.java Tue Jan 29 19:19:10 2013
@@ -0,0 +1,201 @@
+/**
+ * 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.ambari.server.api.predicate;
+
+import org.apache.ambari.server.controller.predicate.*;
+import org.apache.ambari.server.controller.spi.Predicate;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.*;
+
+/**
+ * QueryParser unit tests.
+ */
+public class QueryParserTest {
+
+  @Test
+  public void testParse_simple() throws Exception {
+    List<Token> listTokens = new ArrayList<Token>();
+    //a=b
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "a"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "b"));
+
+    QueryParser parser = new QueryParser();
+    Predicate p = parser.parse(listTokens.toArray(new Token[listTokens.size()]));
+
+    assertEquals(new EqualsPredicate<String>("a", "b"), p);
+  }
+
+  @Test
+  public void testParse() throws InvalidQueryException {
+    List<Token> listTokens = new ArrayList<Token>();
+    // foo=bar&(a<1&(b<=2|c>3)&d>=100)|e!=5&!(f=6|g=7)
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "foo"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "bar"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "&"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_OPEN, "("));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "<"));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "a"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "1"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "&"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_OPEN, "("));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "<="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "b"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "2"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "|"));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, ">"));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "c"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "3"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_CLOSE, ")"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "&"));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, ">="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "d"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "100"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_CLOSE, ")"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "|"));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "!="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "e"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "5"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "&"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_UNARY_OPERATOR, "!"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_OPEN, "("));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "f"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "6"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "|"));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "g"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "7"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_CLOSE, ")"));
+
+    QueryParser parser = new QueryParser();
+    Predicate p = parser.parse(listTokens.toArray(new Token[listTokens.size()]));
+
+    EqualsPredicate<String> fooPred = new EqualsPredicate<String>("foo", "bar");
+    LessPredicate<String> aPred = new LessPredicate<String>("a", "1");
+    LessEqualsPredicate<String> bPred = new LessEqualsPredicate<String>("b", "2");
+    GreaterEqualsPredicate<String> cPred = new GreaterEqualsPredicate<String>("c", "3");
+    GreaterEqualsPredicate<String> dPred = new GreaterEqualsPredicate<String>("d", "100");
+    NotPredicate ePred = new NotPredicate(new EqualsPredicate<String>("e", "5"));
+    EqualsPredicate fPred = new EqualsPredicate<String>("f", "6");
+    EqualsPredicate gPRed = new EqualsPredicate<String>("g", "7");
+    OrPredicate bORcPred = new OrPredicate(bPred, cPred);
+    AndPredicate aANDbORcPred = new AndPredicate(aPred, bORcPred);
+    AndPredicate aANDbORcANDdPred = new AndPredicate(aANDbORcPred, dPred);
+    AndPredicate fooANDaANDbORcANDdPred = new AndPredicate(fooPred, aANDbORcANDdPred);
+    OrPredicate fORgPred = new OrPredicate(fPred, gPRed);
+    NotPredicate NOTfORgPred = new NotPredicate(fORgPred);
+    AndPredicate eANDNOTfORgPred = new AndPredicate(ePred, NOTfORgPred);
+    OrPredicate rootPredicate = new OrPredicate(fooANDaANDbORcANDdPred, eANDNOTfORgPred);
+
+    assertEquals(rootPredicate, p);
+  }
+
+  @Test
+  public void testParse_simpleNotOp() throws Exception {
+    List<Token> listTokens = new ArrayList<Token>();
+    //!a=b
+    listTokens.add(new Token(Token.TYPE.LOGICAL_UNARY_OPERATOR, "!"));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "a"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "b"));
+
+    QueryParser parser = new QueryParser();
+    Predicate p = parser.parse(listTokens.toArray(new Token[listTokens.size()]));
+
+    assertEquals(new NotPredicate(new EqualsPredicate<String>("a", "b")), p);
+  }
+
+  @Test
+  public void testParse_NotOp() throws Exception {
+    List<Token> listTokens = new ArrayList<Token>();
+     //a=1&!b=2
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "a"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "1"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "&"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_UNARY_OPERATOR, "!"));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "b"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "2"));
+
+    QueryParser parser = new QueryParser();
+    Predicate p = parser.parse(listTokens.toArray(new Token[listTokens.size()]));
+
+    EqualsPredicate aPred = new EqualsPredicate<String>("a", "1");
+    EqualsPredicate bPred = new EqualsPredicate<String>("b", "2");
+    NotPredicate notPred = new NotPredicate(bPred);
+    AndPredicate andPred = new AndPredicate(aPred, notPred);
+
+    assertEquals(andPred, p);
+  }
+
+  @Test
+  public void testParse_noTokens() throws InvalidQueryException {
+    assertNull(new QueryParser().parse(new Token[0]));
+  }
+
+  @Test
+  public void testParse_mismatchedBrackets() {
+    List<Token> listTokens = new ArrayList<Token>();
+    // a=1&(b<=2|c>3
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "a"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "1"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "&"));
+    listTokens.add(new Token(Token.TYPE.BRACKET_OPEN, "("));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "<="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "b"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "2"));
+    listTokens.add(new Token(Token.TYPE.LOGICAL_OPERATOR, "|"));
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, ">"));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "c"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "3"));
+
+    try {
+      new QueryParser().parse(listTokens.toArray(new Token[listTokens.size()]));
+      fail("Expected InvalidQueryException due to missing closing bracket");
+    } catch (InvalidQueryException e) {
+      // expected
+    }
+  }
+
+  @Test
+  public void testParse_outOfOrderTokens() {
+    List<Token> listTokens = new ArrayList<Token>();
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+    listTokens.add(new Token(Token.TYPE.PROPERTY_OPERAND, "a"));
+    listTokens.add(new Token(Token.TYPE.VALUE_OPERAND, "1"));
+    // should be a logical operator
+    listTokens.add(new Token(Token.TYPE.RELATIONAL_OPERATOR, "="));
+
+    try {
+      new QueryParser().parse(listTokens.toArray(new Token[listTokens.size()]));
+      fail("Expected InvalidQueryException due to invalid last token");
+    } catch (InvalidQueryException e) {
+      // expected
+    }
+  }
+}

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/BaseRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/BaseRequestTest.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/BaseRequestTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/BaseRequestTest.java Tue Jan 29 19:19:10 2013
@@ -18,25 +18,25 @@
 
 package org.apache.ambari.server.api.services;
 
+import org.apache.ambari.server.api.predicate.InvalidQueryException;
+import org.apache.ambari.server.api.predicate.PredicateCompiler;
 import org.apache.ambari.server.controller.internal.TemporalInfoImpl;
-import org.apache.ambari.server.controller.predicate.*;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.TemporalInfo;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
+import org.junit.Test;
 
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.UriInfo;
+import java.io.UnsupportedEncodingException;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URLEncoder;
-import java.util.HashSet;
 import java.util.Map;
-import java.util.Set;
 
 import static org.easymock.EasyMock.*;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 
 /**
  * Base tests for service requests.
@@ -44,40 +44,69 @@ import static org.junit.Assert.assertTru
 public abstract class BaseRequestTest {
 
   public void testGetQueryPredicate(String uriString) throws Exception {
+    PredicateCompiler compiler = createStrictMock(PredicateCompiler.class);
+    Predicate p = createMock(Predicate.class);
     UriInfo uriInfo = createMock(UriInfo.class);
     URI uri = new URI(URLEncoder.encode(uriString, "UTF-8"));
 
     expect(uriInfo.getRequestUri()).andReturn(uri);
+    expect(compiler.compile(uriString.substring(uriString.indexOf("?") + 1))).andReturn(p);
 
-    replay(uriInfo);
+    replay(uriInfo, compiler, p);
 
-    Request request = getTestRequest(null, null, uriInfo);
+    Request request = getTestRequest(null, null, uriInfo, compiler);
 
-    Predicate predicate = request.getQueryPredicate();
+    assertEquals(p, request.getQueryPredicate());
 
-    Set<BasePredicate> setPredicates = new HashSet<BasePredicate>();
-    setPredicates.add(new EqualsPredicate<String>("foo", "bar"));
+    verify(uriInfo, compiler, p);
+  }
+
+  @Test
+  public void testGetQueryPredicate_noQueryString() throws Exception {
+    String uriString = "http://localhost.com:8080/api/v1/clusters";
+    PredicateCompiler compiler = createStrictMock(PredicateCompiler.class);
+    UriInfo uriInfo = createMock(UriInfo.class);
+    URI uri = new URI(URLEncoder.encode(uriString, "UTF-8"));
+
+    expect(uriInfo.getRequestUri()).andReturn(uri);
+
+    replay(uriInfo, compiler);
 
-    Set<BasePredicate> setOrPredicates = new HashSet<BasePredicate>();
-    setOrPredicates.add(new EqualsPredicate<String>("orProp1", "5"));
-    setOrPredicates.add(new NotPredicate(new EqualsPredicate<String>("orProp2", "6")));
-    setOrPredicates.add(new LessPredicate<String>("orProp3", "100"));
-    setPredicates.add(new OrPredicate(setOrPredicates.toArray(new BasePredicate[3])));
+    Request request = getTestRequest(null, null, uriInfo, compiler);
 
-    setPredicates.add(new NotPredicate(new EqualsPredicate<String>("prop", "5")));
-    setPredicates.add(new GreaterPredicate<String>("prop2", "10"));
-    setPredicates.add(new GreaterEqualsPredicate<String>("prop3", "20"));
-    setPredicates.add(new LessPredicate<String>("prop4", "500"));
-    setPredicates.add(new LessEqualsPredicate<String>("prop5", "1"));
-    Predicate expectedPredicate = new AndPredicate(setPredicates.toArray(new BasePredicate[6]));
+    assertEquals(null, request.getQueryPredicate());
+
+    verify(uriInfo, compiler);
+  }
+
+  @Test
+  public void testGetQueryPredicate_invalidQuery() throws Exception {
+    String uriString = "http://localhost.com:8080/api/v1/clusters?&foo|";
+    PredicateCompiler compiler = createStrictMock(PredicateCompiler.class);
+    UriInfo uriInfo = createMock(UriInfo.class);
+    URI uri = new URI(URLEncoder.encode(uriString, "UTF-8"));
+
+    expect(uriInfo.getRequestUri()).andReturn(uri);
+    expect(compiler.compile(uriString.substring(uriString.indexOf("?") + 1))).
+        andThrow(new InvalidQueryException("test"));
+    replay(uriInfo, compiler);
+
+    Request request = getTestRequest(null, null, uriInfo, compiler);
+
+    try {
+      request.getQueryPredicate();
+      fail("Expected InvalidQueryException due to invalid query");
+    } catch (InvalidQueryException e) {
+      //expected
+    }
 
-    assertEquals(expectedPredicate, predicate);
+    verify(uriInfo, compiler);
   }
 
   public void testGetFields(String fields) {
     UriInfo uriInfo = createMock(UriInfo.class);
     MultivaluedMap<String, String> mapQueryParams = createMock(MultivaluedMap.class);
-    Request request = getTestRequest(null, null, uriInfo);
+    Request request = getTestRequest(null, null, uriInfo, null);
 
     expect(uriInfo.getQueryParameters()).andReturn(mapQueryParams);
     expect(mapQueryParams.getFirst("fields")).andReturn(fields);
@@ -119,5 +148,6 @@ public abstract class BaseRequestTest {
     verify(uriInfo, mapQueryParams);
   }
 
-  protected abstract Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo);
+   protected abstract Request getTestRequest(HttpHeaders headers, String body,
+                                             UriInfo uriInfo, PredicateCompiler compiler);
 }

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/DeleteRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/DeleteRequestTest.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/DeleteRequestTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/DeleteRequestTest.java Tue Jan 29 19:19:10 2013
@@ -18,6 +18,7 @@
 
 package org.apache.ambari.server.api.services;
 
+import org.apache.ambari.server.api.predicate.PredicateCompiler;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -46,7 +47,12 @@ public class DeleteRequestTest extends B
     super.testGetFields(fields);
   }
 
-  protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo) {
-    return new DeleteRequest(headers, body, uriInfo, null);
+  protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo, final PredicateCompiler compiler) {
+    return new DeleteRequest(headers, body, uriInfo, null) {
+      @Override
+      protected PredicateCompiler getPredicateCompiler() {
+        return compiler;
+      }
+    };
   }
 }

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/GetRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/GetRequestTest.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/GetRequestTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/GetRequestTest.java Tue Jan 29 19:19:10 2013
@@ -18,6 +18,7 @@
 
 package org.apache.ambari.server.api.services;
 
+import org.apache.ambari.server.api.predicate.PredicateCompiler;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -47,7 +48,12 @@ public class GetRequestTest extends Base
     super.testGetFields(fields);
   }
 
-  protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo) {
-    return new GetRequest(headers, body, uriInfo, null);
+  protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo, final PredicateCompiler compiler) {
+    return new GetRequest(headers, body, uriInfo, null) {
+      @Override
+      protected PredicateCompiler getPredicateCompiler() {
+        return compiler;
+      }
+    };
   }
 }

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PostRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PostRequestTest.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PostRequestTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PostRequestTest.java Tue Jan 29 19:19:10 2013
@@ -18,6 +18,7 @@
 
 package org.apache.ambari.server.api.services;
 
+import org.apache.ambari.server.api.predicate.PredicateCompiler;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -46,7 +47,12 @@ public class PostRequestTest extends Bas
     super.testGetFields(fields);
   }
 
-  protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo) {
-    return new PostRequest(headers, body, uriInfo, null);
+  protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo, final PredicateCompiler compiler) {
+    return new PostRequest(headers, body, uriInfo, null) {
+      @Override
+      protected PredicateCompiler getPredicateCompiler() {
+        return compiler;
+      }
+    };
   }
 }

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PutRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PutRequestTest.java?rev=1440067&r1=1440066&r2=1440067&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PutRequestTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PutRequestTest.java Tue Jan 29 19:19:10 2013
@@ -18,6 +18,7 @@
 
 package org.apache.ambari.server.api.services;
 
+import org.apache.ambari.server.api.predicate.PredicateCompiler;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -46,7 +47,12 @@ public class PutRequestTest extends Base
     super.testGetFields(fields);
   }
 
-  protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo) {
-    return new PutRequest(headers, body, uriInfo, null);
+  protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo, final PredicateCompiler compiler) {
+    return new PutRequest(headers, body, uriInfo, null) {
+      @Override
+      protected PredicateCompiler getPredicateCompiler() {
+        return compiler;
+      }
+    };
   }
 }



Mime
View raw message