Skip to content

Commit 0465926

Browse files
committed
Add matchesUrl method to identity objects. Add tests
1 parent 0940f31 commit 0465926

File tree

13 files changed

+140
-2
lines changed

13 files changed

+140
-2
lines changed

gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public String apply(DiskTypeId diskTypeId) {
4141
}
4242
};
4343

44+
static final String REGEX = ZoneResourceId.REGEX + "diskTypes/[^/]+";
4445
private static final long serialVersionUID = 7337881474103686219L;
4546

4647
private final String diskType;
@@ -112,7 +113,18 @@ public static DiskTypeId of(String project, String zone, String diskType) {
112113
return of(ZoneId.of(project, zone), diskType);
113114
}
114115

116+
/**
117+
* Returns {@code true} if the provided string matches the expected format of a disk type URL.
118+
* Returns {@code false} otherwise.
119+
*/
120+
static boolean matchesUrl(String url) {
121+
return url.matches(REGEX);
122+
}
123+
115124
static DiskTypeId fromUrl(String url) {
125+
if (!matchesUrl(url)) {
126+
throw new IllegalArgumentException(url + " is not a valid disk type URL");
127+
}
116128
int projectsIndex = url.indexOf("/projects/");
117129
int zonesIndex = url.indexOf("/zones/");
118130
int diskTypesIndex = url.indexOf("/diskTypes/");

gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public String apply(LicenseId licenseId) {
4141
}
4242
};
4343

44+
static final String REGEX = ResourceId.REGEX + "global/licenses/[^/]+";
4445
private static final long serialVersionUID = -2239484554024469651L;
4546

4647
private final String license;
@@ -105,7 +106,18 @@ public static LicenseId of(String project, String license) {
105106
return new LicenseId(project, license);
106107
}
107108

109+
/**
110+
* Returns {@code true} if the provided string matches the expected format of a license URL.
111+
* Returns {@code false} otherwise.
112+
*/
113+
static boolean matchesUrl(String url) {
114+
return url.matches(REGEX);
115+
}
116+
108117
static LicenseId fromUrl(String url) {
118+
if (!matchesUrl(url)) {
119+
throw new IllegalArgumentException(url + " is not a valid license URL");
120+
}
109121
int projectsIndex = url.indexOf("/projects/");
110122
int licensesIndex = url.indexOf("/global/licenses/");
111123
String project = url.substring(projectsIndex + 10, licensesIndex);

gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java

+12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public String apply(MachineTypeId machineTypeId) {
4343
}
4444
};
4545

46+
static final String REGEX = ZoneResourceId.REGEX + "machineTypes/[^/]+";
4647
private static final long serialVersionUID = -5819598544478859608L;
4748

4849
private final String machineType;
@@ -101,7 +102,18 @@ public static MachineTypeId of(String project, String zone, String machineType)
101102
return new MachineTypeId(project, zone, machineType);
102103
}
103104

105+
/**
106+
* Returns {@code true} if the provided string matches the expected format of a machine type URL.
107+
* Returns {@code false} otherwise.
108+
*/
109+
static boolean matchesUrl(String url) {
110+
return url.matches(REGEX);
111+
}
112+
104113
static MachineTypeId fromUrl(String url) {
114+
if (!matchesUrl(url)) {
115+
throw new IllegalArgumentException(url + " is not a valid machine type URL");
116+
}
105117
int projectsIndex = url.indexOf("/projects/");
106118
int zonesIndex = url.indexOf("/zones/");
107119
int machineTypesIndex = url.indexOf("/machineTypes/");

gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public String apply(RegionId regionId) {
4141
}
4242
};
4343

44+
static final String REGEX = ResourceId.REGEX + "regions/[^/]+";
4445
private static final long serialVersionUID = 5569092266957249294L;
4546

4647
private final String region;
@@ -105,9 +106,17 @@ public static RegionId of(String region) {
105106
}
106107

