forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicMessageTests.cs
More file actions
159 lines (141 loc) · 4.43 KB
/
DynamicMessageTests.cs
File metadata and controls
159 lines (141 loc) · 4.43 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace ServiceStack.Text.Tests.DynamicModels
{
public class DynamicMessage : IMessageHeaders
{
public Guid Id { get; set; }
public string ReplyTo { get; set; }
public int Priority { get; set; }
public int RetryAttempts { get; set; }
public object Body { get; set; }
public Type Type { get; set; }
public object GetBody()
{
//When deserialized this.Body is a string so use the serilaized
//this.Type to deserialize it back into the original type
return this.Body is string
? TypeSerializer.DeserializeFromString((string)this.Body, this.Type)
: this.Body;
}
}
public class GenericMessage<T> : IMessageHeaders
{
public Guid Id { get; set; }
public string ReplyTo { get; set; }
public int Priority { get; set; }
public int RetryAttempts { get; set; }
public T Body { get; set; }
}
public class StrictMessage : IMessageHeaders
{
public Guid Id { get; set; }
public string ReplyTo { get; set; }
public int Priority { get; set; }
public int RetryAttempts { get; set; }
public MessageBody Body { get; set; }
}
public class MessageBody
{
public MessageBody()
{
this.Arguments = new List<string>();
}
public string Action { get; set; }
public List<string> Arguments { get; set; }
}
/// Common interface not required, used only to simplify validation
public interface IMessageHeaders
{
Guid Id { get; set; }
string ReplyTo { get; set; }
int Priority { get; set; }
int RetryAttempts { get; set; }
}
[TestFixture]
public class DynamicMessageTests
{
[Test]
public void Object_Set_To_Object_Test()
{
var original = new DynamicMessage
{
Id = Guid.NewGuid(),
Priority = 3,
ReplyTo = "http://path/to/reply.svc",
RetryAttempts = 1,
Type = typeof(MessageBody),
Body = new Object()
};
var jsv = TypeSerializer.SerializeToString(original);
var json = JsonSerializer.SerializeToString(original);
var jsvDynamicType = TypeSerializer.DeserializeFromString<DynamicMessage>(jsv);
var jsonDynamicType = JsonSerializer.DeserializeFromString<DynamicMessage>(json);
AssertHeadersAreEqual(jsvDynamicType, original);
AssertHeadersAreEqual(jsonDynamicType, original);
AssertHeadersAreEqual(jsvDynamicType, jsonDynamicType);
}
[Test]
public void Can_deserialize_between_dynamic_generic_and_strict_messages()
{
var original = new DynamicMessage
{
Id = Guid.NewGuid(),
Priority = 3,
ReplyTo = "http://path/to/reply.svc",
RetryAttempts = 1,
Type = typeof(MessageBody),
Body = new MessageBody
{
Action = "Alphabet",
Arguments = { "a", "b", "c" }
}
};
var jsv = TypeSerializer.SerializeToString(original);
var dynamicType = TypeSerializer.DeserializeFromString<DynamicMessage>(jsv);
var genericType = TypeSerializer.DeserializeFromString<GenericMessage<MessageBody>>(jsv);
var strictType = TypeSerializer.DeserializeFromString<StrictMessage>(jsv);
AssertHeadersAreEqual(dynamicType, original);
AssertBodyIsEqual(dynamicType.GetBody(), (MessageBody)original.Body);
AssertHeadersAreEqual(genericType, original);
AssertBodyIsEqual(genericType.Body, (MessageBody)original.Body);
AssertHeadersAreEqual(strictType, original);
AssertBodyIsEqual(strictType.Body, (MessageBody)original.Body);
//Debug purposes
Console.WriteLine(strictType.Dump());
/*
{
Id: 891653ea2d0a4626ab0623fc2dc9dce1,
ReplyTo: http://path/to/reply.svc,
Priority: 3,
RetryAttempts: 1,
Body:
{
Action: Alphabet,
Arguments:
[
a,
b,
c
]
}
}
*/
}
public void AssertHeadersAreEqual(IMessageHeaders actual, IMessageHeaders expected)
{
Assert.That(actual.Id, Is.EqualTo(expected.Id));
Assert.That(actual.ReplyTo, Is.EqualTo(expected.ReplyTo));
Assert.That(actual.Priority, Is.EqualTo(expected.Priority));
Assert.That(actual.RetryAttempts, Is.EqualTo(expected.RetryAttempts));
}
public void AssertBodyIsEqual(object actual, MessageBody expected)
{
var actualBody = actual as MessageBody;
Assert.That(actualBody, Is.Not.Null);
Assert.That(actualBody.Action, Is.EqualTo(expected.Action));
Assert.That(actualBody.Arguments, Is.EquivalentTo(expected.Arguments));
}
}
}