Search in sources :

Example 1 with ExtractionCallback

use of org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback in project ddf by codice.

the class TestGeoNamesFileExtractor method testFileExtractionStreaming.

private void testFileExtractionStreaming(final String fileLocation) throws GeoEntryExtractionException, GeoNamesRemoteDownloadException, GeoEntryIndexingException {
    final ExtractionCallback extractionCallback = mock(ExtractionCallback.class);
    final ArgumentCaptor<GeoEntry> geoEntryArgumentCaptor = ArgumentCaptor.forClass(GeoEntry.class);
    geoNamesFileExtractor.pushGeoEntriesToExtractionCallback(fileLocation, extractionCallback);
    verify(extractionCallback, atLeastOnce()).updateProgress(anyInt());
    verify(extractionCallback, times(3)).extracted(geoEntryArgumentCaptor.capture());
    final List<GeoEntry> capturedGeoEntryList = geoEntryArgumentCaptor.getAllValues();
    verifyGeoEntryList(capturedGeoEntryList);
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback)

Example 2 with ExtractionCallback

use of org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback in project ddf by codice.

the class GazetteerUpdateCommandTest method testFeatureIndexing.

@Test
public void testFeatureIndexing() throws Exception {
    String resource = "example.geojson";
    final FeatureExtractor featureExtractor = spy(new FeatureExtractor() {

        @Override
        public void pushFeaturesToExtractionCallback(String resource, ExtractionCallback extractionCallback) throws FeatureExtractionException {
        /* stub */
        }
    });
    final FeatureIndexer featureIndexer = spy(new FeatureIndexer() {

        @Override
        public void updateIndex(String resource, FeatureExtractor featureExtractor, boolean create, IndexCallback callback) throws FeatureExtractionException, FeatureIndexingException {
        /* stub */
        }
    });
    gazetteerUpdateCommand.setResource(resource);
    gazetteerUpdateCommand.setFeatureExtractor(featureExtractor);
    gazetteerUpdateCommand.setFeatureIndexer(featureIndexer);
    gazetteerUpdateCommand.executeWithSubject();
    verify(featureIndexer, times(1)).updateIndex(eq(resource), eq(featureExtractor), eq(false), any(FeatureIndexer.IndexCallback.class));
}
Also used : FeatureExtractor(org.codice.ddf.spatial.geocoding.FeatureExtractor) FeatureIndexingException(org.codice.ddf.spatial.geocoding.FeatureIndexingException) FeatureExtractionException(org.codice.ddf.spatial.geocoding.FeatureExtractionException) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback) FeatureIndexer(org.codice.ddf.spatial.geocoding.FeatureIndexer) Test(org.junit.Test)

Example 3 with ExtractionCallback

use of org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback in project ddf by codice.

the class GazetteerUpdateCommandTest method testProgressOutput.

