Search in sources :

Example 1 with QueryBuilder

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());
    });
}
Also used : RunAs(com.infiniteautomation.mango.spring.components.RunAs) Role(com.serotonin.m2m2.vo.role.Role) Common(com.serotonin.m2m2.Common) DataSourceDao(com.serotonin.m2m2.db.dao.DataSourceDao) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) Permissions(com.infiniteautomation.mango.db.tables.Permissions) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Collectors(java.util.stream.Collectors) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) QueryBuilder(com.serotonin.m2m2.db.dao.QueryBuilder) List(java.util.List) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DSLContext(org.jooq.DSLContext) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) Assert.assertEquals(org.junit.Assert.assertEquals) PermissionsMinterms(com.infiniteautomation.mango.db.tables.PermissionsMinterms) Before(org.junit.Before) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) Set(java.util.Set) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Role(com.serotonin.m2m2.vo.role.Role) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Test(org.junit.Test)

Example 2 with QueryBuilder

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));
        }
    });
}
Also used : RunAs(com.infiniteautomation.mango.spring.components.RunAs) Role(com.serotonin.m2m2.vo.role.Role) Common(com.serotonin.m2m2.Common) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Collectors(java.util.stream.Collectors) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) QueryBuilder(com.serotonin.m2m2.db.dao.QueryBuilder) List(java.util.List) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) Set(java.util.Set) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Role(com.serotonin.m2m2.vo.role.Role) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Test(org.junit.Test)

Example 3 with QueryBuilder

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));
        }
    });
}
Also used : RunAs(com.infiniteautomation.mango.spring.components.RunAs) Role(com.serotonin.m2m2.vo.role.Role) Common(com.serotonin.m2m2.Common) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Collectors(java.util.stream.Collectors) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) QueryBuilder(com.serotonin.m2m2.db.dao.QueryBuilder) List(java.util.List) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) Set(java.util.Set) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Role(com.serotonin.m2m2.vo.role.Role) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Test(org.junit.Test)

Example 4 with QueryBuilder

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));
        }
    });
}
Also used : RunAs(com.infiniteautomation.mango.spring.components.RunAs) Role(com.serotonin.m2m2.vo.role.Role) Common(com.serotonin.m2m2.Common) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Collectors(java.util.stream.Collectors) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) QueryBuilder(com.serotonin.m2m2.db.dao.QueryBuilder) List(java.util.List) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) Set(java.util.Set) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Role(com.serotonin.m2m2.vo.role.Role) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Test(org.junit.Test)

Example 5 with QueryBuilder

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());
}
Also used : BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Measurement(org.openjdk.jmh.annotations.Measurement) Blackhole(org.openjdk.jmh.infra.Blackhole) BenchmarkRunner(com.infiniteautomation.mango.benchmarks.BenchmarkRunner) HashMap(java.util.HashMap) Scope(org.openjdk.jmh.annotations.Scope) Warmup(org.openjdk.jmh.annotations.Warmup) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) PointEventDetectorRT(com.serotonin.m2m2.rt.event.detectors.PointEventDetectorRT) Threads(org.openjdk.jmh.annotations.Threads) DatabaseProxyFactory(com.serotonin.m2m2.db.DatabaseProxyFactory) Setup(org.openjdk.jmh.annotations.Setup) Common(com.serotonin.m2m2.Common) Mode(org.openjdk.jmh.annotations.Mode) Param(org.openjdk.jmh.annotations.Param) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) EventInstanceService(com.infiniteautomation.mango.spring.service.EventInstanceService) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) IOException(java.io.IOException) UUID(java.util.UUID) State(org.openjdk.jmh.annotations.State) Collectors(java.util.stream.Collectors) Benchmark(org.openjdk.jmh.annotations.Benchmark) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) EventManagerImpl(com.serotonin.m2m2.rt.EventManagerImpl) MockMango(com.infiniteautomation.mango.benchmarks.MockMango) Level(org.openjdk.jmh.annotations.Level) Fork(org.openjdk.jmh.annotations.Fork) EventDetectorDao(com.serotonin.m2m2.db.dao.EventDetectorDao) Bean(org.springframework.context.annotation.Bean) Assert(org.junit.Assert) AlarmLevels(com.serotonin.m2m2.rt.event.AlarmLevels) DatabaseType(com.serotonin.m2m2.db.DatabaseType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Threads(org.openjdk.jmh.annotations.Threads) Measurement(org.openjdk.jmh.annotations.Measurement) Warmup(org.openjdk.jmh.annotations.Warmup) Fork(org.openjdk.jmh.annotations.Fork) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Benchmark(org.openjdk.jmh.annotations.Benchmark) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit)

Aggregations

Common (com.serotonin.m2m2.Common)9 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)9 List (java.util.List)9 Collectors (java.util.stream.Collectors)9 RunAs (com.infiniteautomation.mango.spring.components.RunAs)8 DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)8 MangoTestBase (com.serotonin.m2m2.MangoTestBase)8 QueryBuilder (com.serotonin.m2m2.db.dao.QueryBuilder)8 IDataPoint (com.serotonin.m2m2.vo.IDataPoint)8 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)8 Role (com.serotonin.m2m2.vo.role.Role)8 Set (java.util.Set)8 Assert.assertEquals (org.junit.Assert.assertEquals)8 Assert.assertTrue (org.junit.Assert.assertTrue)8 Before (org.junit.Before)8 Test (org.junit.Test)8 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)6 DatabaseProxy (com.serotonin.m2m2.db.DatabaseProxy)3 Permissions (com.infiniteautomation.mango.db.tables.Permissions)2 PermissionsMinterms (com.infiniteautomation.mango.db.tables.PermissionsMinterms)2