31
31
public class AwsServiceExceptionSerializationTest {
32
32
33
33
@ Test
34
- public void serializeServiceException () throws Exception {
34
+ public void serializeBasicServiceException () throws Exception {
35
35
AwsServiceException expectedException = createException ();
36
-
37
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
38
- ObjectOutputStream objectOutputStream = new ObjectOutputStream (outputStream );
39
- objectOutputStream .writeObject (expectedException );
40
- objectOutputStream .flush ();
41
- objectOutputStream .close ();
42
-
43
- ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (outputStream .toByteArray ()));
44
- AwsServiceException resultException = (AwsServiceException ) ois .readObject ();
45
-
36
+ AwsServiceException resultException = serializeServiceException (expectedException );
46
37
assertSameValues (resultException , expectedException );
47
38
}
48
39
49
- private void assertSameValues (AwsServiceException resultException , AwsServiceException expectedException ) {
50
- assertThat (resultException .getMessage ()).isEqualTo (expectedException .getMessage ());
51
- assertThat (resultException .requestId ()).isEqualTo (expectedException .requestId ());
52
- assertThat (resultException .extendedRequestId ()).isEqualTo (expectedException .extendedRequestId ());
53
- assertThat (resultException .toBuilder ().clockSkew ()).isEqualTo (expectedException .toBuilder ().clockSkew ());
54
- assertThat (resultException .toBuilder ().cause ().getMessage ()).isEqualTo (expectedException .toBuilder ().cause ().getMessage ());
55
- assertThat (resultException .awsErrorDetails ()).isEqualTo (expectedException .awsErrorDetails ());
40
+ @ Test
41
+ public void serializeRetryableServiceException () throws Exception {
42
+ AwsServiceException expectedException = createRetryableServiceException ();
43
+ AwsServiceException resultException = serializeServiceException (expectedException );
44
+ assertSameValues (resultException , expectedException );
56
45
}
57
46
47
+ private void assertSameValues (AwsServiceException result , AwsServiceException expected ) {
48
+ assertThat (result .getMessage ()).isEqualTo (expected .getMessage ());
49
+ assertThat (result .requestId ()).isEqualTo (expected .requestId ());
50
+ assertThat (result .extendedRequestId ()).isEqualTo (expected .extendedRequestId ());
51
+ assertThat (result .toBuilder ().clockSkew ()).isEqualTo (expected .toBuilder ().clockSkew ());
52
+ assertThat (result .toBuilder ().cause ().getMessage ()).isEqualTo (expected .toBuilder ().cause ().getMessage ());
53
+ assertThat (result .awsErrorDetails ()).isEqualTo (expected .awsErrorDetails ());
54
+ assertThat (result .numAttempts ()).isEqualTo (expected .numAttempts ());
55
+ }
58
56
59
57
private AwsServiceException createException () {
60
- AbortableInputStream contentStream = AbortableInputStream .create (new StringInputStream ("some content" ));
61
- SdkHttpResponse httpResponse = SdkHttpFullResponse .builder ()
62
- .statusCode (403 )
63
- .statusText ("SomeText" )
64
- .content (contentStream )
65
- .build ();
66
-
67
- AwsErrorDetails errorDetails = AwsErrorDetails .builder ()
68
- .errorCode ("someCode" )
69
- .errorMessage ("message" )
70
- .serviceName ("someService" )
71
- .sdkHttpResponse (httpResponse )
72
- .build ();
73
-
74
58
return AwsServiceException .builder ()
75
- .awsErrorDetails (errorDetails )
59
+ .awsErrorDetails (createErrorDetails ( 403 , "SomeText" ) )
76
60
.statusCode (403 )
77
61
.cause (new RuntimeException ("someThrowable" ))
78
62
.clockSkew (Duration .ofSeconds (2 ))
@@ -81,4 +65,44 @@ private AwsServiceException createException() {
81
65
.message ("message" )
82
66
.build ();
83
67
}
68
+
69
+ private AwsServiceException createRetryableServiceException () {
70
+ return AwsServiceException .builder ()
71
+ .awsErrorDetails (createErrorDetails (429 , "Throttling" ))
72
+ .statusCode (429 )
73
+ .cause (new RuntimeException ("someThrowable" ))
74
+ .clockSkew (Duration .ofSeconds (2 ))
75
+ .requestId ("requestId" )
76
+ .extendedRequestId ("extendedRequestId" )
77
+ .message ("message" )
78
+ .numAttempts (3 )
79
+ .build ();
80
+ }
81
+
82
+ private AwsErrorDetails createErrorDetails (int statusCode , String statusText ) {
83
+ AbortableInputStream contentStream = AbortableInputStream .create (new StringInputStream ("some content" ));
84
+ SdkHttpResponse httpResponse = SdkHttpFullResponse .builder ()
85
+ .statusCode (statusCode )
86
+ .statusText (statusText )
87
+ .content (contentStream )
88
+ .build ();
89
+
90
+ return AwsErrorDetails .builder ()
91
+ .errorCode ("someCode" )
92
+ .errorMessage ("message" )
93
+ .serviceName ("someService" )
94
+ .sdkHttpResponse (httpResponse )
95
+ .build ();
96
+ }
97
+
98
+ private AwsServiceException serializeServiceException (AwsServiceException exception ) throws Exception {
99
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
100
+ ObjectOutputStream objectOutputStream = new ObjectOutputStream (outputStream );
101
+ objectOutputStream .writeObject (exception );
102
+ objectOutputStream .flush ();
103
+ objectOutputStream .close ();
104
+
105
+ ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (outputStream .toByteArray ()));
106
+ return (AwsServiceException ) ois .readObject ();
107
+ }
84
108
}
0 commit comments