Search in sources :

Example 11 with DataPointService

use of com.infiniteautomation.mango.spring.service.DataPointService 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 12 with DataPointService

use of com.infiniteautomation.mango.spring.service.DataPointService 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 13 with DataPointService

use of com.infiniteautomation.mango.spring.service.DataPointService 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 14 with DataPointService

use of com.infiniteautomation.mango.spring.service.DataPointService in project ma-core-public by infiniteautomation.

the class MangoTestBase method createMockDataPoint.

protected DataPointVO createMockDataPoint(int id, String xid, String name, String deviceName, boolean enabled, int dataSourceId, String dataSourceXid, MangoPermission readPermission, MangoPermission setPermission, MockPointLocatorVO vo) {
    DataPointService service = Common.getBean(DataPointService.class);
    DataPointVO dp = new DataPointVO();
    dp.setId(id);
    dp.setXid(xid);
    dp.setName(name);
    dp.setDeviceName(deviceName);
    dp.setEnabled(enabled);
    dp.setPointLocator(vo);
    dp.setDataSourceId(dataSourceId);
    dp.setDataSourceXid(dataSourceXid);
    dp.setReadPermission(readPermission);
    dp.setSetPermission(setPermission);
    try {
        return service.insert(dp);
    } catch (ValidationException e) {
        String failureMessage = "";
        for (ProcessMessage m : e.getValidationResult().getMessages()) {
            String messagePart = m.getContextKey() + " -> " + m.getContextualMessage().translate(Common.getTranslations()) + "\n";
            failureMessage += messagePart;
        }
        fail(failureMessage);
        return null;
    }
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage)

Example 15 with DataPointService

use of com.infiniteautomation.mango.spring.service.DataPointService in project ma-core-public by infiniteautomation.

the class MangoTestBase method createMockDataPoint.

protected DataPointVO createMockDataPoint(MockDataSourceVO ds, Consumer<DataPointVO> customizer) {
    DataPointService service = Common.getBean(DataPointService.class);
    DataPointVO dp = new DataPointVO();
    dp.setName(UUID.randomUUID().toString());
    dp.setDeviceName(ds.getName());
    dp.setPointLocator(new MockPointLocatorVO(DataType.NUMERIC, true));
    dp.setDataSourceId(ds.getId());
    customizer.accept(dp);
    try {
        return service.insert(dp);
    } catch (ValidationException e) {
        fail(e.getValidationErrorMessage(Common.getTranslations()));
        return null;
    }
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)

Aggregations

DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)32 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)30 IDataPoint (com.serotonin.m2m2.vo.IDataPoint)20 List (java.util.List)19 Common (com.serotonin.m2m2.Common)18 MangoTestBase (com.serotonin.m2m2.MangoTestBase)18 Collectors (java.util.stream.Collectors)18 Test (org.junit.Test)18 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)17 Role (com.serotonin.m2m2.vo.role.Role)17 Set (java.util.Set)17 RunAs (com.infiniteautomation.mango.spring.components.RunAs)16 QueryBuilder (com.serotonin.m2m2.db.dao.QueryBuilder)16 Assert.assertEquals (org.junit.Assert.assertEquals)16 Assert.assertTrue (org.junit.Assert.assertTrue)16 Before (org.junit.Before)16 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)12 MockPointLocatorVO (com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)10 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)6 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)6