7
7
using System . Linq ;
8
8
using System . Text . Json ;
9
9
using Microsoft . VisualStudio . TestTools . UnitTesting ;
10
+ using OnTopic . Data . Transfer . Converters ;
10
11
11
12
namespace OnTopic . Data . Transfer . Tests {
12
13
@@ -38,77 +39,146 @@ public void Deserialize_TopicData_ReturnsExpectedResults() {
38
39
$ "\" Key\" :\" { sourceData . Key } \" ," +
39
40
$ "\" UniqueKey\" :\" { sourceData . UniqueKey } \" ," +
40
41
$ "\" ContentType\" :\" { sourceData . ContentType } \" ," +
41
- $ "\" DerivedTopicKey\" :null," +
42
42
$ "\" Attributes\" :[]," +
43
43
$ "\" Relationships\" :[]," +
44
44
$ "\" Children\" :[]" +
45
45
$ "}}";
46
46
47
47
var topicData = JsonSerializer . Deserialize < TopicData > ( json ) ;
48
48
49
- Assert . AreEqual < string > ( sourceData . Key , topicData . Key ) ;
50
- Assert . AreEqual < string > ( sourceData . UniqueKey , topicData . UniqueKey ) ;
51
- Assert . AreEqual < string > ( sourceData . ContentType , topicData . ContentType ) ;
52
- Assert . AreEqual < string > ( sourceData . DerivedTopicKey , topicData . DerivedTopicKey ) ;
53
- Assert . AreEqual < int > ( 0 , topicData . Relationships . Count ) ;
54
- Assert . AreEqual < int > ( 0 , topicData . Attributes . Count ) ;
55
- Assert . AreEqual < int > ( 0 , topicData . Children . Count ) ;
49
+ Assert . AreEqual < string ? > ( sourceData . Key , topicData ? . Key ) ;
50
+ Assert . AreEqual < string ? > ( sourceData . UniqueKey , topicData ? . UniqueKey ) ;
51
+ Assert . AreEqual < string ? > ( sourceData . ContentType , topicData ? . ContentType ) ;
52
+ Assert . AreEqual < int ? > ( 0 , topicData ? . Relationships . Count ) ;
53
+ Assert . AreEqual < int ? > ( 0 , topicData ? . Attributes . Count ) ;
54
+ Assert . AreEqual < int ? > ( 0 , topicData ? . Children . Count ) ;
55
+
56
+ }
57
+
58
+ /*==========================================================================================================================
59
+ | TEST: DESERIALIZE: DERIVED TOPIC KEY: RETURNS EXPECTED RESULTS
60
+ \-------------------------------------------------------------------------------------------------------------------------*/
61
+ /// <summary>
62
+ /// Creates a json string with a legacy <c>DerivedTopicKey</c> and attempts to deserialize it as a <see cref="TopicData"/>
63
+ /// class.
64
+ /// </summary>
65
+ [ TestMethod ]
66
+ #pragma warning disable CS0618 // Type or member is obsolete
67
+ public void Deserialize_DerivedTopicKey_ReturnsExpectedResults ( ) {
68
+
69
+ var sourceData = new TopicData ( ) {
70
+ Key = "Test" ,
71
+ UniqueKey = "Root:Test" ,
72
+ ContentType = "Container" ,
73
+ DerivedTopicKey = "Root:Meta:Test"
74
+ } ;
75
+
76
+ var json = $ "{{" +
77
+ $ "\" Key\" :\" { sourceData . Key } \" ," +
78
+ $ "\" UniqueKey\" :\" { sourceData . UniqueKey } \" ," +
79
+ $ "\" ContentType\" :\" { sourceData . ContentType } \" ," +
80
+ $ "\" DerivedTopicKey\" :\" { sourceData . DerivedTopicKey } \" ," +
81
+ $ "\" Attributes\" :[]," +
82
+ $ "\" Relationships\" :[]," +
83
+ $ "\" Children\" :[]" +
84
+ $ "}}";
85
+
86
+ var topicData = JsonSerializer . Deserialize < TopicData > ( json ) ;
87
+
88
+ Assert . AreEqual < string ? > ( sourceData . Key , topicData ? . Key ) ;
89
+ Assert . AreEqual < string ? > ( sourceData . UniqueKey , topicData ? . UniqueKey ) ;
90
+ Assert . AreEqual < string ? > ( sourceData . ContentType , topicData ? . ContentType ) ;
91
+ Assert . AreEqual < string ? > ( sourceData . DerivedTopicKey , topicData ? . DerivedTopicKey ) ;
92
+ Assert . AreEqual < int ? > ( 0 , topicData ? . Relationships . Count ) ;
93
+ Assert . AreEqual < int ? > ( 0 , topicData ? . Attributes . Count ) ;
94
+ Assert . AreEqual < int ? > ( 0 , topicData ? . Children . Count ) ;
95
+
96
+ }
97
+ #pragma warning restore CS0618 // Type or member is obsolete
98
+
99
+ /*==========================================================================================================================
100
+ | TEST: DESERIALIZE: KEY/VALUES PAIR: RETURNS EXPECTED RESULTS
101
+ \-------------------------------------------------------------------------------------------------------------------------*/
102
+ /// <summary>
103
+ /// Creates a JSON string and attempts to deserialize it as a <see cref="KeyValuesPair"/> class.
104
+ /// </summary>
105
+ [ TestMethod ]
106
+ public void Deserialize_KeyValuesPair_ReturnsExpectedResults ( ) {
107
+
108
+ var sourceData = new KeyValuesPair ( ) {
109
+ Key = "Test"
110
+ } ;
111
+ sourceData . Values . Add ( "Root:Web" ) ;
112
+
113
+ var json = $ "{{" +
114
+ $ "\" Key\" :\" { sourceData . Key } \" ," +
115
+ $ "\" Values\" :[\" Root:Web\" ]" +
116
+ $ "}}";
117
+
118
+ var keyValuesPair = JsonSerializer . Deserialize < KeyValuesPair > ( json ) ;
119
+
120
+ Assert . AreEqual < string ? > ( sourceData . Key , keyValuesPair ? . Key ) ;
121
+ Assert . AreEqual < int ? > ( sourceData . Values . Count , keyValuesPair ? . Values . Count ) ;
122
+ Assert . AreEqual < string ? > ( sourceData . Values . FirstOrDefault ( ) , keyValuesPair ? . Values . FirstOrDefault ( ) ) ;
56
123
57
124
}
58
125
59
126
/*==========================================================================================================================
60
127
| TEST: DESERIALIZE: RELATIONSHIP DATA: RETURNS EXPECTED RESULTS
61
128
\-------------------------------------------------------------------------------------------------------------------------*/
62
129
/// <summary>
63
- /// Creates a json string and attempts to deserialize it as a <see cref="RelationshipData"/> class.
130
+ /// Creates a JSON string representing the legacy <c>RelationshipData</c> class (which used a <c>Relationships</c> array),
131
+ /// and attempts to deserialize it as a <see cref="KeyValuesPair"/> class, ensuring that the <see cref="
132
+ /// KeyValuesPairConverter"/> properly translates the <c>Relationships</c> array to the <see cref="KeyValuesPair.Values"/>
133
+ /// collection.
64
134
/// </summary>
65
135
[ TestMethod ]
66
136
public void Deserialize_RelationshipData_ReturnsExpectedResults ( ) {
67
137
68
- var sourceData = new RelationshipData ( ) {
138
+ var sourceData = new KeyValuesPair ( ) {
69
139
Key = "Test"
70
140
} ;
71
- sourceData . Relationships . Add ( "Root:Web" ) ;
141
+ sourceData . Values . Add ( "Root:Web" ) ;
72
142
73
143
var json = $ "{{" +
74
144
$ "\" Key\" :\" { sourceData . Key } \" ," +
75
145
$ "\" Relationships\" :[\" Root:Web\" ]" +
76
146
$ "}}";
77
147
78
- var relationshipData = JsonSerializer . Deserialize < RelationshipData > ( json ) ;
148
+ var keyValuesPair = JsonSerializer . Deserialize < KeyValuesPair > ( json ) ;
79
149
80
- Assert . AreEqual < string > ( sourceData . Key , relationshipData . Key ) ;
81
- Assert . AreEqual < int > ( sourceData . Relationships . Count , relationshipData . Relationships . Count ) ;
82
- Assert . AreEqual < string > ( sourceData . Relationships . FirstOrDefault ( ) , relationshipData . Relationships . FirstOrDefault ( ) ) ;
150
+ Assert . AreEqual < string ? > ( sourceData . Key , keyValuesPair ? . Key ) ;
151
+ Assert . AreEqual < int ? > ( sourceData . Values . Count , keyValuesPair ? . Values . Count ) ;
152
+ Assert . AreEqual < string ? > ( sourceData . Values . FirstOrDefault ( ) , keyValuesPair ? . Values . FirstOrDefault ( ) ) ;
83
153
84
154
}
85
155
86
156
/*==========================================================================================================================
87
- | TEST: DESERIALIZE: ATTRIBUTE DATA: RETURNS EXPECTED RESULTS
157
+ | TEST: DESERIALIZE: RECORD DATA: RETURNS EXPECTED RESULTS
88
158
\-------------------------------------------------------------------------------------------------------------------------*/
89
159
/// <summary>
90
- /// Creates a json string and attempts to deserialize it as a <see cref="AttributeData "/> class.
160
+ /// Creates a json string and attempts to deserialize it as a <see cref="RecordData "/> class.
91
161
/// </summary>
92
162
[ TestMethod ]
93
- public void Deserialize_AttributeData_ReturnsExpectedResults ( ) {
163
+ public void Deserialize_RecordData_ReturnsExpectedResults ( ) {
94
164
95
- var sourceData = new AttributeData ( ) {
165
+ var sourceData = new RecordData ( ) {
96
166
Key = "Test" ,
97
- LastModified = DateTime . Now
167
+ LastModified = new DateTime ( 2021 , 02 , 16 , 16 , 06 , 25 )
98
168
} ;
99
169
100
170
var json = $ "{{" +
101
171
$ "\" Key\" :\" { sourceData . Key } \" ," +
102
172
$ "\" Value\" :null," +
103
- $ "\" LastModified\" :\" { sourceData . LastModified : o } \" " +
173
+ $ "\" LastModified\" :\" { sourceData . LastModified : s } \" " +
104
174
$ "}}";
105
175
106
176
107
- var attributeData = JsonSerializer . Deserialize < AttributeData > ( json ) ;
177
+ var recordData = JsonSerializer . Deserialize < RecordData > ( json ) ;
108
178
109
- Assert . AreEqual < string > ( sourceData . Key , attributeData . Key ) ;
110
- Assert . AreEqual < string > ( sourceData . Value , attributeData . Value ) ;
111
- Assert . AreEqual < DateTime > ( sourceData . LastModified , attributeData . LastModified ) ;
179
+ Assert . AreEqual < string ? > ( sourceData . Key , recordData ? . Key ) ;
180
+ Assert . AreEqual < string ? > ( sourceData . Value , recordData ? . Value ) ;
181
+ Assert . AreEqual < DateTime ? > ( sourceData . LastModified , recordData ? . LastModified ) ;
112
182
113
183
}
114
184
@@ -121,25 +191,32 @@ public void Deserialize_AttributeData_ReturnsExpectedResults() {
121
191
[ TestMethod ]
122
192
public void Deserialize_TopicGraph_ReturnsExpectedResults ( ) {
123
193
194
+ var lastModified = new DateTime ( 2021 , 02 , 16 , 16 , 06 , 25 ) ;
195
+
124
196
var sourceTopicData = new TopicData ( ) {
125
197
Key = "Test" ,
126
198
UniqueKey = "Root:Test" ,
127
199
ContentType = "Container"
128
200
} ;
129
- var sourceRelationshipData = new RelationshipData ( ) {
201
+ var sourceRelationshipData = new KeyValuesPair ( ) {
130
202
Key = "Test"
131
203
} ;
132
- var sourceAttributeData = new AttributeData ( ) {
204
+ var sourceAttributeData = new RecordData ( ) {
205
+ Key = "Test" ,
206
+ LastModified = lastModified
207
+ } ;
208
+ var sourceReferenceData = new RecordData ( ) {
133
209
Key = "Test" ,
134
- LastModified = DateTime . Now
210
+ Value = "Root:Reference" ,
211
+ LastModified = lastModified
135
212
} ;
136
213
var sourceChildTopicData = new TopicData ( ) {
137
214
Key = "Child" ,
138
215
UniqueKey = "Root:Test:Child" ,
139
216
ContentType = "Container"
140
217
} ;
141
218
142
- sourceRelationshipData . Relationships . Add ( "Root:Web" ) ;
219
+ sourceRelationshipData . Values . Add ( "Root:Web" ) ;
143
220
sourceTopicData . Relationships . Add ( sourceRelationshipData ) ;
144
221
sourceTopicData . Attributes . Add ( sourceAttributeData ) ;
145
222
sourceTopicData . Children . Add ( sourceChildTopicData ) ;
@@ -148,26 +225,31 @@ public void Deserialize_TopicGraph_ReturnsExpectedResults() {
148
225
$ "\" Key\" :\" { sourceTopicData . Key } \" ," +
149
226
$ "\" UniqueKey\" :\" { sourceTopicData . UniqueKey } \" ," +
150
227
$ "\" ContentType\" :\" { sourceTopicData . ContentType } \" ," +
151
- $ "\" DerivedTopicKey\" :null," +
152
228
$ "\" Attributes\" :[" +
153
229
$ "{{" +
154
230
$ "\" Key\" :\" { sourceAttributeData . Key } \" ," +
155
231
$ "\" Value\" :null," +
156
- $ "\" LastModified\" :\" { sourceAttributeData . LastModified : o } \" " +
232
+ $ "\" LastModified\" :\" { sourceAttributeData . LastModified : s } \" " +
157
233
$ "}}"+
158
234
$ "]," +
159
235
$ "\" Relationships\" :[" +
160
236
$ "{{" +
161
237
$ "\" Key\" :\" { sourceRelationshipData . Key } \" ," +
162
- $ "\" Relationships \" :[\" Root:Web\" ]" +
238
+ $ "\" Values \" :[\" Root:Web\" ]" +
163
239
$ "}}" +
164
240
$ "]," +
241
+ $ "\" References\" :[" +
242
+ $ "{{" +
243
+ $ "\" Key\" :\" { sourceReferenceData . Key } \" ," +
244
+ $ "\" Value\" :\" { sourceReferenceData . Value } \" ," +
245
+ $ "\" LastModified\" :\" { sourceReferenceData . LastModified : s} \" " +
246
+ $ "}}"+
247
+ $ "]," +
165
248
$ "\" Children\" :[" +
166
249
$ "{{" +
167
250
$ "\" Key\" :\" { sourceChildTopicData . Key } \" ," +
168
251
$ "\" UniqueKey\" :\" { sourceChildTopicData . UniqueKey } \" ," +
169
252
$ "\" ContentType\" :\" { sourceChildTopicData . ContentType } \" ," +
170
- $ "\" DerivedTopicKey\" :null," +
171
253
$ "\" Attributes\" :[]," +
172
254
$ "\" Relationships\" :[]," +
173
255
$ "\" Children\" :[]" +
@@ -176,31 +258,34 @@ public void Deserialize_TopicGraph_ReturnsExpectedResults() {
176
258
$ "}}";
177
259
178
260
179
- var topicData = JsonSerializer . Deserialize < TopicData > ( json ) ;
180
- var relationshipData = topicData . Relationships . FirstOrDefault ( ) ;
181
- var attributeData = topicData . Attributes . FirstOrDefault ( ) ;
182
- var childTopicData = topicData . Children . FirstOrDefault ( ) ;
183
-
184
- Assert . AreEqual < string > ( sourceTopicData . Key , topicData . Key ) ;
185
- Assert . AreEqual < string > ( sourceTopicData . UniqueKey , topicData . UniqueKey ) ;
186
- Assert . AreEqual < string > ( sourceTopicData . ContentType , topicData . ContentType ) ;
187
- Assert . AreEqual < string > ( sourceTopicData . DerivedTopicKey , topicData . DerivedTopicKey ) ;
261
+ var topicData = JsonSerializer . Deserialize < TopicData > ( json ) ;
262
+ var relationshipData = topicData ? . Relationships . FirstOrDefault ( ) ;
263
+ var referenceData = topicData ? . References . FirstOrDefault ( ) ;
264
+ var attributeData = topicData ? . Attributes . FirstOrDefault ( ) ;
265
+ var childTopicData = topicData ? . Children . FirstOrDefault ( ) ;
266
+
267
+ Assert . AreEqual < string ? > ( sourceTopicData . Key , topicData ? . Key ) ;
268
+ Assert . AreEqual < string ? > ( sourceTopicData . UniqueKey , topicData ? . UniqueKey ) ;
269
+ Assert . AreEqual < string ? > ( sourceTopicData . ContentType , topicData ? . ContentType ) ;
188
270
Assert . AreEqual < int > ( 1 , sourceTopicData . Relationships . Count ) ;
189
271
Assert . AreEqual < int > ( 1 , sourceTopicData . Attributes . Count ) ;
190
272
Assert . AreEqual < int > ( 1 , sourceTopicData . Children . Count ) ;
191
273
192
- Assert . AreEqual < string > ( sourceRelationshipData . Key , relationshipData . Key ) ;
193
- Assert . AreEqual < int ? > ( sourceRelationshipData . Relationships . Count , relationshipData . Relationships . Count ) ;
194
- Assert . AreEqual < string > ( sourceRelationshipData . Relationships . FirstOrDefault ( ) , relationshipData . Relationships . FirstOrDefault ( ) ) ;
274
+ Assert . AreEqual < string ? > ( sourceRelationshipData . Key , relationshipData ? . Key ) ;
275
+ Assert . AreEqual < int ? > ( sourceRelationshipData . Values . Count , relationshipData ? . Values . Count ) ;
276
+ Assert . AreEqual < string ? > ( sourceRelationshipData . Values . FirstOrDefault ( ) , relationshipData ? . Values . FirstOrDefault ( ) ) ;
277
+
278
+ Assert . AreEqual < string ? > ( sourceReferenceData . Key , referenceData ? . Key ) ;
279
+ Assert . AreEqual < string ? > ( sourceReferenceData . Value , referenceData ? . Value ) ;
280
+ Assert . AreEqual < DateTime ? > ( sourceReferenceData . LastModified , referenceData ? . LastModified ) ;
195
281
196
- Assert . AreEqual < string > ( sourceAttributeData . Key , attributeData . Key ) ;
197
- Assert . AreEqual < string > ( sourceAttributeData . Value , attributeData . Value ) ;
198
- Assert . AreEqual < DateTime > ( sourceAttributeData . LastModified , attributeData . LastModified ) ;
282
+ Assert . AreEqual < string ? > ( sourceAttributeData . Key , attributeData ? . Key ) ;
283
+ Assert . AreEqual < string ? > ( sourceAttributeData . Value , attributeData ? . Value ) ;
284
+ Assert . AreEqual < DateTime ? > ( sourceAttributeData . LastModified , attributeData ? . LastModified ) ;
199
285
200
- Assert . AreEqual < string > ( sourceChildTopicData . Key , childTopicData . Key ) ;
201
- Assert . AreEqual < string > ( sourceChildTopicData . UniqueKey , childTopicData . UniqueKey ) ;
202
- Assert . AreEqual < string > ( sourceChildTopicData . ContentType , childTopicData . ContentType ) ;
203
- Assert . AreEqual < string > ( sourceChildTopicData . DerivedTopicKey , childTopicData . DerivedTopicKey ) ;
286
+ Assert . AreEqual < string ? > ( sourceChildTopicData . Key , childTopicData ? . Key ) ;
287
+ Assert . AreEqual < string ? > ( sourceChildTopicData . UniqueKey , childTopicData ? . UniqueKey ) ;
288
+ Assert . AreEqual < string ? > ( sourceChildTopicData . ContentType , childTopicData ? . ContentType ) ;
204
289
Assert . AreEqual < int > ( 0 , sourceChildTopicData . Relationships . Count ) ;
205
290
Assert . AreEqual < int > ( 0 , sourceChildTopicData . Children . Count ) ;
206
291
}
0 commit comments