summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth.Test/MessageSerializerTest.cs
blob: 2db834345da50b6b5b82d5730dd9473d0655db7d (plain)
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
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DotNetOAuth.Test {
	[TestClass()]
	public class MessageSerializerTest : TestBase {
		[TestMethod, ExpectedException(typeof(ArgumentNullException))]
		public void SerializeNull() {
			var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
			serializer.Serialize(null);
		}

		[TestMethod, ExpectedException(typeof(ArgumentNullException))]
		public void DeserializeNull() {
			var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
			serializer.Deserialize(null);
		}

		/// <summary>
		/// A test for Deserialize
		/// </summary>
		[TestMethod()]
		public void DeserializeSimple() {
			var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
			Dictionary<string, string> fields = new Dictionary<string, string>(StringComparer.Ordinal);
			// We deliberately do this OUT of alphabetical order (caps would go first),
			// since DataContractSerializer demands things to be IN alphabetical order.
			fields["age"] = "15";
			fields["Name"] = "Andrew";
			var actual = serializer.Deserialize(fields);
			Assert.AreEqual(15, actual.Age);
			Assert.AreEqual("Andrew", actual.Name);
			Assert.AreEqual(null, actual.EmptyMember);
		}

		/// <summary>
		/// A test for Deserialize
		/// </summary>
		[TestMethod()]
		public void DeserializeEmpty() {
			var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
			Dictionary<string, string> fields = new Dictionary<string, string>(StringComparer.Ordinal);
			var actual = serializer.Deserialize(fields);
			Assert.AreEqual(0, actual.Age);
		}

		/// <summary>
		/// A test for Serialize
		/// </summary>
		[TestMethod()]
		public void SerializeTest() {
			var serializer = new ProtocolMessageSerializer<Mocks.TestMessage>();
			var message = new Mocks.TestMessage { Age = 15, Name = "Andrew" };
			IDictionary<string, string> actual = serializer.Serialize(message);
			Assert.AreEqual(2, actual.Count);

			// Test case sensitivity of generated dictionary
			Assert.IsFalse(actual.ContainsKey("Age"));
			Assert.IsTrue(actual.ContainsKey("age"));

			// Test contents of dictionary
			Assert.AreEqual("15", actual["age"]);
			Assert.AreEqual("Andrew", actual["Name"]);
			Assert.IsFalse(actual.ContainsKey("EmptyMember"));
		}
	}
}