Search in sources :

Example 26 with WatchListVO

use of com.serotonin.m2m2.watchlist.WatchListVO in project ma-modules-public by infiniteautomation.

the class WatchlistSqlVisitorTest method testRQL.

@Test
public void testRQL() throws IOException {
    // Create a User
    User user = new User();
    user.setUsername("test");
    user.setName("test");
    user.setEmail("test@test.com");
    user.setPassword("usernametest");
    user.setPermissions("user,test,permission1");
    validate(user);
    UserDao.instance.saveUser(user);
    // Insert some watchlists
    for (int i = 0; i < 120; i++) {
        WatchListVO wl = new WatchListVO();
        wl.setXid(WatchListDao.instance.generateUniqueXid());
        wl.setName("Watchilst " + i);
        wl.setUserId(user.getId());
        wl.setReadPermission("permission1");
        WatchListDao.instance.saveWatchList(wl);
    }
    String rql = "limit(100,0)";
    RQLParser parser = new RQLParser();
    ASTNode root = null;
    ASTNode queryNode = parser.parse(rql);
    // Combine the existing query with an AND node
    if (queryNode == null) {
        root = new ASTNode("eq", "userId", user.getId());
    } else {
        // Filter by Permissions
        Set<String> permissions = Permissions.explodePermissionGroups(user.getPermissions());
        ASTNode permRQL = new ASTNode("in", "readPermission", permissions);
        root = new ASTNode("or", new ASTNode("eq", "userId", user.getId()), permRQL, queryNode);
    }
    final AtomicLong selectCounter = new AtomicLong();
    final AtomicLong countValue = new AtomicLong();
    StreamableRowCallback<WatchListVO> selectCallback = new StreamableRowCallback<WatchListVO>() {

        @Override
        public void row(WatchListVO row, int index) throws Exception {
            selectCounter.incrementAndGet();
        }
    };
    StreamableRowCallback<Long> countCallback = new StreamableRowCallback<Long>() {

        @Override
        public void row(Long row, int index) throws Exception {
            countValue.set(row);
        }
    };
    StreamableSqlQuery<WatchListVO> query = WatchListDao.instance.createQuery(root, selectCallback, countCallback, modelMap, appenders, true);
    query.query();
    query.count();
    assertEquals(100L, selectCounter.get());
    assertEquals(120L, countValue.get());
}
Also used : User(com.serotonin.m2m2.vo.User) AtomicLong(java.util.concurrent.atomic.AtomicLong) RQLParser(net.jazdw.rql.parser.RQLParser) ASTNode(net.jazdw.rql.parser.ASTNode) AtomicLong(java.util.concurrent.atomic.AtomicLong) StreamableRowCallback(com.infiniteautomation.mango.db.query.StreamableRowCallback) Test(org.junit.Test)

Aggregations

User (com.serotonin.m2m2.vo.User)18 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)13 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)11 WatchListVO (com.serotonin.m2m2.watchlist.WatchListVO)6 ArrayList (java.util.ArrayList)6 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)5 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)5 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)5 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)3 WatchListDataPointModel (com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListDataPointModel)3 WatchListModel (com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListModel)3 ASTNode (net.jazdw.rql.parser.ASTNode)3 IntStringPair (com.serotonin.db.pair.IntStringPair)2 JsonException (com.serotonin.json.JsonException)2