use of ddf.catalog.data.BinaryContent in project ddf by codice.
the class TestXmlResponseQueueTransformer method testCompareSerialToFork.
@Test
public void testCompareSerialToFork() throws IOException, CatalogTransformerException, MimeTypeParseException {
SourceResponse response = givenSourceResponse(new MetacardStub("source1", "id1"), new MetacardStub("source2", "id2"), new MetacardStub("source3", "id3"), new MetacardStub("source4", "id4"));
PrintWriterProvider pwp = new PrintWriterProviderImpl();
MetacardMarshaller mcm = new MetacardMarshallerImpl(parser, pwp);
XmlResponseQueueTransformer serialXform = new XmlResponseQueueTransformer(parser, FJP, pwp, mcm, getMimeType());
serialXform.setThreshold(2);
XmlResponseQueueTransformer forkXForm = new XmlResponseQueueTransformer(parser, FJP, pwp, mcm, getMimeType());
forkXForm.setThreshold(10);
BinaryContent serialBc = serialXform.transform(response, null);
BinaryContent forkBc = forkXForm.transform(response, null);
String serialOutput = new String(serialBc.getByteArray());
String forkOutput = new String(forkBc.getByteArray());
// There are expected whitespace differences between the outputs.
// This is an overly aggressive conversion; a better test would be to unmarshal the
// xml metacards back into Metacard instances and compare equality.
assertEquals(serialOutput.replaceAll("\\s", ""), forkOutput.replaceAll("\\s", ""));
}
use of ddf.catalog.data.BinaryContent in project ddf by codice.
the class TestRestEndpoint method testGetMetacardAsXml.
/**
* Tests that a geojson input has its InputTransformer invoked by the REST endpoint to create
* a metacard that is then converted to XML and returned from the REST endpoint.
*
* @throws Exception
*/
@Test
public void testGetMetacardAsXml() throws Exception {
String filename = "src/test/resources/ValidGeojson.json";
CatalogFramework framework = givenCatalogFramework(SAMPLE_ID);
String metacardXml = "<metacard ns2:id=\"assigned-when-ingested\">\r\n" + "<type>type.metacard</type>\r\n" + "<string name=\"title\">\r\n" + "<value>Title goes here ...</value>\r\n" + "</string>\r\n" + "<string name=\"metadata\">\r\n" + "<value>metadata goes here ...</value>\r\n" + "</metacard>";
// Mock XmlMetacardTransformer that CatalogFramework will call to convert generated
// metacard into XML to be returned from REST endpoint.
final BinaryContent content = mock(BinaryContent.class);
InputStream inputStream = new ByteArrayInputStream(metacardXml.getBytes(GET_OUTPUT_TYPE));
when(content.getInputStream()).thenReturn(inputStream);
when(content.getMimeTypeValue()).thenReturn("application/json;id=geojson");
when(framework.transform(isA(Metacard.class), anyString(), isNull(Map.class))).thenAnswer(new Answer<BinaryContent>() {
@Override
public BinaryContent answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
Metacard metacard = (Metacard) args[0];
return content;
}
});
RESTEndpoint restEndpoint = new RESTEndpoint(framework);
// Add a MimeTypeToINputTransformer that the REST endpoint will call to create the metacard
addMatchingService(restEndpoint, Arrays.asList(getSimpleTransformer()));
restEndpoint.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
restEndpoint.setFilterBuilder(filterBuilder);
String json = "{\r\n" + " \"properties\": {\r\n" + " \"title\": \"myTitle\",\r\n" + " \"thumbnail\": \"CA==\",\r\n" + " \"resource-uri\": \"http://example.com\",\r\n" + " \"created\": \"2012-09-01T00:09:19.368+0000\",\r\n" + " \"metadata-content-type-version\": \"myVersion\",\r\n" + " \"metadata-content-type\": \"myType\",\r\n" + " \"metadata\": \"<xml>metadata goes here ...</xml>\",\r\n" + " \"modified\": \"2012-09-01T00:09:19.368+0000\"\r\n" + " },\r\n" + " \"type\": \"Feature\",\r\n" + " \"geometry\": {\r\n" + " \"type\": \"Point\",\r\n" + " \"coordinates\": [\r\n" + " 30.0,\r\n" + " 10.0\r\n" + " ]\r\n" + " }\r\n" + "} ";
// Sample headers for a multipart body specifying a geojson file to have a metacard created for:
// Content-Disposition: form-data; name="file"; filename="C:\DDF\geojson_valid.json"
// Content-Type: application/json;id=geojson
InputStream is = IOUtils.toInputStream(json);
List<Attachment> attachments = new ArrayList<>();
ContentDisposition contentDisposition = new ContentDisposition("form-data; name=file; filename=C:\\DDF\\geojson_valid.json");
Attachment attachment = new Attachment("file_part", is, contentDisposition);
attachments.add(attachment);
MediaType mediaType = new MediaType(MediaType.APPLICATION_JSON, "id=geojson");
MultipartBody multipartBody = new MultipartBody(attachments, mediaType, true);
UriInfo uriInfo = createSpecificUriInfo(LOCAL_RETRIEVE_ADDRESS);
Response response = restEndpoint.createMetacard(multipartBody, uriInfo, RESTEndpoint.DEFAULT_METACARD_TRANSFORMER);
assertEquals(OK, response.getStatus());
InputStream responseEntity = (InputStream) response.getEntity();
String responseXml = IOUtils.toString(responseEntity);
assertEquals(metacardXml, responseXml);
}
use of ddf.catalog.data.BinaryContent in project ddf by codice.
the class TestXmlResponseQueueTransformer method testMimeTypeInitException.
@Test(expected = ExceptionInInitializerError.class)
public void testMimeTypeInitException() throws IOException, CatalogTransformerException, XmlPullParserException, MimeTypeParseException {
SourceResponse response = givenSourceResponse(new MetacardStub("source1", "id1"));
PrintWriterProvider pwp = new PrintWriterProviderImpl();
MetacardMarshaller mockMetacardMarshaller = mock(MetacardMarshaller.class);
MimeType mockMimeType = mock(MimeType.class);
doThrow(new MimeTypeParseException("")).when(mockMimeType).setSubType(anyString());
XmlResponseQueueTransformer xrqt = new XmlResponseQueueTransformer(parser, FJP, pwp, mockMetacardMarshaller, mockMimeType);
xrqt.setThreshold(2);
BinaryContent bc = xrqt.transform(response, null);
// then exception
}
use of ddf.catalog.data.BinaryContent in project ddf by codice.
the class TestXmlResponseQueueTransformer method testMetacardTypeNameEmpty.
/**
* No {@link MetacardType} name should use the default name.
*
* @throws CatalogTransformerException
* @throws IOException
* @throws SAXException
* @throws XpathException
*/
@Test
public void testMetacardTypeNameEmpty() throws CatalogTransformerException, IOException, XpathException, SAXException {
// given
transformer.setThreshold(2);
SourceResponse response = givenMetacardTypeName("");
// when
BinaryContent binaryContent = transformer.transform(response, null);
// then
assertThat(binaryContent.getMimeType(), is(mimeType));
byte[] bytes = binaryContent.getByteArray();
String output = new String(bytes);
print(output, verboseDebug);
assertXpathEvaluatesTo(DEFAULT_TYPE_NAME, "/mc:metacards/mc:metacard/mc:type", output);
}
use of ddf.catalog.data.BinaryContent in project ddf by codice.
the class TestZipCompression method testCompressionWithDerivedContent.
@Test
public void testCompressionWithDerivedContent() throws Exception {
SourceResponse sourceResponse = createSourceResponseWithURISchemes(CONTENT_SCHEME, "content:id3#preview");
BinaryContent binaryContent = zipCompression.transform(sourceResponse, filePathArgument);
assertThat(binaryContent, notNullValue());
assertZipContents(binaryContent, METACARD_RESULT_LIST_WITH_CONTENT_AND_DERIVED_RESOURCES);
}
Aggregations