DeviceMessageCleanupJob implements Job { @Override public void execute(final JobExecutionContext context) { LOGGER.info("Quartz triggered cleanup of database - device message records."); final DateTime start = DateTime.now(); try { final Date retention = this.calculateRetentionDate(); final List<DeviceLogItem> oldDeviceMessages = this.transactionalDeviceLogItemService .findDeviceLogItemsBeforeDate(retention, this.deviceMessagePageSize); if (!oldDeviceMessages.isEmpty()) { this.saveDeviceMessagesToCsvFile(oldDeviceMessages); LOGGER.info("Deleting device messages..."); this.transactionalDeviceLogItemService.deleteDeviceLogItems(oldDeviceMessages); } } catch (final Exception e) { LOGGER.error("Exception during CSV file creation, compression or device message deletion.", e); } final DateTime end = DateTime.now(); LOGGER.info("Start: {}, end: {}, duration: {} milliseconds.", start, end, end.getMillis() - start.getMillis()); }  @Override void execute(final JobExecutionContext context);  }
@Test public void test() throws IOException { final List<DeviceLogItem> deviceLogItems = this.createDeviceLogItems(); Mockito.when(this.transactionalDeviceLogItemService.findDeviceLogItemsBeforeDate(any(Date.class), any(Integer.class))).thenReturn(deviceLogItems); this.deviceMessageCleanupJob.execute(null); final String path = this.folder.resolve(JUST_A_FILE).getParent().toString(); final File zipFile = FileUtils.findFileInFolderUsingFilePrefix(path, this.filePrefix); assertThat(zipFile.exists()).isTrue(); assertThat(zipFile).hasExtension("zip"); assertThat(zipFile.length()).isNotZero(); ZipFileReader.extractZipFile(zipFile.getAbsolutePath(), path); zipFile.delete(); final File csvFile = FileUtils.findFileInFolderUsingFilePrefix(path, this.filePrefix); assertThat(csvFile.exists()).isTrue(); assertThat(csvFile).hasExtension("csv"); assertThat(csvFile.length()).isNotZero(); final long numberOfLines = FileUtils.countNumberOfLinesInFile(csvFile); assertThat(numberOfLines).isEqualTo(2l); }