Skip to content

Commit 08295ee

Browse files
author
Ajay Kannan
committed
Document exceptions and satisfy codacy's demands.
1 parent ee78b22 commit 08295ee

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

gcloud-java-core/src/main/java/com/google/gcloud/IamPolicy.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public abstract static class Builder<R, B extends Builder<R, B>> {
5858
private String etag;
5959
private Integer version;
6060

61+
/**
62+
* Constructor for IAM Policy builder.
63+
*/
6164
protected Builder() {}
6265

6366
/**
@@ -73,6 +76,8 @@ public final B bindings(Map<R, Set<Identity>> bindings) {
7376

7477
/**
7578
* Adds a binding to the policy.
79+
*
80+
* @throws IllegalArgumentException if the policy already contains a binding with the same role
7681
*/
7782
public final B addBinding(R role, Set<Identity> identities) {
7883
checkArgument(!bindings.containsKey(role),
@@ -83,6 +88,8 @@ public final B addBinding(R role, Set<Identity> identities) {
8388

8489
/**
8590
* Adds a binding to the policy.
91+
*
92+
* @throws IllegalArgumentException if the policy already contains a binding with the same role
8693
*/
8794
public final B addBinding(R role, Identity first, Identity... others) {
8895
checkArgument(!bindings.containsKey(role),
@@ -104,8 +111,12 @@ public final B removeBinding(R role) {
104111

105112
/**
106113
* Adds one or more identities to an existing binding.
114+
*
115+
* @throws IllegalArgumentException if the policy doesn't contain a binding with the specified
116+
* role
107117
*/
108118
public final B addIdentity(R role, Identity first, Identity... others) {
119+
checkArgument(bindings.containsKey(role), "The policy doesn't contain the specified role.");
109120
Set<Identity> identities = bindings.get(role);
110121
identities.add(first);
111122
identities.addAll(Arrays.asList(others));
@@ -114,8 +125,12 @@ public final B addIdentity(R role, Identity first, Identity... others) {
114125

115126
/**
116127
* Removes one or more identities from an existing binding.
128+
*
129+
* @throws IllegalArgumentException if the policy doesn't contain a binding with the specified
130+
* role
117131
*/
118132
public final B removeIdentity(R role, Identity first, Identity... others) {
133+
checkArgument(bindings.containsKey(role), "The policy doesn't contain the specified role.");
119134
bindings.get(role).remove(first);
120135
bindings.get(role).removeAll(Arrays.asList(others));
121136
return self();

gcloud-java-core/src/test/java/com/google/gcloud/IdentityTest.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertNull;
21-
import static org.junit.Assert.fail;
2221

2322
import org.junit.Test;
2423

@@ -48,48 +47,44 @@ public void testAllAuthenticatedUsers() {
4847
public void testUser() {
4948
assertEquals(Identity.Type.USER, USER.type());
5049
assertEquals("abc@gmail.com", USER.id());
51-
try {
52-
Identity.user(null);
53-
fail("Should have thrown exception due to null email address.");
54-
} catch (NullPointerException e) {
55-
// expected
56-
}
50+
}
51+
52+
@Test(expected = NullPointerException.class)
53+
public void testUserNullEmail() {
54+
Identity.user(null);
5755
}
5856

5957
@Test
6058
public void testServiceAccount() {
6159
assertEquals(Identity.Type.SERVICE_ACCOUNT, SERVICE_ACCOUNT.type());
6260
assertEquals("service-account@gmail.com", SERVICE_ACCOUNT.id());
63-
try {
64-
Identity.serviceAccount(null);
65-
fail("Should have thrown exception due to null email address.");
66-
} catch (NullPointerException e) {
67-
// expected
68-
}
61+
}
62+
63+
@Test(expected = NullPointerException.class)
64+
public void testServiceAccountNullEmail() {
65+
Identity.serviceAccount(null);
6966
}
7067

7168
@Test
7269
public void testGroup() {
7370
assertEquals(Identity.Type.GROUP, GROUP.type());
7471
assertEquals("group@gmail.com", GROUP.id());
75-
try {
76-
Identity.group(null);
77-
fail("Should have thrown exception due to null email address.");
78-
} catch (NullPointerException e) {
79-
// expected
80-
}
72+
}
73+
74+
@Test(expected = NullPointerException.class)
75+
public void testGroupNullEmail() {
76+
Identity.group(null);
8177
}
8278

8379
@Test
8480
public void testDomain() {
8581
assertEquals(Identity.Type.DOMAIN, DOMAIN.type());
8682
assertEquals("google.com", DOMAIN.id());
87-
try {
88-
Identity.domain(null);
89-
fail("Should have thrown exception due to null domain.");
90-
} catch (NullPointerException e) {
91-
// expected
92-
}
83+
}
84+
85+
@Test(expected = NullPointerException.class)
86+
public void testDomainNullId() {
87+
Identity.domain(null);
9388
}
9489

9590
@Test

0 commit comments

Comments
 (0)