10
10
import io .grpc .Metadata .Key ;
11
11
import io .grpc .Status ;
12
12
import io .grpc .StatusException ;
13
+ import io .grpc .StatusRuntimeException ;
13
14
import java .util .Map ;
14
15
import java .util .Optional ;
15
16
import java .util .Set ;
16
17
import org .junit .jupiter .api .Test ;
18
+ import org .junit .jupiter .api .extension .ExtendWith ;
19
+ import org .mockito .junit .jupiter .MockitoExtension ;
17
20
21
+ @ ExtendWith (MockitoExtension .class )
18
22
class ContextualExceptionDetailsTest {
19
23
static final String TEST_REQUEST_ID = "example-request-id" ;
20
24
static final String TEST_TENANT = "example-tenant" ;
@@ -86,7 +90,8 @@ void parsesMetadataWithoutMessage() {
86
90
@ Test
87
91
void parsesMetadataWithMessage () {
88
92
assertEquals (
89
- Optional .of (new ContextualExceptionDetails (TEST_CONTEXT , "test message" )),
93
+ Optional .of (
94
+ new ContextualExceptionDetails (TEST_CONTEXT ).withExternalMessage ("test message" )),
90
95
ContextualExceptionDetails .fromMetadata (
91
96
metadataFromMap (
92
97
Map .of (
@@ -98,6 +103,44 @@ void parsesMetadataWithMessage() {
98
103
"test message" ))));
99
104
}
100
105
106
+ @ Test
107
+ void buildsFromExistingException () {
108
+ StatusRuntimeException exception =
109
+ ContextualStatusExceptionBuilder .from (
110
+ Status .INVALID_ARGUMENT
111
+ .withDescription ("test message" )
112
+ .asException (TEST_CONTEXT .buildTrailers ()))
113
+ .useStatusDescriptionAsExternalMessage ()
114
+ .buildRuntimeException ();
115
+
116
+ assertEquals (
117
+ Set .of (REQUEST_ID_HEADER_KEY , TENANT_ID_HEADER_KEY , EXTERNAL_MESSAGE_KEY .originalName ()),
118
+ exception .getTrailers ().keys ());
119
+ assertEquals (TEST_CONTEXT , RequestContext .fromMetadata (exception .getTrailers ()));
120
+ assertEquals ("test message" , exception .getTrailers ().get (EXTERNAL_MESSAGE_KEY ));
121
+ }
122
+
123
+ @ Test
124
+ void buildsFromExceptionWithCustomTrailers () {
125
+ Key <String > customKey = Key .of ("test" , ASCII_STRING_MARSHALLER );
126
+ Metadata customMetadata = new Metadata ();
127
+ customMetadata .put (customKey , "test-value" );
128
+ customMetadata .put (EXTERNAL_MESSAGE_KEY , "should be ignored" );
129
+ StatusException exception =
130
+ ContextualStatusExceptionBuilder .from (
131
+ Status .INVALID_ARGUMENT
132
+ .withDescription ("test message" )
133
+ .asRuntimeException (customMetadata ))
134
+ .withExternalMessage ("custom message" )
135
+ .buildCheckedException ();
136
+
137
+ assertEquals (
138
+ Set .of (customKey .originalName (), EXTERNAL_MESSAGE_KEY .originalName ()),
139
+ exception .getTrailers ().keys ());
140
+ assertEquals ("custom message" , exception .getTrailers ().get (EXTERNAL_MESSAGE_KEY ));
141
+ assertEquals ("test-value" , exception .getTrailers ().get (customKey ));
142
+ }
143
+
101
144
Metadata metadataFromMap (Map <String , String > metadataMap ) {
102
145
Metadata metadata = new Metadata ();
103
146
metadataMap .forEach ((key , value ) -> metadata .put (Key .of (key , ASCII_STRING_MARSHALLER ), value ));
0 commit comments