@Test
public void testProgressOutput() throws Exception {
    final GeoEntryExtractor geoEntryExtractor = spy(new GeoEntryExtractor() {

        @Override
        public List<GeoEntry> getGeoEntries(final String resource, final ProgressCallback progressCallback) {
            return null;
        }

        @Override
        public void pushGeoEntriesToExtractionCallback(final String resource, final ExtractionCallback extractionCallback) {
            extractionCallback.updateProgress(50);
            assertThat(consoleInterceptor.getOutput(), containsString("50%"));
            extractionCallback.updateProgress(100);
            assertThat(consoleInterceptor.getOutput(), containsString("100%"));
        }

        @Override
        public void setUrl(String url) {
            return;
        }
    });
    final GeoEntryExtractor geoEntryUrlExtractor = spy(new GeoEntryExtractor() {

        @Override
        public List<GeoEntry> getGeoEntries(final String resource, final ProgressCallback progressCallback) {
            return null;
        }

        @Override
        public void pushGeoEntriesToExtractionCallback(final String resource, final ExtractionCallback extractionCallback) {
            extractionCallback.updateProgress(50);
            assertThat(consoleInterceptor.getOutput(), containsString("50%"));
            extractionCallback.updateProgress(100);
            assertThat(consoleInterceptor.getOutput(), containsString("100%"));
        }

        @Override
        public void setUrl(String url) {
            return;
        }
    });
    final GeoEntryIndexer geoEntryIndexer = spy(new GeoEntryIndexer() {

        @Override
        public void updateIndex(final List<GeoEntry> newEntries, final boolean create, final ProgressCallback progressCallback, final String entrySource) {
        /* stub */
        }

        @Override
        public void updateIndex(final String resource, final GeoEntryExtractor geoEntryExtractor, final boolean create, final ProgressCallback progressCallback) throws GeoNamesRemoteDownloadException, GeoEntryIndexingException, GeoEntryExtractionException {
            final ExtractionCallback extractionCallback = new ExtractionCallback() {

                @Override
                public void extracted(final GeoEntry newEntry) {
                // Not used in test
                }

                @Override
                public void updateProgress(final int progress) {
                    progressCallback.updateProgress(progress);
                }
            };
            geoEntryExtractor.pushGeoEntriesToExtractionCallback(resource, extractionCallback);
        }
    });
    List<GeoEntryExtractor> geoEntryExtractors = new ArrayList<>();
    geoEntryExtractors.add(geoEntryExtractor);
    geoEntryExtractors.add(geoEntryUrlExtractor);
    gazetteerUpdateCommand.setGeoEntryExtractor(geoEntryExtractor);
    gazetteerUpdateCommand.setGeoEntryIndexer(geoEntryIndexer);
    gazetteerUpdateCommand.setResource("test");
    gazetteerUpdateCommand.executeWithSubject();
    consoleInterceptor.resetSystemOut();
    consoleInterceptor.closeBuffer();
}
Also used : GeoNamesRemoteDownloadException(org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GeoEntryExtractor(org.codice.ddf.spatial.geocoding.GeoEntryExtractor) GeoEntryIndexer(org.codice.ddf.spatial.geocoding.GeoEntryIndexer) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ArrayList(java.util.ArrayList) List(java.util.List) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) Test(org.junit.Test)

Example 4 with ExtractionCallback

use of org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback in project ddf by codice.

the class TestGeoNamesUpdateCommand method testProgressOutput.

@Test
public void testProgressOutput() throws IOException {
    final GeoEntryExtractor geoEntryExtractor = spy(new GeoEntryExtractor() {

        @Override
        public List<GeoEntry> getGeoEntries(final String resource, final ProgressCallback progressCallback) {
            return null;
        }

        @Override
        public void pushGeoEntriesToExtractionCallback(final String resource, final ExtractionCallback extractionCallback) {
            extractionCallback.updateProgress(50);
            assertThat(consoleInterceptor.getOutput(), containsString("50%"));
            extractionCallback.updateProgress(100);
            assertThat(consoleInterceptor.getOutput(), containsString("100%"));
        }

        @Override
        public void setUrl(String url) {
            return;
        }
    });
    final GeoEntryExtractor geoEntryUrlExtractor = spy(new GeoEntryExtractor() {

        @Override
        public List<GeoEntry> getGeoEntries(final String resource, final ProgressCallback progressCallback) {
            return null;
        }

        @Override
        public void pushGeoEntriesToExtractionCallback(final String resource, final ExtractionCallback extractionCallback) {
            extractionCallback.updateProgress(50);
            assertThat(consoleInterceptor.getOutput(), containsString("50%"));
            extractionCallback.updateProgress(100);
            assertThat(consoleInterceptor.getOutput(), containsString("100%"));
        }

        @Override
        public void setUrl(String url) {
            return;
        }
    });
    final GeoEntryIndexer geoEntryIndexer = spy(new GeoEntryIndexer() {

        @Override
        public void updateIndex(final List<GeoEntry> newEntries, final boolean create, final ProgressCallback progressCallback) {
        }

        @Override
        public void updateIndex(final String resource, final GeoEntryExtractor geoEntryExtractor, final boolean create, final ProgressCallback progressCallback) throws GeoNamesRemoteDownloadException, GeoEntryIndexingException, GeoEntryExtractionException {
            final ExtractionCallback extractionCallback = new ExtractionCallback() {

                @Override
                public void extracted(final GeoEntry newEntry) {
                }

                @Override
                public void updateProgress(final int progress) {
                    progressCallback.updateProgress(progress);
                }
            };
            geoEntryExtractor.pushGeoEntriesToExtractionCallback(resource, extractionCallback);
        }
    });
    List<GeoEntryExtractor> geoEntryExtractors = new ArrayList<GeoEntryExtractor>();
    geoEntryExtractors.add(geoEntryExtractor);
    geoEntryExtractors.add(geoEntryUrlExtractor);
    geoNamesUpdateCommand.setGeoEntryExtractor(geoEntryExtractor);
    geoNamesUpdateCommand.setGeoEntryIndexer(geoEntryIndexer);
    geoNamesUpdateCommand.setResource("test");
    geoNamesUpdateCommand.execute();
    consoleInterceptor.resetSystemOut();
    consoleInterceptor.closeBuffer();
}
Also used : GeoNamesRemoteDownloadException(org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoEntryExtractor(org.codice.ddf.spatial.geocoding.GeoEntryExtractor) GeoEntryIndexer(org.codice.ddf.spatial.geocoding.GeoEntryIndexer) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ArrayList(java.util.ArrayList) List(java.util.List) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) Test(org.junit.Test)

