FileResourceCleanUpJob extends AbstractJob { @Override public void execute( JobConfiguration jobConfiguration ) { FileResourceRetentionStrategy retentionStrategy = (FileResourceRetentionStrategy) systemSettingManager .getSystemSetting( SettingKey.FILE_RESOURCE_RETENTION_STRATEGY ); List<Pair<String, String>> deletedOrphans = new ArrayList<>(); List<Pair<String, String>> deletedAuditFiles = new ArrayList<>(); if ( !FileResourceRetentionStrategy.FOREVER.equals( retentionStrategy ) ) { List<FileResource> expired = fileResourceService.getExpiredFileResources( retentionStrategy ); expired.forEach( this::safeDelete ); expired.forEach( fr -> deletedAuditFiles.add( ImmutablePair.of( fr.getName(), fr.getUid() ) ) ); } fileResourceService.getOrphanedFileResources().stream() .filter( fr -> !isFileStored( fr ) ) .filter( this::safeDelete ) .forEach( fr -> deletedOrphans.add( ImmutablePair.of( fr.getName(), fr.getUid() ) ) ); if ( !deletedOrphans.isEmpty() ) { log.info( String.format( "Deleted %d orphaned FileResources: %s", deletedOrphans.size(), prettyPrint( deletedOrphans ) ) ); } if ( !deletedAuditFiles.isEmpty() ) { log.info( String.format( "Deleted %d expired FileResource audits: %s", deletedAuditFiles.size(), prettyPrint( deletedAuditFiles ) ) ); } }  @Override JobType getJobType(); @Override void execute( JobConfiguration jobConfiguration );  }
@Test public void testNoRetention() { systemSettingManager.saveSystemSetting( SettingKey.FILE_RESOURCE_RETENTION_STRATEGY, FileResourceRetentionStrategy.NONE ); content = "filecontentA".getBytes(); dataValueA = createFileResourceDataValue( 'A', content ); assertNotNull( fileResourceService.getFileResource( dataValueA.getValue() ) ); dataValueService.deleteDataValue( dataValueA ); cleanUpJob.execute( null ); assertNull( fileResourceService.getFileResource( dataValueA.getValue() ) ); }
@Test public void testRetention() { systemSettingManager.saveSystemSetting( SettingKey.FILE_RESOURCE_RETENTION_STRATEGY, FileResourceRetentionStrategy.THREE_MONTHS ); content = "filecontentA".getBytes(); dataValueA = createFileResourceDataValue( 'A', content ); assertNotNull( fileResourceService.getFileResource( dataValueA.getValue() ) ); content = "filecontentB".getBytes(); dataValueB = createFileResourceDataValue( 'B', content ); assertNotNull( fileResourceService.getFileResource( dataValueB.getValue() ) ); content = "fileResourceC".getBytes(); FileResource fileResource = createFileResource( 'C', content ); dataValueB.setValue( fileResource.getUid() ); dataValueService.updateDataValue( dataValueB ); fileResource.setAssigned( true ); dataValueAuditService.getDataValueAudits( dataValueB ).get( 0 ) .setCreated( getDate( 2000, 1, 1 ) ); cleanUpJob.execute( null ); assertNotNull( fileResourceService.getFileResource( dataValueA.getValue() ) ); assertTrue( fileResourceService.getFileResource( dataValueA.getValue() ).isAssigned() ); assertNull( dataValueService.getDataValue( dataValueA.getDataElement(), dataValueA.getPeriod(), dataValueA.getSource(), null ) ); assertNull( fileResourceService.getFileResource( dataValueB.getValue() ) ); }
@Test @Ignore public void testFalsePositive() { systemSettingManager.saveSystemSetting( SettingKey.FILE_RESOURCE_RETENTION_STRATEGY, FileResourceRetentionStrategy.THREE_MONTHS ); content = "externalA".getBytes(); ExternalFileResource ex = createExternal( 'A', content ); String uid = ex.getFileResource().getUid(); ex.getFileResource().setAssigned( false ); fileResourceService.updateFileResource( ex.getFileResource() ); cleanUpJob.execute( null ); assertNotNull( externalFileResourceService.getExternalFileResourceByAccessToken( ex.getAccessToken() ) ); assertNotNull( fileResourceService.getFileResource( uid ) ); assertTrue( fileResourceService.getFileResource( uid ).isAssigned() ); }
@Test @Ignore public void testFailedUpload() { systemSettingManager.saveSystemSetting( SettingKey.FILE_RESOURCE_RETENTION_STRATEGY, FileResourceRetentionStrategy.THREE_MONTHS ); content = "externalA".getBytes(); ExternalFileResource ex = createExternal( 'A', content ); String uid = ex.getFileResource().getUid(); ex.getFileResource().setStorageStatus( FileResourceStorageStatus.PENDING ); fileResourceService.updateFileResource( ex.getFileResource() ); cleanUpJob.execute( null ); assertNull( externalFileResourceService.getExternalFileResourceByAccessToken( ex.getAccessToken() ) ); assertNull( fileResourceService.getFileResource( uid ) ); }