use of ddf.catalog.operation.DeleteResponse in project ddf by codice.
the class CatalogComponentFrameworkTest method testDeleteWithListOfIds.
@Test
public /**
* Operation: DELETE
* Body contains: List<String>
*/
void testDeleteWithListOfIds() throws Exception {
resetMocks();
// Setup expectations to verify
final MockEndpoint mockVerifierEndpoint = getMockEndpoint("mock:result");
mockVerifierEndpoint.expectedMessageCount(1);
final List<Metacard> metacards = new ArrayList<Metacard>();
metacards.add(metacard1);
metacards.add(metacard2);
// setup mock catalog framework
final String[] metacardIds = new String[metacards.size()];
for (int i = 0; i < metacards.size(); i++) {
metacardIds[i] = metacards.get(i).getId();
}
final List<String> metacardIdList = Arrays.asList(metacardIds);
DeleteRequest deleteRequest = new DeleteRequestImpl(metacardIds);
DeleteResponse deleteResponse = new DeleteResponseImpl(deleteRequest, new HashMap(), metacards);
when(catalogFramework.delete(any(DeleteRequest.class))).thenReturn(deleteResponse);
// Exercise the route with a DELETE operation
template.sendBodyAndHeader("direct:sampleInput", metacardIdList, "Operation", "DELETE");
// Verify that the number of metacards in the exchange after the records
// is identical to the input
assertListSize(mockVerifierEndpoint.getExchanges(), 1);
final Exchange exchange = mockVerifierEndpoint.getExchanges().get(0);
final List<Update> cardsDeleted = (List<Update>) exchange.getIn().getBody();
assertListSize(cardsDeleted, 2);
mockVerifierEndpoint.assertIsSatisfied();
}
use of ddf.catalog.operation.DeleteResponse in project ddf by codice.
the class FrameworkProducer method delete.
/**
* Deletes metacard(s) in the catalog using the Catalog Framework.
*
* @param exchange
* The {@link org.apache.camel.Exchange} can contain a
* {@link org.apache.camel.Message} with a body of type {@link java.util.List} of
* {@link String} or a single {@link String}. Each String represents the ID of a
* Metacard to be deleted.
* @throws ddf.catalog.source.SourceUnavailableException
* @throws ddf.catalog.source.IngestException
* @throws ddf.camel.component.catalog.framework.FrameworkProducerException
*/
private void delete(final Exchange exchange) throws SourceUnavailableException, IngestException, FrameworkProducerException {
DeleteResponse deleteResponse = null;
// read in data
final List<String> metacardIdsToBeDeleted = readBodyDataAsMetacardIds(exchange);
// process if data is valid
if (!validateList(metacardIdsToBeDeleted, String.class)) {
LOGGER.debug("Validation of Metacard id list failed");
processCatalogResponse(deleteResponse, exchange);
throw new FrameworkProducerException("Validation of Metacard id list failed");
}
LOGGER.debug("Validation of Metacard id list passed...");
final String[] metacardIdsToBeDeletedArray = new String[metacardIdsToBeDeleted.size()];
final DeleteRequest deleteRequest = new DeleteRequestImpl(metacardIdsToBeDeleted.toArray(metacardIdsToBeDeletedArray));
final int expectedNumberOfDeletedMetacards = metacardIdsToBeDeleted.size();
if (expectedNumberOfDeletedMetacards < 1) {
LOGGER.debug("Empty list of Metacard id...nothing to process");
processCatalogResponse(deleteResponse, exchange);
return;
}
LOGGER.debug("Making DELETE call to Catalog Framework...");
deleteResponse = catalogFramework.delete(deleteRequest);
if (deleteResponse == null) {
LOGGER.debug("DeleteResponse is null from catalog framework");
processCatalogResponse(deleteResponse, exchange);
return;
}
final List<Metacard> deletedMetacards = deleteResponse.getDeletedMetacards();
if (deletedMetacards == null) {
LOGGER.debug("DeleteResponse returned null metacards list");
processCatalogResponse(deleteResponse, exchange);
return;
}
final int numberOfDeletedMetacards = deletedMetacards.size();
if (numberOfDeletedMetacards != expectedNumberOfDeletedMetacards) {
LOGGER.debug("Expected {} metacards deleted but only {} were successfully deleted", expectedNumberOfDeletedMetacards, numberOfDeletedMetacards);
processCatalogResponse(deleteResponse, exchange);
return;
}
LOGGER.debug("Deleted {} metacards", numberOfDeletedMetacards);
processCatalogResponse(deleteResponse, exchange);
}
use of ddf.catalog.operation.DeleteResponse in project ddf by codice.
the class RemoveAllCommand method executeRemoveAllFromStore.
private Object executeRemoveAllFromStore() throws Exception {
CatalogFacade catalog = getCatalog();
QueryRequest firstQuery = getIntendedQuery(filterBuilder, true);
QueryRequest subsequentQuery = getIntendedQuery(filterBuilder, false);
long totalAmountDeleted = 0;
long start = System.currentTimeMillis();
SourceResponse response;
try {
response = catalog.query(firstQuery);
} catch (UnsupportedQueryException e) {
firstQuery = getAlternateQuery(filterBuilder, true);
subsequentQuery = getAlternateQuery(filterBuilder, false);
response = catalog.query(firstQuery);
}
if (response == null) {
printErrorMessage("No response from Catalog.");
return null;
}
if (needsAlternateQueryAndResponse(response)) {
firstQuery = getAlternateQuery(filterBuilder, true);
subsequentQuery = getAlternateQuery(filterBuilder, false);
response = catalog.query(firstQuery);
}
String totalAmount = getTotalAmount(response.getHits());
while (response.getResults().size() > 0) {
// Add metacard ids to string array
List<String> ids = response.getResults().stream().filter(Objects::nonNull).map(Result::getMetacard).filter(Objects::nonNull).map(Metacard::getId).collect(Collectors.toList());
// Delete the records
DeleteRequestImpl request = new DeleteRequestImpl(ids.toArray(new String[ids.size()]));
DeleteResponse deleteResponse = catalog.delete(request);
int amountDeleted = deleteResponse.getDeletedMetacards().size();
totalAmountDeleted += amountDeleted;
console.print(String.format(PROGRESS_FORMAT, totalAmountDeleted, totalAmount));
console.flush();
// Break out if there are no more records to delete
if (amountDeleted < batchSize || batchSize < 1) {
break;
}
// Re-query when necessary
response = catalog.query(subsequentQuery);
}
long end = System.currentTimeMillis();
String info = String.format(" %d file(s) removed in %3.3f seconds%n", totalAmountDeleted, (end - start) / MS_PER_SECOND);
LOGGER.info(info);
LOGGER.info(totalAmountDeleted + " files removed using cache:removeAll command");
console.println();
console.print(info);
return null;
}
use of ddf.catalog.operation.DeleteResponse in project ddf by codice.
the class RemoveCommand method executeRemoveFromStore.
private Object executeRemoveFromStore() throws Exception {
CatalogFacade catalogProvider = getCatalog();
if (hasFilter()) {
QueryImpl query = new QueryImpl(getFilter());
query.setRequestsTotalResultsCount(true);
query.setPageSize(-1);
Map<String, Serializable> properties = new HashMap<>();
properties.put("mode", "native");
SourceResponse queryResponse = catalogProvider.query(new QueryRequestImpl(query, properties));
final List<String> idsFromFilteredQuery = queryResponse.getResults().stream().map(result -> result.getMetacard().getId()).collect(Collectors.toList());
if (ids == null) {
ids = idsFromFilteredQuery;
} else {
ids = ids.stream().filter(id -> idsFromFilteredQuery.contains(id)).collect(Collectors.toList());
}
}
final int numberOfMetacardsToRemove = ids.size();
if (numberOfMetacardsToRemove > 0) {
printSuccessMessage("Found " + numberOfMetacardsToRemove + " metacards to remove.");
} else {
printErrorMessage("No records found meeting filter criteria.");
return null;
}
DeleteRequestImpl request = new DeleteRequestImpl(ids.toArray(new String[numberOfMetacardsToRemove]));
DeleteResponse response = catalogProvider.delete(request);
if (response.getDeletedMetacards().size() > 0) {
printSuccessMessage(ids + " successfully deleted.");
LOGGER.info(ids + " removed using catalog:remove command");
} else {
printErrorMessage(ids + " could not be deleted.");
LOGGER.info(ids + " could not be deleted using catalog:remove command");
}
return null;
}
use of ddf.catalog.operation.DeleteResponse in project ddf by codice.
the class RemoveAllCommandTest method testExecuteWithSubject.
/**
* Checks the forced (-f) generic case.
*
* @throws Exception
*/
@Test
public void testExecuteWithSubject() throws Exception {
// given
RemoveAllCommand removeAllCommand = new RemoveAllCommand();
final CatalogFramework catalogFramework = mock(CatalogFramework.class);
QueryResponse queryResponse = mock(QueryResponse.class);
when(queryResponse.getResults()).thenReturn(getResultList(10));
when(catalogFramework.query(isA(QueryRequest.class))).thenReturn(queryResponse);
DeleteResponse deleteResponse = mock(DeleteResponse.class);
when(deleteResponse.getDeletedMetacards()).thenReturn(getMetacardList(10));
when(catalogFramework.delete(isA(DeleteRequest.class))).thenReturn(deleteResponse);
removeAllCommand.catalogFramework = catalogFramework;
removeAllCommand.filterBuilder = new GeotoolsFilterBuilder();
removeAllCommand.batchSize = 11;
removeAllCommand.force = true;
// when
removeAllCommand.executeWithSubject();
// then
verify(catalogFramework, times(1)).delete(isA(DeleteRequest.class));
}
Aggregations