@Override GlobalOperationId parse(String... args) throws Exception { String message; if (args.length == 1) { return GlobalOperationId.of(args[0]); } else if (args.length > 1) { message = "Too many arguments."; } else { message = "Missing required operation id."; } throw new IllegalArgumentException(message); }
@Override GlobalOperationId setProjectId(String projectId) { if (getProject() != null) { return this; } return GlobalOperationId.of(projectId, getOperation()); }
/** Returns a global operation identity given project and operation names. */ public static GlobalOperationId of(String project, String operation) { return new GlobalOperationId(project, operation); }
private void compareOperationId(GlobalOperationId expected, GlobalOperationId value) { assertEquals(expected, value); assertEquals(expected.getProject(), expected.getProject()); assertEquals(expected.getOperation(), expected.getOperation()); assertEquals(expected.getSelfLink(), expected.getSelfLink()); assertEquals(expected.hashCode(), expected.hashCode()); }
@Test public void testOf() { GlobalOperationId operationId = GlobalOperationId.of(PROJECT, NAME); assertEquals(OperationId.Type.GLOBAL, operationId.getType()); assertEquals(PROJECT, operationId.getProject()); assertEquals(NAME, operationId.getOperation()); assertEquals(GLOBAL_URL, operationId.getSelfLink()); operationId = GlobalOperationId.of(NAME); assertEquals(OperationId.Type.GLOBAL, operationId.getType()); assertNull(operationId.getProject()); assertEquals(NAME, operationId.getOperation()); ZoneOperationId zoneOperationId = ZoneOperationId.of(PROJECT, ZONE, NAME); assertEquals(OperationId.Type.ZONE, zoneOperationId.getType());
@Test public void testToAndFromUrlGlobal() { GlobalOperationId operationId = GlobalOperationId.of(PROJECT, NAME); compareOperationId(operationId, GlobalOperationId.fromUrl(operationId.getSelfLink())); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("notMatchingUrl is not a valid global operation URL"); GlobalOperationId.fromUrl("notMatchingUrl"); }
@Test public void testMatchesUrl() { assertTrue(GlobalOperationId.matchesUrl(GlobalOperationId.of(PROJECT, NAME).getSelfLink())); assertFalse(GlobalOperationId.matchesUrl("notMatchingUrl")); assertTrue( RegionOperationId.matchesUrl(RegionOperationId.of(PROJECT, REGION, NAME).getSelfLink())); assertFalse(RegionOperationId.matchesUrl("notMatchingUrl")); assertTrue(ZoneOperationId.matchesUrl(ZoneOperationId.of(PROJECT, REGION, NAME).getSelfLink())); assertFalse(ZoneOperationId.matchesUrl("notMatchingUrl")); }
@Override public String getSelfLink() { return super.getSelfLink() + "/global/operations/" + getOperation(); }
@Test public void testSetProjectId() { GlobalOperationId operationId = GlobalOperationId.of(PROJECT, NAME); assertSame(operationId, operationId.setProjectId(PROJECT)); compareOperationId(operationId, GlobalOperationId.of(NAME).setProjectId(PROJECT)); ZoneOperationId zoneOperationId = ZoneOperationId.of(PROJECT, ZONE, NAME); assertSame(zoneOperationId, zoneOperationId.setProjectId(PROJECT)); compareZoneOperationId(zoneOperationId, ZoneOperationId.of(ZONE, NAME).setProjectId(PROJECT)); RegionOperationId regionOperationId = RegionOperationId.of(PROJECT, REGION, NAME); assertSame(regionOperationId, regionOperationId.setProjectId(PROJECT)); compareRegionOperationId( regionOperationId, RegionOperationId.of(REGION, NAME).setProjectId(PROJECT)); }
operationId = ZoneOperationId.fromUrl(operationPb.getSelfLink()); } else { operationId = GlobalOperationId.fromUrl(operationPb.getSelfLink());
@Override public int hashCode() { return baseHashCode(); }
@Override public boolean equals(Object obj) { return obj == this || obj instanceof GlobalOperationId && baseEquals((GlobalOperationId) obj); }
@Test public void testDeleteGlobalOperation_False() { EasyMock.expect(computeRpcMock.deleteGlobalOperation(GLOBAL_OPERATION_ID.getOperation())) .andReturn(false); EasyMock.replay(computeRpcMock); compute = options.getService(); assertFalse(compute.deleteOperation(GLOBAL_OPERATION_ID)); }
static GlobalOperationId fromUrl(String url) { Matcher matcher = PATTERN.matcher(url); if (!matcher.matches()) { throw new IllegalArgumentException(url + " is not a valid global operation URL"); } return GlobalOperationId.of(matcher.group(1), matcher.group(2)); } }
@Test public void testGetGlobalOperation_Null() { EasyMock.expect( computeRpcMock.getGlobalOperation( GLOBAL_OPERATION_ID.getOperation(), EMPTY_RPC_OPTIONS)) .andReturn(null); EasyMock.replay(computeRpcMock); compute = options.getService(); assertNull(compute.getOperation(GLOBAL_OPERATION_ID)); }
/** Returns a global operation identity given the operation name. */ public static GlobalOperationId of(String operation) { return new GlobalOperationId(null, operation); }
@Test public void testDeleteOperation() { initializeExpectedSubnetwork(2); expect(compute.getOptions()).andReturn(mockOptions); Operation operation = new Operation.Builder(serviceMockReturnsOptions) .setOperationId(GlobalOperationId.of("project", "op")) .build(); expect(compute.deleteSubnetwork(SUBNETWORK_ID)).andReturn(operation); replay(compute); initializeSubnetwork(); assertSame(operation, subnetwork.delete()); }
@Test public void testDeleteGlobalOperation_True() { EasyMock.expect(computeRpcMock.deleteGlobalOperation(GLOBAL_OPERATION_ID.getOperation())) .andReturn(true); EasyMock.replay(computeRpcMock); compute = options.getService(); assertTrue(compute.deleteOperation(GLOBAL_OPERATION_ID)); }
@Test public void testDeleteOperation() { initializeExpectedAddress(4); expect(compute.getOptions()).andReturn(mockOptions); Operation operation = new Operation.Builder(serviceMockReturnsOptions) .setOperationId(GlobalOperationId.of("project", "op")) .build(); expect(compute.deleteAddress(REGION_ADDRESS_ID)).andReturn(operation); replay(compute); initializeAddress(); assertSame(operation, address.delete()); }
@Test public void testGetGlobalOperation() { EasyMock.expect( computeRpcMock.getGlobalOperation( GLOBAL_OPERATION_ID.getOperation(), EMPTY_RPC_OPTIONS)) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); assertEquals(globalOperation, compute.getOperation(GLOBAL_OPERATION_ID)); }