Search in sources :

Example 1 with DatabaseProxy

use of com.serotonin.m2m2.db.DatabaseProxy in project ma-core-public by infiniteautomation.

the class DataPointDaoTest method testDeletePointsAndSeriesIds.

@Test
public void testDeletePointsAndSeriesIds() {
    // Insert some points that won't go away for this test
    for (int i = 0; i < 5; i++) {
        DataPointVO point = newVO();
        dao.insert(point);
    }
    // Insert points to delete
    List<IDataPoint> points = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        DataPointVO point = newVO();
        dao.insert(point);
        points.add(point);
    }
    DatabaseProxy proxy = Common.getBean(DatabaseProxy.class);
    DataPoints dataPoints = DataPoints.DATA_POINTS;
    TimeSeries timeSeries = TimeSeries.TIME_SERIES;
    // Delete the data points but not the series id
    for (IDataPoint point : points) {
        dao.delete((DataPointVO) point);
    }
    // Ensure the series Ids are gone via the delete post relational
    for (IDataPoint point : points) {
        assertEquals(0, proxy.getContext().selectCount().from(timeSeries).where(timeSeries.id.eq(point.getSeriesId())).fetchSingle().value1().intValue());
    }
    // delete the orphaned series ids (none)
    assertEquals(0, dao.deleteOrphanedTimeSeries());
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TimeSeries(com.infiniteautomation.mango.db.tables.TimeSeries) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) ArrayList(java.util.ArrayList) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) DataPoints(com.infiniteautomation.mango.db.tables.DataPoints) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Test(org.junit.Test)

Example 2 with DatabaseProxy

use of com.serotonin.m2m2.db.DatabaseProxy in project ma-core-public by infiniteautomation.

the class DataPointDaoTest method testDeleteOrphanedSeries.

@Test
public void testDeleteOrphanedSeries() {
    // Insert some points that won't go away for this test
    for (int i = 0; i < 5; i++) {
        DataPointVO point = newVO();
        dao.insert(point);
    }
    // Insert points to delete
    List<IDataPoint> points = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        DataPointVO point = newVO();
        dao.insert(point);
        points.add(point);
    }
    DatabaseProxy proxy = Common.getBean(DatabaseProxy.class);
    DataPoints dataPoints = DataPoints.DATA_POINTS;
    TimeSeries timeSeries = TimeSeries.TIME_SERIES;
    // Delete the data points but not the series id
    for (IDataPoint point : points) {
        proxy.getContext().deleteFrom(dataPoints).where(dataPoints.id.eq(point.getId())).execute();
    }
    // Ensure the series Ids are still there
    for (IDataPoint point : points) {
        assertEquals(1, proxy.getContext().selectCount().from(timeSeries).where(timeSeries.id.eq(point.getSeriesId())).fetchSingle().value1().intValue());
    }
    // delete the orphaned series id and ensure it is gone
    assertEquals(5, dao.deleteOrphanedTimeSeries());
    // Ensure the series Id are gone
    for (IDataPoint point : points) {
        assertEquals(0, proxy.getContext().selectCount().from(timeSeries).where(timeSeries.id.eq(point.getSeriesId())).fetchSingle().value1().intValue());
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TimeSeries(com.infiniteautomation.mango.db.tables.TimeSeries) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) ArrayList(java.util.ArrayList) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) DataPoints(com.infiniteautomation.mango.db.tables.DataPoints) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Test(org.junit.Test)

Example 3 with DatabaseProxy

use of com.serotonin.m2m2.db.DatabaseProxy in project ma-core-public by infiniteautomation.

the class DataPointDaoTest method testDeleteOrphanedSeriesUsingCustomSeriesId.

