Search in sources :

Example 76 with CreateRequestImpl

use of ddf.catalog.operation.impl.CreateRequestImpl in project ddf by codice.

the class CatalogFrameworkImplTest method testInjectsAttributesOnQuery.

@Test
public void testInjectsAttributesOnQuery() throws Exception {
    final Metacard original = new MetacardImpl();
    final String id = framework.create(new CreateRequestImpl(Collections.singletonList(original), null)).getCreatedMetacards().get(0).getId();
    final AttributeDescriptor injectAttribute = new AttributeDescriptorImpl("new attribute", true, true, false, false, BasicTypes.DOUBLE_TYPE);
    stubMetacardInjection(injectAttribute);
    final FilterFactory filterFactory = new FilterFactoryImpl();
    final Filter filter = filterFactory.equals(filterFactory.property(Metacard.ID), filterFactory.literal(id));
    final QueryRequest request = new QueryRequestImpl(new QueryImpl(filter));
    final QueryResponse response = framework.query(request);
    final Metacard queryMetacard = response.getResults().get(0).getMetacard();
    final MetacardType originalMetacardType = original.getMetacardType();
    final MetacardType queryMetacardType = queryMetacard.getMetacardType();
    assertThat(originalMetacardType.getName(), is(queryMetacardType.getName()));
    final Set<AttributeDescriptor> expectedAttributeDescriptors = new HashSet<>(originalMetacardType.getAttributeDescriptors());
    expectedAttributeDescriptors.add(injectAttribute);
    assertThat(queryMetacardType.getAttributeDescriptors(), is(expectedAttributeDescriptors));
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AttributeDescriptorImpl(ddf.catalog.data.impl.AttributeDescriptorImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) FilterFactory(org.opengis.filter.FilterFactory) MetacardType(ddf.catalog.data.MetacardType) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 77 with CreateRequestImpl

use of ddf.catalog.operation.impl.CreateRequestImpl in project ddf by codice.

the class CatalogFrameworkQueryTest method testDuringQuery.

@Test
public void testDuringQuery() {
    List<Metacard> metacards = new ArrayList<Metacard>();
    MetacardImpl newCard1 = new MetacardImpl();
    newCard1.setId(null);
    Calendar duringStart = Calendar.getInstance();
    Calendar card1Exp = Calendar.getInstance();
    card1Exp.add(Calendar.YEAR, 1);
    Calendar duringEnd1 = Calendar.getInstance();
    duringEnd1.add(Calendar.YEAR, 2);
    Calendar card2Exp = Calendar.getInstance();
    card2Exp.add(Calendar.YEAR, 3);
    Calendar duringEnd2 = Calendar.getInstance();
    duringEnd2.add(Calendar.YEAR, 4);
    newCard1.setExpirationDate(card1Exp.getTime());
    metacards.add(newCard1);
    MetacardImpl newCard2 = new MetacardImpl();
    newCard2.setId(null);
    newCard2.setExpirationDate(card2Exp.getTime());
    metacards.add(newCard2);
    String mcId1 = null;
    String mcId2 = null;
    CreateResponse createResponse = null;
    try {
        createResponse = framework.create(new CreateRequestImpl(metacards, null));
    } catch (IngestException e1) {
        LOGGER.error("Failure", e1);
        fail();
    } catch (SourceUnavailableException e1) {
        LOGGER.error("Failure", e1);
        fail();
    }
    assertEquals(createResponse.getCreatedMetacards().size(), metacards.size());
    for (Metacard curCard : createResponse.getCreatedMetacards()) {
        if (curCard.getExpirationDate().equals(card1Exp.getTime())) {
            mcId1 = curCard.getId();
        } else {
            mcId2 = curCard.getId();
        }
        assertNotNull(curCard.getId());
    }
    FilterFactory filterFactory = new FilterFactoryImpl();
    Period duringPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(duringStart.getTime())), new DefaultInstant(new DefaultPosition(duringEnd1.getTime())));
    QueryImpl query = new QueryImpl(filterFactory.during(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(duringPeriod)));
    QueryRequest queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("Expecting return 1 result.", 1, response.getHits());
        assertEquals("During filter should return metacard[" + mcId1 + "]", mcId1, response.getResults().get(0).getMetacard().getId());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
    duringPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(card1Exp.getTime())), new DefaultInstant(new DefaultPosition(duringEnd2.getTime())));
    query = new QueryImpl(filterFactory.during(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(duringPeriod)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("During filter should return 1 result", 1, response.getHits());
        assertEquals("During filter should return metacard[" + mcId2 + "]", mcId2, response.getResults().get(0).getMetacard().getId());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
    duringPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(duringStart.getTime())), new DefaultInstant(new DefaultPosition(duringEnd2.getTime())));
    query = new QueryImpl(filterFactory.during(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(duringPeriod)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("During filter should return 2 result", 2, response.getHits());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryRequest(ddf.catalog.operation.QueryRequest) CreateResponse(ddf.catalog.operation.CreateResponse) Calendar(java.util.Calendar) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) Period(org.opengis.temporal.Period) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) DefaultInstant(org.geotools.temporal.object.DefaultInstant) FederationException(ddf.catalog.federation.FederationException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) FilterFactory(org.opengis.filter.FilterFactory) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) DefaultPosition(org.geotools.temporal.object.DefaultPosition) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) Test(org.junit.Test)

