A static class contains all helper methods used in test cases.
        /// <summary>
        /// Create a Calendar instance with 'Subject', 'OrganizerName', 'OrganizerEmail', 'Location'
        /// 'TimeZone', 'Body' and 'UID' elements.
        /// </summary>
        /// <returns>The collection of created Calendar items.</returns>
        public Dictionary <Request.ItemsChoiceType8, object> CreateDefaultCalendar()
        {
            Dictionary <Request.ItemsChoiceType8, object> calendarItem = new Dictionary <Request.ItemsChoiceType8, object>
            {
                {
                    Request.ItemsChoiceType8.Subject, this.SubjectName
                },
                {
                    Request.ItemsChoiceType8.Timezone,
                    "IP7//ygAVQBUAEMAKwAwADgAOgAwADAAKQAgAEIAZQBpAGoAaQBuAGcALAAgAEMAaABvAG4AZwBxAGkAbgBnACwAIAAAAAAAAAAAAAAAAAAAAAAAAAAAACgAVQBUAEMAKwAwADgAOgAwADAAKQAgAEIAZQBpAGoAaQBuAGcALAAgAEMAaABvAG4AZwBxAGkAbgBnACwAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="
                },
                {
                    Request.ItemsChoiceType8.Body, TestSuiteHelper.CreateCalendarBody(1, this.Content)
                }
            };

            if (Common.GetConfigurationPropertyValue("ActiveSyncProtocolVersion", this.Site) != "12.1" &&
                Common.GetConfigurationPropertyValue("ActiveSyncProtocolVersion", this.Site) != "14.0" &&
                Common.GetConfigurationPropertyValue("ActiveSyncProtocolVersion", this.Site) != "14.1")
            {
                calendarItem.Add(Request.ItemsChoiceType8.ClientUid, Guid.NewGuid().ToString());
            }
            else
            {
                calendarItem.Add(Request.ItemsChoiceType8.OrganizerEmail, Common.GetMailAddress(this.User1Information.UserName, this.User1Information.UserDomain));
                calendarItem.Add(Request.ItemsChoiceType8.OrganizerName, this.User1Information.UserName);
                calendarItem.Add(Request.ItemsChoiceType8.Location, this.Location);
                calendarItem.Add(Request.ItemsChoiceType8.UID, Guid.NewGuid().ToString());
            }

            return(calendarItem);
        }
        /// <summary>
        /// Using mail with mime content to send a meeting request or cancel request
        /// </summary>
        /// <param name="calendarItem">Calendar information</param>
        /// <param name="subjectName">The subject name of meeting request mail</param>
        /// <param name="organizerEmailAddress">The organizer email address</param>
        /// <param name="attendeeEmailAddress">The attendee email address</param>
        /// <param name="method">Specify normal appointments from meeting requests, responses, and cancellations, it can be set to 'REQUEST', 'REPLY', or 'CANCEL'</param>
        /// <param name="replyMethod">Specify REPLY method, it can be set to 'ACCEPTED', 'TENTATIVE', or 'DECLINED'</param>
        protected void SendMimeMeeting(Calendar calendarItem, string subjectName, string organizerEmailAddress, string attendeeEmailAddress, string method, string replyMethod)
        {
            string icalendarContent = string.Empty;

            switch (method.ToUpper(CultureInfo.CurrentCulture))
            {
            case "REQUEST":
            case "CANCEL":
                icalendarContent = TestSuiteHelper.CreateiCalendarFormatContent(calendarItem, method, replyMethod, organizerEmailAddress, attendeeEmailAddress);
                break;

            case "REPLY":
                icalendarContent = TestSuiteHelper.CreateiCalendarFormatContent(calendarItem, method, replyMethod, attendeeEmailAddress, organizerEmailAddress);
                break;
            }

            string body = string.Empty;

            string mime = TestSuiteHelper.CreateMeetingRequestMime(
                organizerEmailAddress,
                attendeeEmailAddress,
                subjectName,
                body,
                icalendarContent);

            // Send a meeting request
            SendMailRequest  sendMailRequest  = TestSuiteHelper.CreateSendMailRequest(TestSuiteHelper.Next(), false, mime);
            SendMailResponse sendMailResponse = this.CALAdapter.SendMail(sendMailRequest);

            Site.Assert.AreEqual <string>(
                string.Empty,
                sendMailResponse.ResponseDataXML,
                "The server should return an empty xml response data to indicate SendMail command success.");
        }
        /// <summary>
        /// Add a calendar to server, and if add is success then sync CalendarFolder.
        /// </summary>
        /// <param name="items">The dictionary store calendar item's element name and element value</param>
        /// <returns>Return the sync response</returns>
        public SyncStore AddSyncCalendar(Dictionary <Request.ItemsChoiceType8, object> items)
        {
            // Create a default calendar instance with Subject, TimeZone, Body, OrganizerEmail, OrganizerName, Location and UID elements
            Dictionary <Request.ItemsChoiceType8, object> calendar = this.CreateDefaultCalendar();

            // Add elements
            if (items != null)
            {
                foreach (KeyValuePair <Request.ItemsChoiceType8, object> item in items)
                {
                    if (calendar.ContainsKey(item.Key))
                    {
                        calendar[item.Key] = item.Value;
                    }
                    else
                    {
                        calendar.Add(item.Key, item.Value);
                    }
                }
            }

            Request.SyncCollectionAddApplicationData addCalendar = new Request.SyncCollectionAddApplicationData
            {
                Items            = calendar.Values.ToArray <object>(),
                ItemsElementName = calendar.Keys.ToArray <Request.ItemsChoiceType8>()
            };

            // Sync to get the SyncKey
            SyncStore initializeSyncResponse = this.InitializeSync(this.CurrentUserInformation.CalendarCollectionId, null);

            // Add the calendar item
            SyncRequest syncRequest          = TestSuiteHelper.CreateSyncAddRequest(this.CurrentUserInformation.CalendarCollectionId, initializeSyncResponse.SyncKey, addCalendar);
            SyncStore   syncCalendarResponse = this.CALAdapter.Sync(syncRequest);

            // Verify sync response, if the Sync command executes successfully, the Status in response should be 1.
            Site.Assert.AreEqual <byte>(
                1,
                syncCalendarResponse.CollectionStatus,
                "If the Sync command executes successfully, the Status in response should be 1.");

            if (syncCalendarResponse.AddResponses != null)
            {
                foreach (Response.SyncCollectionsCollectionResponsesAdd response in syncCalendarResponse.AddResponses)
                {
                    if (response.Status.Equals(byte.Parse("1")))
                    {
                        // Sync command to do an initialization Sync, and get the server changes through sync command
                        syncCalendarResponse = this.SyncChanges(this.CurrentUserInformation.CalendarCollectionId);
                    }
                    else
                    {
                        return(syncCalendarResponse);
                    }
                }
            }

            return(syncCalendarResponse);
        }
        /// <summary>
        /// Delete all items from the specified collection
        /// </summary>
        /// <param name="collectionId">The specified collection id</param>
        public void DeleteAllItems(string collectionId)
        {
            ItemOperationsRequest itemOperationsRequest       = TestSuiteHelper.CreateItemOperationsEmptyFolderContentsRequest(collectionId);
            ItemOperationsStore   emptyFolderContentsResponse = this.CALAdapter.ItemOperations(itemOperationsRequest);

            // Verify itemOperations response, if the command executes successfully, the Status in response should be 1.
            Site.Assert.AreEqual <string>(
                "1",
                emptyFolderContentsResponse.Status,
                "If the command executes successfully, the Status in response should be 1.");
        }
        /// <summary>
        /// Initialize the sync with server
        /// </summary>
        /// <param name="collectionId">Specify the folder collection Id which needs to be synced.</param>
        /// <param name="supported">Specifies which contact and calendar elements in a Sync request are managed by the client and therefore not ghosted.</param>
        /// <returns>Return Sync response</returns>
        public SyncStore InitializeSync(string collectionId, Request.Supported supported)
        {
            // Obtains the key by sending an initial Sync request with a SyncKey element value of zero and the CollectionId element
            SyncRequest initializeSyncRequest  = TestSuiteHelper.InitializeSyncRequest(collectionId, supported);
            SyncStore   initializeSyncResponse = this.CALAdapter.Sync(initializeSyncRequest);

            // Verify sync result
            Site.Assert.AreEqual <byte>(
                1,
                initializeSyncResponse.CollectionStatus,
                "If the Sync command executes successfully, the Status in response should be 1.");

            return(initializeSyncResponse);
        }
        /// <summary>
        /// Delete all the items in a folder.
        /// </summary>
        /// <param name="createdItemsCollection">The collection of items which should be deleted.</param>
        public void DeleteItemsInFolder(Collection <CreatedItems> createdItemsCollection)
        {
            foreach (CreatedItems itemsToFolder in createdItemsCollection)
            {
                SyncStore result = this.SyncChanges(itemsToFolder.CollectionId);

                if (result.AddElements != null)
                {
                    SyncRequest deleteRequest;
                    foreach (SyncItem item in result.AddElements)
                    {
                        if (itemsToFolder.CollectionId == this.CurrentUserInformation.CalendarCollectionId)
                        {
                            foreach (string subject in itemsToFolder.ItemSubject)
                            {
                                if (item.Calendar != null)
                                {
                                    if (item.Calendar.Subject.Equals(subject, StringComparison.CurrentCultureIgnoreCase))
                                    {
                                        deleteRequest = TestSuiteHelper.CreateSyncDeleteRequest(itemsToFolder.CollectionId, result.SyncKey, item.ServerId);
                                        SyncStore deleteResult = this.CALAdapter.Sync(deleteRequest);
                                        Site.Assert.AreEqual <byte>(1, deleteResult.CollectionStatus, "Item should be deleted.");
                                    }
                                }
                            }
                        }

                        if (itemsToFolder.CollectionId == this.CurrentUserInformation.InboxCollectionId || itemsToFolder.CollectionId == this.CurrentUserInformation.DeletedItemsCollectionId || itemsToFolder.CollectionId == this.CurrentUserInformation.SentItemsCollectionId)
                        {
                            foreach (string subject in itemsToFolder.ItemSubject)
                            {
                                if (item.Email != null)
                                {
                                    if (item.Email.Subject.Equals(subject, StringComparison.CurrentCultureIgnoreCase))
                                    {
                                        deleteRequest = TestSuiteHelper.CreateSyncDeleteRequest(itemsToFolder.CollectionId, result.SyncKey, item.ServerId);
                                        SyncStore deleteResult = this.CALAdapter.Sync(deleteRequest);
                                        Site.Assert.AreEqual <byte>(1, deleteResult.CollectionStatus, "Item should be deleted.");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Builds a Sync add request by using the specified sync key, folder collection ID and add application data.
        /// In general, returns the XMl formatted Sync request as follows:
        /// <!--
        /// <?xml version="1.0" encoding="utf-8"?>
        /// <Sync xmlns="AirSync">
        ///   <Collections>
        ///     <Collection>
        ///       <SyncKey>0</SyncKey>
        ///       <CollectionId>5</CollectionId>
        ///       <GetChanges>1</GetChanges>
        ///       <WindowSize>152</WindowSize>
        ///        <Commands>
        ///            <Add>
        ///                <ServerId>5:1</ServerId>
        ///                <ApplicationData>
        ///                    ...
        ///                </ApplicationData>
        ///            </Add>
        ///        </Commands>
        ///     </Collection>
        ///   </Collections>
        /// </Sync>
        /// -->
        /// </summary>
        /// <param name="collectionId">Specify the server ID of the folder to be synchronized, which can be returned by ActiveSync FolderSync command(Refer to [MS-ASCMD]2.2.3.30.5)</param>
        /// <param name="syncKey">Specify the sync key obtained from the last sync response(Refer to [MS-ASCMD]2.2.3.166.4)</param>
        /// <param name="addCalendars">Contains the data used to specify the Add element for Sync command(Refer to [MS-ASCMD]2.2.3.7.2)</param>
        /// <returns>Returns the SyncRequest instance</returns>
        internal static SyncRequest CreateSyncAddRequest(string collectionId, string syncKey, Request.SyncCollectionAddApplicationData addCalendars)
        {
            SyncRequest syncAddRequest;

            Request.SyncCollectionAdd add = new Request.SyncCollectionAdd
            {
                ClientId        = TestSuiteHelper.Next(),
                ApplicationData = addCalendars
            };

            List <object> commandList = new List <object> {
                add
            };

            // The Sync request include the GetChanges element of the Collection element will set to 0 (FALSE)
            syncAddRequest = TestSuiteHelper.CreateSyncRequest(collectionId, syncKey, false);
            syncAddRequest.RequestData.Collections[0].Commands = commandList.ToArray();

            return(syncAddRequest);
        }
        /// <summary>
        /// Sync changes between client and server
        /// </summary>
        /// <param name="collectionId">Specify the folder collection Id which needs to be synced.</param>
        /// <returns>Return the sync response</returns>
        public SyncStore SyncChanges(string collectionId)
        {
            SyncStore syncResponse;

            int retryCount = int.Parse(Common.GetConfigurationPropertyValue("RetryCount", this.Site));
            int waitTime   = int.Parse(Common.GetConfigurationPropertyValue("WaitTime", this.Site));
            int counter    = 0;

            do
            {
                Thread.Sleep(waitTime);

                // Sync to get the SyncKey
                SyncStore initializeSyncResponse = this.InitializeSync(collectionId, null);

                // Get the server changes through sync command
                SyncRequest syncRequest = TestSuiteHelper.CreateSyncRequest(collectionId, initializeSyncResponse.SyncKey, true);
                syncResponse = this.CALAdapter.Sync(syncRequest);
                if (syncResponse != null)
                {
                    if (syncResponse.CollectionStatus == 1)
                    {
                        break;
                    }
                }

                counter++;
            }while (counter < retryCount);

            // Verify sync response
            Site.Assert.AreEqual <byte>(
                1,
                syncResponse.CollectionStatus,
                "If the Sync command executes successfully, the Status in response should be 1.");

            return(syncResponse);
        }