PageRenderTime 54ms CodeModel.GetById 2ms app.highlight 45ms RepoModel.GetById 1ms app.codeStats 1ms

/silverlight/3.4/PubnubSilverlight.Example/UnitTest/WhenAMessageIsPublished.cs

https://github.com/dchenatl/c-sharp
C# | 739 lines | 650 code | 89 blank | 0 comment | 104 complexity | 1c09a8a89155028f7cf750080b363c04 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using Microsoft.VisualStudio.TestTools.UnitTesting;
  6using System.ComponentModel;
  7using System.Threading;
  8using System.Collections;
  9using Newtonsoft.Json;
 10using Newtonsoft.Json.Linq;
 11using Microsoft.Silverlight.Testing;
 12using PubNubMessaging.Core;
 13using Newtonsoft.Json;
 14using Newtonsoft.Json.Linq;
 15
 16namespace PubnubSilverlight.UnitTest
 17{
 18    [TestClass]
 19    public class WhenAMessageIsPublished : SilverlightTest
 20    {
 21        bool isPublished2 = false;
 22        bool isPublished3 = false;
 23
 24        bool isUnencryptPublished = false;
 25        bool isUnencryptObjectPublished = false;
 26        bool isEncryptObjectPublished = false;
 27        bool isUnencryptDH = false;
 28        bool isUnencryptObjectDH = false;
 29        bool isEncryptObjectDH = false;
 30        bool isEncryptPublished = false;
 31        bool isSecretEncryptPublished = false;
 32        bool isEncryptDH = false;
 33        bool isSecretEncryptDH = false;
 34        bool isComplexObjectPublished = false;
 35        bool isComplexObjectDetailedHistory = false;
 36        bool isSerializedObjectMessagePublished = false;
 37        bool isSerializedObjectMessageDetailedHistory = false;
 38        bool isLargeMessagePublished = false;
 39
 40        long unEncryptPublishTimetoken = 0;
 41        long unEncryptObjectPublishTimetoken = 0;
 42        long encryptObjectPublishTimetoken = 0;
 43        long encryptPublishTimetoken = 0;
 44        long secretEncryptPublishTimetoken = 0;
 45        long complexObjectPublishTimetoken = 0;
 46        long serializedMessagePublishTimetoken = 0;
 47
 48        const string messageForUnencryptPublish = "Pubnub Messaging API 1";
 49        const string messageForEncryptPublish = "漢語";
 50        const string messageForSecretEncryptPublish = "Pubnub Messaging API 2";
 51        const string messageLarge2K = "Numerous questions remain about the origins of the chemical and what impact its apparent use could have on the ongoing Syrian civil war and international involvement in it.When asked if the intelligence community's conclusion pushed the situation across President Barack Obama's \"red line\" that could potentially trigger more U.S. involvement in the Syrian civil war, Hagel said it's too soon to say.\"We need all the facts. We need all the information,\" he said. \"What I've just given you is what our intelligence community has said they know. As I also said, they are still assessing and they are still looking at what happened, who was responsible and the other specifics that we'll need.\" In a letter sent to lawmakers before Hagel's announcement, the White House said that intelligence analysts have concluded \"with varying degrees of confidence that the Syrian regime has used chemical weapons on a small scale in Syria, specifically the chemical agent sarin.\" In the letter, signed by White House legislative affairs office Director Miguel Rodriguez, the White House said the \"chain of custody\" of the chemicals was not clear and that intelligence analysts could not confirm the circumstances under which the sarin was used, including the role of Syrian President Bashar al-Assad's regime. Read Rodriguez's letter to Levin (PDF) But, the letter said, \"we do believe that any use of chemical weapons in Syria would very likely have originated with the Assad regime.\" The Syrian government has been battling a rebellion for more than two years, bringing international condemnation of the regime and pleas for greater international assistance. The United Nations estimated in February that more than 70,000 people had died since the conflict began. The administration is \"pressing for a comprehensive United Nations investigation that can credibly evaluate the evidence and establish what took place,\" the White House letter said. Sen. John McCain, one of the lawmakers who received the letter, said the use of";
 52        string messageObjectForUnencryptPublish = "";
 53        string messageObjectForEncryptPublish = "";
 54        string messageComplexObjectForPublish = "";
 55        string serializedObjectMessageForPublish;
 56
 57        bool isCheck = false;
 58        bool isUnencryptCheck = false;
 59        bool isUnencryptObjectPubCheck = false;
 60        bool isUnencryptObjectDHCheck = false;
 61        bool isEncryptObjectPubCheck = false;
 62        bool isEncryptObjectDHCheck = false;
 63        bool isEncryptPubCheck = false;
 64        bool isEncryptDHCheck = false;
 65        bool isSecretEncryptPubCheck = false;
 66        bool isSecretEncryptDHCheck = false;
 67        bool isComplexObjectPublishCheck = false;
 68        bool isComplexObjectDetailedHistoryCheck = false;
 69        bool isSerializedObjectMessageCheck = false;
 70        bool isSerializedMessageDetailedHistoryCheck = false;
 71        bool isPublishMessageTooLargeCheck = false;
 72        bool isCheck2 = false;
 73        bool isCheck3 = false;
 74
 75        [TestMethod]
 76        [Asynchronous]
 77        public void ThenUnencryptPublishShouldReturnSuccessCodeAndInfo()
 78        {
 79            isUnencryptPublished = false;
 80            Pubnub pubnub = new Pubnub("demo","demo","","",false);
 81            string channel = "my/channel";
 82            string message = messageForUnencryptPublish;
 83
 84            PubnubUnitTest unitTest = new PubnubUnitTest();
 85            unitTest.TestClassName = "WhenAMessageIsPublished";
 86            unitTest.TestCaseName = "ThenUnencryptPublishShouldReturnSuccessCodeAndInfo";
 87            pubnub.PubnubUnitTest = unitTest;
 88
 89            EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessUnencryptPublishCodeCallback));
 90            EnqueueConditional(() => isCheck);
 91
 92            EnqueueCallback(() => 
 93            {
 94                if (!isUnencryptPublished)
 95                {
 96                    Assert.IsTrue(isUnencryptPublished, "Unencrypt Publish Failed");
 97                }
 98                else
 99                {
100                    EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, unEncryptPublishTimetoken, -1, false, CaptureUnencryptDetailedHistoryCallback));
101                    EnqueueConditional(() => isUnencryptCheck);
102                    EnqueueCallback(() => Assert.IsTrue(isUnencryptDH, "Unable to match the successful unencrypt Publish"));
103                }
104            });
105
106            EnqueueTestComplete();
107        }
108
109        [Asynchronous]
110        public void ReturnSuccessUnencryptPublishCodeCallback(string result)
111        {
112            if (!string.IsNullOrWhiteSpace(result))
113            {
114                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
115                if (receivedObj is object[])
116                {
117                    long statusCode = Int64.Parse(receivedObj[0].ToString());
118                    string statusMsg = (string)receivedObj[1];
119                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
120                    {
121                        isUnencryptPublished = true;
122                        unEncryptPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
123                    }
124                }
125            }
126            isCheck = true;
127        }
128
129        [Asynchronous]
130        public void CaptureUnencryptDetailedHistoryCallback(string result)
131        {
132            if (!string.IsNullOrWhiteSpace(result))
133            {
134                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
135                if (receivedObj is object[])
136                {
137                    JArray jObj = JArray.Parse(receivedObj[0].ToString());
138                    if (jObj[0].ToString() == messageForUnencryptPublish)
139                    {
140                        isUnencryptDH = true;
141                    }
142                }
143            }
144
145            isUnencryptCheck = true;
146        }
147
148        [TestMethod]
149        [Asynchronous]
150        public void ThenUnencryptObjectPublishShouldReturnSuccessCodeAndInfo()
151        {
152            isUnencryptObjectPublished = false;
153            Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
154            string channel = "my/channel";
155
156            object message = new CustomClass();
157            messageObjectForUnencryptPublish = JsonConvert.SerializeObject(message);
158
159            PubnubUnitTest unitTest = new PubnubUnitTest();
160            unitTest.TestClassName = "WhenAMessageIsPublished";
161            unitTest.TestCaseName = "ThenUnencryptObjectPublishShouldReturnSuccessCodeAndInfo";
162            pubnub.PubnubUnitTest = unitTest;
163
164            EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessUnencryptObjectPublishCodeCallback));
165            EnqueueConditional(() => isUnencryptObjectPubCheck);
166
167            EnqueueCallback(() =>
168            {
169                if (!isUnencryptObjectPublished)
170                {
171                    Assert.IsTrue(isUnencryptObjectPublished, "Unencrypt Publish Failed");
172                }
173                else
174                {
175                    EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, unEncryptObjectPublishTimetoken, -1, false, CaptureUnencryptObjectDetailedHistoryCallback));
176                    EnqueueConditional(() => isUnencryptObjectDHCheck);
177                    EnqueueCallback(() => Assert.IsTrue(isUnencryptObjectDH, "Unable to match the successful unencrypt object Publish"));
178                }
179            });
180
181            EnqueueTestComplete();
182        }
183
184        [Asynchronous]
185        public void ReturnSuccessUnencryptObjectPublishCodeCallback(string result)
186        {
187            if (!string.IsNullOrWhiteSpace(result))
188            {
189                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
190                if (receivedObj is object[])
191                {
192                    long statusCode = Int64.Parse(receivedObj[0].ToString());
193                    string statusMsg = (string)receivedObj[1];
194                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
195                    {
196                        isUnencryptObjectPublished = true;
197                        unEncryptObjectPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
198                    }
199                }
200            }
201            isUnencryptObjectPubCheck = true;
202        }
203
204        [Asynchronous]
205        public void CaptureUnencryptObjectDetailedHistoryCallback(string result)
206        {
207            if (!string.IsNullOrWhiteSpace(result))
208            {
209                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
210                if (receivedObj is object[])
211                {
212                    JArray jArr = JArray.Parse(receivedObj[0].ToString());
213                    if (jArr[0].ToString(Formatting.None) == messageObjectForUnencryptPublish)
214                    {
215                        isUnencryptObjectDH = true;
216                    }
217                }
218            }
219
220            isUnencryptObjectDHCheck = true;
221        }
222
223        [TestMethod]
224        [Asynchronous]
225        public void ThenEncryptObjectPublishShouldReturnSuccessCodeAndInfo()
226        {
227            isEncryptObjectPublished = false;
228            Pubnub pubnub = new Pubnub("demo", "demo", "", "enigma", false);
229            string channel = "my/channel";
230            object message = new SecretCustomClass();
231            messageObjectForEncryptPublish = JsonConvert.SerializeObject(message);
232
233            PubnubUnitTest unitTest = new PubnubUnitTest();
234            unitTest.TestClassName = "WhenAMessageIsPublished";
235            unitTest.TestCaseName = "ThenEncryptObjectPublishShouldReturnSuccessCodeAndInfo";
236            pubnub.PubnubUnitTest = unitTest;
237
238            EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessEncryptObjectPublishCodeCallback));
239            EnqueueConditional(() => isEncryptObjectPubCheck);
240
241            EnqueueCallback(() =>
242            {
243                if (!isEncryptObjectPublished)
244                {
245                    Assert.IsTrue(isEncryptObjectPublished, "Encrypt Object Publish Failed");
246                }
247                else
248                {
249                   EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, encryptObjectPublishTimetoken, -1, false, CaptureEncryptObjectDetailedHistoryCallback));
250                   EnqueueConditional(() => isEncryptObjectDHCheck);
251                   EnqueueCallback(() => Assert.IsTrue(isEncryptObjectDH, "Unable to match the successful encrypt object Publish"));
252                }
253            });
254
255            EnqueueTestComplete();
256        }
257
258        [Asynchronous]
259        public void ReturnSuccessEncryptObjectPublishCodeCallback(string result)
260        {
261            if (!string.IsNullOrWhiteSpace(result))
262            {
263                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
264                if (receivedObj is object[])
265                {
266                    long statusCode = Int64.Parse(receivedObj[0].ToString());
267                    string statusMsg = (string)receivedObj[1];
268                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
269                    {
270                        isEncryptObjectPublished = true;
271                        encryptObjectPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
272                    }
273                }
274            }
275            isEncryptObjectPubCheck = true;
276        }
277
278        [Asynchronous]
279        public void CaptureEncryptObjectDetailedHistoryCallback(string result)
280        {
281            if (!string.IsNullOrWhiteSpace(result))
282            {
283                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
284                if (receivedObj is object[])
285                {
286                    JArray jArr = JArray.Parse(receivedObj[0].ToString());
287                    if (jArr[0].ToString(Formatting.None) == messageObjectForEncryptPublish)
288                    {
289                        isEncryptObjectDH = true;
290                    }
291                }
292            }
293
294            isEncryptObjectDHCheck = true;
295        }
296
297        [TestMethod]
298        [Asynchronous]
299        public void ThenEncryptPublishShouldReturnSuccessCodeAndInfo()
300        {
301            isEncryptPublished = false;
302            Pubnub pubnub = new Pubnub("demo", "demo", "", "enigma", false);
303            string channel = "my/channel";
304            string message = messageForEncryptPublish;
305
306            PubnubUnitTest unitTest = new PubnubUnitTest();
307            unitTest.TestClassName = "WhenAMessageIsPublished";
308            unitTest.TestCaseName = "ThenEncryptPublishShouldReturnSuccessCodeAndInfo";
309            pubnub.PubnubUnitTest = unitTest;
310
311            EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessEncryptPublishCodeCallback));
312            EnqueueConditional(() => isEncryptPubCheck);
313       
314            EnqueueCallback(() =>
315            {
316                if (!isEncryptPublished)
317                {
318                    Assert.IsTrue(isEncryptPublished, "Encrypt Publish Failed");
319                }
320                else
321                {
322                    EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, encryptPublishTimetoken, -1, false, CaptureEncryptDetailedHistoryCallback));
323                    EnqueueConditional(() => isEncryptDHCheck);
324                    EnqueueCallback(() => Assert.IsTrue(isEncryptDH, "Unable to decrypt the successful Publish"));
325                }
326            });
327
328            EnqueueTestComplete();
329        }
330
331        [Asynchronous]
332        public void ReturnSuccessEncryptPublishCodeCallback(string result)
333        {
334            if (!string.IsNullOrWhiteSpace(result))
335            {
336                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
337                if (receivedObj is object[])
338                {
339                    long statusCode = Int64.Parse(receivedObj[0].ToString());
340                    string statusMsg = (string)receivedObj[1];
341                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
342                    {
343                        isEncryptPublished = true;
344                        encryptPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
345                    }
346                }
347            }
348            isEncryptPubCheck = true;
349        }
350
351        [Asynchronous]
352        public void CaptureEncryptDetailedHistoryCallback(string result)
353        {
354            if (!string.IsNullOrWhiteSpace(result))
355            {
356                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
357                if (receivedObj is object[])
358                {
359                    JArray jArr = JArray.Parse(receivedObj[0].ToString());
360                    if (jArr[0].ToString() == messageForEncryptPublish)
361                    {
362                        isEncryptDH = true;
363                    }
364                }
365            }
366
367            isEncryptDHCheck = true;
368        }
369
370        [TestMethod]
371        [Asynchronous]
372        public void ThenSecretKeyWithEncryptPublishShouldReturnSuccessCodeAndInfo()
373        {
374            isSecretEncryptPublished = false;
375            Pubnub pubnub = new Pubnub("demo", "demo", "key", "enigma", false);
376            string channel = "my/channel";
377            string message = messageForSecretEncryptPublish;
378
379            PubnubUnitTest unitTest = new PubnubUnitTest();
380            unitTest.TestClassName = "WhenAMessageIsPublished";
381            unitTest.TestCaseName = "ThenSecretKeyWithEncryptPublishShouldReturnSuccessCodeAndInfo";
382            pubnub.PubnubUnitTest = unitTest;
383
384            EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessSecretEncryptPublishCodeCallback));
385            EnqueueConditional(() => isSecretEncryptPubCheck);
386
387            EnqueueCallback(() =>
388            {
389                if (!isSecretEncryptPublished)
390                {
391                    Assert.IsTrue(isSecretEncryptPublished, "Secret Encrypt Publish Failed");
392                }
393                else
394                {
395                    EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, secretEncryptPublishTimetoken, -1, false, CaptureSecretEncryptDetailedHistoryCallback));
396                    EnqueueConditional(() => isSecretEncryptDHCheck);
397                    EnqueueCallback(() => Assert.IsTrue(isSecretEncryptDH, "Unable to decrypt the successful Secret key Publish"));
398                }
399            });
400
401            EnqueueTestComplete();
402        }
403
404        [Asynchronous]
405        public void ReturnSuccessSecretEncryptPublishCodeCallback(string result)
406        {
407            if (!string.IsNullOrWhiteSpace(result))
408            {
409                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
410                if (receivedObj is object[])
411                {
412                    long statusCode = Int64.Parse(receivedObj[0].ToString());
413                    string statusMsg = (string)receivedObj[1];
414                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
415                    {
416                        isSecretEncryptPublished = true;
417                        secretEncryptPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
418                    }
419                }
420            }
421            isSecretEncryptPubCheck = true;
422        }
423
424        [Asynchronous]
425        private void CaptureSecretEncryptDetailedHistoryCallback(string result)
426        {
427            if (!string.IsNullOrWhiteSpace(result))
428            {
429                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
430                if (receivedObj is object[])
431                {
432                    JArray jArr = JArray.Parse(receivedObj[0].ToString());
433                    if (jArr[0].ToString() == messageForSecretEncryptPublish)
434                    {
435                        isSecretEncryptDH = true;
436                    }
437                }
438            }
439
440            isSecretEncryptDHCheck = true;
441        }
442
443        [TestMethod]
444        public void ThenPubnubShouldGenerateUniqueIdentifier()
445        {
446            Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
447
448            Assert.IsNotNull(pubnub.GenerateGuid());
449        }
450
451        [TestMethod]
452        [ExpectedException(typeof(MissingFieldException))]
453        public void ThenPublishKeyShouldNotBeEmpty()
454        {
455            Pubnub pubnub = new Pubnub("", "demo", "", "", false);
456
457            string channel = "my/channel";
458            string message = "Pubnub API Usage Example";
459
460            pubnub.Publish<string>(channel, message, null);
461        }
462
463        [TestMethod]
464        [Asynchronous]
465        public void ThenOptionalSecretKeyShouldBeProvidedInConstructor()
466        {
467            isPublished2 = false;
468            Pubnub pubnub = new Pubnub("demo", "demo", "key");
469            string channel = "my/channel";
470            string message = "Pubnub API Usage Example";
471
472            PubnubUnitTest unitTest = new PubnubUnitTest();
473            unitTest.TestClassName = "WhenAMessageIsPublished";
474            unitTest.TestCaseName = "ThenOptionalSecretKeyShouldBeProvidedInConstructor";
475            pubnub.PubnubUnitTest = unitTest;
476
477            EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSecretKeyPublishCallback));
478            EnqueueConditional(() => isCheck2);
479            EnqueueCallback(() => Assert.IsTrue(isPublished2, "Publish Failed with secret key"));
480
481            EnqueueTestComplete();
482        }
483
484        [Asynchronous]
485        public void ReturnSecretKeyPublishCallback(string result)
486        {
487            if (!string.IsNullOrWhiteSpace(result))
488            {
489                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
490                if (receivedObj is object[])
491                {
492                    long statusCode = Int64.Parse(receivedObj[0].ToString());
493                    string statusMsg = (string)receivedObj[1];
494                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
495                    {
496                        isPublished2 = true;
497                    }
498                }
499            }
500            isCheck2 = true;
501        }
502
503        [TestMethod]
504        [Asynchronous]
505        public void IfSSLNotProvidedThenDefaultShouldBeFalse()
506        {
507            isPublished3 = false;
508            Pubnub pubnub = new Pubnub("demo", "demo", "");
509            string channel = "my/channel";
510            string message = "Pubnub API Usage Example";
511
512            PubnubUnitTest unitTest = new PubnubUnitTest();
513            unitTest.TestClassName = "WhenAMessageIsPublished";
514            unitTest.TestCaseName = "IfSSLNotProvidedThenDefaultShouldBeFalse";
515            pubnub.PubnubUnitTest = unitTest;
516
517            EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnNoSSLDefaultFalseCallback));
518            EnqueueConditional(() => isCheck3);
519            EnqueueCallback(() => Assert.IsTrue(isPublished3, "Publish Failed with no SSL"));
520
521            EnqueueTestComplete();
522        }
523
524        [Asynchronous]
525        public void ReturnNoSSLDefaultFalseCallback(string result)
526        {
527            if (!string.IsNullOrWhiteSpace(result))
528            {
529                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
530                if (receivedObj is object[])
531                {
532                    long statusCode = Int64.Parse(receivedObj[0].ToString());
533                    string statusMsg = (string)receivedObj[1];
534                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
535                    {
536                        isPublished3 = true;
537                    }
538                }
539            }
540            isCheck3 = true;
541        }
542
543        [TestMethod]
544        [Asynchronous]
545        public void ThenComplexMessageObjectShouldReturnSuccessCodeAndInfo()
546        {
547            isComplexObjectPublished = false;
548            Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
549
550            PubnubUnitTest unitTest = new PubnubUnitTest();
551            unitTest.TestClassName = "WhenAMessageIsPublished";
552            unitTest.TestCaseName = "ThenComplexMessageObjectShouldReturnSuccessCodeAndInfo";
553            pubnub.PubnubUnitTest = unitTest;
554
555            string channel = "my/channel";
556            object message = new PubnubDemoObject();
557            messageComplexObjectForPublish = JsonConvert.SerializeObject(message);
558
559            EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessComplexObjectPublishCodeCallback));
560            EnqueueConditional(() => isComplexObjectPublishCheck);
561
562            EnqueueCallback(() =>
563                {
564                    if (!isComplexObjectPublished)
565                    {
566                        Assert.IsTrue(isComplexObjectPublished, "Complex Object Publish Failed");
567                    }
568                    else
569                    {
570                        EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, complexObjectPublishTimetoken, -1, false, CaptureComplexObjectDetailedHistoryCallback));
571                        EnqueueConditional(() => isComplexObjectDetailedHistoryCheck);
572                        EnqueueCallback(() => Assert.IsTrue(isComplexObjectDetailedHistory, "Unable to match the successful unencrypt object Publish"));
573                    }
574                });
575
576            EnqueueTestComplete();
577        }
578
579        [Asynchronous]
580        private void ReturnSuccessComplexObjectPublishCodeCallback(string result)
581        {
582            if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
583            {
584                object[] deserializedMessage = JsonConvert.DeserializeObject<object[]>(result);
585                if (deserializedMessage is object[])
586                {
587                    long statusCode = Int64.Parse(deserializedMessage[0].ToString());
588                    string statusMessage = (string)deserializedMessage[1];
589                    if (statusCode == 1 && statusMessage.ToLower() == "sent")
590                    {
591                        isComplexObjectPublished = true;
592                        complexObjectPublishTimetoken = Convert.ToInt64(deserializedMessage[2].ToString());
593                    }
594                }
595            }
596
597            isComplexObjectPublishCheck = true;
598
599        }
600
601        [Asynchronous]
602        private void CaptureComplexObjectDetailedHistoryCallback(string result)
603        {
604            if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
605            {
606                object[] deserializedMessage = JsonConvert.DeserializeObject<object[]>(result);
607                if (deserializedMessage is object[])
608                {
609                    JArray message = deserializedMessage[0] as JArray;
610                    if (message != null && message[0].ToString(Formatting.None) == messageComplexObjectForPublish)
611                    {
612                        isComplexObjectDetailedHistory = true;
613                    }
614                }
615            }
616
617            isComplexObjectDetailedHistoryCheck = true;
618        }
619
620        [TestMethod]
621        [Asynchronous]
622        public void ThenDisableJsonEncodeShouldSendSerializedObjectMessage()
623        {
624            isSerializedObjectMessagePublished = false;
625            Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
626            pubnub.EnableJsonEncodingForPublish = false;
627
628            PubnubUnitTest unitTest = new PubnubUnitTest();
629            unitTest.TestClassName = "WhenAMessageIsPublished";
630            unitTest.TestCaseName = "ThenDisableJsonEncodeShouldSendSerializedObjectMessage";
631            pubnub.PubnubUnitTest = unitTest;
632
633            string channel = "my/channel";
634            object message = "{\"operation\":\"ReturnData\",\"channel\":\"Mobile1\",\"sequenceNumber\":0,\"data\":[\"ping 1.0.0.1\"]}";
635            serializedObjectMessageForPublish = message.ToString();
636
637            EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessSerializedObjectMessageForPublishCallback));
638            EnqueueConditional(() => isSerializedObjectMessageCheck);
639
640            EnqueueCallback(() =>
641                {
642                    if (!isSerializedObjectMessagePublished)
643                    {
644                        EnqueueCallback(() => Assert.IsTrue(isSerializedObjectMessagePublished, "Serialized Object Message Publish Failed"));
645                    }
646                    else
647                    {
648                        EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, serializedMessagePublishTimetoken, -1, false, CaptureSerializedMessagePublishDetailedHistoryCallback));
649                        EnqueueConditional(() => isSerializedMessageDetailedHistoryCheck);
650                        EnqueueCallback(() => Assert.IsTrue(isSerializedObjectMessageDetailedHistory, "Unable to match the successful serialized object message Publish"));
651                    }
652                });
653
654            EnqueueTestComplete();
655        }
656
657        [Asynchronous]
658        private void ReturnSuccessSerializedObjectMessageForPublishCallback(string result)
659        {
660            if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
661            {
662                object[] deserializedResult = JsonConvert.DeserializeObject<object[]>(result);
663                if (deserializedResult is object[])
664                {
665                    long statusCode = Int64.Parse(deserializedResult[0].ToString());
666                    string statusMessage = (string)deserializedResult[1];
667                    if (statusCode == 1 && statusMessage.ToLower() == "sent")
668                    {
669                        isSerializedObjectMessagePublished = true;
670                        serializedMessagePublishTimetoken = Convert.ToInt64(deserializedResult[2].ToString());
671                    }
672                }
673            }
674            isSerializedObjectMessageCheck = true;
675        }
676
677        [Asynchronous]
678        private void CaptureSerializedMessagePublishDetailedHistoryCallback(string result)
679        {
680            if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
681            {
682                object[] deserializedMessage = JsonConvert.DeserializeObject<object[]>(result);
683                if (deserializedMessage is object[])
684                {
685                    JArray message = deserializedMessage[0] as JArray;
686                    if (message != null && message[0].ToString(Formatting.None) == serializedObjectMessageForPublish)
687                    {
688                        isSerializedObjectMessageDetailedHistory = true;
689                    }
690                }
691            }
692
693            isSerializedMessageDetailedHistoryCheck = true;
694        }
695
696        [TestMethod]
697        [Asynchronous]
698        public void ThenLargeMessageShoudFailWithMessageTooLargeInfo()
699        {
700            isLargeMessagePublished = false;
701            Pubnub pubnub = new Pubnub("demo", "demo", "", "", true);
702
703            PubnubUnitTest unitTest = new PubnubUnitTest();
704            unitTest.TestClassName = "WhenAMessageIsPublished";
705            unitTest.TestCaseName = "ThenLargeMessageShoudFailWithMessageTooLargeInfo";
706            pubnub.PubnubUnitTest = unitTest;
707
708            string channel = "my/channel";
709            string message = messageLarge2K.Substring(0,1320);
710
711            EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnPublishMessageTooLargeInfoCallback));
712            EnqueueConditional(() => isPublishMessageTooLargeCheck);
713            EnqueueCallback(() => Assert.IsTrue(isLargeMessagePublished, "Message Too Large is not failing as expected."));
714
715            EnqueueTestComplete();
716        }
717
718        [Asynchronous]
719        private void ReturnPublishMessageTooLargeInfoCallback(string result)
720        {
721            if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
722            {
723                object[] deserializedMessage = JsonConvert.DeserializeObject<object[]>(result);
724                if (deserializedMessage is object[])
725                {
726                    long statusCode = Int64.Parse(deserializedMessage[0].ToString());
727                    string statusMessage = (string)deserializedMessage[1];
728                    if (statusCode == 0 && statusMessage.ToLower() == "message too large")
729                    {
730                        isLargeMessagePublished = true;
731                    }
732                }
733            }
734            isPublishMessageTooLargeCheck = true;
735        }
736
737
738    }
739}