107108
/**
108-
* Returns a new region identity given a region URL.
109+
* Returns {@code true} if the provided string matches the expected format of a region URL.
110+
* Returns {@code false} otherwise.
109111
*/
112+
static boolean matchesUrl(String url) {
113+
return url.matches(REGEX);
114+
}
115+
110116
static RegionId fromUrl(String url) {
117+
if (!matchesUrl(url)) {
118+
throw new IllegalArgumentException(url + " is not a valid region URL");
119+
}
111120
int projectsIndex = url.indexOf("/projects/");
112121
int regionsIndex = url.indexOf("/regions/");
113122
String project = url.substring(projectsIndex + 10, regionsIndex);

gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828
public abstract class RegionResourceId extends ResourceId {
2929

30+
static final String REGEX = ResourceId.REGEX + "regions/[^/]+/";
3031
private static final long serialVersionUID = 5569092266957249294L;
3132

3233
private final String region;

gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*/
2727
public abstract class ResourceId implements Serializable {
2828

29+
static final String REGEX =
30+
"(https?://(www|content).googleapis.com/compute/v1/)?projects/[^/]+/";
2931
private static final long serialVersionUID = -8028734746870421573L;
3032

3133
private String project;

gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public String apply(ZoneId zoneId) {
4141
}
4242
};
4343

44+
static final String REGEX = ResourceId.REGEX + "zones/[^/]+";
4445
private static final long serialVersionUID = -7635391994812946733L;
4546

4647
private final String zone;
@@ -100,9 +101,17 @@ public static ZoneId of(String zone) {
100101
}
101102

102103
/**
103-
* Returns a new zone identity given a zone URL.
104+
* Returns {@code true} if the provided string matches the expected format of a zone URL.
105+
* Returns {@code false} otherwise.
104106
*/
107+
static boolean matchesUrl(String url) {
108+
return url.matches(REGEX);
109+
}
110+
105111
static ZoneId fromUrl(String url) {
112+
if (!matchesUrl(url)) {
113+
throw new IllegalArgumentException(url + " is not a valid zone URL");
114+
}
106115
int projectsIndex = url.indexOf("/projects/");
107116
int zonesIndex = url.indexOf("/zones/");
108117
String project = url.substring(projectsIndex + 10, zonesIndex);

gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828
public abstract class ZoneResourceId extends ResourceId {
2929

30+
static final String REGEX = ResourceId.REGEX + "zones/[^/]+/";
3031
private static final long serialVersionUID = -6249546895344926888L;
3132

3233
private final String zone;

gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
package com.google.gcloud.compute;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
2021
import static org.junit.Assert.assertNull;
2122
import static org.junit.Assert.assertSame;
23+
import static org.junit.Assert.assertTrue;
2224

25+
import org.junit.Rule;
2326
import org.junit.Test;
27+
import org.junit.rules.ExpectedException;
2428

2529
public class DiskTypeIdTest {
2630

@@ -30,6 +34,9 @@ public class DiskTypeIdTest {
3034
private static final String URL =
3135
"https://www.googleapis.com/compute/v1/projects/project/zones/zone/diskTypes/diskType";
3236

37+
@Rule
38+
public ExpectedException thrown = ExpectedException.none();
39+
3340
@Test
3441
public void testOf() {
3542
DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE);
@@ -48,6 +55,9 @@ public void testToAndFromUrl() {
4855
DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE);
4956
assertSame(diskTypeId, diskTypeId.setProjectId(PROJECT));
5057
compareDiskTypeId(diskTypeId, DiskTypeId.fromUrl(diskTypeId.toUrl()));
58+
thrown.expect(IllegalArgumentException.class);
59+
thrown.expectMessage("notMatchingUrl is not a valid disk type URL");
60+
diskTypeId = DiskTypeId.fromUrl("notMatchingUrl");
5161
}
5262

5363
@Test
@@ -57,6 +67,12 @@ public void testSetProjectId() {
5767
compareDiskTypeId(diskTypeId, DiskTypeId.of(ZONE, DISK_TYPE).setProjectId(PROJECT));
5868
}
5969

70+
@Test
71+
public void testMatchesUrl() {
72+
assertTrue(DiskTypeId.matchesUrl(DiskTypeId.of(PROJECT, ZONE, DISK_TYPE).toUrl()));
73+
assertFalse(DiskTypeId.matchesUrl("notMatchingUrl"));
74+
}
75+
6076
private void compareDiskTypeId(DiskTypeId expected, DiskTypeId value) {
6177
assertEquals(expected, value);
6278
assertEquals(expected.project(), expected.project());

gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
package com.google.gcloud.compute;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
2021
import static org.junit.Assert.assertNull;
2122
import static org.junit.Assert.assertSame;
23+
import static org.junit.Assert.assertTrue;
2224

25+
import org.junit.Rule;
2326
import org.junit.Test;
27+
import org.junit.rules.ExpectedException;
2428

2529
public class LicenseIdTest {
2630

@@ -29,6 +33,9 @@ public class LicenseIdTest {
2933
private static final String URL =
3034
"https://www.googleapis.com/compute/v1/projects/project/global/licenses/license";
3135

36+
@Rule
37+
public ExpectedException thrown = ExpectedException.none();
38+
3239
@Test
3340
public void testOf() {
3441
LicenseId licenseId = LicenseId.of(PROJECT, LICENSE);
@@ -51,6 +58,15 @@ public void testSetProjectId() {
5158
LicenseId licenseId = LicenseId.of(PROJECT, LICENSE);
5259
assertSame(licenseId, licenseId.setProjectId(PROJECT));
5360
compareLicenseId(licenseId, LicenseId.of(LICENSE).setProjectId(PROJECT));
61+
thrown.expect(IllegalArgumentException.class);
62+
thrown.expectMessage("notMatchingUrl is not a valid license URL");
63+
licenseId = LicenseId.fromUrl("notMatchingUrl");
64+
}
65+
66+
@Test
67+
public void testMatchesUrl() {
68+
assertTrue(LicenseId.matchesUrl(LicenseId.of(PROJECT, LICENSE).toUrl()));
69+
assertFalse(LicenseId.matchesUrl("notMatchingUrl"));
5470
}
5571

5672
private void compareLicenseId(LicenseId expected, LicenseId value) {

gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
package com.google.gcloud.compute;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
2021
import static org.junit.Assert.assertNull;
2122
import static org.junit.Assert.assertSame;
23+
import static org.junit.Assert.assertTrue;
2224

25+
import org.junit.Rule;
2326
import org.junit.Test;
27+
import org.junit.rules.ExpectedException;
2428

2529
public class MachineTypeIdTest {
2630

@@ -30,6 +34,9 @@ public class MachineTypeIdTest {
3034
private static final String URL =
3135
"https://www.googleapis.com/compute/v1/projects/project/zones/zone/machineTypes/type";
3236

37+
@Rule
38+
public ExpectedException thrown = ExpectedException.none();
39+
3340
@Test
3441
public void testOf() {
3542
MachineTypeId machineTypeId = MachineTypeId.of(PROJECT, ZONE, TYPE);
@@ -47,6 +54,9 @@ public void testOf() {
4754
public void testToAndFromUrl() {
4855
MachineTypeId machineTypeId = MachineTypeId.of(PROJECT, ZONE, TYPE);
4956
compareMachineTypeId(machineTypeId, MachineTypeId.fromUrl(machineTypeId.toUrl()));
57+
thrown.expect(IllegalArgumentException.class);
58+
thrown.expectMessage("notMatchingUrl is not a valid machine type URL");
59+
machineTypeId = MachineTypeId.fromUrl("notMatchingUrl");
5060
}
5161

5262
@Test
@@ -56,6 +66,12 @@ public void testSetProjectId() {
5666
compareMachineTypeId(machineTypeId, MachineTypeId.of(ZONE, TYPE).setProjectId(PROJECT));
5767
}
5868

69+
@Test
70+
public void testMatchesUrl() {
71+
assertTrue(MachineTypeId.matchesUrl(MachineTypeId.of(PROJECT, ZONE, TYPE).toUrl()));
72+
assertFalse(MachineTypeId.matchesUrl("notMatchingUrl"));
73+
}
74+
5975
private void compareMachineTypeId(MachineTypeId expected, MachineTypeId value) {
6076
assertEquals(expected, value);
6177
assertEquals(expected.project(), expected.project());

gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
package com.google.gcloud.compute;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
2021
import static org.junit.Assert.assertNull;
2122
import static org.junit.Assert.assertSame;
23+
import static org.junit.Assert.assertTrue;
2224

25+
import org.junit.Rule;
2326
import org.junit.Test;
27+
import org.junit.rules.ExpectedException;
2428

2529
public class RegionIdTest {
2630

@@ -29,6 +33,9 @@ public class RegionIdTest {
2933
private static final String URL =
3034
"https://www.googleapis.com/compute/v1/projects/project/regions/region";
3135

36+
@Rule
37+
public ExpectedException thrown = ExpectedException.none();
38+
3239
@Test
3340
public void testOf() {
3441
RegionId regionId = RegionId.of(PROJECT, REGION);
@@ -51,6 +58,15 @@ public void testSetProjectId() {
5158
RegionId regionId = RegionId.of(PROJECT, REGION);
5259
assertSame(regionId, regionId.setProjectId(PROJECT));
5360
compareRegionId(regionId, RegionId.of(REGION).setProjectId(PROJECT));
61+
thrown.expect(IllegalArgumentException.class);
62+
thrown.expectMessage("notMatchingUrl is not a valid region URL");
63+
regionId = RegionId.fromUrl("notMatchingUrl");
64+
}
65+
66+
@Test
67+
public void testMatchesUrl() {
68+
assertTrue(RegionId.matchesUrl(RegionId.of(PROJECT, REGION).toUrl()));
69+
assertFalse(RegionId.matchesUrl("notMatchingUrl"));
5470
}
5571

5672
private void compareRegionId(RegionId expected, RegionId value) {

gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
package com.google.gcloud.compute;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
2021
import static org.junit.Assert.assertNull;
2122
import static org.junit.Assert.assertSame;
23+
import static org.junit.Assert.assertTrue;
2224

25+
import org.junit.Rule;
2326
import org.junit.Test;
27+
import org.junit.rules.ExpectedException;
2428

2529
public class ZoneIdTest {
2630

@@ -29,6 +33,9 @@ public class ZoneIdTest {
2933
private static final String URL =
3034
"https://www.googleapis.com/compute/v1/projects/project/zones/zone";
3135

36+
@Rule
37+
public ExpectedException thrown = ExpectedException.none();
38+
3239
@Test
3340
public void testOf() {
3441
ZoneId zoneId = ZoneId.of(PROJECT, ZONE);
@@ -51,6 +58,15 @@ public void testSetProjectId() {
5158
ZoneId zoneId = ZoneId.of(PROJECT, ZONE);
5259
assertSame(zoneId, zoneId.setProjectId(PROJECT));
5360
compareZoneId(zoneId, ZoneId.of(ZONE).setProjectId(PROJECT));
61+
thrown.expect(IllegalArgumentException.class);
62+
thrown.expectMessage("notMatchingUrl is not a valid zone URL");
63+
zoneId = ZoneId.fromUrl("notMatchingUrl");
64+
}
65+
66+
@Test
67+
public void testMatchesUrl() {
68+
assertTrue(ZoneId.matchesUrl(ZoneId.of(PROJECT, ZONE).toUrl()));
69+
assertFalse(ZoneId.matchesUrl("notMatchingUrl"));
5470
}
5571

5672
private void compareZoneId(ZoneId expected, ZoneId value) {

0 commit comments

Comments
 (0)