19
19
20
20
import com .google .api .client .googleapis .json .GoogleJsonError ;
21
21
import com .google .api .client .googleapis .json .GoogleJsonResponseException ;
22
-
22
+ import java .io .ByteArrayOutputStream ;
23
+ import java .io .PrintStream ;
24
+ import java .util .UUID ;
25
+ import java .util .regex .Matcher ;
26
+ import java .util .regex .Pattern ;
23
27
import org .junit .After ;
24
28
import org .junit .AfterClass ;
25
29
import org .junit .Before ;
28
32
import org .junit .runner .RunWith ;
29
33
import org .junit .runners .JUnit4 ;
30
34
31
- import java .io .ByteArrayOutputStream ;
32
- import java .io .PrintStream ;
33
- import java .util .regex .Matcher ;
34
- import java .util .regex .Pattern ;
35
-
36
35
/**
37
36
* Integration (system) tests for {@link Snippets}.
38
37
*/
@@ -43,7 +42,7 @@ public class SnippetsIT {
43
42
static final String PROJECT_ID = System .getenv ("GOOGLE_CLOUD_PROJECT" );
44
43
static final String LOCATION_ID = "global" ;
45
44
static final String KEY_RING_ID = "test-snippets-key-ring" ;
46
- static final String CRYPTO_KEY_ID = "test-snippets-crypto-key" ;
45
+ static final String CRYPTO_KEY_ID = UUID . randomUUID (). toString () ;
47
46
static final String TEST_USER = "serviceAccount:"
48
47
+ "131304031188-compute@developer.gserviceaccount.com" ;
49
48
static final String TEST_ROLE = "roles/viewer" ;
@@ -64,7 +63,7 @@ public static void setUpClass() throws Exception {
64
63
// Since you can't delete keyrings & cryptokeys atm, these tests assume they already exist.
65
64
// Use the snippets functions to create them.
66
65
try {
67
- Snippets .createKeyRing (PROJECT_ID ,LOCATION_ID , KEY_RING_ID );
66
+ Snippets .createKeyRing (PROJECT_ID , LOCATION_ID , KEY_RING_ID );
68
67
69
68
// Since there's no way to delete keyrings atm, have two branches - one for the first time the
70
69
// test is run, one for after the key already exists
@@ -90,6 +89,17 @@ public static void setUpClass() throws Exception {
90
89
assertThat (error .getMessage ()).contains (String .format (
91
90
"keyRings/%s/cryptoKeys/%s" , KEY_RING_ID , CRYPTO_KEY_ID ));
92
91
}
92
+
93
+ // Create a CryptoKeyVersion and set it as primary.
94
+ Snippets .createCryptoKeyVersion (PROJECT_ID , LOCATION_ID , KEY_RING_ID , CRYPTO_KEY_ID );
95
+ Matcher matcher = Pattern .compile (
96
+ ".*cryptoKeyVersions/(\\ d+)\" ,\" state\" :\" ENABLED\" .*" ,
97
+ Pattern .DOTALL | Pattern .MULTILINE ).matcher (bout .toString ().trim ());
98
+ assertTrue (matcher .matches ());
99
+
100
+ String primaryVersion = matcher .group (1 );
101
+
102
+ Snippets .setPrimaryVersion (PROJECT_ID , LOCATION_ID , KEY_RING_ID , CRYPTO_KEY_ID , primaryVersion );
93
103
}
94
104
95
105
/**
@@ -121,7 +131,8 @@ public static void tearDownClass() throws Exception {
121
131
}
122
132
123
133
String version = matcher .group (1 );
124
- Snippets .destroyCryptoKeyVersion (PROJECT_ID , LOCATION_ID , KEY_RING_ID , CRYPTO_KEY_ID , version );
134
+ Snippets
135
+ .destroyCryptoKeyVersion (PROJECT_ID , LOCATION_ID , KEY_RING_ID , CRYPTO_KEY_ID , version );
125
136
}
126
137
}
127
138
@@ -130,8 +141,6 @@ public void setUp() throws Exception {
130
141
bout = new ByteArrayOutputStream ();
131
142
out = new PrintStream (bout );
132
143
System .setOut (out );
133
-
134
- Snippets .createCryptoKeyVersion (PROJECT_ID , LOCATION_ID , KEY_RING_ID , CRYPTO_KEY_ID );
135
144
}
136
145
137
146
@ After
@@ -165,7 +174,7 @@ public void listCryptoKeyVersions_printsVersions() throws Exception {
165
174
166
175
@ Test
167
176
public void disableCryptoKeyVersion_disables () throws Exception {
168
- Snippets .listCryptoKeyVersions (PROJECT_ID , LOCATION_ID , KEY_RING_ID , CRYPTO_KEY_ID );
177
+ Snippets .createCryptoKeyVersion (PROJECT_ID , LOCATION_ID , KEY_RING_ID , CRYPTO_KEY_ID );
169
178
170
179
Matcher matcher = Pattern .compile (".*cryptoKeyVersions/(\\ d+)\" ,\" state\" :\" ENABLED\" .*" ,
171
180
Pattern .DOTALL | Pattern .MULTILINE ).matcher (bout .toString ().trim ());
@@ -180,7 +189,7 @@ public void disableCryptoKeyVersion_disables() throws Exception {
180
189
181
190
@ Test
182
191
public void destroyCryptoKeyVersion_destroys () throws Exception {
183
- Snippets .listCryptoKeyVersions (PROJECT_ID , LOCATION_ID , KEY_RING_ID , CRYPTO_KEY_ID );
192
+ Snippets .createCryptoKeyVersion (PROJECT_ID , LOCATION_ID , KEY_RING_ID , CRYPTO_KEY_ID );
184
193
185
194
Matcher matcher = Pattern .compile (".*cryptoKeyVersions/(\\ d+)\" ,\" state\" :\" ENABLED\" .*" ,
186
195
Pattern .DOTALL | Pattern .MULTILINE ).matcher (bout .toString ().trim ());
@@ -195,6 +204,24 @@ public void destroyCryptoKeyVersion_destroys() throws Exception {
195
204
KEY_RING_ID , CRYPTO_KEY_ID , version ));
196
205
}
197
206
207
+ @ Test
208
+ public void setPrimaryVersion_createKeyAndSetPrimaryVersion () throws Exception {
209
+ // We can't test that setPrimaryVersion actually took effect via a list call because of
210
+ // caching. So we test that the call was successful.
211
+ Snippets .createCryptoKeyVersion (PROJECT_ID , LOCATION_ID , KEY_RING_ID , CRYPTO_KEY_ID );
212
+
213
+ Matcher matcher = Pattern .compile (".*cryptoKeyVersions/(\\ d+)\" ,\" state\" :\" ENABLED\" .*" ,
214
+ Pattern .DOTALL | Pattern .MULTILINE ).matcher (bout .toString ().trim ());
215
+ assertTrue (matcher .matches ());
216
+
217
+ String version = matcher .group (1 );
218
+
219
+ Snippets .setPrimaryVersion (PROJECT_ID , LOCATION_ID , KEY_RING_ID , CRYPTO_KEY_ID , version );
220
+ assertThat (bout .toString ()).containsMatch (String .format (
221
+ "primary.*keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s" ,
222
+ KEY_RING_ID , CRYPTO_KEY_ID , version ));
223
+ }
224
+
198
225
@ Test
199
226
public void addAndRemoveMemberToCryptoKeyPolicy_addsDisplaysAndRemoves () throws Exception {
200
227
// Make sure the policy doesn't already have our test user
0 commit comments