Search in sources :

Example 1 with UpdateRequest

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

the class InMemoryProcessingFramework method storeMetacardUpdates.

private void storeMetacardUpdates(Map<String, Metacard> metacardsToUpdate, Map<String, Serializable> properties) {
    if (MapUtils.isNotEmpty(metacardsToUpdate)) {
        LOGGER.trace("Storing metacard updates");
        List<Map.Entry<Serializable, Metacard>> updateList = metacardsToUpdate.values().stream().map(metacard -> new AbstractMap.SimpleEntry<Serializable, Metacard>(metacard.getId(), metacard)).collect(Collectors.toList());
        UpdateRequest updateMetacardsRequest = new UpdateRequestImpl(updateList, UpdateRequest.UPDATE_BY_ID, properties);
        Subject subject = (Subject) updateMetacardsRequest.getProperties().get(SecurityConstants.SECURITY_SUBJECT);
        if (subject == null) {
            LOGGER.debug("No subject to send UpdateRequest. Updates will not be sent back to the catalog.");
        } else {
            subject.execute(() -> {
                try {
                    catalogFramework.update(updateMetacardsRequest);
                    LOGGER.debug("Successfully completed update metacards request");
                } catch (IngestException | SourceUnavailableException | RuntimeException e) {
                    LOGGER.info("Unable to complete update request", e);
                }
                return null;
            });
        }
    } else {
        LOGGER.debug("No metacards to update");
    }
}
Also used : ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) CatalogFramework(ddf.catalog.CatalogFramework) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) LoggerFactory(org.slf4j.LoggerFactory) UpdateStorageRequestImpl(ddf.catalog.content.operation.impl.UpdateStorageRequestImpl) HashMap(java.util.HashMap) ProcessDeleteItem(org.codice.ddf.catalog.async.data.api.internal.ProcessDeleteItem) PostProcessPlugin(org.codice.ddf.catalog.async.plugin.api.internal.PostProcessPlugin) ArrayList(java.util.ArrayList) ProcessingFramework(org.codice.ddf.catalog.async.processingframework.api.internal.ProcessingFramework) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) ContentItem(ddf.catalog.content.data.ContentItem) Metacard(ddf.catalog.data.Metacard) Map(java.util.Map) SecurityConstants(ddf.security.SecurityConstants) UpdateRequest(ddf.catalog.operation.UpdateRequest) ProcessResourceItem(org.codice.ddf.catalog.async.data.api.internal.ProcessResourceItem) ByteSource(com.google.common.io.ByteSource) ExecutorService(java.util.concurrent.ExecutorService) ProcessResource(org.codice.ddf.catalog.async.data.api.internal.ProcessResource) Logger(org.slf4j.Logger) MapUtils(org.apache.commons.collections.MapUtils) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) Subject(ddf.security.Subject) ProcessCreateItem(org.codice.ddf.catalog.async.data.api.internal.ProcessCreateItem) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) IOUtils(org.apache.commons.io.IOUtils) AbstractMap(java.util.AbstractMap) List(java.util.List) Validate.notNull(org.apache.commons.lang3.Validate.notNull) ProcessRequest(org.codice.ddf.catalog.async.data.api.internal.ProcessRequest) ProcessUpdateItem(org.codice.ddf.catalog.async.data.api.internal.ProcessUpdateItem) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) UpdateRequest(ddf.catalog.operation.UpdateRequest) IngestException(ddf.catalog.source.IngestException) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Subject(ddf.security.Subject)

Example 2 with UpdateRequest

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

the class CatalogComponentFrameworkTest method testUpdateWithIngestException.

@Test
public /**
     * Operation: UPDATE
     * Body contains:  Metacard
     */
