@@ -241,7 +241,7 @@ public void publishEventContentTypeOverrideTest() {
241241 return false ;
242242 }
243243
244- if (!"\" hello\" " .equals (new String (publishEventRequest .getData ().toByteArray ()))) {
244+ if (!"hello" .equals (new String (publishEventRequest .getData ().toByteArray ()))) {
245245 return false ;
246246 }
247247 return true ;
@@ -254,6 +254,97 @@ public void publishEventContentTypeOverrideTest() {
254254 result .block ();
255255 }
256256
257+ @ Test
258+ public void publishEventTextContentTypeWithNumberTest () {
259+ doAnswer ((Answer <Void >) invocation -> {
260+ StreamObserver <Empty > observer = (StreamObserver <Empty >) invocation .getArguments ()[1 ];
261+ observer .onNext (Empty .getDefaultInstance ());
262+ observer .onCompleted ();
263+ return null ;
264+ }).when (daprStub ).publishEvent (ArgumentMatchers .argThat (publishEventRequest ->
265+ "text/plain" .equals (publishEventRequest .getDataContentType ())
266+ && "42" .equals (new String (publishEventRequest .getData ().toByteArray ()))), any ());
267+
268+ client .publishEvent (new PublishEventRequest ("pubsubname" , "topic" , 42 ).setContentType ("text/plain" )).block ();
269+ }
270+
271+ @ Test
272+ public void publishEventTextContentTypeWithObjectFallsBackToSerializerTest () {
273+ MyObject event = new MyObject (1 , "Event" );
274+ doAnswer ((Answer <Void >) invocation -> {
275+ StreamObserver <Empty > observer = (StreamObserver <Empty >) invocation .getArguments ()[1 ];
276+ observer .onNext (Empty .getDefaultInstance ());
277+ observer .onCompleted ();
278+ return null ;
279+ }).when (daprStub ).publishEvent (ArgumentMatchers .argThat (publishEventRequest -> {
280+ String data = new String (publishEventRequest .getData ().toByteArray ());
281+ return "text/plain" .equals (publishEventRequest .getDataContentType ())
282+ && ("{\" id\" :1,\" value\" :\" Event\" }" .equals (data ) || "{\" value\" :\" Event\" ,\" id\" :1}" .equals (data ));
283+ }), any ());
284+
285+ client .publishEvent (new PublishEventRequest ("pubsubname" , "topic" , event ).setContentType ("text/plain" )).block ();
286+ }
287+
288+ @ Test
289+ public void publishEventTextContentTypeWithBooleanTest () {
290+ doAnswer ((Answer <Void >) invocation -> {
291+ StreamObserver <Empty > observer = (StreamObserver <Empty >) invocation .getArguments ()[1 ];
292+ observer .onNext (Empty .getDefaultInstance ());
293+ observer .onCompleted ();
294+ return null ;
295+ }).when (daprStub ).publishEvent (ArgumentMatchers .argThat (publishEventRequest ->
296+ "text/plain" .equals (publishEventRequest .getDataContentType ())
297+ && "true" .equals (new String (publishEventRequest .getData ().toByteArray ()))), any ());
298+
299+ client .publishEvent (new PublishEventRequest ("pubsubname" , "topic" , true ).setContentType ("text/plain" )).block ();
300+ }
301+
302+ @ Test
303+ public void publishEventNonTextContentTypeWithStringTest () {
304+ doAnswer ((Answer <Void >) invocation -> {
305+ StreamObserver <Empty > observer = (StreamObserver <Empty >) invocation .getArguments ()[1 ];
306+ observer .onNext (Empty .getDefaultInstance ());
307+ observer .onCompleted ();
308+ return null ;
309+ }).when (daprStub ).publishEvent (ArgumentMatchers .argThat (publishEventRequest ->
310+ "application/json" .equals (publishEventRequest .getDataContentType ())
311+ && "\" hello\" " .equals (new String (publishEventRequest .getData ().toByteArray ()))), any ());
312+
313+ client .publishEvent (
314+ new PublishEventRequest ("pubsubname" , "topic" , "hello" ).setContentType ("application/json" )).block ();
315+ }
316+
317+ @ Test
318+ public void publishEventTextContentTypeWithCustomSerializerTest () throws IOException {
319+ DaprObjectSerializer mockSerializer = mock (DaprObjectSerializer .class );
320+ when (mockSerializer .serialize ("hello" )).thenReturn ("custom" .getBytes ());
321+ client = new DaprClientImpl (channel , daprStub , daprHttp , mockSerializer , new DefaultObjectSerializer ());
322+ doAnswer ((Answer <Void >) invocation -> {
323+ StreamObserver <Empty > observer = (StreamObserver <Empty >) invocation .getArguments ()[1 ];
324+ observer .onNext (Empty .getDefaultInstance ());
325+ observer .onCompleted ();
326+ return null ;
327+ }).when (daprStub ).publishEvent (ArgumentMatchers .argThat (publishEventRequest ->
328+ "text/plain" .equals (publishEventRequest .getDataContentType ())
329+ && "custom" .equals (new String (publishEventRequest .getData ().toByteArray ()))), any ());
330+
331+ client .publishEvent (new PublishEventRequest ("pubsubname" , "topic" , "hello" ).setContentType ("text/plain" )).block ();
332+ }
333+
334+ @ Test
335+ public void publishEventStringWithoutContentTypeTest () {
336+ doAnswer ((Answer <Void >) invocation -> {
337+ StreamObserver <Empty > observer = (StreamObserver <Empty >) invocation .getArguments ()[1 ];
338+ observer .onNext (Empty .getDefaultInstance ());
339+ observer .onCompleted ();
340+ return null ;
341+ }).when (daprStub ).publishEvent (ArgumentMatchers .argThat (publishEventRequest ->
342+ "application/json" .equals (publishEventRequest .getDataContentType ())
343+ && "\" hello\" " .equals (new String (publishEventRequest .getData ().toByteArray ()))), any ());
344+
345+ client .publishEvent ("pubsubname" , "topic" , "hello" ).block ();
346+ }
347+
257348 @ Test
258349 public void invokeBindingIllegalArgumentExceptionTest () {
259350 assertThrows (IllegalArgumentException .class , () -> {
0 commit comments