use of com.serotonin.m2m2.db.dao.QueryBuilder in project ma-core-public by infiniteautomation.
the class DataPointPermissionTest method testDeleteDataPoints.
@Test
public void testDeleteDataPoints() {
// Insert some data points
Set<Role> readRoles = this.createRoles(2).stream().map(r -> r.getRole()).collect(Collectors.toSet());
List<IDataPoint> points = this.createMockDataPoints(2, false, MangoPermission.requireAnyRole(readRoles), new MangoPermission());
DataPointService service = Common.getBean(DataPointService.class);
runAs.runAs(new PermissionHolder() {
@Override
public String getPermissionHolderName() {
return "Test";
}
@Override
public boolean isPermissionHolderDisabled() {
return false;
}
@Override
public Set<Role> getRoles() {
return readRoles;
}
}, () -> {
List<Integer> ids = points.stream().map(dp -> dp.getId()).collect(Collectors.toList());
QueryBuilder<DataPointVO> query = service.buildQuery().in("id", ids.toArray());
List<DataPointVO> vos = query.query();
assertEquals(points.size(), vos.size());
for (DataPointVO vo : vos) {
assertTrue(points.contains(vo));
}
List<Integer> existing = getPermissionIds(null);
// Delete a point
DataPointDao.getInstance().delete(vos.get(0));
// The set and read permission for point 2 still exist
assertEquals(existing.size(), getPermissionIds(null).size());
// ensure all minterms ect still exist for the un-deleted point
vos = query.query();
assertEquals(1, vos.size());
for (DataPointVO vo : vos) {
assertTrue(points.contains(vo));
}
});
}
use of com.serotonin.m2m2.db.dao.QueryBuilder in project ma-core-public by infiniteautomation.
the class DataPointQueryPermissionTest method testEmptyPermission.
@Test
public void testEmptyPermission() {
// Insert some data points
Set<Role> readRoles = this.createRoles(2).stream().map(r -> r.getRole()).collect(Collectors.toSet());
List<IDataPoint> unreadable = this.createMockDataPoints(5, false, new MangoPermission(), new MangoPermission());
DataPointService service = Common.getBean(DataPointService.class);
runAs.runAs(new PermissionHolder() {
@Override
public String getPermissionHolderName() {
return "Test";
}
@Override
public boolean isPermissionHolderDisabled() {
return false;
}
@Override
public Set<Role> getRoles() {
return readRoles;
}
}, () -> {
List<Integer> ids = unreadable.stream().map(dp -> dp.getId()).collect(Collectors.toList());
QueryBuilder<DataPointVO> query = service.buildQuery().in("id", ids.toArray());
List<DataPointVO> vos = query.query();
assertEquals(0, vos.size());
});
}
use of com.serotonin.m2m2.db.dao.QueryBuilder in project ma-core-public by infiniteautomation.
the class DataPointQueryPermissionTest method testDeleteSingleRoleFromOrSet.
@Test
public void testDeleteSingleRoleFromOrSet() {
// Insert some data points
Set<Role> readRoles = this.createRoles(2).stream().map(r -> r.getRole()).collect(Collectors.toSet());
List<IDataPoint> points = this.createMockDataPoints(5, false, MangoPermission.requireAnyRole(readRoles), new MangoPermission());
List<IDataPoint> unreadable = this.createMockDataPoints(5, false, new MangoPermission(), new MangoPermission());
Role role = readRoles.iterator().next();
Common.getBean(RoleDao.class).delete(role.getId());
readRoles.remove(role);
DataPointService service = Common.getBean(DataPointService.class);
runAs.runAs(new PermissionHolder() {
@Override
public String getPermissionHolderName() {
return "Test";
}
@Override
public boolean isPermissionHolderDisabled() {
return false;
}
@Override
public Set<Role> getRoles() {
return readRoles;
}
}, () -> {
List<Integer> ids = points.stream().map(dp -> dp.getId()).collect(Collectors.toList());
ids.addAll(unreadable.stream().map(dp -> dp.getId()).collect(Collectors.toList()));
QueryBuilder<DataPointVO> query = service.buildQuery().in("id", ids.toArray());
List<DataPointVO> vos = query.query();
assertEquals(points.size(), vos.size());
for (DataPointVO vo : vos) {
assertTrue(points.contains(vo));
}
});
}
use of com.serotonin.m2m2.db.dao.QueryBuilder in project ma-core-public by infiniteautomation.
the class DataPointQueryPermissionTest method testDeleteAllRoles.
@Test
public void testDeleteAllRoles() {
// Insert some data points
Set<Role> readRoles = this.createRoles(2).stream().map(r -> r.getRole()).collect(Collectors.toSet());
List<IDataPoint> points = this.createMockDataPoints(5, false, MangoPermission.requireAllRoles(readRoles), new MangoPermission());
List<IDataPoint> unreadable = this.createMockDataPoints(5, false, new MangoPermission(), new MangoPermission());
RoleDao roleDao = Common.getBean(RoleDao.class);
for (Role role : readRoles) {
roleDao.delete(role.getId());
}
DataPointService service = Common.getBean(DataPointService.class);
runAs.runAs(new PermissionHolder() {
@Override
public String getPermissionHolderName() {
return "Test";
}
@Override
public boolean isPermissionHolderDisabled() {
return false;
}
@Override
public Set<Role> getRoles() {
// This is odd as these roles should not be on a user either, but wanted to t
return readRoles;
}
}, () -> {
List<Integer> ids = points.stream().map(dp -> dp.getId()).collect(Collectors.toList());
ids.addAll(unreadable.stream().map(dp -> dp.getId()).collect(Collectors.toList()));
QueryBuilder<DataPointVO> query = service.buildQuery().in("id", ids.toArray());
List<DataPointVO> vos = query.query();
assertEquals(0, vos.size());
});
}
use of com.serotonin.m2m2.db.dao.QueryBuilder in project ma-core-public by infiniteautomation.
the class QueryBuilder method close.
public QueryBuilder<T> close() {
if (stack.isEmpty()) {
throw new TranslatableRuntimeException(new TranslatableMessage("dao.query.noGroupOpen"));
}
Condition condition = group.toCondition();
group = stack.pop();
group.conditions.add(condition);
return this;
}
Aggregations