void testUpdateWithIngestException() 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 Update update = new UpdateImpl(metacard1, metacard2);
    List<Update> updates = new ArrayList<Update>();
    updates.add(update);
    final String[] metacardIds = new String[metacards.size()];
    for (int i = 0; i < metacards.size(); i++) {
        metacardIds[i] = metacards.get(i).getId();
    }
    UpdateRequest updateRequest = new UpdateRequestImpl(metacardIds, metacards);
    UpdateResponse updateResponse = new UpdateResponseImpl(updateRequest, new HashMap(), updates);
    when(catalogFramework.update(any(UpdateRequest.class))).thenThrow(new IngestException());
    // Exercise the route with a UPDATE operation
    template.sendBodyAndHeader("direct:sampleInput", metacards, "Operation", "UPDATE");
    // 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> cardsUpdated = (List<Update>) exchange.getIn().getBody();
    assertListSize(cardsUpdated, 0);
    mockVerifierEndpoint.assertIsSatisfied();
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) UpdateRequest(ddf.catalog.operation.UpdateRequest) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Exchange(org.apache.camel.Exchange) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl) IngestException(ddf.catalog.source.IngestException) ArrayList(java.util.ArrayList) List(java.util.List) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Test(org.junit.Test)

Example 3 with UpdateRequest

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

the class CatalogComponentFrameworkTest method testUpdateWithListOfMetacards.

@Test
public /**
     * Operation: UPDATE
     * Body contains:  List<Metacard>
     */
void testUpdateWithListOfMetacards() 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 Update update = new UpdateImpl(metacard1, metacard2);
    List<Update> updates = new ArrayList<Update>();
    updates.add(update);
    final String[] metacardIds = new String[metacards.size()];
    for (int i = 0; i < metacards.size(); i++) {
        metacardIds[i] = metacards.get(i).getId();
    }
    UpdateRequest updateRequest = new UpdateRequestImpl(metacardIds, metacards);
    UpdateResponse updateResponse = new UpdateResponseImpl(updateRequest, new HashMap(), updates);
    when(catalogFramework.update(any(UpdateRequest.class))).thenReturn(updateResponse);
    // Exercise the route with a UPDATE operation
    template.sendBodyAndHeader("direct:sampleInput", metacards, "Operation", "UPDATE");
    // 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> cardsUpdated = (List<Update>) exchange.getIn().getBody();
    assertListSize(cardsUpdated, 1);
    mockVerifierEndpoint.assertIsSatisfied();
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) UpdateRequest(ddf.catalog.operation.UpdateRequest) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Exchange(org.apache.camel.Exchange) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl) ArrayList(java.util.ArrayList) List(java.util.List) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Test(org.junit.Test)

Example 4 with UpdateRequest

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

the class TestMetacardGroomerPlugin method testUpdateByAlternativeIdentifier.

@Test
public void testUpdateByAlternativeIdentifier() throws PluginExecutionException, StopProcessingException, URISyntaxException {
    Date snapshotOfNow = new Date();
    Metacard inputMetacard = getStandardMetacard(null);
    URI[] uris = new URI[1];
    uris[0] = new URI(SAMPLE_ID);
    UpdateRequestImpl inputRequest = new UpdateRequestImpl(uris, Arrays.asList(copy(inputMetacard)));
    UpdateRequest returnedRequest = plugin.process(inputRequest);
    assertNotNull(returnedRequest);
    assertThat(returnedRequest.getUpdates().size(), is(1));
    Metacard outputMetacard = returnedRequest.getUpdates().get(0).getValue();
    assertThat(inputMetacard.getId(), is(outputMetacard.getId()));
    assertEquals(DEFAULT_TITLE, outputMetacard.getTitle());
    assertEquals(DEFAULT_LOCATION, outputMetacard.getLocation());
    assertEquals(DEFAULT_TYPE, outputMetacard.getContentTypeName());
    assertEquals(DEFAULT_VERSION, outputMetacard.getContentTypeVersion());
    assertThat(outputMetacard.getMetadata(), is(DEFAULT_METADATA));
    assertThat(outputMetacard.getCreatedDate().getTime(), is(inputMetacard.getCreatedDate().getTime()));
    assertThat(((Date) outputMetacard.getAttribute(Core.METACARD_CREATED).getValue()).getTime(), equalTo(((Date) inputMetacard.getAttribute(Core.METACARD_CREATED).getValue()).getTime()));
    assertThat(outputMetacard.getModifiedDate().getTime(), is(equalTo(inputMetacard.getModifiedDate().getTime())));
    assertThat(((Date) outputMetacard.getAttribute(Core.METACARD_MODIFIED).getValue()).getTime(), is(greaterThanOrEqualTo(snapshotOfNow.getTime())));
    assertEquals(inputMetacard.getEffectiveDate(), outputMetacard.getEffectiveDate());
    assertEquals(inputMetacard.getExpirationDate(), outputMetacard.getExpirationDate());
    assertTrue(Arrays.equals(inputMetacard.getThumbnail(), outputMetacard.getThumbnail()));
    assertEquals(inputMetacard.getLocation(), outputMetacard.getLocation());
}
Also used : Metacard(ddf.catalog.data.Metacard) UpdateRequest(ddf.catalog.operation.UpdateRequest) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) URI(java.net.URI) Date(java.util.Date) Test(org.junit.Test)

