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());
}
Aggregations