@Test public void testWriteWithFlush() throws IOException { expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID); Capture<byte[]> capturedBuffer = Capture.newInstance(); storageRpcMock.write( eq(UPLOAD_ID), capture(capturedBuffer), eq(0), eq(0L), eq(CUSTOM_CHUNK_SIZE), eq(false)); replay(storageRpcMock); writer = new BlobWriteChannel(options, BLOB_INFO, EMPTY_RPC_OPTIONS); writer.setChunkSize(CUSTOM_CHUNK_SIZE); ByteBuffer buffer = randomBuffer(CUSTOM_CHUNK_SIZE); assertEquals(CUSTOM_CHUNK_SIZE, writer.write(buffer)); assertArrayEquals(buffer.array(), capturedBuffer.getValue()); }
@Test public void testDefaultWriteOptionsHasExpectedDefaults() { logging.setFlushSeverity(Severity.ERROR); Capture<WriteOption> logNameArg = Capture.newInstance(); Capture<WriteOption> resourceArg = Capture.newInstance(); logging.write((Iterable<LogEntry>) anyObject(), capture(logNameArg), capture(resourceArg)); expectLastCall().once(); replay(logging); loggingAppender.start(); Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0); LoggingEvent loggingEvent = createLoggingEvent(Level.ERROR, timestamp.getSeconds()); loggingAppender.doAppend(loggingEvent); assertThat(logNameArg.getValue()).isEqualTo(defaultWriteOptions[0]); // TODO(chingor): Fix this test to work on GCE and locally // assertThat(resourceArg.getValue()).isEqualTo(defaultWriteOptions[1]); }
@Test public void testReset() { Capture<String> id1 = Capture.newInstance(); Capture<String> id2 = Capture.newInstance(); EasyMock.expect(taskMaster.getSupervisorManager()).andReturn(Optional.of(supervisorManager)).times(2); EasyMock.expect(supervisorManager.resetSupervisor(EasyMock.capture(id1), EasyMock.anyObject(DataSourceMetadata.class))).andReturn(true); EasyMock.expect(supervisorManager.resetSupervisor(EasyMock.capture(id2), EasyMock.anyObject(DataSourceMetadata.class))).andReturn(false); replayAll(); Response response = supervisorResource.reset("my-id"); Assert.assertEquals(200, response.getStatus()); Assert.assertEquals(ImmutableMap.of("id", "my-id"), response.getEntity()); response = supervisorResource.reset("my-id-2"); Assert.assertEquals(404, response.getStatus()); Assert.assertEquals("my-id", id1.getValue()); Assert.assertEquals("my-id-2", id2.getValue()); verifyAll(); resetAll(); EasyMock.expect(taskMaster.getSupervisorManager()).andReturn(Optional.absent()); replayAll(); response = supervisorResource.terminate("my-id"); Assert.assertEquals(503, response.getStatus()); verifyAll(); }
@Test public void testCreateImageWithOptions() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect(computeRpcMock.createImage(eq(IMAGE.toPb()), capture(capturedOptions))) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.create(IMAGE, OPERATION_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(OPERATION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); assertTrue(selector.contains("id")); assertTrue(selector.contains("description")); assertEquals(23, selector.length()); assertEquals(globalOperation, operation); }
@Test public void testCreateNetworkWithOptions() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect(computeRpcMock.createNetwork(eq(NETWORK.toPb()), capture(capturedOptions))) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.create(NETWORK, OPERATION_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(OPERATION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); assertTrue(selector.contains("id")); assertTrue(selector.contains("description")); assertEquals(23, selector.length()); assertEquals(globalOperation, operation); }
@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 testWritesAndFlush() throws IOException { expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID); Capture<byte[]> capturedBuffer = Capture.newInstance(); storageRpcMock.write( eq(UPLOAD_ID), capture(capturedBuffer), eq(0), eq(0L), eq(DEFAULT_CHUNK_SIZE), eq(false)); replay(storageRpcMock); writer = new BlobWriteChannel(options, BLOB_INFO, EMPTY_RPC_OPTIONS); ByteBuffer[] buffers = new ByteBuffer[DEFAULT_CHUNK_SIZE / MIN_CHUNK_SIZE]; for (int i = 0; i < buffers.length; i++) { buffers[i] = randomBuffer(MIN_CHUNK_SIZE); assertEquals(MIN_CHUNK_SIZE, writer.write(buffers[i])); } for (int i = 0; i < buffers.length; i++) { assertArrayEquals( buffers[i].array(), Arrays.copyOfRange( capturedBuffer.getValue(), MIN_CHUNK_SIZE * i, MIN_CHUNK_SIZE * (i + 1))); } }
@Test public void testGetZoneWithSelectedFields() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect(computeRpcMock.getZone(eq(ZONE_ID.getZone()), capture(capturedOptions))) .andReturn(ZONE.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Zone zone = compute.getZone(ZONE_ID.getZone(), ZONE_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(ZONE_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); assertTrue(selector.contains("id")); assertTrue(selector.contains("description")); assertEquals(23, selector.length()); assertEquals(ZONE, zone); }
@Test public void testGetRegionWithSelectedFields() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect(computeRpcMock.getRegion(eq(REGION_ID.getRegion()), capture(capturedOptions))) .andReturn(REGION.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Region region = compute.getRegion(REGION_ID.getRegion(), REGION_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(REGION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); assertTrue(selector.contains("id")); assertTrue(selector.contains("description")); assertEquals(23, selector.length()); assertEquals(REGION, region); }
@Test public void testExceptionalDelete() EasyMock.expect(request.getRemoteAddr()).andReturn(ip).once(); final Capture<AuditInfo> auditInfoCapture = Capture.newInstance(); final LookupCoordinatorManager lookupCoordinatorManager = EasyMock.createStrictMock( LookupCoordinatorManager.class); EasyMock.expect(lookupCoordinatorManager.deleteLookup( EasyMock.eq(LOOKUP_TIER), EasyMock.eq(LOOKUP_NAME), EasyMock.capture(auditInfoCapture) )).andThrow(new RuntimeException(errMsg)).once(); EasyMock.replay(lookupCoordinatorManager, request); ); Assert.assertEquals(500, response.getStatus()); Assert.assertEquals(ImmutableMap.of("error", errMsg), response.getEntity()); Assert.assertTrue(auditInfoCapture.hasCaptured()); final AuditInfo auditInfo = auditInfoCapture.getValue(); Assert.assertEquals(author, auditInfo.getAuthor()); Assert.assertEquals(comment, auditInfo.getComment()); Assert.assertEquals(ip, auditInfo.getIp());
@Test public void testDeleteSnapshotWithSelectedFields_Operation() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( computeRpcMock.deleteSnapshot(eq(SNAPSHOT_ID.getSnapshot()), capture(capturedOptions))) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.deleteSnapshot(SNAPSHOT_ID, OPERATION_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(OPERATION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); assertTrue(selector.contains("id")); assertTrue(selector.contains("description")); assertEquals(23, selector.length()); assertEquals(globalOperation, operation); }
@Test public void testGetLicenseFromStringWithOptions() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( computeRpcMock.getLicense( eq(PROJECT), eq(LICENSE_ID.getLicense()), capture(capturedOptions))) .andReturn(LICENSE.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); License license = compute.getLicense(LICENSE_ID.getLicense(), LICENSE_OPTION_FIELDS); assertEquals(LICENSE, license); String selector = (String) capturedOptions.getValue().get(LICENSE_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); assertTrue(selector.contains("chargesUseFee")); assertEquals(22, selector.length()); assertEquals(LICENSE, license); }
@Test public void testFailedNew() throws Exception EasyMock.expect(request.getContentType()).andReturn(MediaType.APPLICATION_JSON).once(); EasyMock.expect(request.getRemoteAddr()).andReturn(ip).once(); final Capture<AuditInfo> auditInfoCapture = Capture.newInstance(); final LookupCoordinatorManager lookupCoordinatorManager = EasyMock.createStrictMock( LookupCoordinatorManager.class); EasyMock.expect(lookupCoordinatorManager.updateLookups( EasyMock.eq(SINGLE_TIER_MAP), EasyMock.capture(auditInfoCapture) )).andReturn(false).once(); EasyMock.replay(lookupCoordinatorManager, request); ); Assert.assertEquals(500, response.getStatus()); Assert.assertEquals(ImmutableMap.of("error", "Unknown error updating configuration"), response.getEntity()); Assert.assertTrue(auditInfoCapture.hasCaptured()); final AuditInfo auditInfo = auditInfoCapture.getValue(); Assert.assertEquals(author, auditInfo.getAuthor()); Assert.assertEquals(comment, auditInfo.getComment()); Assert.assertEquals(ip, auditInfo.getIp());
@Test public void testDBErrNewLookup() throws Exception EasyMock.expect(request.getContentType()).andReturn(MediaType.APPLICATION_JSON).once(); EasyMock.expect(request.getRemoteAddr()).andReturn(ip).once(); final Capture<AuditInfo> auditInfoCapture = Capture.newInstance(); final LookupCoordinatorManager lookupCoordinatorManager = EasyMock.createStrictMock( LookupCoordinatorManager.class); EasyMock.expect(lookupCoordinatorManager.updateLookup( EasyMock.eq(LOOKUP_TIER), EasyMock.eq(LOOKUP_NAME), EasyMock.eq(SINGLE_LOOKUP), EasyMock.capture(auditInfoCapture) )).andReturn(false).once(); EasyMock.replay(lookupCoordinatorManager, request); ); Assert.assertEquals(500, response.getStatus()); Assert.assertEquals(ImmutableMap.of("error", "Unknown error updating configuration"), response.getEntity()); Assert.assertTrue(auditInfoCapture.hasCaptured()); final AuditInfo auditInfo = auditInfoCapture.getValue(); Assert.assertEquals(author, auditInfo.getAuthor()); Assert.assertEquals(comment, auditInfo.getComment()); Assert.assertEquals(ip, auditInfo.getIp());
@Test public void testCreateGlobalAddressWithOptions() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( computeRpcMock.createGlobalAddress(eq(GLOBAL_ADDRESS.toPb()), capture(capturedOptions))) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.create(GLOBAL_ADDRESS, OPERATION_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(OPERATION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); assertTrue(selector.contains("id")); assertTrue(selector.contains("description")); assertEquals(23, selector.length()); assertEquals(globalOperation, operation); }
@Test public void testSimpleDelete() EasyMock.expect(request.getRemoteAddr()).andReturn(ip).once(); final Capture<AuditInfo> auditInfoCapture = Capture.newInstance(); final LookupCoordinatorManager lookupCoordinatorManager = EasyMock.createStrictMock( LookupCoordinatorManager.class); EasyMock.expect(lookupCoordinatorManager.deleteLookup( EasyMock.eq(LOOKUP_TIER), EasyMock.eq(LOOKUP_NAME), EasyMock.capture(auditInfoCapture) )).andReturn(true).once(); EasyMock.replay(lookupCoordinatorManager, request); ); Assert.assertEquals(202, response.getStatus()); Assert.assertTrue(auditInfoCapture.hasCaptured()); final AuditInfo auditInfo = auditInfoCapture.getValue(); Assert.assertEquals(author, auditInfo.getAuthor()); Assert.assertEquals(comment, auditInfo.getComment()); Assert.assertEquals(ip, auditInfo.getIp());
@Test public void testProjectGetWithOptions() { Capture<Map<DnsRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect(dnsRpcMock.getProject(EasyMock.capture(capturedOptions))) .andReturn(PROJECT_INFO.toPb()); EasyMock.replay(dnsRpcMock); dns = options.getService(); // creates DnsImpl ProjectInfo projectInfo = dns.getProject(PROJECT_FIELDS); String selector = (String) capturedOptions.getValue().get(PROJECT_FIELDS.getRpcOption()); assertEquals(PROJECT_INFO, projectInfo); assertTrue(selector.contains(Dns.ProjectField.QUOTA.getSelector())); assertTrue(selector.contains(Dns.ProjectField.PROJECT_ID.getSelector())); }
@Test public void testMissingDelete() EasyMock.expect(request.getRemoteAddr()).andReturn(ip).once(); final Capture<AuditInfo> auditInfoCapture = Capture.newInstance(); final LookupCoordinatorManager lookupCoordinatorManager = EasyMock.createStrictMock( LookupCoordinatorManager.class); EasyMock.expect(lookupCoordinatorManager.deleteLookup( EasyMock.eq(LOOKUP_TIER), EasyMock.eq(LOOKUP_NAME), EasyMock.capture(auditInfoCapture) )).andReturn(false).once(); EasyMock.replay(lookupCoordinatorManager, request); ); Assert.assertEquals(404, response.getStatus()); Assert.assertTrue(auditInfoCapture.hasCaptured()); final AuditInfo auditInfo = auditInfoCapture.getValue(); Assert.assertEquals(author, auditInfo.getAuthor()); Assert.assertEquals(comment, auditInfo.getComment()); Assert.assertEquals(ip, auditInfo.getIp());
@Test public void testDeleteNetworkWithSelectedFields_Operation() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( computeRpcMock.deleteNetwork(eq(NETWORK_ID.getNetwork()), capture(capturedOptions))) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.deleteNetwork(NETWORK_ID, OPERATION_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(OPERATION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); assertTrue(selector.contains("id")); assertTrue(selector.contains("description")); assertEquals(23, selector.length()); assertEquals(globalOperation, operation); }
@Test public void testGetTableWithSelectedFields() { Capture<Map<BigQueryRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( bigqueryRpcMock.getTable(eq(PROJECT), eq(DATASET), eq(TABLE), capture(capturedOptions))) .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Table table = bigquery.getTable(TABLE_ID, TABLE_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(TABLE_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("tableReference")); assertTrue(selector.contains("schema")); assertTrue(selector.contains("etag")); assertEquals(31, selector.length()); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); }