Skip to content

Commit

Permalink
Add matchesUrl method to identity objects. Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Feb 24, 2016
1 parent 0940f31 commit 0465926
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public String apply(DiskTypeId diskTypeId) {
}
};

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

private final String diskType;
Expand Down Expand Up @@ -112,7 +113,18 @@ public static DiskTypeId of(String project, String zone, String diskType) {
return of(ZoneId.of(project, zone), diskType);
}

/**
* Returns {@code true} if the provided string matches the expected format of a disk type URL.
* Returns {@code false} otherwise.
*/
static boolean matchesUrl(String url) {
return url.matches(REGEX);
}

static DiskTypeId fromUrl(String url) {
if (!matchesUrl(url)) {
throw new IllegalArgumentException(url + " is not a valid disk type URL");
}
int projectsIndex = url.indexOf("/projects/");
int zonesIndex = url.indexOf("/zones/");
int diskTypesIndex = url.indexOf("/diskTypes/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public String apply(LicenseId licenseId) {
}
};

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

private final String license;
Expand Down Expand Up @@ -105,7 +106,18 @@ public static LicenseId of(String project, String license) {
return new LicenseId(project, license);
}

/**
* Returns {@code true} if the provided string matches the expected format of a license URL.
* Returns {@code false} otherwise.
*/
static boolean matchesUrl(String url) {
return url.matches(REGEX);
}

static LicenseId fromUrl(String url) {
if (!matchesUrl(url)) {
throw new IllegalArgumentException(url + " is not a valid license URL");
}
int projectsIndex = url.indexOf("/projects/");
int licensesIndex = url.indexOf("/global/licenses/");
String project = url.substring(projectsIndex + 10, licensesIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public String apply(MachineTypeId machineTypeId) {
}
};

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

private final String machineType;
Expand Down Expand Up @@ -101,7 +102,18 @@ public static MachineTypeId of(String project, String zone, String machineType)
return new MachineTypeId(project, zone, machineType);
}

/**
* Returns {@code true} if the provided string matches the expected format of a machine type URL.
* Returns {@code false} otherwise.
*/
static boolean matchesUrl(String url) {
return url.matches(REGEX);
}

static MachineTypeId fromUrl(String url) {
if (!matchesUrl(url)) {
throw new IllegalArgumentException(url + " is not a valid machine type URL");
}
int projectsIndex = url.indexOf("/projects/");
int zonesIndex = url.indexOf("/zones/");
int machineTypesIndex = url.indexOf("/machineTypes/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public String apply(RegionId regionId) {
}
};

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

private final String region;
Expand Down Expand Up @@ -105,9 +106,17 @@ public static RegionId of(String region) {
}

/**
* Returns a new region identity given a region URL.
* Returns {@code true} if the provided string matches the expected format of a region URL.
* Returns {@code false} otherwise.
*/
static boolean matchesUrl(String url) {
return url.matches(REGEX);
}

