Search in sources :

Example 16 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer in project alliance by codice.

the class OverviewSupplierTest method setUp.

@Before
public void setUp() throws CatalogTransformerException {
    final BinaryContent overviewContent = mock(BinaryContent.class);
    doAnswer(invocationOnMock -> getClass().getClassLoader().getResourceAsStream("flower.jpg")).when(overviewContent).getInputStream();
    final MetacardTransformer resourceMetacardTransformer = mock(MetacardTransformer.class);
    doReturn(overviewContent).when(resourceMetacardTransformer).transform(argThat(isMetacardWithDerivedOverviewResource()), eq(Collections.singletonMap(ContentItem.QUALIFIER_KEYWORD, "overview")));
    doThrow(CatalogTransformerException.class).when(resourceMetacardTransformer).transform(argThat(not(isMetacardWithDerivedOverviewResource())), eq(Collections.singletonMap(ContentItem.QUALIFIER_KEYWORD, "overview")));
    supplier = new OverviewSupplier(resourceMetacardTransformer);
}
Also used : MetacardTransformer(ddf.catalog.transform.MetacardTransformer) BinaryContent(ddf.catalog.data.BinaryContent) Before(org.junit.Before)

Example 17 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.

the class CatalogFrameworkImplTest method testMetacardTransform.

@Test
public void testMetacardTransform() throws Exception {
    BundleContext context = mock(BundleContext.class);
    MetacardTransformer transformer = mock(MetacardTransformer.class);
    ServiceReference reference = mock(ServiceReference.class);
    ServiceReference[] serviceReferences = new ServiceReference[] { reference };
    when(context.getServiceReferences(anyString(), anyString())).thenReturn(serviceReferences);
    when(context.getService(isA(ServiceReference.class))).thenReturn(transformer);
    when(transformer.transform(isA(Metacard.class), isA(Map.class))).thenReturn(new BinaryContentImpl(null));
    CatalogFramework framework = this.createDummyCatalogFramework(provider, storageProvider, context, eventAdmin, true);
    MetacardImpl newCard = new MetacardImpl();
    newCard.setId(null);
    BinaryContent content = framework.transform(newCard, "NONE", new HashMap<String, Serializable>());
    assertNotNull(content);
}
Also used : Serializable(java.io.Serializable) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) ServiceReference(org.osgi.framework.ServiceReference) Metacard(ddf.catalog.data.Metacard) CatalogFramework(ddf.catalog.CatalogFramework) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 18 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.

the class CswQueryResponseTransformer method multiThreadedMarshal.

/**
     * Multi-threaded marshal of metacard assumes that the query size is unbounded to guard against
     * resource exhaustion with fixed thread-pool and fixed work-queue.  CPU-bound for optimum utilization
     * from availableProcessors()+1 thread pool.
     *
     * @param results - the list of results to marshal
     * @param recordSchema - the schema
     * @param arguments - additional args
     * @return - the marshaled results
     * @throws CatalogTransformerException
     */
private String multiThreadedMarshal(List<Result> results, AtomicLong numResults, String recordSchema, final Map<String, Serializable> arguments) throws CatalogTransformerException {
    CompletionService<BinaryContent> completionService = new ExecutorCompletionService<>(queryExecutor);
    final MetacardTransformer transformer = metacardTransformerManager.getTransformerBySchema(recordSchema);
    if (transformer == null) {
        throw new CatalogTransformerException("Cannot find transformer for schema: " + recordSchema);
    }
    Map<Future<BinaryContent>, Result> futures = new HashMap<>(results.size());
    for (Result result : results) {
        final Metacard mc = result.getMetacard();
        // the "current" thread will run submitted task when queueSize exceeded; effectively
        // blocking enqueue of more tasks.
        futures.put(completionService.submit(() -> {
            BinaryContent content = transformer.transform(mc, arguments);
            return content;
        }), result);
    }
    InputStream[] contents = new InputStream[results.size()];
    while (!futures.isEmpty()) {
        try {
            Future<BinaryContent> completedFuture = completionService.take();
            int index = results.indexOf(futures.get(completedFuture));
            try {
                contents[index] = completedFuture.get().getInputStream();
            } catch (ExecutionException | CancellationException | InterruptedException e) {
                LOGGER.debug("Error transforming Metacard", e);
                numResults.decrementAndGet();
            } finally {
                futures.remove(completedFuture);
            }
        } catch (InterruptedException e) {
            LOGGER.debug("Metacard transform interrupted", e);
        }
    }
    CharArrayWriter accum = new CharArrayWriter(ACCUM_INITIAL_SIZE);
    for (InputStream is : contents) {
        try {
            if (is != null) {
                IOUtils.copy(is, accum);
            }
        } catch (IOException e) {
            LOGGER.debug("Error copying Metacard Binary content", e);
        }
    }
    return accum.toString();
}
Also used : MetacardTransformer(ddf.catalog.transform.MetacardTransformer) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) BinaryContent(ddf.catalog.data.BinaryContent) CharArrayWriter(java.io.CharArrayWriter) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) CancellationException(java.util.concurrent.CancellationException) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 19 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.

