Search in sources :

Example 1 with DataPointService

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

the class DataPointTagsDaoTest method updateTags.

// @Test
public void updateTags() {
    System.out.println(Common.MA_DATA_PATH);
    LongSummaryStatistics stats;
    DataPointService dataPointService = Common.getBean(DataPointService.class);
    MockDataSourceVO ds = createMockDataSource(true);
    List<DataPointVO> points = Stream.generate(() -> createMockDataPoint(ds, new MockPointLocatorVO(DataType.NUMERIC, true), true)).limit(numPoints).collect(Collectors.toList());
    AtomicInteger count = new AtomicInteger();
    List<String> tagKeys = Stream.generate(() -> "key" + count.getAndIncrement()).limit(numTags).collect(Collectors.toList());
    // add half the tags
    stats = points.stream().mapToLong(point -> {
        point.setTags(tagKeys.stream().limit(numTags / 2).collect(Collectors.toMap(Function.identity(), k -> k + "_value" + random.nextInt(10))));
        long start = System.currentTimeMillis();
        dataPointService.update(point.getId(), point);
        return System.currentTimeMillis() - start;
    }).summaryStatistics();
    System.out.println("add half the tags: " + stats);
    // add/update the tags
    stats = points.stream().mapToLong(point -> {
        point.setTags(tagKeys.stream().collect(Collectors.toMap(Function.identity(), k -> k + "_value" + random.nextInt(10))));
        long start = System.currentTimeMillis();
        dataPointService.update(point.getId(), point);
        return System.currentTimeMillis() - start;
    }).summaryStatistics();
    System.out.println("add/update the tags: " + stats);
    // save without updating tags
    stats = points.stream().mapToLong(point -> {
        long start = System.currentTimeMillis();
        dataPointService.update(point.getId(), point);
        return System.currentTimeMillis() - start;
    }).summaryStatistics();
    System.out.println("save without updating tags: " + stats);
    // remove half of the tags
    stats = points.stream().mapToLong(point -> {
        Map<String, String> existing = point.getTags();
        point.setTags(tagKeys.stream().limit(numTags / 2).collect(Collectors.toMap(Function.identity(), existing::get)));
        long start = System.currentTimeMillis();
        dataPointService.update(point.getId(), point);
        return System.currentTimeMillis() - start;
    }).summaryStatistics();
    System.out.println("remove half of the tags: " + stats);
}
Also used : LongSummaryStatistics(java.util.LongSummaryStatistics) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)

Example 2 with DataPointService

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

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

the class MockMango method createDataPoints.

/**
 * Create points asynchronously and wait for all to be created
 */
public List<DataPointVO> createDataPoints(int count, Map<String, String> tags) throws ExecutionException, InterruptedException {
    MockDataSourceVO ds = createMockDataSource();
    DataPointService service = Common.getBean(DataPointService.class);
    List<CompletableFuture<DataPointVO>> points = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        DataPointVO dp = new DataPointVO();
        dp.setName(UUID.randomUUID().toString());
        dp.setDeviceName(ds.getName());
        dp.setPointLocator(new MockPointLocatorVO(DataType.NUMERIC, true));
        dp.setDataSourceId(ds.getId());
        dp.setTags(tags);
        points.add(service.insertAsync(dp).toCompletableFuture());
    }
    return CompletableFuture.allOf(points.toArray(new CompletableFuture[0])).thenApply(ignored -> points.stream().map(CompletableFuture::join).collect(Collectors.toList())).get();
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) CompletableFuture(java.util.concurrent.CompletableFuture) Scope(org.openjdk.jmh.annotations.Scope) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ArrayList(java.util.ArrayList) EventDetectorsService(com.infiniteautomation.mango.spring.service.EventDetectorsService) DataType(com.serotonin.m2m2.DataType) SQLException(java.sql.SQLException) Map(java.util.Map) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) TearDown(org.openjdk.jmh.annotations.TearDown) AbstractEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO) Setup(org.openjdk.jmh.annotations.Setup) Common(com.serotonin.m2m2.Common) EventDetectorDefinition(com.serotonin.m2m2.module.EventDetectorDefinition) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) IOException(java.io.IOException) UUID(java.util.UUID) State(org.openjdk.jmh.annotations.State) Collectors(java.util.stream.Collectors) ModuleRegistry(com.serotonin.m2m2.module.ModuleRegistry) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) MockMangoLifecycle(com.serotonin.m2m2.MockMangoLifecycle) Level(org.openjdk.jmh.annotations.Level) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)

Example 4 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 5 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)

Aggregations

DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)17 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)15 IDataPoint (com.serotonin.m2m2.vo.IDataPoint)10 List (java.util.List)10 Common (com.serotonin.m2m2.Common)9 MangoTestBase (com.serotonin.m2m2.MangoTestBase)9 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)9 Role (com.serotonin.m2m2.vo.role.Role)9 Set (java.util.Set)9 Collectors (java.util.stream.Collectors)9 Test (org.junit.Test)9 RunAs (com.infiniteautomation.mango.spring.components.RunAs)8 QueryBuilder (com.serotonin.m2m2.db.dao.QueryBuilder)8 Assert.assertEquals (org.junit.Assert.assertEquals)8 Assert.assertTrue (org.junit.Assert.assertTrue)8 Before (org.junit.Before)8 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)6 MockPointLocatorVO (com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)5 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)3 MockDataSourceVO (com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO)3