use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class CatalogServiceImplTest method addMatchingService.
@SuppressWarnings({ "unchecked" })
private MimeTypeToTransformerMapper addMatchingService(CatalogServiceImpl catalogServiceImpl, List<InputTransformer> sortedListOfTransformers) {
MimeTypeToTransformerMapper matchingService = mock(MimeTypeToTransformerMapper.class);
when(matchingService.findMatches(eq(InputTransformer.class), isA(MimeType.class))).thenReturn((List) sortedListOfTransformers);
catalogServiceImpl.setMimeTypeToTransformerMapper(matchingService);
return matchingService;
}
use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class CatalogServiceImplTest method testParseAttachments.
@Test
@SuppressWarnings({ "unchecked" })
public void testParseAttachments() throws IOException, CatalogTransformerException, SourceUnavailableException, IngestException, InvalidSyntaxException {
CatalogFramework framework = givenCatalogFramework();
BundleContext bundleContext = mock(BundleContext.class);
Collection<ServiceReference<InputTransformer>> serviceReferences = new ArrayList<>();
ServiceReference serviceReference = mock(ServiceReference.class);
InputTransformer inputTransformer = mock(InputTransformer.class);
MetacardImpl metacard = new MetacardImpl();
metacard.setMetadata("Some Text Again");
when(inputTransformer.transform(any())).thenReturn(metacard);
when(bundleContext.getService(serviceReference)).thenReturn(inputTransformer);
serviceReferences.add(serviceReference);
when(bundleContext.getServiceReferences(InputTransformer.class, "(id=xml)")).thenReturn(serviceReferences);
CatalogServiceImpl catalogServiceImpl = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry) {
@Override
protected BundleContext getBundleContext() {
return bundleContext;
}
};
when(attributeRegistry.lookup(Core.METADATA)).thenReturn(Optional.of(new CoreAttributes().getAttributeDescriptor(Core.METADATA)));
when(attributeRegistry.lookup("foo")).thenReturn(Optional.empty());
addMatchingService(catalogServiceImpl, Collections.singletonList(getSimpleTransformer()));
List<Attachment> attachments = new ArrayList<>();
ContentDisposition contentDisposition = new ContentDisposition("form-data; name=parse.resource; filename=C:\\DDF\\metacard.txt");
Attachment attachment = new Attachment("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), contentDisposition);
attachments.add(attachment);
ContentDisposition contentDisposition1 = new ContentDisposition("form-data; name=parse.metadata; filename=C:\\DDF\\metacard.xml");
Attachment attachment1 = new Attachment("parse.metadata", new ByteArrayInputStream("Some Text Again".getBytes()), contentDisposition1);
attachments.add(attachment1);
Pair<AttachmentInfo, Metacard> attachmentInfoAndMetacard = catalogServiceImpl.parseAttachments(attachments, "xml");
assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("Some Text Again"));
ContentDisposition contentDisposition2 = new ContentDisposition("form-data; name=metadata; filename=C:\\DDF\\metacard.xml");
Attachment attachment2 = new Attachment("metadata", new ByteArrayInputStream("<meta>beta</meta>".getBytes()), contentDisposition2);
attachments.add(attachment2);
ContentDisposition contentDisposition3 = new ContentDisposition("form-data; name=foo; filename=C:\\DDF\\metacard.xml");
Attachment attachment3 = new Attachment("foo", new ByteArrayInputStream("bar".getBytes()), contentDisposition3);
attachments.add(attachment3);
attachmentInfoAndMetacard = catalogServiceImpl.parseAttachments(attachments, "xml");
assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("<meta>beta</meta>"));
assertThat(attachmentInfoAndMetacard.getValue().getAttribute("foo"), equalTo(null));
}
use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class CatalogServiceImplTest method testAddDocumentWithMetadataPositiveCase.
@Test
@SuppressWarnings({ "unchecked" })
public void testAddDocumentWithMetadataPositiveCase() throws Exception {
CatalogFramework framework = givenCatalogFramework();
HttpHeaders headers = createHeaders(Collections.singletonList(MediaType.APPLICATION_JSON));
BundleContext bundleContext = mock(BundleContext.class);
Collection<ServiceReference<InputTransformer>> serviceReferences = new ArrayList<>();
ServiceReference serviceReference = mock(ServiceReference.class);
InputTransformer inputTransformer = mock(InputTransformer.class);
when(inputTransformer.transform(any())).thenReturn(new MetacardImpl());
when(bundleContext.getService(serviceReference)).thenReturn(inputTransformer);
serviceReferences.add(serviceReference);
when(bundleContext.getServiceReferences(InputTransformer.class, "(id=xml)")).thenReturn(serviceReferences);
CatalogServiceImpl catalogService = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry) {
@Override
protected BundleContext getBundleContext() {
return bundleContext;
}
};
UuidGenerator uuidGenerator = mock(UuidGenerator.class);
when(uuidGenerator.generateUuid()).thenReturn(UUID.randomUUID().toString());
catalogService.setUuidGenerator(uuidGenerator);
when(attributeRegistry.lookup(Core.METADATA)).thenReturn(Optional.of(new CoreAttributes().getAttributeDescriptor(Core.METADATA)));
addMatchingService(catalogService, Collections.singletonList(getSimpleTransformer()));
List<Attachment> attachments = new ArrayList<>();
ContentDisposition contentDisposition = new ContentDisposition("form-data; name=parse.resource; filename=C:\\DDF\\metacard.txt");
Attachment attachment = new Attachment("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), contentDisposition);
attachments.add(attachment);
ContentDisposition contentDisposition1 = new ContentDisposition("form-data; name=parse.metadata; filename=C:\\DDF\\metacard.xml");
Attachment attachment1 = new Attachment("parse.metadata", new ByteArrayInputStream("Some Text Again".getBytes()), contentDisposition1);
attachments.add(attachment1);
ContentDisposition contentDisposition2 = new ContentDisposition("form-data; name=metadata; filename=C:\\DDF\\metacard.xml");
Attachment attachment2 = new Attachment("metadata", new ByteArrayInputStream("<meta>beta</meta>".getBytes()), contentDisposition2);
attachments.add(attachment2);
MultipartBody multipartBody = new MultipartBody(attachments);
String response = catalogService.addDocument(headers.getRequestHeader(HttpHeaders.CONTENT_TYPE), multipartBody, null, new ByteArrayInputStream("".getBytes()));
LOGGER.debug(ToStringBuilder.reflectionToString(response));
assertThat(response, equalTo(SAMPLE_ID));
}
use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class OpenSearchSourceTest method testForeignMarkupWithBadTransformer.
@Test
public void testForeignMarkupWithBadTransformer() throws UnsupportedQueryException, IOException, CatalogTransformerException, InvalidSyntaxException {
InputTransformer inputTransformer = mock(InputTransformer.class);
when(inputTransformer.transform(isA(InputStream.class))).thenThrow(new CatalogTransformerException());
when(inputTransformer.transform(isA(InputStream.class), isA(String.class))).thenThrow(new CatalogTransformerException());
source.setBundle(getMockBundleContext(inputTransformer));
testForeignMarkupExample();
}
use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class OpenSearchSourceTest method testQueryBySearchPhraseContentTypeSet.
@Test
public void testQueryBySearchPhraseContentTypeSet() throws UnsupportedQueryException, IOException, CatalogTransformerException, InvalidSyntaxException {
when(response.getEntity()).thenReturn(getSampleAtomStream());
InputTransformer inputTransformer = mock(InputTransformer.class);
MetacardImpl generatedMetacard = new MetacardImpl();
generatedMetacard.setMetadata(getSample());
generatedMetacard.setId(SAMPLE_ID);
generatedMetacard.setContentTypeName("myType");
when(inputTransformer.transform(isA(InputStream.class))).thenReturn(generatedMetacard);
when(inputTransformer.transform(isA(InputStream.class), isA(String.class))).thenReturn(generatedMetacard);
source.setBundle(getMockBundleContext(inputTransformer));
Filter filter = FILTER_BUILDER.attribute(NOT_ID_ATTRIBUTE_NAME).like().text(SAMPLE_SEARCH_PHRASE);
SourceResponse response = source.query(getQueryRequest(filter));
assertThat(response.getHits(), is(1L));
List<Result> results = response.getResults();
assertThat(results.size(), is(1));
Result result = results.get(0);
Metacard metacard = result.getMetacard();
assertThat(metacard, notNullValue());
assertThat(metacard.getContentTypeName(), is("myType"));
}
Aggregations