use of com.google.api.client.http.LowLevelHttpResponse in project beam by apache.
the class BigQueryServicesImplTest method verifyAllResponsesAreRead.
/**
* Verifies the test interacted the mock objects in {@link #responses}.
*
* <p>The implementation of google-api-client or google-http-client may influence the number of
* interaction in future
*/
private void verifyAllResponsesAreRead() throws IOException {
Verify.verify(responses != null, "The test setup is incorrect. Responses are not setup");
for (LowLevelHttpResponse response : responses) {
// Google-http-client reads the field twice per response.
verify(response, atLeastOnce()).getStatusCode();
verify(response, times(1)).getContent();
verify(response, times(1)).getContentType();
}
}
use of com.google.api.client.http.LowLevelHttpResponse in project beam by apache.
the class BigQueryServicesImplTest method setUp.
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
// Set up the MockHttpRequest for future inspection.
request = new MockLowLevelHttpRequest() {
int index = 0;
@Override
public LowLevelHttpResponse execute() throws IOException {
Verify.verify(index < responses.length, "The number of HttpRequest invocation exceeded the number of prepared mock requests. Index: %d", index);
return responses[index++];
}
};
// A mock transport that lets us mock the API responses.
MockHttpTransport transport = new MockHttpTransport.Builder().setLowLevelHttpRequest(request).build();
// A sample BigQuery API client that uses default JsonFactory and RetryHttpInitializer.
bigquery = new Bigquery.Builder(transport, Transport.getJsonFactory(), new RetryHttpRequestInitializer()).build();
// Setup the ProcessWideContainer for testing metrics are set.
MetricsContainerImpl container = new MetricsContainerImpl(null);
MetricsEnvironment.setProcessWideContainer(container);
MetricsEnvironment.setCurrentContainer(container);
}
use of com.google.api.client.http.LowLevelHttpResponse in project beam by apache.
the class GcsUtilTest method testGetSizeBytesWhenFileNotFoundBatchRetry.
@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")));
}
use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.
the class DeleteServletTest method doPost_deleteGame.
@Test
public void doPost_deleteGame() throws Exception {
// Insert a game
Objectify ofy = ObjectifyService.ofy();
Game game = new Game(USER_ID, "my-opponent", " ", true);
ofy.save().entity(game).now();
String gameKey = game.getId();
when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
// Mock out the firebase response. See
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(200);
return response;
}
};
}
});
FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
Mockito.doReturn(null).when(servletUnderTest).getServletContext();
servletUnderTest.doPost(mockRequest, mockResponse);
verify(mockHttpTransport, times(1)).buildRequest(eq("DELETE"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
}
use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.
the class FirebaseChannelTest method sendFirebaseMessage_delete.
@Test
public void sendFirebaseMessage_delete() throws Exception {
// Mock out the firebase response. See
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(200);
return response;
}
};
}
});
FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
firebaseChannel.sendFirebaseMessage("my_key", null);
verify(mockHttpTransport, times(1)).buildRequest("DELETE", FIREBASE_DB_URL + "/channels/my_key.json");
}
Aggregations