use of ddf.catalog.content.operation.UpdateStorageRequest in project ddf by codice.
the class Historian method version.
/**
* Versions updated {@link Metacard}s and {@link ContentItem}s.
*
* @param streamUpdateRequest Needed to pass {@link ddf.catalog.core.versioning.MetacardVersion#SKIP_VERSIONING}
* flag into downstream update
* @param updateStorageResponse Versions this response's updated items
* @return the update response originally passed in
* @throws UnsupportedQueryException
* @throws SourceUnavailableException
* @throws IngestException
*/
public UpdateStorageResponse version(UpdateStorageRequest streamUpdateRequest, UpdateStorageResponse updateStorageResponse, UpdateResponse updateResponse) throws UnsupportedQueryException, SourceUnavailableException, IngestException {
if (doSkip(updateStorageResponse)) {
return updateStorageResponse;
}
setSkipFlag(streamUpdateRequest);
setSkipFlag(updateStorageResponse);
List<Metacard> updatedMetacards = updateStorageResponse.getUpdatedContentItems().stream().filter(ci -> StringUtils.isBlank(ci.getQualifier())).map(ContentItem::getMetacard).filter(Objects::nonNull).filter(isNotVersionNorDeleted).collect(Collectors.toList());
Map<String, Metacard> originalMetacards = query(forIds(updatedMetacards.stream().map(Metacard::getId).collect(Collectors.toList())));
Collection<ReadStorageRequest> ids = getReadStorageRequests(updatedMetacards);
Map<String, List<ContentItem>> content = getContent(ids);
Function<String, Action> getAction = (id) -> content.containsKey(id) ? Action.VERSIONED_CONTENT : Action.VERSIONED;
Map<String, Metacard> versionMetacards = getVersionMetacards(originalMetacards.values(), getAction, (Subject) updateResponse.getProperties().get(SecurityConstants.SECURITY_SUBJECT));
CreateStorageResponse createStorageResponse = versionContentItems(content, versionMetacards);
if (createStorageResponse == null) {
LOGGER.debug("Could not version content items.");
return updateStorageResponse;
}
setResourceUriForContent(/*mutable*/
versionMetacards, createStorageResponse);
storeVersionMetacards(versionMetacards);
return updateStorageResponse;
}
use of ddf.catalog.content.operation.UpdateStorageRequest in project ddf by codice.
the class FileSystemStorageProviderTest method testCreateWithQualifierAndOneInvalidItem.
@Test
public void testCreateWithQualifierAndOneInvalidItem() throws Exception {
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return IOUtils.toInputStream("This data is my data, this data is your data.");
}
};
ContentItem contentItem = new ContentItemImpl(uuid, null, byteSource, "application/text", "datadatadata", byteSource.size(), mock(Metacard.class));
String invalidUuid = "wow-this-isn't-a-valid-uuid-right?!@#%";
ContentItem badContentItem = new ContentItemImpl(invalidUuid, null, byteSource, "application/text", "datadatadata", byteSource.size(), mock(Metacard.class));
CreateStorageRequest createRequest = new CreateStorageRequestImpl(Lists.newArrayList(contentItem, badContentItem), null);
CreateStorageResponse createResponse = provider.create(createRequest);
assertThat(createResponse.getCreatedContentItems().size(), is(1));
assertThat(createResponse.getCreatedContentItems().get(0).getId(), is(uuid));
ContentItem updateContentItem = new ContentItemImpl(invalidUuid, null, byteSource, "application/text", "datadatadata", byteSource.size(), mock(Metacard.class));
UpdateStorageRequest updateRequest = new UpdateStorageRequestImpl(Lists.newArrayList(updateContentItem), null);
UpdateStorageResponse updateResponse = provider.update(updateRequest);
assertThat(updateResponse.getUpdatedContentItems().size(), is(0));
}
use of ddf.catalog.content.operation.UpdateStorageRequest in project ddf by codice.
the class CatalogFrameworkImplTest method testUpdateStorage.
/**
* Tests that the framework properly passes an update request to the local provider.
*/
@Test
public void testUpdateStorage() throws Exception {
List<ContentItem> contentItems = new ArrayList<>();
MetacardImpl metacard = new MetacardImpl();
metacard.setId(null);
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream("blah".getBytes());
}
};
ContentItemImpl contentItem = new ContentItemImpl(uuidGenerator.generateUuid(), byteSource, "application/octet-stream", "blah", 0L, metacard);
contentItems.add(contentItem);
CreateResponse response = framework.create(new CreateStorageRequestImpl(contentItems, null));
Metacard insertedCard = response.getCreatedMetacards().get(0);
List<ContentItem> updatedContentItems = new ArrayList<>();
updatedContentItems.add(new ContentItemImpl(insertedCard.getId(), byteSource, "application/octet-stream", insertedCard));
UpdateStorageRequest request = new UpdateStorageRequestImpl(updatedContentItems, null);
List<Result> mockFederationResults = Stream.of(insertedCard).map(m -> {
Result mockResult = mock(Result.class);
when(mockResult.getMetacard()).thenReturn(m);
return mockResult;
}).collect(Collectors.toList());
QueryResponseImpl queryResponse = new QueryResponseImpl(mock(QueryRequest.class), mockFederationResults, 1);
when(mockFederationStrategy.federate(anyList(), anyObject())).thenReturn(queryResponse);
// send update to framework
List<Update> returnedCards = framework.update(request).getUpdatedMetacards();
assertThat(returnedCards, hasSize(1));
final Metacard newMetacard = returnedCards.get(0).getNewMetacard();
assertThat(newMetacard.getId(), notNullValue());
assertThat(newMetacard.getResourceURI().toString(), is(contentItem.getUri()));
assertThat(newMetacard.getResourceSize(), is(Long.toString(byteSource.size())));
assertThat(response.getCreatedMetacards(), hasSize(storageProvider.size()));
// make sure that the event was posted correctly
assertThat(eventAdmin.wasEventPosted(), is(true));
}
use of ddf.catalog.content.operation.UpdateStorageRequest in project ddf by codice.
the class HistorianTest method testReadStorageException.
@Test
public void testReadStorageException() throws StorageException, SourceUnavailableException, IngestException, UnsupportedQueryException, URISyntaxException {
StorageProvider exceptionStorageProvider = mock(StorageProvider.class);
when(exceptionStorageProvider.read(any())).thenThrow(StorageException.class);
historian.setStorageProviders(Collections.singletonList(exceptionStorageProvider));
Metacard metacard = getMetacardUpdatePair().get(0);
// Parameters for historian
UpdateStorageRequest storageRequest = mock(UpdateStorageRequest.class);
UpdateStorageResponse storageResponse = mock(UpdateStorageResponse.class);
UpdateResponse updateResponse = mock(UpdateResponse.class);
// create the update request
updateMetacard(storageRequest, storageResponse, metacard);
mockQuery(metacard);
historian.version(storageRequest, storageResponse, updateResponse);
// Verify that it wasn't updated
verify(catalogProvider, times(0)).create(anyObject());
}
use of ddf.catalog.content.operation.UpdateStorageRequest in project ddf by codice.
the class HistorianTest method testUpdateStorageResponse.
@Test
public void testUpdateStorageResponse() throws UnsupportedQueryException, SourceUnavailableException, IngestException, URISyntaxException, StorageException {
// The metacard and updated metacard
List<Metacard> metacards = getMetacardUpdatePair();
// Parameters for historian
UpdateStorageRequest storageRequest = mock(UpdateStorageRequest.class);
UpdateStorageResponse storageResponse = mock(UpdateStorageResponse.class);
UpdateResponse updateResponse = mock(UpdateResponse.class);
storeMetacard(metacards.get(0));
// send a request to update the metacard
updateMetacard(storageRequest, storageResponse, metacards.get(1));
storageProvider.update(storageRequest);
mockQuery(metacards.get(1));
historian.version(storageRequest, storageResponse, updateResponse);
// Verify that the metacard updated
Metacard update = readMetacard();
assertThat(update, equalTo(metacards.get(1)));
assertThat(storageResponse.getUpdatedContentItems().get(0).getUri(), not(equalTo(RESOURCE_URI)));
}
Aggregations