Search in sources :

Example 16 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class ReliableResourceDownloadManagerTest method getMockResourceResponse.

private ResourceResponse getMockResourceResponse() throws Exception {
    resourceRequest = mock(ResourceRequest.class);
    Map<String, Serializable> requestProperties = new HashMap<String, Serializable>();
    when(resourceRequest.getPropertyNames()).thenReturn(requestProperties.keySet());
    resource = mock(Resource.class);
    when(resource.getInputStream()).thenReturn(mis);
    when(resource.getName()).thenReturn("test-resource");
    when(resource.getMimeType()).thenReturn(new MimeType("text/plain"));
    resourceResponse = mock(ResourceResponse.class);
    when(resourceResponse.getRequest()).thenReturn(resourceRequest);
    when(resourceResponse.getResource()).thenReturn(resource);
    Map<String, Serializable> responseProperties = new HashMap<String, Serializable>();
    when(resourceResponse.getProperties()).thenReturn(responseProperties);
    return resourceResponse;
}
Also used : Serializable(java.io.Serializable) ResourceResponse(ddf.catalog.operation.ResourceResponse) HashMap(java.util.HashMap) ReliableResource(ddf.catalog.resource.data.ReliableResource) Resource(ddf.catalog.resource.Resource) ResourceRequest(ddf.catalog.operation.ResourceRequest) MimeType(javax.activation.MimeType)

Example 17 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class TestCswTransformProvider method testMarshalOtherSchema.

@Test
public void testMarshalOtherSchema() throws Exception {
    when(mockMetacardManager.getTransformerByProperty(TransformerManager.SCHEMA, OTHER_SCHEMA)).thenReturn(mockMetacardTransformer);
    when(mockMetacardTransformer.transform(any(Metacard.class), any(Map.class))).thenReturn(new BinaryContentImpl(IOUtils.toInputStream(getRecord()), new MimeType(MediaType.APPLICATION_XML)));
    StringWriter stringWriter = new StringWriter();
    HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
    CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    context.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.SCHEMA);
    context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, OTHER_SCHEMA);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.marshal(getMetacard(), writer, context);
    // Verify the context arguments were set correctly
    verify(mockMetacardManager, times(1)).getTransformerByProperty(captor.capture(), captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(OTHER_SCHEMA));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) HashMap(java.util.HashMap) Map(java.util.Map) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 18 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class CswRecordCollectionMessageBodyWriterTest method testWriteToProductData.

@Test
public void testWriteToProductData() throws MimeTypeParseException, IOException {
    CswRecordCollectionMessageBodyWriter writer = new CswRecordCollectionMessageBodyWriter(mockManager);
    byte[] data = "SampleData".getBytes();
    ByteArrayInputStream productData = new ByteArrayInputStream(data);
    MimeType mimeType = new MimeType("text", "plain");
    MultivaluedMap<String, Object> httpHeaders = new MultivaluedHashMap<>();
    Resource resource = new ResourceImpl(productData, mimeType, "ResourceName");
    CswRecordCollection collection = new CswRecordCollection();
    collection.setMimeType(mimeType.toString());
    collection.setResource(resource);
    collection.setOutputSchema("http://www.iana.org/assignments/media-types/application/octet-stream");
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    writer.writeTo(collection, null, null, null, null, httpHeaders, stream);
    assertThat(stream.toByteArray(), is(equalTo(resource.getByteArray())));
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) Resource(ddf.catalog.resource.Resource) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) Matchers.anyString(org.mockito.Matchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 19 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class TestCswQueryResponseTransformer method verifyResultOrderIsMaintained.

@Test
public void verifyResultOrderIsMaintained() throws CatalogTransformerException, IOException {
    // when
    when(mockPrintWriterProvider.build((Class<Metacard>) notNull())).thenReturn(mockPrintWriter);
    when(mockPrintWriter.makeString()).thenReturn(new String());
    when(mockSourceResponse.getResults()).thenReturn(createResults(1, 10));
    when(mockSourceResponse.getRequest()).thenReturn(mockQueryRequest);
    when(mockQueryRequest.getQuery()).thenReturn(mockQuery);
    when(mockArguments.get(CswConstants.RESULT_TYPE_PARAMETER)).thenReturn(ResultType.RESULTS);
    when(mockTransformerManager.getTransformerBySchema(anyString())).thenReturn(mockMetacardTransformer);
    when(mockMetacardTransformer.transform(any(Metacard.class), any(Map.class))).thenAnswer(invocationOnMock -> {
        Metacard metacard = (Metacard) invocationOnMock.getArguments()[0];
        BinaryContentImpl bci = new BinaryContentImpl(IOUtils.toInputStream(metacard.getId() + ","), new MimeType("application/xml"));
        return bci;
    });
    // given
    transformer.init();
    transformer.transform(mockSourceResponse, mockArguments);
    transformer.destroy();
    // then
    ArgumentCaptor<String> tmCaptor = ArgumentCaptor.forClass(String.class);
    verify(mockTransformerManager, times(1)).getTransformerBySchema(tmCaptor.capture());
    ArgumentCaptor<Map> mapCaptor = ArgumentCaptor.forClass(Map.class);
    ArgumentCaptor<Metacard> mcCaptor = ArgumentCaptor.forClass(Metacard.class);
    verify(mockMetacardTransformer, times(10)).transform(mcCaptor.capture(), mapCaptor.capture());
    ArgumentCaptor<String> strCaptor = ArgumentCaptor.forClass(String.class);
    verify(mockPrintWriter, times(2)).setRawValue(strCaptor.capture());
    String order = strCaptor.getAllValues().get(1);
    String[] ids = order.split(",");
    for (int i = 1; i < ids.length; i++) {
        assertThat(ids[i - 1], is(String.valueOf("id_" + i)));
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 20 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class TestResourceMetacardTransformer method testGetResourceJpeg.

@Test
public void testGetResourceJpeg() throws Exception {
    String filePath = ABSOLUTE_PATH + TEST_PATH + JPEG_FILE_NAME_1;
    URI uri = getUri(filePath);
    Metacard metacard = getMockMetacard(uri);
    boolean expectSuccess = true;
    MimeType mimeType = getMimeType(JPEG_MIME_TYPE);
    CatalogFramework framework = getFramework(getResourceResponse(getResource(mimeType, uri)));
    testGetResource(metacard, filePath, mimeType, framework, expectSuccess);
}
Also used : Metacard(ddf.catalog.data.Metacard) CatalogFramework(ddf.catalog.CatalogFramework) URI(java.net.URI) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Aggregations

MimeType (javax.activation.MimeType)67 Test (org.junit.Test)38 Metacard (ddf.catalog.data.Metacard)21 URI (java.net.URI)14 HashMap (java.util.HashMap)14 MimeTypeParseException (javax.activation.MimeTypeParseException)14 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)13 BundleContext (org.osgi.framework.BundleContext)13 ServiceReference (org.osgi.framework.ServiceReference)13 CatalogFramework (ddf.catalog.CatalogFramework)10 ResourceResponse (ddf.catalog.operation.ResourceResponse)10 Serializable (java.io.Serializable)9 Resource (ddf.catalog.resource.Resource)8 IOException (java.io.IOException)8 File (java.io.File)7 Matchers.anyString (org.mockito.Matchers.anyString)7 MetacardCreationException (ddf.catalog.data.MetacardCreationException)6 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)6 Response (javax.ws.rs.core.Response)6 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)5