Example 5 with ExtractionCallback

use of org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback in project ddf by codice.

the class GeoNamesLuceneIndexer method updateIndex.

@Override
public void updateIndex(final String resource, final GeoEntryExtractor geoEntryExtractor, final boolean create, final ProgressCallback progressCallback) throws GeoEntryIndexingException, GeoEntryExtractionException, GeoNamesRemoteDownloadException {
    Directory directory;
    try {
        directory = FSDirectory.open(Paths.get(indexLocation));
    } catch (IOException e) {
        throw new GeoEntryIndexingException("Couldn't open the directory for the index, " + indexLocation, e);
    }
    // Try-with-resources to ensure the IndexWriter always gets closed.
    try (final IndexWriter indexWriter = createIndexWriter(create, directory)) {
        final SpatialPrefixTree grid = new GeohashPrefixTree(SPATIAL_CONTEXT, GeoNamesLuceneConstants.GEOHASH_LEVELS);
        final SpatialStrategy strategy = new RecursivePrefixTreeStrategy(grid, GeoNamesLuceneConstants.GEO_FIELD);
        final ExtractionCallback extractionCallback = new ExtractionCallback() {

            @Override
            public void extracted(final GeoEntry newEntry) throws GeoEntryIndexingException {
                try {
                    addDocument(indexWriter, newEntry, strategy);
                } catch (IOException e) {
                    throw new GeoEntryIndexingException("Error writing to the index.", e);
                }
            }

            @Override
            public void updateProgress(final int progress) {
                if (progressCallback != null) {
                    progressCallback.updateProgress(progress);
                }
            }
        };
        try {
            geoEntryExtractor.pushGeoEntriesToExtractionCallback(resource, extractionCallback);
        } catch (GeoEntryExtractionException e) {
            // Need to roll back here before the IndexWriter is closed at the end of the try
            // block.
            indexWriter.rollback();
            throw e;
        }
    } catch (IOException e) {
        throw new GeoEntryIndexingException("Error writing to the index.", e);
    }
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) IndexWriter(org.apache.lucene.index.IndexWriter) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) IOException(java.io.IOException) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

Aggregations

ExtractionCallback (org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback)6 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)5 GeoEntryExtractionException (org.codice.ddf.spatial.geocoding.GeoEntryExtractionException)3 GeoEntryIndexingException (org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 GeoEntryExtractor (org.codice.ddf.spatial.geocoding.GeoEntryExtractor)2 GeoEntryIndexer (org.codice.ddf.spatial.geocoding.GeoEntryIndexer)2 GeoNamesRemoteDownloadException (org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException)2 ProgressCallback (org.codice.ddf.spatial.geocoding.ProgressCallback)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 IOException (java.io.IOException)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 SpatialStrategy (org.apache.lucene.spatial.SpatialStrategy)1 RecursivePrefixTreeStrategy (org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)1 GeohashPrefixTree (org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)1 SpatialPrefixTree (org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree)1 Directory (org.apache.lucene.store.Directory)1