1
+ /*
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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
+ * A copy of the License is located at
7
+ *
8
+ * http://aws.amazon.com/apache2.0
9
+ *
10
+ * or in the "license" file accompanying this file. This file is distributed
11
+ * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12
+ * express or implied. See the License for the specific language governing
13
+ * permissions and limitations under the License.
14
+ */
15
+
16
+ package software .amazon .awssdk .transfer .s3 .internal ;
17
+
18
+ import static org .mockito .Mockito .when ;
19
+
20
+ import org .assertj .core .api .Assertions ;
21
+ import org .junit .Test ;
22
+ import org .junit .runner .RunWith ;
23
+ import org .mockito .Mock ;
24
+ import org .mockito .runners .MockitoJUnitRunner ;
25
+ import software .amazon .awssdk .core .exception .SdkClientException ;
26
+ import software .amazon .awssdk .core .exception .SdkServiceException ;
27
+ import software .amazon .awssdk .crt .CrtRuntimeException ;
28
+ import software .amazon .awssdk .crt .s3 .CrtS3RuntimeException ;
29
+ import software .amazon .awssdk .services .s3 .model .BucketAlreadyExistsException ;
30
+ import software .amazon .awssdk .services .s3 .model .InvalidObjectStateException ;
31
+
32
+ @ RunWith (MockitoJUnitRunner .class )
33
+ public class CrtErrorHandlerTest {
34
+
35
+ @ Mock
36
+ private CrtS3RuntimeException mockCrtS3RuntimeException ;
37
+
38
+ @ Test
39
+ public void crtS3ExceptionAreTransformed (){
40
+ CrtErrorHandler crtErrorHandler = new CrtErrorHandler ();
41
+ when (mockCrtS3RuntimeException .getAwsErrorCode ()).thenReturn ("BucketAlreadyExists" );
42
+ when (mockCrtS3RuntimeException .getAwsErrorMessage ()).thenReturn ("Bucket Already Exists" );
43
+ when (mockCrtS3RuntimeException .getStatusCode ()).thenReturn (404 );
44
+ Exception transformException = crtErrorHandler .transformException (mockCrtS3RuntimeException );
45
+ Assertions .assertThat (transformException ).isInstanceOf (BucketAlreadyExistsException .class );
46
+ Assertions .assertThat (transformException .getMessage ()).contains ("Bucket Already Exists" );
47
+ }
48
+
49
+ @ Test
50
+ public void nonCrtS3ExceptionAreNotTransformed (){
51
+ CrtErrorHandler crtErrorHandler = new CrtErrorHandler ();
52
+ Exception transformException = crtErrorHandler .transformException (new CrtRuntimeException ("AWS_ERROR" ));
53
+ Assertions .assertThat (transformException ).isInstanceOf (SdkClientException .class );
54
+ }
55
+
56
+
57
+ @ Test
58
+ public void crtS3ExceptionAreTransformedWhenExceptionIsInCause (){
59
+ CrtErrorHandler crtErrorHandler = new CrtErrorHandler ();
60
+ when (mockCrtS3RuntimeException .getAwsErrorCode ()).thenReturn ("InvalidObjectState" );
61
+ when (mockCrtS3RuntimeException .getAwsErrorMessage ()).thenReturn ("Invalid Object State" );
62
+ when (mockCrtS3RuntimeException .getStatusCode ()).thenReturn (404 );
63
+ final Exception transformException = crtErrorHandler .transformException (new Exception ("Some Exception" , mockCrtS3RuntimeException ));
64
+
65
+ System .out .println ("transformException " +transformException );
66
+
67
+ Assertions .assertThat (transformException ).isInstanceOf (InvalidObjectStateException .class );
68
+ Assertions .assertThat (transformException .getMessage ()).contains ("Invalid Object State" );
69
+ Assertions .assertThat (transformException .getCause ()).isInstanceOf (CrtS3RuntimeException .class );
70
+ }
71
+
72
+ @ Test
73
+ public void nonCrtS3ExceptionAreNotTransformedWhenExceptionIsInCause (){
74
+ CrtErrorHandler crtErrorHandler = new CrtErrorHandler ();
75
+ final Exception crtRuntimeException = new Exception ("Some Exception" , new CrtRuntimeException ("AWS_ERROR" ));
76
+ Exception transformException = crtErrorHandler .transformException (
77
+ crtRuntimeException );
78
+ Assertions .assertThat (transformException ).isNotInstanceOf (CrtRuntimeException .class );
79
+ Assertions .assertThat (transformException ).isInstanceOf (SdkClientException .class );
80
+ Assertions .assertThat (transformException .getMessage ()).isEqualTo ("Some Exception" );
81
+ Assertions .assertThat (transformException .getCause ()).isEqualTo (crtRuntimeException );
82
+ }
83
+
84
+ @ Test
85
+ public void crtS3ExceptionWithErrorCodeNodeNotInS3Model () {
86
+ CrtErrorHandler crtErrorHandler = new CrtErrorHandler ();
87
+ when (mockCrtS3RuntimeException .getAwsErrorCode ()).thenReturn ("NewS3ExceptionFromCrt" );
88
+ when (mockCrtS3RuntimeException .getAwsErrorMessage ()).thenReturn ("New S3 Exception From Crt" );
89
+ when (mockCrtS3RuntimeException .getStatusCode ()).thenReturn (404 );
90
+ Exception transformException = crtErrorHandler .transformException (mockCrtS3RuntimeException );
91
+ Assertions .assertThat (transformException ).isInstanceOf (SdkServiceException .class );
92
+ Assertions .assertThat (transformException .getCause ()).isEqualTo (mockCrtS3RuntimeException );
93
+ Assertions .assertThat (transformException .getMessage ()).isEqualTo (mockCrtS3RuntimeException .getMessage ());
94
+ Assertions .assertThat (((SdkServiceException )transformException ).statusCode ())
95
+ .isEqualTo (mockCrtS3RuntimeException .getStatusCode ());
96
+ }
97
+
98
+ @ Test
99
+ public void crtS3ExceptionInCauseWithErrorCodeNodeNotInS3Model () {
100
+ CrtErrorHandler crtErrorHandler = new CrtErrorHandler ();
101
+ when (mockCrtS3RuntimeException .getAwsErrorCode ()).thenReturn ("NewS3ExceptionFromCrt" );
102
+ when (mockCrtS3RuntimeException .getAwsErrorMessage ()).thenReturn ("New S3 Exception From Crt" );
103
+ when (mockCrtS3RuntimeException .getStatusCode ()).thenReturn (404 );
104
+ final Exception crtRuntimeException = new Exception (mockCrtS3RuntimeException );
105
+ Exception transformException = crtErrorHandler .transformException (crtRuntimeException );
106
+ Assertions .assertThat (transformException ).isInstanceOf (SdkServiceException .class );
107
+ Assertions .assertThat (transformException .getCause ()).isEqualTo (mockCrtS3RuntimeException );
108
+ Assertions .assertThat (transformException .getMessage ()).isEqualTo (mockCrtS3RuntimeException .getMessage ());
109
+ Assertions .assertThat (((SdkServiceException ) transformException ).statusCode ()).isEqualTo (404 );
110
+ }
111
+ }
0 commit comments