RebuildAssetUploadMetadataTask extends TaskSupport { @Override protected Object execute() { long totalAssets = assetStore.countAssets(null); long processedAssets = 0; OIndexCursor assetCursor = assetStore.getIndex(AssetEntityAdapter.I_BUCKET_COMPONENT_NAME).cursor(); ProgressLogIntervalHelper progressLogger = new ProgressLogIntervalHelper(log, 60); List<Entry<OCompositeKey, EntityId>> assets = assetStore.getNextPage(assetCursor, limit); if (!Iterables.isEmpty(assets) && !Strings2.isBlank(assetStore.getById(assets.get(0).getValue()).createdBy())) { return null; } while (assets != null && !assets.isEmpty()) { checkContinuation(); Iterable<EntityId> assetIds = assets.stream().map(Entry::getValue).collect(toList()); Collection<Asset> assetsToUpdate = stream(assetStore.getByIds(assetIds)) .filter(asset -> Strings2.isEmpty(asset.createdBy())) .filter(asset -> asset.blobRef() != null).map(asset -> { BlobStore blobStore = blobStoreManager.get(asset.blobRef().getStore()); Blob blob = blobStore.get(asset.blobRef().getBlobId()); if (blob != null) { asset.createdBy(blob.getHeaders().get(BlobStore.CREATED_BY_HEADER)); asset.createdByIp(blob.getHeaders().get(BlobStore.CREATED_BY_IP_HEADER)); asset.blobCreated(blob.getMetrics().getCreationTime()); } return asset; }).collect(toList()); assetStore.save(assetsToUpdate); processedAssets += size(assetsToUpdate); progressLogger.info("{} / {} asset upload metadata processed in {} ms", processedAssets, totalAssets, progressLogger.getElapsed()); assets = assetStore.getNextPage(assetCursor, limit); } progressLogger.flush(); return null; } @Inject  RebuildAssetUploadMetadataTask(final AssetStore assetStore,
                                        final BlobStoreManager blobStoreManager,
                                        final RebuildAssetUploadMetadataConfiguration configuration); @Override String getMessage();  }
@Test(expected = TaskInterruptedException.class) public void executeCancelledTaskThrowsTaskInterruptedException() { task.cancel(); Blob blob = createMockBlob("blobId"); Asset asset = createAsset(blob); assetStore.save(asset); task.execute(); }
@Test public void executeUpdatesCandidateAssets() { Blob blob = createMockBlob("blobId"); Asset asset = createAsset(blob); assetStore.save(asset); task.execute(); Asset updatedAsset = assetStore.getById(id(asset)); assertThat(updatedAsset.name(), is(asset.name())); assertThat(updatedAsset.createdBy(), is(blob.getHeaders().get(CREATED_BY_HEADER))); assertThat(updatedAsset.createdByIp(), is(blob.getHeaders().get(CREATED_BY_IP_HEADER))); assertThat(updatedAsset.blobCreated(), is(blob.getMetrics().getCreationTime())); }
@Test public void executeSkipsAssetsWithANonEmptyCreatedBy() { Blob blob = createMockBlob("blobId"); Asset asset = createAsset(blob); asset.createdBy("somebody"); assetStore.save(asset); task.execute(); Asset updatedAsset = assetStore.getById(id(asset)); assertThat(updatedAsset.name(), is(asset.name())); assertThat(updatedAsset.createdBy(), is("somebody")); assertThat(updatedAsset.createdByIp(), is(nullValue())); }
@Test public void executeSkipsAssetsWithANoBlobRef() { Asset assetWithNoBlobRef = createAsset(null); assetWithNoBlobRef.createdBy("somebody"); assetStore.save(assetWithNoBlobRef); task.execute(); Asset updatedAssetWithNoBlobRef = assetStore.getById(id(assetWithNoBlobRef)); assertThat(updatedAssetWithNoBlobRef.name(), is(assetWithNoBlobRef.name())); assertThat(updatedAssetWithNoBlobRef.createdBy(), is("somebody")); assertThat(updatedAssetWithNoBlobRef.createdByIp(), is(nullValue())); }
@Test public void executeSkipsFirstAssetWithAnEmptyCreatedBy() { Blob blob = createMockBlob("blobId"); when(blob.getHeaders()).thenReturn(Collections.singletonMap(CREATED_BY_IP_HEADER, "192.168.0.1")); Asset asset = createAsset(blob); asset.createdBy("alreadySet"); assetStore.save(asset); Blob blob2 = createMockBlob("blobId2"); when(blob2.getHeaders()).thenReturn(Collections.singletonMap(CREATED_BY_IP_HEADER, "192.168.0.2")); Asset asset2 = createAsset(blob2); asset2.createdBy(null); assetStore.save(asset2); task.execute(); assertThat(assetStore.getById(id(asset)).createdByIp(), is(nullValue())); assertThat(assetStore.getById(id(asset2)).createdByIp(), is(nullValue())); }