Search in sources :

Example 51 with MultivaluedHashMap

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);
}
Also used : Response(javax.ws.rs.core.Response) ResourceResponse(ddf.catalog.operation.ResourceResponse) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 52 with MultivaluedHashMap

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);
}
Also used : Response(javax.ws.rs.core.Response) ResourceResponse(ddf.catalog.operation.ResourceResponse) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) Test(org.junit.Test)

Example 53 with MultivaluedHashMap

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);
}
Also used : Response(javax.ws.rs.core.Response) ResourceResponse(ddf.catalog.operation.ResourceResponse) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceResponse(ddf.catalog.operation.ResourceResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) Test(org.junit.Test)

Example 54 with MultivaluedHashMap

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);
}
Also used : Response(javax.ws.rs.core.Response) ResourceResponse(ddf.catalog.operation.ResourceResponse) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ResourceResponse(ddf.catalog.operation.ResourceResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) Test(org.junit.Test)

Example 55 with MultivaluedHashMap

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())));
}
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)

Aggregations

MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)133 Response (javax.ws.rs.core.Response)98 Builder (javax.ws.rs.client.Invocation.Builder)78 JSONException (org.codehaus.jettison.json.JSONException)77 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)77 Test (org.testng.annotations.Test)75 JSONObject (org.codehaus.jettison.json.JSONObject)73 Parameters (org.testng.annotations.Parameters)73 BaseTest (org.xdi.oxauth.BaseTest)73 TokenRequest (org.xdi.oxauth.client.TokenRequest)39 Test (org.junit.Test)28 URISyntaxException (java.net.URISyntaxException)27 OxAuthCryptoProvider (org.xdi.oxauth.model.crypto.OxAuthCryptoProvider)21 RegisterResponse (org.xdi.oxauth.client.RegisterResponse)18 ByteArrayInputStream (java.io.ByteArrayInputStream)12 UserInfoRequest (org.xdi.oxauth.client.UserInfoRequest)12 URI (java.net.URI)11 ResourceResponse (ddf.catalog.operation.ResourceResponse)10 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)10 Matchers.containsString (org.hamcrest.Matchers.containsString)9