Exemplo n.º 1
0
  @Test(expected = ContentEndpointException.class)
  public void readContentEndpointExceptionExceptionExpected()
      throws ContentEndpointException, ContentFrameworkException, MimeTypeResolutionException {
    ContentFramework framework = mock(ContentFramework.class);
    when(framework.read(isA(ReadRequest.class)))
        .thenThrow(
            new ContentEndpointException("Content Item not found.", Response.Status.NOT_FOUND));

    ContentEndpoint endpoint = new ContentEndpoint(framework, getMockMimeTypeMapper());
    endpoint.read(anyString());
  }
  protected void executeReadTest(String content, String fileName, Long size, String mimeType)
      throws IOException, MimeTypeParseException, ContentFrameworkException,
          MimeTypeResolutionException {

    ContentFramework framework = mock(ContentFramework.class);
    ReadResponse readResponse = mock(ReadResponse.class);

    ContentItem contentItem =
        getMockGoodContentItem(content, fileName, "contentIdValue", size, mimeType);
    when(readResponse.getContentItem()).thenReturn(contentItem);

    when(framework.read(isA(ReadRequest.class))).thenReturn(readResponse);

    ContentEndpoint endpoint = new ContentEndpoint(framework, getMockMimeTypeMapper());
    Response response = endpoint.read(anyString());

    // Assertions for all valid headers returned
    assertThat(response.getStatus(), equalTo(200));
    assertThat(IOUtils.toString((InputStream) response.getEntity()), equalTo(content));

    if (fileName != null) {
      assertThat(
          (String) response.getMetadata().getFirst(CONTENT_DISPOSITION),
          equalToIgnoringWhiteSpace("inline; filename=" + fileName));
    } else {
      assertThat(response.getMetadata().getFirst(CONTENT_DISPOSITION), is(nullValue()));
    }

    if (mimeType != null) {
      assertThat(
          (String) response.getMetadata().getFirst(HttpHeaders.CONTENT_TYPE), equalTo(mimeType));
    } else {
      assertThat(
          (String) response.getMetadata().getFirst(HttpHeaders.CONTENT_TYPE),
          equalTo(MediaType.APPLICATION_OCTET_STREAM));
    }

    if (size != null) {
      assertThat(
          (String) response.getMetadata().getFirst(HttpHeaders.CONTENT_LENGTH),
          equalTo(String.valueOf(size)));
    } else {
      assertThat(response.getMetadata().getFirst(HttpHeaders.CONTENT_LENGTH), is(nullValue()));
    }
  }