Search in sources :

Example 36 with BinaryContent

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", ""));
}
Also used : MetacardMarshallerImpl(ddf.catalog.transformer.xml.MetacardMarshallerImpl) PrintWriterProvider(ddf.catalog.transformer.api.PrintWriterProvider) SourceResponse(ddf.catalog.operation.SourceResponse) MetacardMarshaller(ddf.catalog.transformer.api.MetacardMarshaller) PrintWriterProviderImpl(ddf.catalog.transformer.xml.PrintWriterProviderImpl) XmlResponseQueueTransformer(ddf.catalog.transformer.xml.XmlResponseQueueTransformer) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) Test(org.junit.Test)

Example 37 with BinaryContent

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TikaMimeTypeResolver(ddf.mime.tika.TikaMimeTypeResolver) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) Metacard(ddf.catalog.data.Metacard) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) FilterBuilder(ddf.catalog.filter.FilterBuilder) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) CatalogFramework(ddf.catalog.CatalogFramework) MediaType(javax.ws.rs.core.MediaType) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 38 with BinaryContent

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
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) PrintWriterProvider(ddf.catalog.transformer.api.PrintWriterProvider) SourceResponse(ddf.catalog.operation.SourceResponse) MetacardMarshaller(ddf.catalog.transformer.api.MetacardMarshaller) PrintWriterProviderImpl(ddf.catalog.transformer.xml.PrintWriterProviderImpl) XmlResponseQueueTransformer(ddf.catalog.transformer.xml.XmlResponseQueueTransformer) BinaryContent(ddf.catalog.data.BinaryContent) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 39 with BinaryContent

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);
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) Test(org.junit.Test)

Example 40 with BinaryContent

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);
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) BinaryContent(ddf.catalog.data.BinaryContent) Test(org.junit.Test)

Aggregations

BinaryContent (ddf.catalog.data.BinaryContent)214 Test (org.junit.Test)164 Metacard (ddf.catalog.data.Metacard)87 SourceResponse (ddf.catalog.operation.SourceResponse)78 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)51 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)44 HashMap (java.util.HashMap)42 Result (ddf.catalog.data.Result)41 Map (java.util.Map)39 Serializable (java.io.Serializable)33 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)27 ResultImpl (ddf.catalog.data.impl.ResultImpl)26 LineString (ddf.geo.formatter.LineString)21 MultiLineString (ddf.geo.formatter.MultiLineString)21 Date (java.util.Date)21 File (java.io.File)20 JSONObject (org.json.simple.JSONObject)20 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)19 FileOutputStream (java.io.FileOutputStream)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17