Search in sources :

Example 6 with GeoEntryExtractor

use of org.codice.ddf.spatial.geocoding.GeoEntryExtractor 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 7 with GeoEntryExtractor

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

the class TestGeoNamesUpdateCommand method testExceptionDuringExtraction.

@Test
public void testExceptionDuringExtraction() throws IOException, GeoNamesRemoteDownloadException, GeoEntryExtractionException, GeoEntryIndexingException {
    final String errorText = "Extraction error text";
    final GeoEntryExtractor geoEntryExtractor = mock(GeoEntryExtractor.class);
    final GeoEntryExtractionException geoEntryExtractionException = new GeoEntryExtractionException(errorText);
    doThrow(geoEntryExtractionException).when(geoEntryExtractor).pushGeoEntriesToExtractionCallback(anyString(), any(ExtractionCallback.class));
    final GeoEntryIndexer geoEntryIndexer = 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, GeoEntryExtractionException, GeoEntryIndexingException {
            geoEntryExtractor.pushGeoEntriesToExtractionCallback(resource, mock(ExtractionCallback.class));
        }
    };
    geoNamesUpdateCommand.setGeoEntryIndexer(geoEntryIndexer);
    geoNamesUpdateCommand.setGeoEntryExtractor(geoEntryExtractor);
    geoNamesUpdateCommand.setResource("temp.txt");
    geoNamesUpdateCommand.execute();
    assertThat(consoleInterceptor.getOutput(), containsString(errorText));
    consoleInterceptor.resetSystemOut();
    consoleInterceptor.closeBuffer();
}
Also used : GeoEntryIndexer(org.codice.ddf.spatial.geocoding.GeoEntryIndexer) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) ArrayList(java.util.ArrayList) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.containsString(org.hamcrest.Matchers.containsString) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback) GeoEntryExtractor(org.codice.ddf.spatial.geocoding.GeoEntryExtractor) Test(org.junit.Test)

Example 8 with GeoEntryExtractor

use of org.codice.ddf.spatial.geocoding.GeoEntryExtractor 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 9 with GeoEntryExtractor

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

the class TestGeoNamesLuceneIndexer method testCreateIndexFromExtractor.

@Test
public void testCreateIndexFromExtractor() throws IOException, GeoEntryIndexingException, GeoNamesRemoteDownloadException, GeoEntryExtractionException {
    configureMocks();
    final ProgressCallback progressCallback = mock(ProgressCallback.class);
    geoNamesLuceneIndexer.updateIndex(null, 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) throws GeoEntryExtractionException {
            try {
                extractionCallback.extracted(GEO_ENTRY_1);
                extractionCallback.extracted(GEO_ENTRY_2);
                extractionCallback.extracted(GEO_ENTRY_3);
                extractionCallback.extracted(GEO_ENTRY_4);
                extractionCallback.extracted(GEO_ENTRY_5);
                extractionCallback.extracted(GEO_ENTRY_6);
                extractionCallback.extracted(GEO_ENTRY_7);
                extractionCallback.extracted(GEO_ENTRY_8);
                extractionCallback.extracted(GEO_ENTRY_9);
                extractionCallback.updateProgress(100);
            } catch (GeoEntryIndexingException e) {
                throw new GeoEntryExtractionException("Unable to add entry.", e);
            }
        }

        @Override
        public void setUrl(String url) {
            return;
        }
    }, true, progressCallback);
    verify(indexWriter, times(9)).addDocument(documentArgumentCaptor.capture());
    final List<Document> documentList = documentArgumentCaptor.getAllValues();
    verifyDocumentList(documentList);
    verify(progressCallback, times(1)).updateProgress(100);
}
Also used : GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) List(java.util.List) Document(org.apache.lucene.document.Document) GeoEntryExtractor(org.codice.ddf.spatial.geocoding.GeoEntryExtractor) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) Test(org.junit.Test)

Example 10 with GeoEntryExtractor

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

the class GazetteerUpdateCommand method executeWithSubject.

