PageRenderTime 49ms CodeModel.GetById 18ms app.highlight 21ms RepoModel.GetById 5ms app.codeStats 0ms

/silverlight/PubnubSilverlightUnitTest/WhenAMessageIsPublished.cs

https://github.com/Sfyler/c-sharp
C# | 835 lines | 727 code | 108 blank | 0 comment | 110 complexity | 6ca2ddbb649d82724ae70f8df249a690 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;
 13
 14namespace PubnubSilverlight.UnitTest
 15{
 16    [TestClass]
 17    public class WhenAMessageIsPublished : SilverlightTest
 18    {
 19        bool isPublished2 = false;
 20        bool isPublished3 = false;
 21
 22        bool isUnencryptPublished = false;
 23        bool isUnencryptObjectPublished = false;
 24        bool isEncryptObjectPublished = false;
 25        bool isUnencryptDH = false;
 26        bool isUnencryptObjectDH = false;
 27        bool isEncryptObjectDH = false;
 28        bool isEncryptPublished = false;
 29        bool isSecretEncryptPublished = false;
 30        bool isEncryptDH = false;
 31        bool isSecretEncryptDH = false;
 32        bool isComplexObjectPublished = false;
 33        bool isComplexObjectDetailedHistory = false;
 34        bool isSerializedObjectMessagePublished = false;
 35        bool isSerializedObjectMessageDetailedHistory = false;
 36        bool isLargeMessagePublished = false;
 37
 38        long unEncryptPublishTimetoken = 0;
 39        long unEncryptObjectPublishTimetoken = 0;
 40        long encryptObjectPublishTimetoken = 0;
 41        long encryptPublishTimetoken = 0;
 42        long secretEncryptPublishTimetoken = 0;
 43        long complexObjectPublishTimetoken = 0;
 44        long serializedMessagePublishTimetoken = 0;
 45
 46        ManualResetEvent mrePublish = new ManualResetEvent(false);
 47        ManualResetEvent mreDetailedHistory = new ManualResetEvent(false);
 48        ManualResetEvent mreGrant = new ManualResetEvent(false);
 49
 50        const string messageForUnencryptPublish = "Pubnub Messaging API 1";
 51        const string messageForEncryptPublish = "漢語";
 52        const string messageForSecretEncryptPublish = "Pubnub Messaging API 2";
 53        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";
 54        string messageObjectForUnencryptPublish = "";
 55        string messageObjectForEncryptPublish = "";
 56        string messageComplexObjectForPublish = "";
 57        string serializedObjectMessageForPublish;
 58
 59        bool receivedGrantMessage = false;
 60        bool grantInitCallbackInvoked = false;
 61
 62        [ClassInitialize, Asynchronous]
 63        public void Init()
 64        {
 65            if (!PubnubCommon.PAMEnabled)
 66            {
 67                EnqueueTestComplete();
 68                return;
 69            }
 70
 71            receivedGrantMessage = false;
 72
 73            ThreadPool.QueueUserWorkItem((s) =>
 74                {
 75                    Pubnub pubnub = new Pubnub(PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, PubnubCommon.SecretKey, "", false);
 76
 77                    PubnubUnitTest unitTest = new PubnubUnitTest();
 78                    unitTest.TestClassName = "GrantRequestUnitTest";
 79                    unitTest.TestCaseName = "Init";
 80                    pubnub.PubnubUnitTest = unitTest;
 81
 82                    string channel = "hello_my_channel";
 83
 84                    EnqueueCallback(() => pubnub.GrantAccess<string>(channel, true, true, 20, ThenPublishInitializeShouldReturnGrantMessage, DummyErrorCallback));
 85                    mreGrant.WaitOne(310 * 1000);
 86
 87                    EnqueueCallback(() => Assert.IsTrue(receivedGrantMessage, "WhenAClientIsPresent Grant access failed."));
 88
 89                    EnqueueTestComplete();
 90                });
 91        }
 92
 93        [Asynchronous]
 94        void ThenPublishInitializeShouldReturnGrantMessage(string receivedMessage)
 95        {
 96            try
 97            {
 98                if (!string.IsNullOrEmpty(receivedMessage) && !string.IsNullOrEmpty(receivedMessage.Trim()))
 99                {
100                    object[] serializedMessage = JsonConvert.DeserializeObject<object[]>(receivedMessage);
101                    JContainer dictionary = serializedMessage[0] as JContainer;
102                    var status = dictionary["status"].ToString();
103                    if (status == "200")
104                    {
105                        receivedGrantMessage = true;
106                    }
107                }
108            }
109            catch { }
110            finally
111            {
112                mreGrant.Set();
113            }
114        }
115
116        [TestMethod, Asynchronous]
117        public void ThenUnencryptPublishShouldReturnSuccessCodeAndInfo()
118        {
119            isUnencryptPublished = false;
120            mrePublish = new ManualResetEvent(false);
121            mreDetailedHistory = new ManualResetEvent(false);
122
123            ThreadPool.QueueUserWorkItem((s) =>
124                {
125                    Pubnub pubnub = new Pubnub(PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, "", "", false);
126                    string channel = "hello_my_channel";
127                    string message = messageForUnencryptPublish;
128
129                    PubnubUnitTest unitTest = new PubnubUnitTest();
130                    unitTest.TestClassName = "WhenAMessageIsPublished";
131                    unitTest.TestCaseName = "ThenUnencryptPublishShouldReturnSuccessCodeAndInfo";
132                    pubnub.PubnubUnitTest = unitTest;
133
134                    EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessUnencryptPublishCodeCallback, DummyErrorCallback));
135                    mrePublish.WaitOne(310 * 1000);
136
137                    EnqueueCallback(() =>
138                    {
139                        if (!isUnencryptPublished)
140                        {
141                            Assert.IsTrue(isUnencryptPublished, "Unencrypt Publish Failed");
142                        }
143                        else
144                        {
145                            pubnub.DetailedHistory<string>(channel, -1, unEncryptPublishTimetoken, -1, false, CaptureUnencryptDetailedHistoryCallback, DummyErrorCallback);
146                            mreDetailedHistory.WaitOne(310 * 1000);
147                            Assert.IsTrue(isUnencryptDH, "Unable to match the successful unencrypt Publish");
148                        }
149                    });
150
151                    EnqueueTestComplete();
152                });
153        }
154
155        [Asynchronous]
156        public void ReturnSuccessUnencryptPublishCodeCallback(string result)
157        {
158            if (!string.IsNullOrWhiteSpace(result))
159            {
160                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
161                if (receivedObj is object[])
162                {
163                    long statusCode = Int64.Parse(receivedObj[0].ToString());
164                    string statusMsg = (string)receivedObj[1];
165                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
166                    {
167                        isUnencryptPublished = true;
168                        unEncryptPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
169                    }
170                }
171            }
172            mrePublish.Set();
173        }
174
175        [Asynchronous]
176        public void CaptureUnencryptDetailedHistoryCallback(string result)
177        {
178            if (!string.IsNullOrWhiteSpace(result))
179            {
180                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
181                if (receivedObj is object[])
182                {
183                    JArray jObj = JArray.Parse(receivedObj[0].ToString());
184                    if (jObj[0].ToString() == messageForUnencryptPublish)
185                    {
186                        isUnencryptDH = true;
187                    }
188                }
189            }
190
191            mreDetailedHistory.Set();
192        }
193
194        [TestMethod, Asynchronous]
195        public void ThenUnencryptObjectPublishShouldReturnSuccessCodeAndInfo()
196        {
197            isUnencryptObjectPublished = false;
198            mrePublish = new ManualResetEvent(false);
199            mreDetailedHistory = new ManualResetEvent(false);
200
201            ThreadPool.QueueUserWorkItem((s) =>
202                {
203                    Pubnub pubnub = new Pubnub(PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, "", "", false);
204                    string channel = "hello_my_channel";
205
206                    object message = new CustomClass();
207                    messageObjectForUnencryptPublish = JsonConvert.SerializeObject(message);
208
209                    PubnubUnitTest unitTest = new PubnubUnitTest();
210                    unitTest.TestClassName = "WhenAMessageIsPublished";
211                    unitTest.TestCaseName = "ThenUnencryptObjectPublishShouldReturnSuccessCodeAndInfo";
212                    pubnub.PubnubUnitTest = unitTest;
213
214                    EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessUnencryptObjectPublishCodeCallback, DummyErrorCallback));
215                    mrePublish.WaitOne(310 * 1000);
216
217                    EnqueueCallback(() =>
218                    {
219                        if (!isUnencryptObjectPublished)
220                        {
221                            Assert.IsTrue(isUnencryptObjectPublished, "Unencrypt Publish Failed");
222                        }
223                        else
224                        {
225                            pubnub.DetailedHistory<string>(channel, -1, unEncryptObjectPublishTimetoken, -1, false, CaptureUnencryptObjectDetailedHistoryCallback, DummyErrorCallback);
226                            mreDetailedHistory.WaitOne(310 * 1000);
227                            Assert.IsTrue(isUnencryptObjectDH, "Unable to match the successful unencrypt object Publish");
228                        }
229                    });
230
231                    EnqueueTestComplete();
232                });
233        }
234
235        [Asynchronous]
236        public void ReturnSuccessUnencryptObjectPublishCodeCallback(string result)
237        {
238            if (!string.IsNullOrWhiteSpace(result))
239            {
240                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
241                if (receivedObj is object[])
242                {
243                    long statusCode = Int64.Parse(receivedObj[0].ToString());
244                    string statusMsg = (string)receivedObj[1];
245                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
246                    {
247                        isUnencryptObjectPublished = true;
248                        unEncryptObjectPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
249                    }
250                }
251            }
252            mrePublish.Set();
253        }
254
255        [Asynchronous]
256        public void CaptureUnencryptObjectDetailedHistoryCallback(string result)
257        {
258            if (!string.IsNullOrWhiteSpace(result))
259            {
260                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
261                if (receivedObj is object[])
262                {
263                    JArray jArr = JArray.Parse(receivedObj[0].ToString());
264                    if (jArr[0].ToString(Formatting.None) == messageObjectForUnencryptPublish)
265                    {
266                        isUnencryptObjectDH = true;
267                    }
268                }
269            }
270
271            mreDetailedHistory.Set();
272        }
273
274        [TestMethod, Asynchronous]
275        public void ThenEncryptObjectPublishShouldReturnSuccessCodeAndInfo()
276        {
277            isEncryptObjectPublished = false;
278            mrePublish = new ManualResetEvent(false);
279            mreDetailedHistory = new ManualResetEvent(false);
280
281            ThreadPool.QueueUserWorkItem((s) =>
282                {
283                    Pubnub pubnub = new Pubnub(PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, "", "enigma", false);
284                    string channel = "hello_my_channel";
285                    object message = new SecretCustomClass();
286                    messageObjectForEncryptPublish = JsonConvert.SerializeObject(message);
287
288                    PubnubUnitTest unitTest = new PubnubUnitTest();
289                    unitTest.TestClassName = "WhenAMessageIsPublished";
290                    unitTest.TestCaseName = "ThenEncryptObjectPublishShouldReturnSuccessCodeAndInfo";
291                    pubnub.PubnubUnitTest = unitTest;
292
293                    EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessEncryptObjectPublishCodeCallback, DummyErrorCallback));
294                    mrePublish.WaitOne(310 * 1000);
295
296                    EnqueueCallback(() =>
297                    {
298                        if (!isEncryptObjectPublished)
299                        {
300                            Assert.IsTrue(isEncryptObjectPublished, "Encrypt Object Publish Failed");
301                        }
302                        else
303                        {
304                            pubnub.DetailedHistory<string>(channel, -1, encryptObjectPublishTimetoken, -1, false, CaptureEncryptObjectDetailedHistoryCallback, DummyErrorCallback);
305                            mreDetailedHistory.WaitOne(310 * 1000);
306                            Assert.IsTrue(isEncryptObjectDH, "Unable to match the successful encrypt object Publish");
307                        }
308                    });
309
310                    EnqueueTestComplete();
311                });
312        }
313
314        [Asynchronous]
315        public void ReturnSuccessEncryptObjectPublishCodeCallback(string result)
316        {
317            if (!string.IsNullOrWhiteSpace(result))
318            {
319                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
320                if (receivedObj is object[])
321                {
322                    long statusCode = Int64.Parse(receivedObj[0].ToString());
323                    string statusMsg = (string)receivedObj[1];
324                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
325                    {
326                        isEncryptObjectPublished = true;
327                        encryptObjectPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
328                    }
329                }
330            }
331            mrePublish.Set();
332        }
333
334        [Asynchronous]
335        public void CaptureEncryptObjectDetailedHistoryCallback(string result)
336        {
337            if (!string.IsNullOrWhiteSpace(result))
338            {
339                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
340                if (receivedObj is object[])
341                {
342                    JArray jArr = JArray.Parse(receivedObj[0].ToString());
343                    if (jArr[0].ToString(Formatting.None) == messageObjectForEncryptPublish)
344                    {
345                        isEncryptObjectDH = true;
346                    }
347                }
348            }
349
350            mreDetailedHistory.Set();
351        }
352
353        [TestMethod, Asynchronous]
354        public void ThenEncryptPublishShouldReturnSuccessCodeAndInfo()
355        {
356            isEncryptPublished = false;
357            mrePublish = new ManualResetEvent(false);
358            mreDetailedHistory = new ManualResetEvent(false);
359
360            ThreadPool.QueueUserWorkItem((s) =>
361                {
362                    Pubnub pubnub = new Pubnub(PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, "", "enigma", false);
363                    string channel = "hello_my_channel";
364                    string message = messageForEncryptPublish;
365
366                    PubnubUnitTest unitTest = new PubnubUnitTest();
367                    unitTest.TestClassName = "WhenAMessageIsPublished";
368                    unitTest.TestCaseName = "ThenEncryptPublishShouldReturnSuccessCodeAndInfo";
369                    pubnub.PubnubUnitTest = unitTest;
370
371                    EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessEncryptPublishCodeCallback, DummyErrorCallback));
372                    mrePublish.WaitOne(310 * 1000);
373
374                    EnqueueCallback(() =>
375                    {
376                        if (!isEncryptPublished)
377                        {
378                            Assert.IsTrue(isEncryptPublished, "Encrypt Publish Failed");
379                        }
380                        else
381                        {
382                            pubnub.DetailedHistory<string>(channel, -1, encryptPublishTimetoken, -1, false, CaptureEncryptDetailedHistoryCallback, DummyErrorCallback);
383                            mreDetailedHistory.WaitOne(310 * 1000);
384                            Assert.IsTrue(isEncryptDH, "Unable to decrypt the successful Publish");
385                        }
386                    });
387
388                    EnqueueTestComplete();
389                });
390        }
391
392        [Asynchronous]
393        public void ReturnSuccessEncryptPublishCodeCallback(string result)
394        {
395            if (!string.IsNullOrWhiteSpace(result))
396            {
397                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
398                if (receivedObj is object[])
399                {
400                    long statusCode = Int64.Parse(receivedObj[0].ToString());
401                    string statusMsg = (string)receivedObj[1];
402                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
403                    {
404                        isEncryptPublished = true;
405                        encryptPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
406                    }
407                }
408            }
409            mrePublish.Set();
410        }
411
412        [Asynchronous]
413        public void CaptureEncryptDetailedHistoryCallback(string result)
414        {
415            if (!string.IsNullOrWhiteSpace(result))
416            {
417                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
418                if (receivedObj is object[])
419                {
420                    JArray jArr = JArray.Parse(receivedObj[0].ToString());
421                    if (jArr[0].ToString() == messageForEncryptPublish)
422                    {
423                        isEncryptDH = true;
424                    }
425                }
426            }
427
428            mreDetailedHistory.Set();
429        }
430
431        [TestMethod, Asynchronous]
432        public void ThenSecretKeyWithEncryptPublishShouldReturnSuccessCodeAndInfo()
433        {
434            isSecretEncryptPublished = false;
435            mrePublish = new ManualResetEvent(false);
436            mreDetailedHistory = new ManualResetEvent(false);
437
438            ThreadPool.QueueUserWorkItem((s) =>
439                {
440                    Pubnub pubnub = new Pubnub(PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, "key", "enigma", false);
441                    string channel = "hello_my_channel";
442                    string message = messageForSecretEncryptPublish;
443
444                    PubnubUnitTest unitTest = new PubnubUnitTest();
445                    unitTest.TestClassName = "WhenAMessageIsPublished";
446                    unitTest.TestCaseName = "ThenSecretKeyWithEncryptPublishShouldReturnSuccessCodeAndInfo";
447                    pubnub.PubnubUnitTest = unitTest;
448
449                    EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessSecretEncryptPublishCodeCallback, DummyErrorCallback));
450                    mrePublish.WaitOne(310 * 1000);
451
452                    EnqueueCallback(() =>
453                    {
454                        if (!isSecretEncryptPublished)
455                        {
456                            Assert.IsTrue(isSecretEncryptPublished, "Secret Encrypt Publish Failed");
457                        }
458                        else
459                        {
460                            pubnub.DetailedHistory<string>(channel, -1, secretEncryptPublishTimetoken, -1, false, CaptureSecretEncryptDetailedHistoryCallback, DummyErrorCallback);
461                            mreDetailedHistory.WaitOne(310 * 1000);
462                            Assert.IsTrue(isSecretEncryptDH, "Unable to decrypt the successful Secret key Publish");
463                        }
464                    });
465
466                    EnqueueTestComplete();
467                });
468        }
469
470        [Asynchronous]
471        public void ReturnSuccessSecretEncryptPublishCodeCallback(string result)
472        {
473            if (!string.IsNullOrWhiteSpace(result))
474            {
475                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
476                if (receivedObj is object[])
477                {
478                    long statusCode = Int64.Parse(receivedObj[0].ToString());
479                    string statusMsg = (string)receivedObj[1];
480                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
481                    {
482                        isSecretEncryptPublished = true;
483                        secretEncryptPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
484                    }
485                }
486            }
487            mrePublish.Set();
488        }
489
490        [Asynchronous]
491        private void CaptureSecretEncryptDetailedHistoryCallback(string result)
492        {
493            if (!string.IsNullOrWhiteSpace(result))
494            {
495                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
496                if (receivedObj is object[])
497                {
498                    JArray jArr = JArray.Parse(receivedObj[0].ToString());
499                    if (jArr[0].ToString() == messageForSecretEncryptPublish)
500                    {
501                        isSecretEncryptDH = true;
502                    }
503                }
504            }
505
506            mreDetailedHistory.Set();
507        }
508
509        [TestMethod]
510        public void ThenPubnubShouldGenerateUniqueIdentifier()
511        {
512            Pubnub pubnub = new Pubnub(PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, "", "", false);
513
514            Assert.IsNotNull(pubnub.GenerateGuid());
515        }
516
517        [TestMethod]
518        [ExpectedException(typeof(MissingFieldException))]
519        public void ThenPublishKeyShouldNotBeEmpty()
520        {
521            Pubnub pubnub = new Pubnub("", PubnubCommon.SubscribeKey, "", "", false);
522
523            string channel = "hello_my_channel";
524            string message = "Pubnub API Usage Example";
525
526            pubnub.Publish<string>(channel, message, null, DummyErrorCallback);
527        }
528
529        [TestMethod, Asynchronous]
530        public void ThenOptionalSecretKeyShouldBeProvidedInConstructor()
531        {
532            isPublished2 = false;
533            mrePublish = new ManualResetEvent(false);
534
535            ThreadPool.QueueUserWorkItem((s) =>
536                {
537                    Pubnub pubnub = new Pubnub(PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, "key");
538                    string channel = "hello_my_channel";
539                    string message = "Pubnub API Usage Example";
540
541                    PubnubUnitTest unitTest = new PubnubUnitTest();
542                    unitTest.TestClassName = "WhenAMessageIsPublished";
543                    unitTest.TestCaseName = "ThenOptionalSecretKeyShouldBeProvidedInConstructor";
544                    pubnub.PubnubUnitTest = unitTest;
545
546                    EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSecretKeyPublishCallback, DummyErrorCallback));
547                    mrePublish.WaitOne(310 * 1000);
548                    EnqueueCallback(() => Assert.IsTrue(isPublished2, "Publish Failed with secret key"));
549
550                    EnqueueTestComplete();
551                });
552        }
553
554        [Asynchronous]
555        public void ReturnSecretKeyPublishCallback(string result)
556        {
557            if (!string.IsNullOrWhiteSpace(result))
558            {
559                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
560                if (receivedObj is object[])
561                {
562                    long statusCode = Int64.Parse(receivedObj[0].ToString());
563                    string statusMsg = (string)receivedObj[1];
564                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
565                    {
566                        isPublished2 = true;
567                    }
568                }
569            }
570            mrePublish.Set();
571        }
572
573        [TestMethod, Asynchronous]
574        public void IfSSLNotProvidedThenDefaultShouldBeFalse()
575        {
576            isPublished3 = false;
577            mrePublish = new ManualResetEvent(false);
578
579            ThreadPool.QueueUserWorkItem((s) =>
580                {
581                    Pubnub pubnub = new Pubnub(PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, "");
582                    string channel = "hello_my_channel";
583                    string message = "Pubnub API Usage Example";
584
585                    PubnubUnitTest unitTest = new PubnubUnitTest();
586                    unitTest.TestClassName = "WhenAMessageIsPublished";
587                    unitTest.TestCaseName = "IfSSLNotProvidedThenDefaultShouldBeFalse";
588                    pubnub.PubnubUnitTest = unitTest;
589
590                    EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnNoSSLDefaultFalseCallback, DummyErrorCallback));
591                    mrePublish.WaitOne(310 * 1000);
592                    EnqueueCallback(() => Assert.IsTrue(isPublished3, "Publish Failed with no SSL"));
593
594                    EnqueueTestComplete();
595                });
596        }
597
598        [Asynchronous]
599        public void ReturnNoSSLDefaultFalseCallback(string result)
600        {
601            if (!string.IsNullOrWhiteSpace(result))
602            {
603                object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
604                if (receivedObj is object[])
605                {
606                    long statusCode = Int64.Parse(receivedObj[0].ToString());
607                    string statusMsg = (string)receivedObj[1];
608                    if (statusCode == 1 && statusMsg.ToLower() == "sent")
609                    {
610                        isPublished3 = true;
611                    }
612                }
613            }
614            mrePublish.Set();
615        }
616
617        [TestMethod, Asynchronous]
618        public void ThenComplexMessageObjectShouldReturnSuccessCodeAndInfo()
619        {
620            isComplexObjectPublished = false;
621            mrePublish = new ManualResetEvent(false);
622            mreDetailedHistory = new ManualResetEvent(false);
623
624            ThreadPool.QueueUserWorkItem((s) =>
625                {
626                    Pubnub pubnub = new Pubnub(PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, "", "", false);
627
628                    PubnubUnitTest unitTest = new PubnubUnitTest();
629                    unitTest.TestClassName = "WhenAMessageIsPublished";
630                    unitTest.TestCaseName = "ThenComplexMessageObjectShouldReturnSuccessCodeAndInfo";
631                    pubnub.PubnubUnitTest = unitTest;
632
633                    string channel = "hello_my_channel";
634                    object message = new PubnubDemoObject();
635                    messageComplexObjectForPublish = JsonConvert.SerializeObject(message);
636
637                    EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessComplexObjectPublishCodeCallback, DummyErrorCallback));
638                    mrePublish.WaitOne(310 * 1000);
639
640                    EnqueueCallback(() =>
641                        {
642                            if (!isComplexObjectPublished)
643                            {
644                                Assert.IsTrue(isComplexObjectPublished, "Complex Object Publish Failed");
645                            }
646                            else
647                            {
648                                pubnub.DetailedHistory<string>(channel, -1, complexObjectPublishTimetoken, -1, false, CaptureComplexObjectDetailedHistoryCallback, DummyErrorCallback);
649                                mreDetailedHistory.WaitOne(310 * 1000);
650                                Assert.IsTrue(isComplexObjectDetailedHistory, "Unable to match the successful unencrypt object Publish");
651                            }
652                        });
653
654                    EnqueueTestComplete();
655                });
656        }
657
658        [Asynchronous]
659        private void ReturnSuccessComplexObjectPublishCodeCallback(string result)
660        {
661            if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
662            {
663                object[] deserializedMessage = JsonConvert.DeserializeObject<object[]>(result);
664                if (deserializedMessage is object[])
665                {
666                    long statusCode = Int64.Parse(deserializedMessage[0].ToString());
667                    string statusMessage = (string)deserializedMessage[1];
668                    if (statusCode == 1 && statusMessage.ToLower() == "sent")
669                    {
670                        isComplexObjectPublished = true;
671                        complexObjectPublishTimetoken = Convert.ToInt64(deserializedMessage[2].ToString());
672                    }
673                }
674            }
675
676            mrePublish.Set();
677        }
678
679        [Asynchronous]
680        private void CaptureComplexObjectDetailedHistoryCallback(string result)
681        {
682            if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
683            {
684                object[] deserializedMessage = JsonConvert.DeserializeObject<object[]>(result);
685                if (deserializedMessage is object[])
686                {
687                    JArray message = deserializedMessage[0] as JArray;
688                    if (message != null && message[0].ToString(Formatting.None) == messageComplexObjectForPublish)
689                    {
690                        isComplexObjectDetailedHistory = true;
691                    }
692                }
693            }
694
695            mreDetailedHistory.Set();
696        }
697
698        [TestMethod, Asynchronous]
699        public void ThenDisableJsonEncodeShouldSendSerializedObjectMessage()
700        {
701            isSerializedObjectMessagePublished = false;
702            mrePublish = new ManualResetEvent(false);
703            mreDetailedHistory = new ManualResetEvent(false);
704
705            ThreadPool.QueueUserWorkItem((s) =>
706                {
707                    Pubnub pubnub = new Pubnub(PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, "", "", false);
708                    pubnub.EnableJsonEncodingForPublish = false;
709
710                    PubnubUnitTest unitTest = new PubnubUnitTest();
711                    unitTest.TestClassName = "WhenAMessageIsPublished";
712                    unitTest.TestCaseName = "ThenDisableJsonEncodeShouldSendSerializedObjectMessage";
713                    pubnub.PubnubUnitTest = unitTest;
714
715                    string channel = "hello_my_channel";
716                    object message = "{\"operation\":\"ReturnData\",\"channel\":\"Mobile1\",\"sequenceNumber\":0,\"data\":[\"ping 1.0.0.1\"]}";
717                    serializedObjectMessageForPublish = message.ToString();
718
719                    EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessSerializedObjectMessageForPublishCallback, DummyErrorCallback));
720                    mrePublish.WaitOne(310 * 1000);
721
722                    EnqueueCallback(() =>
723                        {
724                            if (!isSerializedObjectMessagePublished)
725                            {
726                                EnqueueCallback(() => Assert.IsTrue(isSerializedObjectMessagePublished, "Serialized Object Message Publish Failed"));
727                            }
728                            else
729                            {
730                                pubnub.DetailedHistory<string>(channel, -1, serializedMessagePublishTimetoken, -1, false, CaptureSerializedMessagePublishDetailedHistoryCallback, DummyErrorCallback);
731                                mreDetailedHistory.WaitOne(310 * 1000);
732                                Assert.IsTrue(isSerializedObjectMessageDetailedHistory, "Unable to match the successful serialized object message Publish");
733                            }
734                        });
735
736                    EnqueueTestComplete();
737                });
738        }
739
740        [Asynchronous]
741        private void ReturnSuccessSerializedObjectMessageForPublishCallback(string result)
742        {
743            if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
744            {
745                object[] deserializedResult = JsonConvert.DeserializeObject<object[]>(result);
746                if (deserializedResult is object[])
747                {
748                    long statusCode = Int64.Parse(deserializedResult[0].ToString());
749                    string statusMessage = (string)deserializedResult[1];
750                    if (statusCode == 1 && statusMessage.ToLower() == "sent")
751                    {
752                        isSerializedObjectMessagePublished = true;
753                        serializedMessagePublishTimetoken = Convert.ToInt64(deserializedResult[2].ToString());
754                    }
755                }
756            }
757            mrePublish.Set();
758        }
759
760        [Asynchronous]
761        private void CaptureSerializedMessagePublishDetailedHistoryCallback(string result)
762        {
763            if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
764            {
765                object[] deserializedMessage = JsonConvert.DeserializeObject<object[]>(result);
766                if (deserializedMessage is object[])
767                {
768                    JArray message = deserializedMessage[0] as JArray;
769                    if (message != null && message[0].ToString(Formatting.None) == serializedObjectMessageForPublish)
770                    {
771                        isSerializedObjectMessageDetailedHistory = true;
772                    }
773                }
774            }
775
776            mreDetailedHistory.Set();
777        }
778
779        [TestMethod, Asynchronous]
780        public void ThenLargeMessageShoudFailWithMessageTooLargeInfo()
781        {
782            isLargeMessagePublished = false;
783            mrePublish = new ManualResetEvent(false);
784
785            ThreadPool.QueueUserWorkItem((s) =>
786                {
787                    Pubnub pubnub = new Pubnub(PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, "", "", true);
788
789                    PubnubUnitTest unitTest = new PubnubUnitTest();
790                    unitTest.TestClassName = "WhenAMessageIsPublished";
791                    unitTest.TestCaseName = "ThenLargeMessageShoudFailWithMessageTooLargeInfo";
792                    pubnub.PubnubUnitTest = unitTest;
793
794                    string channel = "hello_my_channel";
795                    string message = messageLarge2K.Substring(0, 1320);
796                    EnqueueCallback(() => pubnub.Publish<string>(channel, message, DummyPublishMessageTooLargeInfoCallback, PublishMessageTooLargeErrorCallback));
797                    mrePublish.WaitOne(310 * 100);
798                    EnqueueCallback(() => Assert.IsTrue(isLargeMessagePublished, "Message Too Large is not failing as expected."));
799
800                    EnqueueTestComplete();
801                });
802        }
803
804        [Asynchronous]
805        private void DummyPublishMessageTooLargeInfoCallback(string result)
806        {
807        }
808
809        [Asynchronous]
810        private void PublishMessageTooLargeErrorCallback(PubnubClientError result)
811        {
812            if (result != null && result.StatusCode > 0)
813            {
814                string message = result.Message;
815                object[] deserializedMessage = JsonConvert.DeserializeObject<object[]>(message);
816                if (deserializedMessage is object[])
817                {
818                    long statusCode = Int64.Parse(deserializedMessage[0].ToString());
819                    string statusMessage = (string)deserializedMessage[1];
820                    if (statusCode == 0 && statusMessage.ToLower() == "message too large")
821                    {
822                        isLargeMessagePublished = true;
823                    }
824                }
825            }
826            mrePublish.Set();
827        }
828
829        [Asynchronous]
830        private void DummyErrorCallback(PubnubClientError result)
831        {
832        }
833
834    }
835}