3333import com .google .cloud .storage .HmacKey .HmacKeyState ;
3434import com .google .cloud .storage .ServiceAccount ;
3535import com .google .cloud .storage .Storage ;
36- import com .google .cloud .storage .testing .RemoteStorageHelper ;
37- import java .io .ByteArrayOutputStream ;
38- import java .io .FileDescriptor ;
39- import java .io .FileOutputStream ;
40- import java .io .PrintStream ;
36+ import com .google .cloud .testing .junit4 .MultipleAttemptsRule ;
4137import org .junit .Assert ;
4238import org .junit .Before ;
43- import org .junit .BeforeClass ;
39+ import org .junit .Rule ;
4440import org .junit .Test ;
4541
46- public class ITHmacSnippets {
42+ public class ITHmacSnippets extends TestBase {
4743 private static final String PROJECT_ID = ServiceOptions .getDefaultProjectId ();
4844 private static final String HMAC_KEY_TEST_SERVICE_ACCOUNT =
4945 PROJECT_ID + "@" + PROJECT_ID + ".iam.gserviceaccount.com" ;
50- private final PrintStream standardOut = new PrintStream (new FileOutputStream (FileDescriptor .out ));
51-
52- private static Storage storage ;
53-
54- @ BeforeClass
55- public static void beforeClass () {
56- RemoteStorageHelper helper = RemoteStorageHelper .create ();
57- storage =
58- helper
59- .getOptions ()
60- .toBuilder ()
61- .setRetrySettings (
62- helper
63- .getOptions ()
64- .getRetrySettings ()
65- .toBuilder ()
66- .setRetryDelayMultiplier (3.0 )
67- .build ())
68- .build ()
69- .getService ();
70- }
7146
7247 @ Before
7348 public void before () {
7449 cleanUpHmacKeys (ServiceAccount .of (HMAC_KEY_TEST_SERVICE_ACCOUNT ));
75-
76- // This is just in case any tests failed before they could reset the value
77- System .setOut (standardOut );
7850 }
7951
80- private static void cleanUpHmacKeys (ServiceAccount serviceAccount ) {
52+ @ Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule (5 );
53+
54+ private void cleanUpHmacKeys (ServiceAccount serviceAccount ) {
8155 Page <HmacKey .HmacKeyMetadata > metadatas =
8256 storage .listHmacKeys (Storage .ListHmacKeysOption .serviceAccount (serviceAccount ));
8357 for (HmacKey .HmacKeyMetadata hmacKeyMetadata : metadatas .iterateAll ()) {
@@ -92,11 +66,9 @@ private static void cleanUpHmacKeys(ServiceAccount serviceAccount) {
9266
9367 @ Test
9468 public void testCreateHmacKey () throws Exception {
95- final ByteArrayOutputStream snippetOutputCapture = new ByteArrayOutputStream ();
96- System .setOut (new PrintStream (snippetOutputCapture ));
69+ ;
9770 CreateHmacKey .createHmacKey (HMAC_KEY_TEST_SERVICE_ACCOUNT , PROJECT_ID );
98- String snippetOutput = snippetOutputCapture .toString ();
99- System .setOut (standardOut );
71+ String snippetOutput = stdOut .getCapturedOutputAsUtf8String ();
10072 String accessId = snippetOutput .split ("Access ID: " )[1 ].split ("\n " )[0 ];
10173 Thread .sleep (5000 );
10274 assertNotNull (storage .getHmacKey (accessId ));
@@ -105,13 +77,9 @@ public void testCreateHmacKey() throws Exception {
10577 @ Test
10678 public void testGetHmacKey () throws Exception {
10779 HmacKey hmacKey = storage .createHmacKey (ServiceAccount .of (HMAC_KEY_TEST_SERVICE_ACCOUNT ));
108-
109- final ByteArrayOutputStream snippetOutputCapture = new ByteArrayOutputStream ();
110- System .setOut (new PrintStream (snippetOutputCapture ));
11180 Thread .sleep (5000 );
11281 GetHmacKey .getHmacKey (hmacKey .getMetadata ().getAccessId (), PROJECT_ID );
113- String snippetOutput = snippetOutputCapture .toString ();
114- System .setOut (standardOut );
82+ String snippetOutput = stdOut .getCapturedOutputAsUtf8String ();
11583 Assert .assertTrue (snippetOutput .contains (HMAC_KEY_TEST_SERVICE_ACCOUNT ));
11684 }
11785
@@ -129,9 +97,8 @@ public void testActivateHmacKey() throws Exception {
12997 @ Test
13098 public void testDeactivateHmacKey () throws Exception {
13199 HmacKey hmacKey = storage .createHmacKey (ServiceAccount .of (HMAC_KEY_TEST_SERVICE_ACCOUNT ));
132-
133- DeactivateHmacKey .deactivateHmacKey (hmacKey .getMetadata ().getAccessId (), PROJECT_ID );
134100 Thread .sleep (5000 );
101+ DeactivateHmacKey .deactivateHmacKey (hmacKey .getMetadata ().getAccessId (), PROJECT_ID );
135102 assertEquals (
136103 HmacKeyState .INACTIVE , storage .getHmacKey (hmacKey .getMetadata ().getAccessId ()).getState ());
137104 }
@@ -158,12 +125,9 @@ public void testListHmacKeys() {
158125 ServiceAccount .of (HMAC_KEY_TEST_SERVICE_ACCOUNT ),
159126 Storage .CreateHmacKeyOption .projectId (PROJECT_ID ));
160127
161- final ByteArrayOutputStream snippetOutputCapture = new ByteArrayOutputStream ();
162- System .setOut (new PrintStream (snippetOutputCapture ));
163128 ListHmacKeys .listHmacKeys (PROJECT_ID );
164- String snippetOutput = snippetOutputCapture . toString ();
129+ String snippetOutput = stdOut . getCapturedOutputAsUtf8String ();
165130 assertTrue (snippetOutput .contains (one .getMetadata ().getAccessId ()));
166131 assertTrue (snippetOutput .contains (two .getMetadata ().getAccessId ()));
167- System .setOut (standardOut );
168132 }
169133}
0 commit comments