• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.cloud;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNull;
21 
22 import org.junit.Test;
23 
24 public class IdentityTest {
25 
26   private static final Identity ALL_USERS = Identity.allUsers();
27   private static final Identity ALL_AUTH_USERS = Identity.allAuthenticatedUsers();
28   private static final Identity USER = Identity.user("abc@gmail.com");
29   private static final Identity SERVICE_ACCOUNT =
30       Identity.serviceAccount("service-account@gmail.com");
31   private static final Identity GROUP = Identity.group("group@gmail.com");
32   private static final Identity DOMAIN = Identity.domain("google.com");
33   private static final Identity PROJECT_OWNER = Identity.projectOwner("my-sample-project");
34   private static final Identity PROJECT_EDITOR = Identity.projectEditor("my-sample-project");
35   private static final Identity PROJECT_VIEWER = Identity.projectViewer("my-sample-project");
36 
37   @Test
testAllUsers()38   public void testAllUsers() {
39     assertEquals(Identity.Type.ALL_USERS, ALL_USERS.getType());
40     assertNull(ALL_USERS.getValue());
41   }
42 
43   @Test
testAllAuthenticatedUsers()44   public void testAllAuthenticatedUsers() {
45     assertEquals(Identity.Type.ALL_AUTHENTICATED_USERS, ALL_AUTH_USERS.getType());
46     assertNull(ALL_AUTH_USERS.getValue());
47   }
48 
49   @Test
testUser()50   public void testUser() {
51     assertEquals(Identity.Type.USER, USER.getType());
52     assertEquals("abc@gmail.com", USER.getValue());
53   }
54 
55   @Test(expected = NullPointerException.class)
testUserNullEmail()56   public void testUserNullEmail() {
57     Identity.user(null);
58   }
59 
60   @Test
testServiceAccount()61   public void testServiceAccount() {
62     assertEquals(Identity.Type.SERVICE_ACCOUNT, SERVICE_ACCOUNT.getType());
63     assertEquals("service-account@gmail.com", SERVICE_ACCOUNT.getValue());
64   }
65 
66   @Test(expected = NullPointerException.class)
testServiceAccountNullEmail()67   public void testServiceAccountNullEmail() {
68     Identity.serviceAccount(null);
69   }
70 
71   @Test
testGroup()72   public void testGroup() {
73     assertEquals(Identity.Type.GROUP, GROUP.getType());
74     assertEquals("group@gmail.com", GROUP.getValue());
75   }
76 
77   @Test(expected = NullPointerException.class)
testGroupNullEmail()78   public void testGroupNullEmail() {
79     Identity.group(null);
80   }
81 
82   @Test
testDomain()83   public void testDomain() {
84     assertEquals(Identity.Type.DOMAIN, DOMAIN.getType());
85     assertEquals("google.com", DOMAIN.getValue());
86   }
87 
88   @Test(expected = NullPointerException.class)
testDomainNullId()89   public void testDomainNullId() {
90     Identity.domain(null);
91   }
92 
93   @Test
testProjectOwner()94   public void testProjectOwner() {
95     assertEquals(Identity.Type.PROJECT_OWNER, PROJECT_OWNER.getType());
96     assertEquals("my-sample-project", PROJECT_OWNER.getValue());
97   }
98 
99   @Test(expected = NullPointerException.class)
testProjectOwnerNullId()100   public void testProjectOwnerNullId() {
101     Identity.projectOwner(null);
102   }
103 
104   @Test
testProjectEditor()105   public void testProjectEditor() {
106     assertEquals(Identity.Type.PROJECT_EDITOR, PROJECT_EDITOR.getType());
107     assertEquals("my-sample-project", PROJECT_EDITOR.getValue());
108   }
109 
110   @Test(expected = NullPointerException.class)
testProjectEditorNullId()111   public void testProjectEditorNullId() {
112     Identity.projectEditor(null);
113   }
114 
115   @Test
testProjectViewer()116   public void testProjectViewer() {
117     assertEquals(Identity.Type.PROJECT_VIEWER, PROJECT_VIEWER.getType());
118     assertEquals("my-sample-project", PROJECT_VIEWER.getValue());
119   }
120 
121   @Test(expected = NullPointerException.class)
testProjectViewerNullId()122   public void testProjectViewerNullId() {
123     Identity.projectViewer(null);
124   }
125 
126   @Test
testIdentityToAndFromPb()127   public void testIdentityToAndFromPb() {
128     compareIdentities(ALL_USERS, Identity.valueOf(ALL_USERS.strValue()));
129     compareIdentities(ALL_AUTH_USERS, Identity.valueOf(ALL_AUTH_USERS.strValue()));
130     compareIdentities(USER, Identity.valueOf(USER.strValue()));
131     compareIdentities(SERVICE_ACCOUNT, Identity.valueOf(SERVICE_ACCOUNT.strValue()));
132     compareIdentities(GROUP, Identity.valueOf(GROUP.strValue()));
133     compareIdentities(DOMAIN, Identity.valueOf(DOMAIN.strValue()));
134     compareIdentities(PROJECT_OWNER, Identity.valueOf(PROJECT_OWNER.strValue()));
135     compareIdentities(PROJECT_EDITOR, Identity.valueOf(PROJECT_EDITOR.strValue()));
136     compareIdentities(PROJECT_VIEWER, Identity.valueOf(PROJECT_VIEWER.strValue()));
137   }
138 
139   @Test(expected = IllegalArgumentException.class)
testValueOfEmpty()140   public void testValueOfEmpty() {
141     Identity.valueOf("");
142   }
143 
144   @Test
testUnrecognizedToString()145   public void testUnrecognizedToString() {
146     assertEquals("a:b", Identity.valueOf("a:b").strValue());
147   }
148 
149   @Test
testValueOfThreePart()150   public void testValueOfThreePart() {
151     Identity identity = Identity.valueOf("a:b:c");
152     assertEquals("A", identity.getType().name());
153     assertEquals("b:c", identity.getValue());
154   }
155 
compareIdentities(Identity expected, Identity actual)156   private void compareIdentities(Identity expected, Identity actual) {
157     assertEquals(expected, actual);
158     assertEquals(expected.getType(), actual.getType());
159     assertEquals(expected.getValue(), actual.getValue());
160   }
161 }
162