Search in sources :

Example 1 with DeleteRequest

use of ddf.catalog.operation.DeleteRequest in project ddf by codice.

the class CatalogBackupPluginTest method getDeleteResponse.

private DeleteResponse getDeleteResponse(List<String> metacardIds) {
    MetacardType mockMetacardType = mock(MetacardType.class);
    when(mockMetacardType.getName()).thenReturn(MetacardType.DEFAULT_METACARD_TYPE_NAME);
    List<Metacard> deletedMetacards = new ArrayList<>(metacardIds.size());
    for (String metacardId : metacardIds) {
        Metacard mockMetacard = mock(Metacard.class);
        when(mockMetacard.getId()).thenReturn(metacardId);
        when(mockMetacard.getMetacardType()).thenReturn(mockMetacardType);
        deletedMetacards.add(mockMetacard);
    }
    DeleteRequest request = mock(DeleteRequest.class);
    DeleteResponse mockDeleteResponse = mock(DeleteResponse.class);
    when(mockDeleteResponse.getDeletedMetacards()).thenReturn(deletedMetacards);
    when(mockDeleteResponse.getRequest()).thenReturn(request);
    return mockDeleteResponse;
}
Also used : Metacard(ddf.catalog.data.Metacard) DeleteResponse(ddf.catalog.operation.DeleteResponse) ArrayList(java.util.ArrayList) DeleteRequest(ddf.catalog.operation.DeleteRequest) MetacardType(ddf.catalog.data.MetacardType)

Example 2 with DeleteRequest

use of ddf.catalog.operation.DeleteRequest in project ddf by codice.

the class ContentProducerDataAccessObject method createContentItem.

public void createContentItem(FileSystemPersistenceProvider fileIdMap, ContentEndpoint endpoint, File ingestedFile, WatchEvent.Kind<Path> eventType, String mimeType, Map<String, Object> headers) throws SourceUnavailableException, IngestException {
    LOGGER.debug("Creating content item.");
    String key = String.valueOf(ingestedFile.getAbsolutePath().hashCode());
    String id = fileIdMap.loadAllKeys().contains(key) ? String.valueOf(fileIdMap.loadFromPersistence(key)) : null;
    if (StandardWatchEventKinds.ENTRY_CREATE.equals(eventType)) {
        CreateStorageRequest createRequest = new CreateStorageRequestImpl(Collections.singletonList(new ContentItemImpl(uuidGenerator.generateUuid(), Files.asByteSource(ingestedFile), mimeType, ingestedFile.getName(), 0L, null)), null);
        processHeaders(headers, createRequest, ingestedFile);
        CreateResponse createResponse = endpoint.getComponent().getCatalogFramework().create(createRequest);
        if (createResponse != null) {
            List<Metacard> createdMetacards = createResponse.getCreatedMetacards();
            for (Metacard metacard : createdMetacards) {
                fileIdMap.store(key, metacard.getId());
                LOGGER.debug("content item created with id = {}", metacard.getId());
            }
        }
    } else if (StandardWatchEventKinds.ENTRY_MODIFY.equals(eventType)) {
        UpdateStorageRequest updateRequest = new UpdateStorageRequestImpl(Collections.singletonList(new ContentItemImpl(id, Files.asByteSource(ingestedFile), mimeType, ingestedFile.getName(), 0, null)), null);
        processHeaders(headers, updateRequest, ingestedFile);
        UpdateResponse updateResponse = endpoint.getComponent().getCatalogFramework().update(updateRequest);
        if (updateResponse != null) {
            List<Update> updatedMetacards = updateResponse.getUpdatedMetacards();
            for (Update update : updatedMetacards) {
                LOGGER.debug("content item updated with id = {}", update.getNewMetacard().getId());
            }
        }
    } else if (StandardWatchEventKinds.ENTRY_DELETE.equals(eventType)) {
        DeleteRequest deleteRequest = new DeleteRequestImpl(id);
        DeleteResponse deleteResponse = endpoint.getComponent().getCatalogFramework().delete(deleteRequest);
        if (deleteResponse != null) {
            List<Metacard> deletedMetacards = deleteResponse.getDeletedMetacards();
            for (Metacard delete : deletedMetacards) {
                fileIdMap.delete(ingestedFile.getAbsolutePath());
                LOGGER.debug("content item deleted with id = {}", delete.getId());
            }
        }
    }
}
Also used : UpdateStorageRequestImpl(ddf.catalog.content.operation.impl.UpdateStorageRequestImpl) CreateResponse(ddf.catalog.operation.CreateResponse) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) Update(ddf.catalog.operation.Update) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) DeleteResponse(ddf.catalog.operation.DeleteResponse) CreateStorageRequestImpl(ddf.catalog.content.operation.impl.CreateStorageRequestImpl) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) List(java.util.List) DeleteRequest(ddf.catalog.operation.DeleteRequest) CreateStorageRequest(ddf.catalog.content.operation.CreateStorageRequest) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Example 3 with DeleteRequest

