Search in sources :

Example 6 with InvalidQueryException

use of org.alfresco.rest.framework.resource.parameters.where.InvalidQueryException in project alfresco-remote-api by Alfresco.

the class WhereTests method existClauseTest.

@Test
public void existClauseTest() {
    Query theQuery = getWhereClause(null);
    assertNotNull(theQuery);
    assertTrue("Null passed in so nothing to theQuery.", theQuery.getTree() == null);
    try {
        theQuery = getWhereClause("fred");
        fail("Should throw an InvalidQueryException");
    } catch (InvalidQueryException error) {
    // this is correct
    }
    try {
        theQuery = getWhereClause("(noClosingBracket");
        fail("Should throw an InvalidQueryException");
    } catch (InvalidQueryException error) {
    // this is correct
    }
    try {
        theQuery = getWhereClause("noOpeningBracket)");
        fail("Should throw an InvalidQueryException");
    } catch (InvalidQueryException error) {
    // this is correct
    }
    try {
        theQuery = getWhereClause("(EXISTS(target.file))");
        fail("Should throw an InvalidQueryException");
    } catch (InvalidQueryException error) {
    // this is correct
    }
    theQuery = getWhereClause("(exists(/target/file))");
    assertExistsPropertyEquals("/target/file", theQuery, false);
    theQuery = getWhereClause("(EXISTS(b))");
    assertExistsPropertyEquals("b", theQuery, false);
    theQuery = getWhereClause("  ( EXISTS ( whitespace ) )  ");
    assertExistsPropertyEquals("whitespace", theQuery, false);
    theQuery = getWhereClause("(exists ( folder ))");
    assertExistsPropertyEquals("folder", theQuery, false);
    theQuery = getWhereClause("(NOT EXISTS(b))");
    assertExistsPropertyEquals("b", theQuery, true);
    theQuery = getWhereClause(" (NOT EXISTS(b))");
    assertExistsPropertyEquals("b", theQuery, true);
    theQuery = getWhereClause("(  NOT EXISTS(b))");
    assertExistsPropertyEquals("b", theQuery, true);
    theQuery = getWhereClause("  (  NOT EXISTS(b))");
    assertExistsPropertyEquals("b", theQuery, true);
    try {
        theQuery = getWhereClause("(exists  folder)");
        fail("Should throw an InvalidQueryException, 'folder' should have a bracket around it");
    } catch (InvalidQueryException error) {
    // this is correct
    }
    theQuery = getWhereClause("(EXISTS(/target/folder) AND NOT EXISTS(/target/site))");
    assertNotNull(theQuery);
    CommonTree tree = theQuery.getTree();
    assertNotNull(tree);
    QueryHelper.walk(theQuery, new WalkerCallbackAdapter() {

        int i = 0;

        @Override
        public void exists(String propertyName, boolean negated) {
            if (i == 0) {
                assertTrue("/target/folder".equals(propertyName));
            } else {
                assertTrue("/target/site".equals(propertyName));
            }
            i++;
        }

        @Override
        public void and() {
        // We don't need to do anything in this method.  However, overriding the method indicates that AND is
        // supported.  OR is not supported at the same time.
        }
    });
    try {
        theQuery = getWhereClause("(EXISTS(/target/folder)OR EXISTS(/target/site))");
        fail("Should throw an InvalidQueryException, the OR should have a space before it.");
    } catch (InvalidQueryException error) {
    // this is correct
    }
    theQuery = getWhereClause("(NOT EXISTS(/target/folder) OR EXISTS(/target/site))");
    QueryHelper.walk(theQuery, new WalkerCallbackAdapter() {

        @Override
        public void exists(String propertyName, boolean negated) {
            if (negated) {
                assertTrue("/target/folder".equals(propertyName));
            } else {
                assertTrue("/target/site".equals(propertyName));
            }
        }

        @Override
        public void or() {
        // We don't need to do anything in this method.  However, overriding the method indicates that OR is
        // supported.  AND is not supported at the same time.
        }
    });
    theQuery = getWhereClause("(EXISTS   (  /target/folder  )   OR   EXISTS(  /target/site  )  )");
    QueryHelper.walk(theQuery, new WalkerCallbackAdapter() {

        int i = 0;

        @Override
        public void exists(String propertyName, boolean negated) {
            if (i == 0) {
                assertTrue("/target/folder".equals(propertyName));
            } else {
                assertTrue("/target/site".equals(propertyName));
            }
            i++;
        }

        @Override
        public void or() {
        // We don't need to do anything in this method.  However, overriding the method indicates that OR is
        // supported.  AND is not supported at the same time.
        }
    });
    theQuery = getWhereClause("(EXISTS(target/file) AND EXISTS(target/folder) AND EXISTS(target/site))");
    QueryHelper.walk(theQuery, new WalkerCallbackAdapter() {

        int i = 0;

        @Override
        public void exists(String propertyName, boolean negated) {
            switch(i) {
                case 0:
                    assertTrue("target/file".equals(propertyName));
                    break;
                case 1:
                    assertTrue("target/folder".equals(propertyName));
                    break;
                case 2:
                    assertTrue("target/site".equals(propertyName));
                    break;
                default:
                    break;
            }
            i++;
        }

        @Override
        public void and() {
        // We don't need to do anything in this method.  However, overriding the method indicates that AND is
        // supported.  OR is not supported at the same time.
        }
    });
}
Also used : Query(org.alfresco.rest.framework.resource.parameters.where.Query) CommonTree(org.antlr.runtime.tree.CommonTree) InvalidQueryException(org.alfresco.rest.framework.resource.parameters.where.InvalidQueryException) WalkerCallbackAdapter(org.alfresco.rest.framework.resource.parameters.where.QueryHelper.WalkerCallbackAdapter) Test(org.junit.Test)

Aggregations

InvalidQueryException (org.alfresco.rest.framework.resource.parameters.where.InvalidQueryException)6 Test (org.junit.Test)5 Query (org.alfresco.rest.framework.resource.parameters.where.Query)4 WalkerCallbackAdapter (org.alfresco.rest.framework.resource.parameters.where.QueryHelper.WalkerCallbackAdapter)2 CommonTree (org.antlr.runtime.tree.CommonTree)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 FormNotFoundException (org.alfresco.repo.forms.FormNotFoundException)1 IntegrityException (org.alfresco.repo.node.integrity.IntegrityException)1 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)1 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)1 DeletedResourceException (org.alfresco.rest.framework.core.exceptions.DeletedResourceException)1 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)1 ErrorResponse (org.alfresco.rest.framework.core.exceptions.ErrorResponse)1 InsufficientStorageException (org.alfresco.rest.framework.core.exceptions.InsufficientStorageException)1 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)1 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)1 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)1 RelationshipResourceNotFoundException (org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException)1 StaleEntityException (org.alfresco.rest.framework.core.exceptions.StaleEntityException)1