Example 78 with CreateRequestImpl

use of ddf.catalog.operation.impl.CreateRequestImpl in project ddf by codice.

the class CatalogFeatureIndexerTest method testCreateIndex.

@Test
public void testCreateIndex() throws FeatureIndexingException, FeatureExtractionException, SourceUnavailableException, IngestException {
    FeatureIndexer.IndexCallback indexCallback = mock(FeatureIndexer.IndexCallback.class);
    featureIndexer.updateIndex(RESOURCE_PATH, featureExtractor, true, indexCallback);
    ArgumentCaptor<DeleteRequestImpl> deleteRequestCaptor = ArgumentCaptor.forClass(DeleteRequestImpl.class);
    verify(catalogFramework, times(1)).delete(deleteRequestCaptor.capture());
    assertTrue(deleteRequestCaptor.getValue().getAttributeValues().contains(exampleMetacard.getId()));
    ArgumentCaptor<CreateRequestImpl> createRequestCaptor = ArgumentCaptor.forClass(CreateRequestImpl.class);
    verify(catalogFramework, times(1)).create(createRequestCaptor.capture());
    Metacard createdMetacard = createRequestCaptor.getValue().getMetacards().get(0);
    assertMetacardsAreEqual(createdMetacard, exampleMetacard);
    ArgumentCaptor<Integer> indexCallbackCaptor = ArgumentCaptor.forClass(Integer.class);
    verify(indexCallback, times(1)).indexed(indexCallbackCaptor.capture());
    assertThat(indexCallbackCaptor.getValue(), is(1));
}
Also used : Metacard(ddf.catalog.data.Metacard) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) FeatureIndexer(org.codice.ddf.spatial.geocoding.FeatureIndexer) Test(org.junit.Test)

Example 79 with CreateRequestImpl

use of ddf.catalog.operation.impl.CreateRequestImpl in project ddf by codice.

the class CatalogFeatureIndexer method createOrUpdateMetacardForFeature.

private void createOrUpdateMetacardForFeature(SimpleFeature feature, boolean create) throws FeatureIndexingException {
    try {
        security.runWithSubjectOrElevate(() -> {
            Metacard metacard = null;
            if (!create) {
                metacard = findMetacardForFeature(feature);
            }
            if (metacard == null) {
                metacard = createMetacardForFeature(feature);
                catalogFramework.create(new CreateRequestImpl(metacard));
            } else {
                catalogFramework.update(new UpdateRequestImpl(metacard.getId(), metacard));
            }
            return null;
        });
    } catch (SecurityServiceException | InvocationTargetException e) {
        throw new FeatureIndexingException(e.getMessage());
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) SecurityServiceException(ddf.security.service.SecurityServiceException) FeatureIndexingException(org.codice.ddf.spatial.geocoding.FeatureIndexingException) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 80 with CreateRequestImpl

use of ddf.catalog.operation.impl.CreateRequestImpl in project ddf by codice.

the class GeoNamesCatalogIndexer method executeCreateMetacardRequest.

private void executeCreateMetacardRequest(List<Metacard> metacards) {
    int totalMetacards = metacards.size();
    for (int i = 0; i < totalMetacards; i += BATCH_SIZE) {
        int lastIndex = i + BATCH_SIZE;
        if (lastIndex > metacards.size()) {
            lastIndex = metacards.size();
        }
        List<Metacard> sublist = metacards.subList(i, lastIndex);
        Map<String, Serializable> properties = new HashMap<>();
        CreateRequest createRequest = new CreateRequestImpl(sublist, properties);
        try {
            CreateResponse createResponse = catalogFramework.create(createRequest);
            List<Metacard> createdMetacards = createResponse.getCreatedMetacards();
            LOGGER.trace("Created {} metacards.", createdMetacards == null ? 0 : createdMetacards.size());
        } catch (IngestException | SourceUnavailableException e) {
            LOGGER.debug("Unable to create Metacards", e);
        }
    }
    LOGGER.trace("Created {} metacards.", totalMetacards);
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Serializable(java.io.Serializable) HashMap(java.util.HashMap) CreateRequest(ddf.catalog.operation.CreateRequest) CreateResponse(ddf.catalog.operation.CreateResponse) Metacard(ddf.catalog.data.Metacard) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException)

Aggregations

CreateRequestImpl (ddf.catalog.operation.impl.CreateRequestImpl)80 Test (org.junit.Test)60 Metacard (ddf.catalog.data.Metacard)53 CreateResponse (ddf.catalog.operation.CreateResponse)44 CreateRequest (ddf.catalog.operation.CreateRequest)42 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)29 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)26 Serializable (java.io.Serializable)22 CreateResponseImpl (ddf.catalog.operation.impl.CreateResponseImpl)19 IngestException (ddf.catalog.source.IngestException)19 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)17 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)17 InputStream (java.io.InputStream)17 HashSet (java.util.HashSet)16 QueryImpl (ddf.catalog.operation.impl.QueryImpl)15 Subject (ddf.security.Subject)15 QueryRequest (ddf.catalog.operation.QueryRequest)13 QueryResponse (ddf.catalog.operation.QueryResponse)13 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)12