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);
}
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);
}
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();
}
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 }));
}
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>());
}
Aggregations