use of javax.ws.rs.core.MultivaluedHashMap in project ddf by codice.
the class ResourceReaderTest method verifyFileFromURLResourceReader.
private void verifyFileFromURLResourceReader(URI uri, String filename, String expectedMimeType) throws URISyntaxException, IOException, ResourceNotFoundException {
Response mockResponse = mock(Response.class);
when(mockWebClient.get()).thenReturn(mockResponse);
MultivaluedMap<String, Object> map = new MultivaluedHashMap<>();
map.put(HttpHeaders.CONTENT_DISPOSITION, Arrays.<Object>asList("inline; filename=\"" + filename + "\""));
when(mockResponse.getHeaders()).thenReturn(map);
when(mockResponse.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
when(mockResponse.getEntity()).thenReturn(getBinaryData());
verifyFileFromURLResourceReader(uri, filename, expectedMimeType, null);
}
use of javax.ws.rs.core.MultivaluedHashMap in project ddf by codice.
the class ResourceReaderTest method testServerSupportsPartialContentResponseTooMuchOffset.
/**
* Tests that a Partial Content response that has a higher byte offset as what was requested
* throws an IOException in order to prevent data loss.
* @throws Exception
*/
@Test(expected = ResourceNotFoundException.class)
public void testServerSupportsPartialContentResponseTooMuchOffset() throws Exception {
URI uri = new URI(HTTP_SCHEME_PLUS_SEP + HOST + TEST_PATH + BAD_FILE_NAME);
Response mockResponse = mock(Response.class);
when(mockWebClient.get()).thenReturn(mockResponse);
MultivaluedMap<String, Object> map = new MultivaluedHashMap<>();
map.put(HttpHeaders.CONTENT_DISPOSITION, Arrays.asList("inline; filename=\"" + JPEG_FILE_NAME_1 + "\""));
map.put(HttpHeaders.CONTENT_RANGE, Arrays.asList("Bytes 3-4/5"));
when(mockResponse.getHeaders()).thenReturn(map);
when(mockResponse.getStatus()).thenReturn(Response.Status.PARTIAL_CONTENT.getStatusCode());
when(mockResponse.getEntity()).thenReturn(getBinaryDataWithOffset(3));
String bytesToSkip = "2";
// this should throw an IOException since more bytes were skipped than requested
verifyFileFromURLResourceReader(uri, JPEG_FILE_NAME_1, JPEG_MIME_TYPE, bytesToSkip);
}
use of javax.ws.rs.core.MultivaluedHashMap in project ddf by codice.
the class ResourceReaderTest method testServerSupportsPartialContentResponseWithNotEnoughOffset.
/**
* Tests that a Partial Content response that has a smaller byte offset than what was requested
* still returns an input stream starting at the requested byte offset by skipping ahead in the
* input stream.
* @throws Exception
*/
@Test
public void testServerSupportsPartialContentResponseWithNotEnoughOffset() throws Exception {
URI uri = new URI(HTTP_SCHEME_PLUS_SEP + HOST + TEST_PATH + BAD_FILE_NAME);
Response mockResponse = mock(Response.class);
when(mockWebClient.get()).thenReturn(mockResponse);
MultivaluedMap<String, Object> map = new MultivaluedHashMap<>();
map.put(HttpHeaders.CONTENT_DISPOSITION, Arrays.asList("inline; filename=\"" + JPEG_FILE_NAME_1 + "\""));
map.put(HttpHeaders.CONTENT_RANGE, Arrays.asList("Bytes 1-4/5"));
when(mockResponse.getHeaders()).thenReturn(map);
when(mockResponse.getStatus()).thenReturn(Response.Status.PARTIAL_CONTENT.getStatusCode());
when(mockResponse.getEntity()).thenReturn(getBinaryDataWithOffset(1));
String bytesToSkip = "2";
ResourceResponse response = verifyFileFromURLResourceReader(uri, JPEG_FILE_NAME_1, JPEG_MIME_TYPE, bytesToSkip);
// verify that the requested bytes 3-5 were returned
assertEquals(3, response.getResource().getByteArray().length);
}
use of javax.ws.rs.core.MultivaluedHashMap in project ddf by codice.
the class ResourceReaderTest method testServerSupportsPartialContentResponseWithCorrectOffset.
/**
* Tests that a Partial Content response that has the same byte offset as what was requested
* returns an input stream starting at the requested byte offset.
* @throws Exception
*/
@Test
public void testServerSupportsPartialContentResponseWithCorrectOffset() throws Exception {
URI uri = new URI(HTTP_SCHEME_PLUS_SEP + HOST + TEST_PATH + BAD_FILE_NAME);
Response mockResponse = mock(Response.class);
when(mockWebClient.get()).thenReturn(mockResponse);
MultivaluedMap<String, Object> map = new MultivaluedHashMap<>();
map.put(HttpHeaders.CONTENT_DISPOSITION, Arrays.asList("inline; filename=\"" + JPEG_FILE_NAME_1 + "\""));
map.put(HttpHeaders.CONTENT_RANGE, Arrays.asList("Bytes 2-4/5"));
when(mockResponse.getHeaders()).thenReturn(map);
when(mockResponse.getStatus()).thenReturn(Response.Status.PARTIAL_CONTENT.getStatusCode());
when(mockResponse.getEntity()).thenReturn(getBinaryDataWithOffset(2));
String bytesToSkip = "2";
ResourceResponse response = verifyFileFromURLResourceReader(uri, JPEG_FILE_NAME_1, JPEG_MIME_TYPE, bytesToSkip);
// verify that the requested bytes 3-5 were returned
assertEquals(3, response.getResource().getByteArray().length);
}
use of javax.ws.rs.core.MultivaluedHashMap 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())));
}
Aggregations