-
Notifications
You must be signed in to change notification settings - Fork 83
/
PartitioningTest.cs
73 lines (63 loc) · 2.54 KB
/
PartitioningTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2021 Cloud Native Foundation.
// Licensed under the Apache 2.0 license.
// See LICENSE file in the project root for full license information.
using CloudNative.CloudEvents.NewtonsoftJson;
using System.Text;
using Xunit;
using static CloudNative.CloudEvents.UnitTests.CloudEventFormatterExtensions;
namespace CloudNative.CloudEvents.Extensions.UnitTests
{
public class PartitioningTest
{
private static readonly string sampleJson = @"
{
'specversion' : '1.0',
'type' : 'com.github.pull.create',
'id' : 'A234-1234-1234',
'source' : '//event-source',
'partitionkey' : 'abc',
}".Replace('\'', '"');
[Fact]
public void ParseJson()
{
var jsonFormatter = new JsonEventFormatter();
var cloudEvent = jsonFormatter.DecodeStructuredModeText(sampleJson);
Assert.Equal("abc", cloudEvent["partitionkey"]);
Assert.Equal("abc", cloudEvent[Partitioning.PartitionKeyAttribute]);
Assert.Equal("abc", cloudEvent.GetPartitionKey());
}
[Fact]
public void Transcode()
{
var jsonFormatter = new JsonEventFormatter();
var cloudEvent1 = jsonFormatter.DecodeStructuredModeText(sampleJson);
var jsonData = jsonFormatter.EncodeStructuredModeMessage(cloudEvent1, out var contentType);
var cloudEvent = jsonFormatter.DecodeStructuredModeMessage(jsonData, contentType, null);
Assert.Equal("abc", cloudEvent["partitionkey"]);
Assert.Equal("abc", cloudEvent[Partitioning.PartitionKeyAttribute]);
Assert.Equal("abc", cloudEvent.GetPartitionKey());
}
[Fact]
public void SetPartitionKey()
{
var cloudEvent = new CloudEvent();
cloudEvent.SetPartitionKey("xyz");
Assert.Equal("xyz", cloudEvent["partitionkey"]);
Assert.Equal("xyz", cloudEvent[Partitioning.PartitionKeyAttribute]);
cloudEvent.SetPartitionKey(null);
Assert.Null(cloudEvent["partitionkey"]);
Assert.Null(cloudEvent[Partitioning.PartitionKeyAttribute]);
}
[Fact]
public void GetPartitionKey()
{
var cloudEvent = new CloudEvent
{
["partitionkey"] = "xyz"
};
Assert.Equal("xyz", cloudEvent.GetPartitionKey());
cloudEvent["partitionkey"] = null;
Assert.Null(cloudEvent.GetPartitionKey());
}
}
}