@@ -37,16 +37,9 @@ public void IsCloudEvent(string headerName, string headerValue, bool expectedRes
37
37
public void IsCloudEvent_NoHeaders ( ) =>
38
38
Assert . False ( new Message < string ? , byte [ ] > ( ) . IsCloudEvent ( ) ) ;
39
39
40
- [ Fact ]
41
- public void KafkaStructuredMessageTest ( )
40
+ private static CloudEvent CreateTestCloudEvent ( )
42
41
{
43
- // Kafka doesn't provide any way to get to the message transport level to do the test properly
44
- // and it doesn't have an embedded version of a server for .Net so the lowest we can get is
45
- // the `Message<T, K>`
46
-
47
- var jsonEventFormatter = new JsonEventFormatter ( ) ;
48
-
49
- var cloudEvent = new CloudEvent
42
+ return new CloudEvent
50
43
{
51
44
Type = "com.github.pull.create" ,
52
45
Source = new Uri ( "https://github.com/cloudevents/spec/pull" ) ,
@@ -55,21 +48,12 @@ public void KafkaStructuredMessageTest()
55
48
Time = new DateTimeOffset ( 2018 , 4 , 5 , 17 , 31 , 0 , TimeSpan . Zero ) ,
56
49
DataContentType = MediaTypeNames . Text . Xml ,
57
50
Data = "<much wow=\" xml\" />" ,
58
- [ "comexampleextension1" ] = "value"
51
+ [ "comexampleextension1" ] = "value" ,
59
52
} ;
53
+ }
60
54
61
- var message = cloudEvent . ToKafkaMessage ( ContentMode . Structured , new JsonEventFormatter ( ) ) ;
62
-
63
- Assert . True ( message . IsCloudEvent ( ) ) ;
64
-
65
- // Using serialization to create fully independent copy thus simulating message transport.
66
- // The real transport will work in a similar way.
67
- var serialized = JsonConvert . SerializeObject ( message , new HeaderConverter ( ) ) ;
68
- var messageCopy = JsonConvert . DeserializeObject < Message < string ? , byte [ ] > > ( serialized , new HeadersConverter ( ) , new HeaderConverter ( ) ) ! ;
69
-
70
- Assert . True ( messageCopy . IsCloudEvent ( ) ) ;
71
- var receivedCloudEvent = messageCopy . ToCloudEvent ( jsonEventFormatter ) ;
72
-
55
+ private static void VerifyTestCloudEvent ( CloudEvent receivedCloudEvent )
56
+ {
73
57
Assert . Equal ( CloudEventsSpecVersion . Default , receivedCloudEvent . SpecVersion ) ;
74
58
Assert . Equal ( "com.github.pull.create" , receivedCloudEvent . Type ) ;
75
59
Assert . Equal ( new Uri ( "https://github.com/cloudevents/spec/pull" ) , receivedCloudEvent . Source ) ;
@@ -82,6 +66,109 @@ public void KafkaStructuredMessageTest()
82
66
Assert . Equal ( "value" , ( string ? ) receivedCloudEvent [ "comexampleextension1" ] ) ;
83
67
}
84
68
69
+ private static Message < TKey , byte [ ] > ? SimulateMessageTransport < TKey > ( Message < TKey , byte [ ] > message )
70
+ {
71
+ // Using serialization to create fully independent copy thus simulating message transport.
72
+ // The real transport will work in a similar way.
73
+ var serialized = JsonConvert . SerializeObject ( message , new HeaderConverter ( ) ) ;
74
+ var messageCopy = JsonConvert . DeserializeObject < Message < TKey , byte [ ] > > ( serialized , new HeadersConverter ( ) , new HeaderConverter ( ) ) ! ;
75
+ return messageCopy ;
76
+ }
77
+
78
+ [ Fact ]
79
+ public void KafkaStructuredMessageTest ( )
80
+ {
81
+ // Kafka doesn't provide any way to get to the message transport level to do the test properly
82
+ // and it doesn't have an embedded version of a server for .Net so the lowest we can get is
83
+ // the `Message<T, K>`
84
+
85
+ var jsonEventFormatter = new JsonEventFormatter ( ) ;
86
+ var key = "Test" ;
87
+ var cloudEvent = CreateTestCloudEvent ( ) ;
88
+ cloudEvent [ Partitioning . PartitionKeyAttribute ] = key ;
89
+
90
+ var message = cloudEvent . ToKafkaMessage ( ContentMode . Structured , jsonEventFormatter ) ;
91
+
92
+ Assert . True ( message . IsCloudEvent ( ) ) ;
93
+
94
+ var messageCopy = SimulateMessageTransport ( message ) ;
95
+
96
+ Assert . NotNull ( messageCopy ) ;
97
+ Assert . Equal ( key , messageCopy . Key ) ;
98
+ Assert . True ( messageCopy . IsCloudEvent ( ) ) ;
99
+ var receivedCloudEvent = messageCopy . ToCloudEvent ( jsonEventFormatter , null ) ;
100
+
101
+ VerifyTestCloudEvent ( receivedCloudEvent ) ;
102
+ }
103
+
104
+ [ Fact ]
105
+ public void KafkaBinaryGuidKeyedStructuredMessageTest ( )
106
+ {
107
+ // In order to test the most extreme case of key management we will simulate
108
+ // using Guid Keys serialized in their binary form in kafka that are converted
109
+ // back to their string representation in the cloudEvent.
110
+ var partitionKeyAdapter = new PartitionKeyAdapters . BinaryGuidPartitionKeyAdapter ( ) ;
111
+ var jsonEventFormatter = new JsonEventFormatter ( ) ;
112
+ var key = Guid . NewGuid ( ) ;
113
+ var cloudEvent = CreateTestCloudEvent ( ) ;
114
+ cloudEvent [ Partitioning . PartitionKeyAttribute ] = key . ToString ( ) ;
115
+
116
+ var message = cloudEvent . ToKafkaMessage < byte [ ] ? > (
117
+ ContentMode . Structured ,
118
+ jsonEventFormatter ,
119
+ partitionKeyAdapter ) ;
120
+
121
+ Assert . True ( message . IsCloudEvent ( ) ) ;
122
+
123
+ var messageCopy = SimulateMessageTransport ( message ) ;
124
+
125
+ Assert . NotNull ( messageCopy ) ;
126
+ Assert . True ( messageCopy . IsCloudEvent ( ) ) ;
127
+
128
+ var receivedCloudEvent = messageCopy . ToCloudEvent < byte [ ] ? > (
129
+ jsonEventFormatter ,
130
+ null ,
131
+ partitionKeyAdapter ) ;
132
+
133
+ Assert . NotNull ( message . Key ) ;
134
+ // The key should be the original Guid in the binary representation.
135
+ Assert . Equal ( key , new Guid ( messageCopy . Key ! ) ) ;
136
+ VerifyTestCloudEvent ( receivedCloudEvent ) ;
137
+ }
138
+
139
+ [ Fact ]
140
+ public void KafkaNullKeyedStructuredMessageTest ( )
141
+ {
142
+ // It will test the serialization using Confluent's Confluent.Kafka.Null type for the key.
143
+ // As the default behavior without adapter is to skip the key it will work properly.
144
+ var partitionKeyAdapter = new PartitionKeyAdapters . NullPartitionKeyAdapter < Confluent . Kafka . Null > ( ) ;
145
+ var jsonEventFormatter = new JsonEventFormatter ( ) ;
146
+ var cloudEvent = CreateTestCloudEvent ( ) ;
147
+ // Even if the key is established in the cloud event it won't flow.
148
+ cloudEvent [ Partitioning . PartitionKeyAttribute ] = "Test" ;
149
+
150
+ var message = cloudEvent . ToKafkaMessage < Confluent . Kafka . Null > (
151
+ ContentMode . Structured ,
152
+ jsonEventFormatter ,
153
+ partitionKeyAdapter ) ;
154
+
155
+ Assert . True ( message . IsCloudEvent ( ) ) ;
156
+
157
+ var messageCopy = SimulateMessageTransport ( message ) ;
158
+
159
+ Assert . NotNull ( messageCopy ) ;
160
+ Assert . True ( messageCopy . IsCloudEvent ( ) ) ;
161
+
162
+ var receivedCloudEvent = messageCopy . ToCloudEvent < Confluent . Kafka . Null > (
163
+ jsonEventFormatter ,
164
+ null ,
165
+ partitionKeyAdapter ) ;
166
+
167
+ //The Message key will continue to be null.
168
+ Assert . Null ( message . Key ) ;
169
+ VerifyTestCloudEvent ( receivedCloudEvent ) ;
170
+ }
171
+
85
172
[ Fact ]
86
173
public void KafkaBinaryMessageTest ( )
87
174
{
0 commit comments