16
16
17
17
package org .springframework .cloud .function .context .catalog ;
18
18
19
+ import java .io .ByteArrayOutputStream ;
20
+ import java .io .IOException ;
19
21
import java .lang .reflect .Field ;
20
22
import java .lang .reflect .Type ;
21
23
import java .util .ArrayList ;
28
30
import java .util .concurrent .ExecutorService ;
29
31
import java .util .concurrent .Executors ;
30
32
import java .util .concurrent .atomic .AtomicInteger ;
33
+ import java .util .concurrent .atomic .AtomicReference ;
31
34
import java .util .function .BiConsumer ;
32
35
import java .util .function .Consumer ;
33
36
import java .util .function .Function ;
37
40
38
41
import com .fasterxml .jackson .databind .ObjectMapper ;
39
42
import com .google .gson .Gson ;
43
+ import com .google .protobuf .StringValue ;
40
44
import org .junit .jupiter .api .Assertions ;
41
45
import org .junit .jupiter .api .BeforeEach ;
42
46
import org .junit .jupiter .api .Disabled ;
72
76
import org .springframework .messaging .converter .ByteArrayMessageConverter ;
73
77
import org .springframework .messaging .converter .CompositeMessageConverter ;
74
78
import org .springframework .messaging .converter .MessageConverter ;
79
+ import org .springframework .messaging .converter .ProtobufMessageConverter ;
75
80
import org .springframework .messaging .converter .StringMessageConverter ;
76
81
import org .springframework .messaging .support .MessageBuilder ;
77
82
import org .springframework .util .MimeType ;
78
83
import org .springframework .util .ReflectionUtils ;
79
84
80
85
import static org .assertj .core .api .Assertions .assertThat ;
86
+ import static org .assertj .core .api .Assertions .assertThatNoException ;
81
87
82
88
/**
83
89
* @author Oleg Zhurakousky
@@ -97,6 +103,7 @@ public void before() {
97
103
messageConverters .add (new JsonMessageConverter (jsonMapper ));
98
104
messageConverters .add (new ByteArrayMessageConverter ());
99
105
messageConverters .add (new StringMessageConverter ());
106
+ messageConverters .add (new ProtobufMessageConverter ());
100
107
this .messageConverter = new SmartCompositeMessageConverter (messageConverters );
101
108
102
109
this .conversionService = new DefaultConversionService ();
@@ -207,6 +214,37 @@ void textContentTypeWithValueWrappedBracketsIsOk(String inputMessagePayloadValue
207
214
assertThat (functionResult ).isEqualTo (inputMessagePayloadValue );
208
215
}
209
216
217
+ @ ParameterizedTest
218
+ @ ValueSource (strings = {
219
+ "aaaaaaaaaa" , // no problem
220
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]" // protobuf encoder prepends '[' for length (91 bytes)
221
+ })
222
+ public void testSCF1094 (String stringValue ) throws IOException {
223
+
224
+ Function <StringValue , String > getValue = msg -> msg != null ? msg .getValue () : null ;
225
+ Type functionType = ResolvableType .forClassWithGenerics (Function .class , ResolvableType .forClass (StringValue .class ), ResolvableType .forClass (String .class )).getType ();
226
+
227
+ var catalog = new SimpleFunctionRegistry (this .conversionService , this .messageConverter , new JacksonMapper (new ObjectMapper ()));
228
+ catalog .register (new FunctionRegistration <>(getValue , "getValue" ).type (functionType ));
229
+ FunctionInvocationWrapper lookedUpFunction = catalog .lookup ("getValue" );
230
+
231
+ ByteArrayOutputStream payload = new ByteArrayOutputStream ();
232
+ StringValue .newBuilder ()
233
+ .setValue (stringValue )
234
+ .build ()
235
+ .writeTo (payload );
236
+
237
+ var inputMessage = MessageBuilder .withPayload (payload .toByteArray ())
238
+ .setHeader ("contentType" , "application/x-protobuf" )
239
+ .build ();
240
+
241
+ final AtomicReference <Object > result = new AtomicReference <>();
242
+ assertThatNoException ().isThrownBy (() -> {
243
+ result .set (lookedUpFunction .apply (inputMessage ));
244
+ });
245
+ assertThat (result .get ()).isEqualTo (stringValue );
246
+ }
247
+
210
248
@ SuppressWarnings ("unchecked" )
211
249
@ Test
212
250
public void testSCF762 () {
0 commit comments