@Test
public void testDeleteOrphanedSeriesUsingCustomSeriesId() {
    DatabaseProxy proxy = Common.getBean(DatabaseProxy.class);
    DataPoints dataPoints = DataPoints.DATA_POINTS;
    TimeSeries timeSeries = TimeSeries.TIME_SERIES;
    int seriesIdCounter = 10;
    // Insert some points that won't go away for this test
    for (int i = 0; i < 5; i++) {
        DataPointVO point = newVO();
        proxy.getContext().insertInto(timeSeries).columns(timeSeries.id).values(seriesIdCounter).execute();
        point.setSeriesId(seriesIdCounter++);
        dao.insert(point);
    }
    // Insert points to delete
    List<IDataPoint> points = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        DataPointVO point = newVO();
        proxy.getContext().insertInto(timeSeries).columns(timeSeries.id).values(seriesIdCounter).execute();
        point.setSeriesId(seriesIdCounter++);
        dao.insert(point);
        points.add(point);
    }
    // Delete the data points but not the series id
    for (IDataPoint point : points) {
        proxy.getContext().deleteFrom(dataPoints).where(dataPoints.id.eq(point.getId())).execute();
    }
    // Ensure the series Ids are still there
    for (IDataPoint point : points) {
        assertEquals(1, proxy.getContext().selectCount().from(timeSeries).where(timeSeries.id.eq(point.getSeriesId())).fetchSingle().value1().intValue());
    }
    // delete the orphaned series id and ensure it is gone
    assertEquals(5, dao.deleteOrphanedTimeSeries());
    // Ensure the series Id are gone
    for (IDataPoint point : points) {
        assertEquals(0, proxy.getContext().selectCount().from(timeSeries).where(timeSeries.id.eq(point.getSeriesId())).fetchSingle().value1().intValue());
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TimeSeries(com.infiniteautomation.mango.db.tables.TimeSeries) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) ArrayList(java.util.ArrayList) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) DataPoints(com.infiniteautomation.mango.db.tables.DataPoints) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Test(org.junit.Test)

Example 4 with DatabaseProxy

use of com.serotonin.m2m2.db.DatabaseProxy in project ma-core-public by infiniteautomation.

the class MangoTestBase method after.

@After
public void after() {
    // Tear down to simulate some form of shutdown
    for (Module module : ModuleRegistry.getModules()) {
        module.postRuntimeManagerTerminate(false);
    }
    // Need to restart background processing and clear it out
    Common.backgroundProcessing.terminate();
    Common.backgroundProcessing.joinTermination();
    for (Module module : ModuleRegistry.getModules()) {
        module.postTerminate(false);
    }
    // Setup for next test in class
    Common.eventManager.purgeAllEvents();
    Common.backgroundProcessing = lifecycle.getBackgroundProcessing();
    Common.backgroundProcessing.initialize(false);
    SimulationTimerProvider provider = (SimulationTimerProvider) Providers.get(TimerProvider.class);
    provider.reset();
    // Clean the noSQL database
    if (properties.getBoolean("tests.after.deleteAllPointData", true)) {
        try {
            Common.getBean(PointValueDao.class).deleteAllPointData();
        } catch (UnsupportedOperationException e) {
            LOG.warn("PointValueDao does not support deleteAllPointData() method. Your tests may be effected if you reuse this Mango lifecycle");
        }
    }
    // Clear all caches in services
    Common.getRuntimeContext().getBeansOfType(CachingService.class).values().forEach(s -> s.clearCaches(true));
    DatabaseProxy databaseProxy = Common.getBean(DatabaseProxy.class);
    // Try to release active connections if possible
    if (databaseProxy instanceof BasePooledProxy) {
        ((BasePooledProxy) databaseProxy).softEvictConnections();
    }
    // Clean database
    databaseProxy.clean();
}
Also used : PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) TimerProvider(com.serotonin.provider.TimerProvider) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) Module(com.serotonin.m2m2.module.Module) BasePooledProxy(com.serotonin.m2m2.db.BasePooledProxy) After(org.junit.After)

Aggregations

DatabaseProxy (com.serotonin.m2m2.db.DatabaseProxy)4 DataPoints (com.infiniteautomation.mango.db.tables.DataPoints)3 TimeSeries (com.infiniteautomation.mango.db.tables.TimeSeries)3 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 IDataPoint (com.serotonin.m2m2.vo.IDataPoint)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 BasePooledProxy (com.serotonin.m2m2.db.BasePooledProxy)1 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)1 Module (com.serotonin.m2m2.module.Module)1 TimerProvider (com.serotonin.provider.TimerProvider)1 After (org.junit.After)1