throw exception; GoogleJsonError error = new GoogleJsonError(); error.setCode(code); error.setMessage(message);
@Test public void testUpdateAllIterable() { RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture<RpcBatch.Callback<StorageObject>> callback1 = Capture.newInstance(); Capture<RpcBatch.Callback<StorageObject>> callback2 = Capture.newInstance(); batchMock.addPatch( EasyMock.eq(BLOB_INFO1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); batchMock.addPatch( EasyMock.eq(BLOB_INFO2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); EasyMock.replay(storageRpcMock, batchMock); initializeService(); List<Blob> resultBlobs = storage.update(ImmutableList.of(BLOB_INFO1, BLOB_INFO2)); callback1.getValue().onSuccess(BLOB_INFO1.toPb()); callback2.getValue().onFailure(new GoogleJsonError()); assertEquals(2, resultBlobs.size()); assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO1)), resultBlobs.get(0)); assertNull(resultBlobs.get(1)); EasyMock.verify(batchMock); }
@Test public void testDeleteAllArray() { BlobId blobId1 = BlobId.of(BUCKET_NAME1, BLOB_NAME1); BlobId blobId2 = BlobId.of(BUCKET_NAME1, BLOB_NAME2); RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture<RpcBatch.Callback<Void>> callback1 = Capture.newInstance(); Capture<RpcBatch.Callback<Void>> callback2 = Capture.newInstance(); batchMock.addDelete( EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); batchMock.addDelete( EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); EasyMock.replay(storageRpcMock, batchMock); initializeService(); List<Boolean> result = storage.delete(blobId1, blobId2); callback1.getValue().onSuccess(null); callback2.getValue().onFailure(new GoogleJsonError()); assertEquals(2, result.size()); assertTrue(result.get(0)); assertFalse(result.get(1)); EasyMock.verify(batchMock); }
@Test public void testDeleteAllIterable() { BlobId blobId1 = BlobId.of(BUCKET_NAME1, BLOB_NAME1); BlobId blobId2 = BlobId.of(BUCKET_NAME1, BLOB_NAME2); RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture<RpcBatch.Callback<Void>> callback1 = Capture.newInstance(); Capture<RpcBatch.Callback<Void>> callback2 = Capture.newInstance(); batchMock.addDelete( EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); batchMock.addDelete( EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); EasyMock.replay(storageRpcMock, batchMock); initializeService(); List<Boolean> result = storage.delete(blobId1, blobId2); callback1.getValue().onSuccess(null); callback2.getValue().onFailure(new GoogleJsonError()); assertEquals(2, result.size()); assertTrue(result.get(0)); assertFalse(result.get(1)); EasyMock.verify(batchMock); }
GoogleJsonError error = new GoogleJsonError(); error.setCode(response.getStatusCode()); error.setMessage(response.getStatusMessage());
@Test public void testUpdateAllArray() { RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture<RpcBatch.Callback<StorageObject>> callback1 = Capture.newInstance(); Capture<RpcBatch.Callback<StorageObject>> callback2 = Capture.newInstance(); batchMock.addPatch( EasyMock.eq(BLOB_INFO1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); batchMock.addPatch( EasyMock.eq(BLOB_INFO2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); EasyMock.replay(storageRpcMock, batchMock); initializeService(); List<Blob> resultBlobs = storage.update(BLOB_INFO1, BLOB_INFO2); callback1.getValue().onSuccess(BLOB_INFO1.toPb()); callback2.getValue().onFailure(new GoogleJsonError()); assertEquals(2, resultBlobs.size()); assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO1)), resultBlobs.get(0)); assertNull(resultBlobs.get(1)); EasyMock.verify(batchMock); }
@Test public void testGetAllArrayIterable() { BlobId blobId1 = BlobId.of(BUCKET_NAME1, BLOB_NAME1); BlobId blobId2 = BlobId.of(BUCKET_NAME1, BLOB_NAME2); RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture<RpcBatch.Callback<StorageObject>> callback1 = Capture.newInstance(); Capture<RpcBatch.Callback<StorageObject>> callback2 = Capture.newInstance(); batchMock.addGet( EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); batchMock.addGet( EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); EasyMock.replay(storageRpcMock, batchMock); initializeService(); List<Blob> resultBlobs = storage.get(ImmutableList.of(blobId1, blobId2)); callback1.getValue().onSuccess(BLOB_INFO1.toPb()); callback2.getValue().onFailure(new GoogleJsonError()); assertEquals(2, resultBlobs.size()); assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO1)), resultBlobs.get(0)); assertNull(resultBlobs.get(1)); EasyMock.verify(batchMock); }
@Test public void testGetAllArray() { BlobId blobId1 = BlobId.of(BUCKET_NAME1, BLOB_NAME1); BlobId blobId2 = BlobId.of(BUCKET_NAME1, BLOB_NAME2); RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture<RpcBatch.Callback<StorageObject>> callback1 = Capture.newInstance(); Capture<RpcBatch.Callback<StorageObject>> callback2 = Capture.newInstance(); batchMock.addGet( EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); batchMock.addGet( EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.<StorageRpc.Option, Object>of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); EasyMock.replay(storageRpcMock, batchMock); initializeService(); List<Blob> resultBlobs = storage.get(blobId1, blobId2); callback1.getValue().onSuccess(BLOB_INFO1.toPb()); callback2.getValue().onFailure(new GoogleJsonError()); assertEquals(2, resultBlobs.size()); assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO1)), resultBlobs.get(0)); assertNull(resultBlobs.get(1)); EasyMock.verify(batchMock); }
@Test public void testGetZoneNotFound() { EasyMock.reset(batchMock); Capture<RpcBatch.Callback<ManagedZone>> callback = Capture.newInstance(); Capture<Map<DnsRpc.Option, Object>> capturedOptions = Capture.newInstance(); batchMock.addGetZone( EasyMock.eq(ZONE_NAME), EasyMock.capture(callback), EasyMock.capture(capturedOptions)); EasyMock.replay(batchMock); DnsBatchResult<Zone> batchResult = dnsBatch.getZone(ZONE_NAME); assertEquals(0, capturedOptions.getValue().size()); GoogleJsonError error = new GoogleJsonError(); error.setCode(404); RpcBatch.Callback<ManagedZone> capturedCallback = callback.getValue(); capturedCallback.onFailure(error); assertNull(batchResult.get()); }
@Test public void testGetChangeRequestNotFound() { EasyMock.reset(batchMock); Capture<RpcBatch.Callback<Change>> callback = Capture.newInstance(); Capture<Map<DnsRpc.Option, Object>> capturedOptions = Capture.newInstance(); batchMock.addGetChangeRequest( EasyMock.eq(ZONE_NAME), EasyMock.eq(CHANGE_REQUEST_COMPLETE.getGeneratedId()), EasyMock.capture(callback), EasyMock.capture(capturedOptions)); EasyMock.replay(batchMock); DnsBatchResult<ChangeRequest> batchResult = dnsBatch.getChangeRequest(ZONE_NAME, CHANGE_REQUEST_COMPLETE.getGeneratedId()); assertEquals(0, capturedOptions.getValue().size()); RpcBatch.Callback<Change> capturedCallback = callback.getValue(); GoogleJsonError error = new GoogleJsonError(); GoogleJsonError.ErrorInfo errorInfo = new GoogleJsonError.ErrorInfo(); errorInfo.setReason("reason"); errorInfo.setLocation("entity.parameters.changeId"); error.setCode(404); error.setErrors(ImmutableList.of(errorInfo)); capturedCallback.onFailure(error); assertNull(batchResult.get()); }
GoogleJsonError error = new GoogleJsonError(); error.setCode(404); capturedCallback.onFailure(error);
GoogleJsonError error = new GoogleJsonError(); error.setCode(404); capturedCallback.onFailure(error);
assertEquals(exception, serviceException.getCause()); GoogleJsonError error = new GoogleJsonError(); error.setCode(CODE); error.setMessage(MESSAGE);
assertSame(cause, exception.getCause()); GoogleJsonError error = new GoogleJsonError(); error.setCode(503); error.setMessage("message");
assertSame(cause, exception.getCause()); GoogleJsonError error = new GoogleJsonError(); error.setCode(503); error.setMessage("message");
private GoogleJsonError getGoogleJsonError(Exception e) { if (e instanceof GoogleJsonResponseException) { return ((GoogleJsonResponseException) e).getDetails(); } boolean retryableException = (e instanceof SSLHandshakeException || e instanceof SocketTimeoutException); if (retryableException) { logger.log(Level.WARNING, "Retrying request failed with exception:", e); } // Using retryable 504 Gateway Timeout error code. int errorCode = retryableException ? 504 : 0; GoogleJsonError error = new GoogleJsonError(); error.setMessage(e.getMessage()); error.setCode(errorCode); return error; } }
/** * Validates itemNotFound(). */ @Test public void testItemNotFound() { // Check success cases. assertThat(errorExtractor.itemNotFound(notFound)).isTrue(); GoogleJsonError gje = new GoogleJsonError(); gje.setCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND); assertThat(errorExtractor.itemNotFound(gje)).isTrue(); assertThat(errorExtractor.itemNotFound(new IOException(notFound))).isTrue(); assertThat(errorExtractor.itemNotFound(new IOException(new IOException(notFound)))).isTrue(); // Check failure case. assertThat(errorExtractor.itemNotFound(statusOk)).isFalse(); assertThat(errorExtractor.itemNotFound(new IOException())).isFalse(); assertThat(errorExtractor.itemNotFound(new IOException(new IOException()))).isFalse(); }
/** A helper that generates the error JSON payload that Google APIs produce. */ private static GoogleJsonErrorContainer errorWithReasonAndStatus(String reason, int status) { ErrorInfo info = new ErrorInfo(); info.setReason(reason); info.setDomain("global"); // GoogleJsonError contains one or more ErrorInfo objects; our utiities read the first one. GoogleJsonError error = new GoogleJsonError(); error.setErrors(ImmutableList.of(info)); error.setCode(status); error.setMessage(reason); // The actual JSON response is an error container. GoogleJsonErrorContainer container = new GoogleJsonErrorContainer(); container.setError(error); return container; }
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { errorInfo.setFactory(jsonFactory); GoogleJsonError jsonError = new GoogleJsonError(); jsonError.setCode(status); jsonError.setErrors(Collections.singletonList(errorInfo));
@Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { errorInfo.setFactory(jsonFactory); GoogleJsonError jsonError = new GoogleJsonError(); jsonError.setCode(status); jsonError.setErrors(Collections.singletonList(errorInfo)); jsonError.setMessage(httpStatusString); jsonError.setFactory(jsonFactory); GenericJson errorResponse = new GenericJson(); errorResponse.set("error", jsonError); errorResponse.setFactory(jsonFactory); return new MockLowLevelHttpRequest() .setResponse( new MockLowLevelHttpResponse() .setContent(errorResponse.toPrettyString()) .setContentType(Json.MEDIA_TYPE) .setStatusCode(status)); } };