use of ddf.catalog.operation.DeleteRequest in project ddf by codice.

the class CatalogComponentFrameworkTest method testDeleteWithIngestException.

@Test
public /**
     * Operation: DELETE
     * Body contains: 12345678900987654321abcdeffedcba
     */
void testDeleteWithIngestException() 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);
    // 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();
    }
    DeleteRequest deleteRequest = new DeleteRequestImpl(metacardIds);
    DeleteResponse deleteResponse = new DeleteResponseImpl(deleteRequest, new HashMap(), metacards);
    when(catalogFramework.delete(any(DeleteRequest.class))).thenThrow(new IngestException());
    // Exercise the route with a DELETE operation
    template.sendBodyAndHeader("direct:sampleInput", metacardIds, "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, 0);
    mockVerifierEndpoint.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) HashMap(java.util.HashMap) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Exchange(org.apache.camel.Exchange) Metacard(ddf.catalog.data.Metacard) DeleteResponse(ddf.catalog.operation.DeleteResponse) DeleteResponseImpl(ddf.catalog.operation.impl.DeleteResponseImpl) IngestException(ddf.catalog.source.IngestException) ArrayList(java.util.ArrayList) List(java.util.List) DeleteRequest(ddf.catalog.operation.DeleteRequest) Test(org.junit.Test)

Example 4 with DeleteRequest

use of ddf.catalog.operation.DeleteRequest 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();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) HashMap(java.util.HashMap) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Exchange(org.apache.camel.Exchange) Metacard(ddf.catalog.data.Metacard) DeleteResponse(ddf.catalog.operation.DeleteResponse) DeleteResponseImpl(ddf.catalog.operation.impl.DeleteResponseImpl) ArrayList(java.util.ArrayList) List(java.util.List) DeleteRequest(ddf.catalog.operation.DeleteRequest) Test(org.junit.Test)

Example 5 with DeleteRequest

use of ddf.catalog.operation.DeleteRequest 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);
}
Also used : Metacard(ddf.catalog.data.Metacard) DeleteResponse(ddf.catalog.operation.DeleteResponse) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) DeleteRequest(ddf.catalog.operation.DeleteRequest) Endpoint(org.apache.camel.Endpoint)

Aggregations

DeleteRequest (ddf.catalog.operation.DeleteRequest)39 Test (org.junit.Test)29 DeleteResponse (ddf.catalog.operation.DeleteResponse)22 DeleteRequestImpl (ddf.catalog.operation.impl.DeleteRequestImpl)22 Metacard (ddf.catalog.data.Metacard)18 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)11 Serializable (java.io.Serializable)10 DeleteResponseImpl (ddf.catalog.operation.impl.DeleteResponseImpl)9 List (java.util.List)9 CatalogFramework (ddf.catalog.CatalogFramework)7 QueryResponse (ddf.catalog.operation.QueryResponse)7 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)7 Result (ddf.catalog.data.Result)6 QueryRequest (ddf.catalog.operation.QueryRequest)6 ContentType (ddf.catalog.data.ContentType)5 ResourceRequest (ddf.catalog.operation.ResourceRequest)5 QueryImpl (ddf.catalog.operation.impl.QueryImpl)5 Filter (org.opengis.filter.Filter)5 ResultImpl (ddf.catalog.data.impl.ResultImpl)4