@Override
protected Object executeWithSubject() throws Exception {
    final PrintStream console = System.out;
    final ProgressCallback progressCallback = progress -> {
        console.printf("\r%d%%", progress);
        console.flush();
    };
    final FeatureIndexer.IndexCallback featureIndexCallback = count -> {
        console.printf("\r%d features indexed", count);
        console.flush();
    };
    console.println("Updating...");
    try {
        if (isResourceGeoJSON()) {
            featureIndexer.updateIndex(resource, featureExtractor, create, featureIndexCallback);
        } else {
            geoEntryIndexer.updateIndex(resource, geoEntryExtractor, create, progressCallback);
        }
        console.println("\nDone.");
    } catch (GeoEntryExtractionException | FeatureExtractionException e) {
        LOGGER.info("Error extracting data from resource {}", resource, e);
        console.printf("Could not extract data from resource %s.%n Message: %s%n Check the logs for more details.%n", resource, e.getMessage());
    } catch (GeoEntryIndexingException | FeatureIndexingException e) {
        LOGGER.info("Error indexing data", e);
        console.printf("Could not index the  data.%n Message: %s%n Check the logs for more details.%n", e.getMessage());
    } catch (GeoNamesRemoteDownloadException e) {
        LOGGER.info("Error downloading resource from remote source {}", resource, e);
        console.printf("Could not download the GeoNames file %s.%n  Message: %s%n Check the logs for more details.%n", resource, e.getMessage());
    }
    return null;
}
Also used : PrintStream(java.io.PrintStream) GeoEntryExtractor(org.codice.ddf.spatial.geocoding.GeoEntryExtractor) Logger(org.slf4j.Logger) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) GeoEntryIndexer(org.codice.ddf.spatial.geocoding.GeoEntryIndexer) FeatureIndexer(org.codice.ddf.spatial.geocoding.FeatureIndexer) LoggerFactory(org.slf4j.LoggerFactory) Argument(org.apache.karaf.shell.api.action.Argument) FeatureExtractor(org.codice.ddf.spatial.geocoding.FeatureExtractor) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) Command(org.apache.karaf.shell.api.action.Command) Reference(org.apache.karaf.shell.api.action.lifecycle.Reference) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) GeoNamesRemoteDownloadException(org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException) Locale(java.util.Locale) FeatureIndexingException(org.codice.ddf.spatial.geocoding.FeatureIndexingException) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Option(org.apache.karaf.shell.api.action.Option) FeatureExtractionException(org.codice.ddf.spatial.geocoding.FeatureExtractionException) SubjectCommands(org.codice.ddf.commands.catalog.SubjectCommands) GeoNamesRemoteDownloadException(org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException) PrintStream(java.io.PrintStream) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) FeatureIndexingException(org.codice.ddf.spatial.geocoding.FeatureIndexingException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) FeatureExtractionException(org.codice.ddf.spatial.geocoding.FeatureExtractionException) FeatureIndexer(org.codice.ddf.spatial.geocoding.FeatureIndexer) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)

Aggregations

GeoEntryExtractor (org.codice.ddf.spatial.geocoding.GeoEntryExtractor)11 GeoEntryIndexer (org.codice.ddf.spatial.geocoding.GeoEntryIndexer)8 ProgressCallback (org.codice.ddf.spatial.geocoding.ProgressCallback)8 GeoEntryExtractionException (org.codice.ddf.spatial.geocoding.GeoEntryExtractionException)7 Test (org.junit.Test)7 GeoEntryIndexingException (org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)4 ExtractionCallback (org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback)4 GeoNamesRemoteDownloadException (org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException)3 Matchers.anyString (org.mockito.Matchers.anyString)3 Before (org.junit.Before)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Metacard (ddf.catalog.data.Metacard)1 File (java.io.File)1 PrintStream (java.io.PrintStream)1 Locale (java.util.Locale)1 ExecutorService (java.util.concurrent.ExecutorService)1