use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.
the class AttachmentParserImplTest method testWithoutFilenameAndUnrecognizedContentType.
@Test
public void testWithoutFilenameAndUnrecognizedContentType() throws IOException {
String unrecognizedContentType = "foo/bar";
try (InputStream inputStream = createTestInputStream()) {
AttachmentInfo attachmentInfo = attachmentParser.generateAttachmentInfo(inputStream, unrecognizedContentType, null);
assertThat(attachmentInfo.getFilename(), is("file.bin"));
assertThat(attachmentInfo.getContentType(), is(unrecognizedContentType));
assertThat(attachmentInfo.getStream(), is(inputStream));
}
}
use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.
the class AttachmentParserImplTest method testWithoutFilename.
@Test
public void testWithoutFilename() throws IOException {
try (InputStream inputStream = createTestInputStream()) {
AttachmentInfo attachmentInfo = attachmentParser.generateAttachmentInfo(inputStream, TEXT_PLAIN, null);
assertThat(attachmentInfo.getFilename(), is("file." + TEXT_EXT));
assertThat(attachmentInfo.getContentType(), is(TEXT_PLAIN));
assertThat(attachmentInfo.getStream(), is(inputStream));
}
}
use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.
the class CatalogServiceImplTest method testParseAttachmentsTooLarge.
@Test
@SuppressWarnings({ "unchecked" })
public void testParseAttachmentsTooLarge() 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(Strings.repeat("hi", 100_000).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");
// Ensure that the metadata was not overriden because it was too large to be parsed
assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("Some Text Again"));
assertThat(attachmentInfoAndMetacard.getValue().getAttribute("foo"), equalTo(null));
}
use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.
the class CatalogServiceImplTest method testParsePartsTooLarge.
@Test
@SuppressWarnings({ "unchecked" })
public void testParsePartsTooLarge() throws IOException, CatalogTransformerException, SourceUnavailableException, IngestException, InvalidSyntaxException, ServletException {
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<Part> parts = new ArrayList<>();
Part part = createPart("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), "form-data; name=parse.resource; filename=C:\\DDF\\metacard.txt");
Part part1 = createPart("parse.metadata", new ByteArrayInputStream("Some Text Again".getBytes()), "form-data; name=parse.metadata; filename=C:\\DDF\\metacard.xml");
parts.add(part);
parts.add(part1);
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
when(httpServletRequest.getParts()).thenReturn(parts);
Map.Entry<AttachmentInfo, Metacard> attachmentInfoAndMetacard = catalogServiceImpl.parseParts(parts, "xml");
assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("Some Text Again"));
Part part2 = createPart("metadata", new ByteArrayInputStream(Strings.repeat("hi", 100_000).getBytes()), "form-data; name=metadata; filename=C:\\DDF\\metacard.xml");
Part part3 = createPart("foo", new ByteArrayInputStream("bar".getBytes()), "form-data; name=foo; filename=C:\\DDF\\metacard.xml");
parts.add(part2);
parts.add(part3);
attachmentInfoAndMetacard = catalogServiceImpl.parseParts(parts, "xml");
// Ensure that the metadata was not overriden because it was too large to be parsed
assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("Some Text Again"));
assertThat(attachmentInfoAndMetacard.getValue().getAttribute("foo"), equalTo(null));
}
use of org.codice.ddf.attachment.AttachmentInfo in project ddf by codice.
the class CatalogServiceImplTest method testParseParts.
@Test
@SuppressWarnings({ "unchecked" })
public void testParseParts() throws IOException, CatalogTransformerException, SourceUnavailableException, IngestException, InvalidSyntaxException, ServletException {
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<Part> parts = new ArrayList<>();
Part part = createPart("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), "form-data; name=parse.resource; filename=C:\\DDF\\metacard.txt");
Part part1 = createPart("parse.metadata", new ByteArrayInputStream("Some Text Again".getBytes()), "form-data; name=parse.metadata; filename=C:\\DDF\\metacard.xml");
parts.add(part);
parts.add(part1);
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
when(httpServletRequest.getParts()).thenReturn(parts);
Map.Entry<AttachmentInfo, Metacard> attachmentInfoAndMetacard = catalogServiceImpl.parseParts(parts, "xml");
assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("Some Text Again"));
Part part2 = createPart("metadata", new ByteArrayInputStream("<meta>beta</meta>".getBytes()), "form-data; name=metadata; filename=C:\\DDF\\metacard.xml");
Part part3 = createPart("foo", new ByteArrayInputStream("bar".getBytes()), "form-data; name=foo; filename=C:\\DDF\\metacard.xml");
parts.add(part2);
parts.add(part3);
attachmentInfoAndMetacard = catalogServiceImpl.parseParts(parts, "xml");
assertThat(attachmentInfoAndMetacard.getValue().getMetadata(), equalTo("<meta>beta</meta>"));
assertThat(attachmentInfoAndMetacard.getValue().getAttribute("foo"), equalTo(null));
}
Aggregations