Java Code Examples for com.google.api.client.json.GenericJson#toString()

The following examples show how to use com.google.api.client.json.GenericJson#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GcsUtilTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetSizeBytesWhenFileNotFoundBatch() throws Exception {
  JsonFactory jsonFactory = new JacksonFactory();

  String contentBoundary = "batch_foobarbaz";
  String contentBoundaryLine = "--" + contentBoundary;
  String endOfContentBoundaryLine = "--" + contentBoundary + "--";

  GenericJson error = new GenericJson().set("error", new GenericJson().set("code", 404));
  error.setFactory(jsonFactory);

  String content =
      contentBoundaryLine
          + "\n"
          + "Content-Type: application/http\n"
          + "\n"
          + "HTTP/1.1 404 Not Found\n"
          + "Content-Length: -1\n"
          + "\n"
          + error.toString()
          + "\n"
          + "\n"
          + endOfContentBoundaryLine
          + "\n";
  thrown.expect(FileNotFoundException.class);
  MockLowLevelHttpResponse notFoundResponse =
      new MockLowLevelHttpResponse()
          .setContentType("multipart/mixed; boundary=" + contentBoundary)
          .setContent(content)
          .setStatusCode(HttpStatusCodes.STATUS_CODE_OK);

  MockHttpTransport mockTransport =
      new MockHttpTransport.Builder().setLowLevelHttpResponse(notFoundResponse).build();

  GcsUtil gcsUtil = gcsOptionsWithTestCredential().getGcsUtil();

  gcsUtil.setStorageClient(new Storage(mockTransport, Transport.getJsonFactory(), null));
  gcsUtil.fileSizes(ImmutableList.of(GcsPath.fromComponents("testbucket", "testobject")));
}
 
Example 2
Source File: GcsUtilTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetSizeBytesWhenFileNotFoundBatchRetry() throws Exception {
  JsonFactory jsonFactory = new JacksonFactory();

  String contentBoundary = "batch_foobarbaz";
  String contentBoundaryLine = "--" + contentBoundary;
  String endOfContentBoundaryLine = "--" + contentBoundary + "--";

  GenericJson error = new GenericJson().set("error", new GenericJson().set("code", 404));
  error.setFactory(jsonFactory);

  String content =
      contentBoundaryLine
          + "\n"
          + "Content-Type: application/http\n"
          + "\n"
          + "HTTP/1.1 404 Not Found\n"
          + "Content-Length: -1\n"
          + "\n"
          + error.toString()
          + "\n"
          + "\n"
          + endOfContentBoundaryLine
          + "\n";
  thrown.expect(FileNotFoundException.class);

  final LowLevelHttpResponse mockResponse = Mockito.mock(LowLevelHttpResponse.class);
  when(mockResponse.getContentType()).thenReturn("multipart/mixed; boundary=" + contentBoundary);

  // 429: Too many requests, then 200: OK.
  when(mockResponse.getStatusCode()).thenReturn(429, 200);
  when(mockResponse.getContent()).thenReturn(toStream("error"), toStream(content));

  // A mock transport that lets us mock the API responses.
  MockHttpTransport mockTransport =
      new MockHttpTransport.Builder()
          .setLowLevelHttpRequest(
              new MockLowLevelHttpRequest() {
                @Override
                public LowLevelHttpResponse execute() throws IOException {
                  return mockResponse;
                }
              })
          .build();

  GcsUtil gcsUtil = gcsOptionsWithTestCredential().getGcsUtil();

  gcsUtil.setStorageClient(
      new Storage(mockTransport, Transport.getJsonFactory(), new RetryHttpRequestInitializer()));
  gcsUtil.fileSizes(ImmutableList.of(GcsPath.fromComponents("testbucket", "testobject")));
}
 
Example 3
Source File: GcsUtilTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void testRemoveWhenFileNotFound() throws Exception {
  JsonFactory jsonFactory = new JacksonFactory();

  String contentBoundary = "batch_foobarbaz";
  String contentBoundaryLine = "--" + contentBoundary;
  String endOfContentBoundaryLine = "--" + contentBoundary + "--";

  GenericJson error = new GenericJson().set("error", new GenericJson().set("code", 404));
  error.setFactory(jsonFactory);

  String content =
      contentBoundaryLine
          + "\n"
          + "Content-Type: application/http\n"
          + "\n"
          + "HTTP/1.1 404 Not Found\n"
          + "Content-Length: -1\n"
          + "\n"
          + error.toString()
          + "\n"
          + "\n"
          + endOfContentBoundaryLine
          + "\n";

  final LowLevelHttpResponse mockResponse = Mockito.mock(LowLevelHttpResponse.class);
  when(mockResponse.getContentType()).thenReturn("multipart/mixed; boundary=" + contentBoundary);
  when(mockResponse.getStatusCode()).thenReturn(200);
  when(mockResponse.getContent()).thenReturn(toStream(content));

  // A mock transport that lets us mock the API responses.
  MockLowLevelHttpRequest request =
      new MockLowLevelHttpRequest() {
        @Override
        public LowLevelHttpResponse execute() throws IOException {
          return mockResponse;
        }
      };
  MockHttpTransport mockTransport =
      new MockHttpTransport.Builder().setLowLevelHttpRequest(request).build();

  GcsUtil gcsUtil = gcsOptionsWithTestCredential().getGcsUtil();
  gcsUtil.setStorageClient(
      new Storage(mockTransport, Transport.getJsonFactory(), new RetryHttpRequestInitializer()));
  gcsUtil.remove(Arrays.asList("gs://some-bucket/already-deleted"));
}