static RegionId fromUrl(String url) {
if (!matchesUrl(url)) {
throw new IllegalArgumentException(url + " is not a valid region URL");
}
int projectsIndex = url.indexOf("/projects/");
int regionsIndex = url.indexOf("/regions/");
String project = url.substring(projectsIndex + 10, regionsIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
public abstract class RegionResourceId extends ResourceId {

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

private final String region;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
public abstract class ResourceId implements Serializable {

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

private String project;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public String apply(ZoneId zoneId) {
}
};

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

private final String zone;
Expand Down Expand Up @@ -100,9 +101,17 @@ public static ZoneId of(String zone) {
}

/**
* Returns a new zone identity given a zone URL.
* Returns {@code true} if the provided string matches the expected format of a zone URL.
* Returns {@code false} otherwise.
*/
static boolean matchesUrl(String url) {
return url.matches(REGEX);
}

static ZoneId fromUrl(String url) {
if (!matchesUrl(url)) {
throw new IllegalArgumentException(url + " is not a valid zone URL");
}
int projectsIndex = url.indexOf("/projects/");
int zonesIndex = url.indexOf("/zones/");
String project = url.substring(projectsIndex + 10, zonesIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
public abstract class ZoneResourceId extends ResourceId {

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

private final String zone;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
package com.google.gcloud.compute;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class DiskTypeIdTest {

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

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testOf() {
DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE);
Expand All @@ -48,6 +55,9 @@ public void testToAndFromUrl() {
DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE);
assertSame(diskTypeId, diskTypeId.setProjectId(PROJECT));
compareDiskTypeId(diskTypeId, DiskTypeId.fromUrl(diskTypeId.toUrl()));
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("notMatchingUrl is not a valid disk type URL");
diskTypeId = DiskTypeId.fromUrl("notMatchingUrl");
}

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

@Test
public void testMatchesUrl() {
assertTrue(DiskTypeId.matchesUrl(DiskTypeId.of(PROJECT, ZONE, DISK_TYPE).toUrl()));
assertFalse(DiskTypeId.matchesUrl("notMatchingUrl"));
}

private void compareDiskTypeId(DiskTypeId expected, DiskTypeId value) {
assertEquals(expected, value);
assertEquals(expected.project(), expected.project());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
package com.google.gcloud.compute;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class LicenseIdTest {

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

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testOf() {
LicenseId licenseId = LicenseId.of(PROJECT, LICENSE);
Expand All @@ -51,6 +58,15 @@ public void testSetProjectId() {
LicenseId licenseId = LicenseId.of(PROJECT, LICENSE);
assertSame(licenseId, licenseId.setProjectId(PROJECT));
compareLicenseId(licenseId, LicenseId.of(LICENSE).setProjectId(PROJECT));
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("notMatchingUrl is not a valid license URL");
licenseId = LicenseId.fromUrl("notMatchingUrl");
}

@Test
public void testMatchesUrl() {
assertTrue(LicenseId.matchesUrl(LicenseId.of(PROJECT, LICENSE).toUrl()));
assertFalse(LicenseId.matchesUrl("notMatchingUrl"));
}

private void compareLicenseId(LicenseId expected, LicenseId value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
package com.google.gcloud.compute;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class MachineTypeIdTest {

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

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testOf() {
MachineTypeId machineTypeId = MachineTypeId.of(PROJECT, ZONE, TYPE);
Expand All @@ -47,6 +54,9 @@ public void testOf() {
public void testToAndFromUrl() {
MachineTypeId machineTypeId = MachineTypeId.of(PROJECT, ZONE, TYPE);
compareMachineTypeId(machineTypeId, MachineTypeId.fromUrl(machineTypeId.toUrl()));
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("notMatchingUrl is not a valid machine type URL");
machineTypeId = MachineTypeId.fromUrl("notMatchingUrl");
}

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

@Test
public void testMatchesUrl() {
assertTrue(MachineTypeId.matchesUrl(MachineTypeId.of(PROJECT, ZONE, TYPE).toUrl()));
assertFalse(MachineTypeId.matchesUrl("notMatchingUrl"));
}

private void compareMachineTypeId(MachineTypeId expected, MachineTypeId value) {
assertEquals(expected, value);
assertEquals(expected.project(), expected.project());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
package com.google.gcloud.compute;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class RegionIdTest {

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

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testOf() {
RegionId regionId = RegionId.of(PROJECT, REGION);
Expand All @@ -51,6 +58,15 @@ public void testSetProjectId() {
RegionId regionId = RegionId.of(PROJECT, REGION);
assertSame(regionId, regionId.setProjectId(PROJECT));
compareRegionId(regionId, RegionId.of(REGION).setProjectId(PROJECT));
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("notMatchingUrl is not a valid region URL");
regionId = RegionId.fromUrl("notMatchingUrl");
}

@Test
public void testMatchesUrl() {
assertTrue(RegionId.matchesUrl(RegionId.of(PROJECT, REGION).toUrl()));
assertFalse(RegionId.matchesUrl("notMatchingUrl"));
}

private void compareRegionId(RegionId expected, RegionId value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
package com.google.gcloud.compute;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class ZoneIdTest {

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

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testOf() {
ZoneId zoneId = ZoneId.of(PROJECT, ZONE);
Expand All @@ -51,6 +58,15 @@ public void testSetProjectId() {
ZoneId zoneId = ZoneId.of(PROJECT, ZONE);
assertSame(zoneId, zoneId.setProjectId(PROJECT));
compareZoneId(zoneId, ZoneId.of(ZONE).setProjectId(PROJECT));
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("notMatchingUrl is not a valid zone URL");
zoneId = ZoneId.fromUrl("notMatchingUrl");
}

@Test
public void testMatchesUrl() {
assertTrue(ZoneId.matchesUrl(ZoneId.of(PROJECT, ZONE).toUrl()));
assertFalse(ZoneId.matchesUrl("notMatchingUrl"));
}

private void compareZoneId(ZoneId expected, ZoneId value) {
Expand Down

0 comments on commit 0465926

Please sign in to comment.