the class MetacardBackupPluginTest method setUp.

@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
    metacardTransformer = mock(MetacardTransformer.class);
    BinaryContent binaryContent = new BinaryContentImpl(new ByteArrayInputStream(XML_METADATA.getBytes(StandardCharsets.UTF_8)));
    when(metacardTransformer.transform(any(Metacard.class), anyMap())).thenReturn(binaryContent);
    doThrow(new MetacardBackupException("Not Implemented")).when(mockProvider).store(any(), any());
    doThrow(new MetacardBackupException("Not Implemented")).when(mockProvider).delete(any());
    doReturn(MOCK_ID).when(mockProvider).getId();
    metacardBackupPlugin = new MetacardBackupPlugin();
    metacardBackupPlugin.setMetacardTransformerId(METACARD_TRANSFORMER_ID);
    metacardBackupPlugin.setMetacardTransformer(metacardTransformer);
    createRequest = generateProcessRequest(ProcessCreateItem.class, true);
    updateRequest = generateProcessRequest(ProcessUpdateItem.class, true);
    deleteRequest = generateDeleteRequest();
    fileStorageProvider.setId(FILE_STORAGE_PROVIDER_ID);
    fileStorageProvider.setOutputDirectory(OUTPUT_DIRECTORY);
    metacardBackupPlugin.setMetacardOutputProviderIds(Collections.singletonList(FILE_STORAGE_PROVIDER_ID));
    metacardBackupPlugin.setStorageBackupPlugins(Arrays.asList(new MetacardBackupStorageProvider[] { fileStorageProvider }));
}
Also used : MetacardBackupException(org.codice.ddf.catalog.plugin.metacard.backup.storage.internal.MetacardBackupException) Metacard(ddf.catalog.data.Metacard) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) ByteArrayInputStream(java.io.ByteArrayInputStream) ProcessCreateItem(org.codice.ddf.catalog.async.data.api.internal.ProcessCreateItem) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) MetacardBackupStorageProvider(org.codice.ddf.catalog.plugin.metacard.backup.storage.internal.MetacardBackupStorageProvider) BinaryContent(ddf.catalog.data.BinaryContent) ProcessUpdateItem(org.codice.ddf.catalog.async.data.api.internal.ProcessUpdateItem) Before(org.junit.Before)

Example 20 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.

the class CatalogFrameworkImplTest method testMetacardTransformWithTransformException.

@Test(expected = CatalogTransformerException.class)
public void testMetacardTransformWithTransformException() throws Exception {
    BundleContext context = mock(BundleContext.class);
    MetacardTransformer transformer = mock(MetacardTransformer.class);
    ServiceReference reference = mock(ServiceReference.class);
    ServiceReference[] serviceReferences = new ServiceReference[] { reference };
    when(context.getServiceReferences(anyString(), anyString())).thenReturn(serviceReferences);
    when(context.getService(isA(ServiceReference.class))).thenReturn(transformer);
    when(transformer.transform(isA(Metacard.class), isA(Map.class))).thenThrow(new CatalogTransformerException("Could not transform"));
    CatalogFramework framework = this.createDummyCatalogFramework(provider, storageProvider, context, eventAdmin, true);
    MetacardImpl newCard = new MetacardImpl();
    newCard.setId(null);
    framework.transform(newCard, "NONE", new HashMap<String, Serializable>());
}
Also used : Metacard(ddf.catalog.data.Metacard) Serializable(java.io.Serializable) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) CatalogFramework(ddf.catalog.CatalogFramework) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Matchers.anyString(org.mockito.Matchers.anyString) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

MetacardTransformer (ddf.catalog.transform.MetacardTransformer)30 BinaryContent (ddf.catalog.data.BinaryContent)26 Test (org.junit.Test)21 SourceResponse (ddf.catalog.operation.SourceResponse)19 Metacard (ddf.catalog.data.Metacard)16 Result (ddf.catalog.data.Result)12 HashMap (java.util.HashMap)12 ResultImpl (ddf.catalog.data.impl.ResultImpl)10 FileOutputStream (java.io.FileOutputStream)9 Map (java.util.Map)9 File (java.io.File)8 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)4 ServiceReference (org.osgi.framework.ServiceReference)4 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)3 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)3 Serializable (java.io.Serializable)3 Matchers.anyString (org.mockito.Matchers.anyString)3 Action (ddf.action.Action)2 ActionProvider (ddf.action.ActionProvider)2 CatalogFramework (ddf.catalog.CatalogFramework)2