1919
2020import com .google .api .client .googleapis .json .GoogleJsonError ;
2121import 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 ;
2327import org .junit .After ;
2428import org .junit .AfterClass ;
2529import org .junit .Before ;
2832import org .junit .runner .RunWith ;
2933import org .junit .runners .JUnit4 ;
3034
31- import java .io .ByteArrayOutputStream ;
32- import java .io .PrintStream ;
33- import java .util .regex .Matcher ;
34- import java .util .regex .Pattern ;
35-
3635/**
3736 * Integration (system) tests for {@link Snippets}.
3837 */
@@ -43,7 +42,7 @@ public class SnippetsIT {
4342 static final String PROJECT_ID = System .getenv ("GOOGLE_CLOUD_PROJECT" );
4443 static final String LOCATION_ID = "global" ;
4544 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 () ;
4746 static final String TEST_USER = "serviceAccount:"
4847 + "131304031188-compute@developer.gserviceaccount.com" ;
4948 static final String TEST_ROLE = "roles/viewer" ;
@@ -64,7 +63,7 @@ public static void setUpClass() throws Exception {
6463 // Since you can't delete keyrings & cryptokeys atm, these tests assume they already exist.
6564 // Use the snippets functions to create them.
6665 try {
67- Snippets .createKeyRing (PROJECT_ID ,LOCATION_ID , KEY_RING_ID );
66+ Snippets .createKeyRing (PROJECT_ID , LOCATION_ID , KEY_RING_ID );
6867
6968 // Since there's no way to delete keyrings atm, have two branches - one for the first time the
7069 // test is run, one for after the key already exists
@@ -90,6 +89,17 @@ public static void setUpClass() throws Exception {
9089 assertThat (error .getMessage ()).contains (String .format (
9190 "keyRings/%s/cryptoKeys/%s" , KEY_RING_ID , CRYPTO_KEY_ID ));
9291 }
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 );
93103 }
94104
95105 /**
@@ -121,7 +131,8 @@ public static void tearDownClass() throws Exception {
121131 }
122132
123133 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 );
125136 }
126137 }
127138
@@ -130,8 +141,6 @@ public void setUp() throws Exception {
130141 bout = new ByteArrayOutputStream ();
131142 out = new PrintStream (bout );
132143 System .setOut (out );
133-
134- Snippets .createCryptoKeyVersion (PROJECT_ID , LOCATION_ID , KEY_RING_ID , CRYPTO_KEY_ID );
135144 }
136145
137146 @ After
@@ -165,7 +174,7 @@ public void listCryptoKeyVersions_printsVersions() throws Exception {
165174
166175 @ Test
167176 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 );
169178
170179 Matcher matcher = Pattern .compile (".*cryptoKeyVersions/(\\ d+)\" ,\" state\" :\" ENABLED\" .*" ,
171180 Pattern .DOTALL | Pattern .MULTILINE ).matcher (bout .toString ().trim ());
@@ -180,7 +189,7 @@ public void disableCryptoKeyVersion_disables() throws Exception {
180189
181190 @ Test
182191 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 );
184193
185194 Matcher matcher = Pattern .compile (".*cryptoKeyVersions/(\\ d+)\" ,\" state\" :\" ENABLED\" .*" ,
186195 Pattern .DOTALL | Pattern .MULTILINE ).matcher (bout .toString ().trim ());
@@ -195,6 +204,24 @@ public void destroyCryptoKeyVersion_destroys() throws Exception {
195204 KEY_RING_ID , CRYPTO_KEY_ID , version ));
196205 }
197206
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+
198225 @ Test
199226 public void addAndRemoveMemberToCryptoKeyPolicy_addsDisplaysAndRemoves () throws Exception {
200227 // Make sure the policy doesn't already have our test user
0 commit comments