Example 5 with UpdateRequest

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

the class SolrProviderTest method testUpdateNonUniqueAttributeValue2.

/**
     * Tests if we catch a rare case where some attribute value match multiple Metacards while
     * others do not match any records.
     *
     * @throws IngestException
     * @throws UnsupportedQueryException
     */
@Test(expected = IngestException.class)
public void testUpdateNonUniqueAttributeValue2() throws IngestException, UnsupportedQueryException {
    deleteAllIn(provider);
    MockMetacard m1 = new MockMetacard(Library.getFlagstaffRecord());
    MockMetacard m2 = new MockMetacard(Library.getFlagstaffRecord());
    List<Metacard> list = Arrays.asList((Metacard) m1, m2);
    create(list);
    provider.update(new UpdateRequest() {

        @Override
        public boolean hasProperties() {
            return false;
        }

        @Override
        public Serializable getPropertyValue(String name) {
            return null;
        }

        @Override
        public Set<String> getPropertyNames() {
            return null;
        }

        @Override
        public Map<String, Serializable> getProperties() {
            return null;
        }

        @Override
        public boolean containsPropertyName(String name) {
            return false;
        }

        @Override
        public List<Entry<Serializable, Metacard>> getUpdates() {
            MockMetacard newMetacard = new MockMetacard(Library.getShowLowRecord());
            List<Entry<Serializable, Metacard>> updateList = new ArrayList<Entry<Serializable, Metacard>>();
            updateList.add(new SimpleEntry<Serializable, Metacard>(MockMetacard.DEFAULT_TITLE, newMetacard));
            updateList.add(new SimpleEntry<Serializable, Metacard>(TAMPA_QUERY_PHRASE, newMetacard));
            return updateList;
        }

        @Override
        public String getAttributeName() {
            return Metacard.TITLE;
        }
    });
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) UpdateRequest(ddf.catalog.operation.UpdateRequest) SimpleEntry(java.util.AbstractMap.SimpleEntry) Matchers.containsString(org.hamcrest.Matchers.containsString) Metacard(ddf.catalog.data.Metacard) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Map(java.util.Map) Test(org.junit.Test)

Aggregations

UpdateRequest (ddf.catalog.operation.UpdateRequest)61 Test (org.junit.Test)44 Metacard (ddf.catalog.data.Metacard)41 ArrayList (java.util.ArrayList)32 Serializable (java.io.Serializable)25 UpdateResponse (ddf.catalog.operation.UpdateResponse)23 UpdateRequestImpl (ddf.catalog.operation.impl.UpdateRequestImpl)23 HashMap (java.util.HashMap)21 Update (ddf.catalog.operation.Update)17 List (java.util.List)16 Entry (java.util.Map.Entry)16 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)15 Map (java.util.Map)12 SimpleEntry (java.util.AbstractMap.SimpleEntry)11 IngestException (ddf.catalog.source.IngestException)10 URI (java.net.URI)10 Result (ddf.catalog.data.Result)9 CreateRequest (ddf.catalog.operation.CreateRequest)9 CatalogFramework (ddf.catalog.CatalogFramework)8 CreateResponse (ddf.catalog.operation.CreateResponse)8