use of com.serotonin.m2m2.db.dao.QueryBuilder in project ma-core-public by infiniteautomation.
the class DataPointPermissionTest method testDeleteDataPoint.
/**
* Test to ensure an un-used permission is deleted
*/
@Test
public void testDeleteDataPoint() {
// Insert some data points
Set<Role> readRoles = this.createRoles(2).stream().map(r -> r.getRole()).collect(Collectors.toSet());
List<IDataPoint> points = this.createMockDataPoints(1, 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 the source and point
DataSourceDao.getInstance().delete(vos.get(0).getDataSourceId());
// We should have removed 1 permission
assertEquals(existing.size() - 1, getPermissionIds(null).size());
});
}
use of com.serotonin.m2m2.db.dao.QueryBuilder in project ma-core-public by infiniteautomation.
the class DataPointQueryPermissionTest method testDeleteSingleRoleFromAndSet.
/**
* Delete a single role from an and set (this results in the user still having permission)
*/
@Test
public void testDeleteSingleRoleFromAndSet() {
// Insert some data points
Set<Role> readRoles = this.createRoles(3).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());
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 testOrPermission.
@Test
public void testOrPermission() {
// 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());
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 testAndPermission.
@Test
public void testAndPermission() {
// 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());
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 DataPointEventsBenchmarks method queryEventsByDataPointTag.
@Benchmark
@Threads(1)
@Fork(1)
@BenchmarkMode({ Mode.SampleTime })
@Measurement(iterations = 1, batchSize = 5)
@Warmup(iterations = 0)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void queryEventsByDataPointTag(BenchmarkParams params, Blackhole blackhole) throws IOException {
// TODO there is a bug where we cannot use the QueryBuilder from this service to query on tags
AtomicInteger count = new AtomicInteger();
String rql = params.tags.entrySet().stream().map(entry -> "eq(tags." + entry.getKey() + "," + entry.getValue() + ")").collect(Collectors.joining("&"));
params.eventInstanceService.query(rql, evt -> {
if (params.loadReadPermission) {
evt.getReadPermission();
}
count.getAndIncrement();
blackhole.consume(evt);
});
Assert.assertEquals(params.dataPoints, count.get());
}
Aggregations