Microsoft.Protocols.TestSuites.MS_OXORULE.S03_ProcessOutOfOfficeRule.MSOXORULE_S03_TC04_OOFBehaviorsForST_KEEP_OOF_HIST C# (CSharp) Method

MSOXORULE_S03_TC04_OOFBehaviorsForST_KEEP_OOF_HIST() private method

        public void MSOXORULE_S03_TC04_OOFBehaviorsForST_KEEP_OOF_HIST()
        {
            this.CheckMAPIHTTPTransportSupported();
            Site.Assume.IsTrue(Common.IsRequirementEnabled(583, this.Site), "This case runs only when the server supports to keep a list of rules with ST_KEEP_OOF_HIST flag.");

            #region Prepare value for ruleProperties variable.
            RuleProperties ruleProperties = AdapterHelper.GenerateRuleProperties(this.Site, Constants.RuleNameOOFReply);
            string setOOFMailAddress = this.User1Name + "@" + this.Domain;
            string userPassword = this.User1Password;
            #endregion

            #region Set TestUser1 to OOF state.
            bool isSetOOFSuccess = this.SUTSetOOFAdapter.SetUserOOFSettings(setOOFMailAddress, userPassword, true);
            Site.Assert.IsTrue(isSetOOFSuccess, "Turn Out of Office on for {0} should succeed.", this.User1Name);
            Thread.Sleep(this.WaitForSetOOFComplete);
            #endregion

            #region Create one reply template for OP_OOF_REPLY action Type.
            ulong replyTemplateMessageID;
            uint replyTemplateMessageHandle;
            TaggedPropertyValue[] addReplyBody = new TaggedPropertyValue[1];
            addReplyBody[0] = new TaggedPropertyValue();
            PropertyTag addReplyBodyPropertyTag = new PropertyTag
            {
                PropertyId = (ushort)PropertyId.PidTagBody,
                PropertyType = (ushort)PropertyType.PtypString
            };
            addReplyBody[0].PropertyTag = addReplyBodyPropertyTag;
            string replyMessageBody = Common.GenerateResourceName(this.Site, Constants.MessageOfOOFReply);
            addReplyBody[0].Value = Encoding.Unicode.GetBytes(replyMessageBody + "\0");
            string replyTemplateSubject = Common.GenerateResourceName(this.Site, Constants.ReplyTemplateSubject);
            byte[] replyTemplateGUID = this.OxoruleAdapter.CreateReplyTemplate(this.InboxFolderHandle, this.InboxFolderID, true, replyTemplateSubject, addReplyBody, out replyTemplateMessageID, out replyTemplateMessageHandle);
            #endregion

            #region TestUser1 adds OP_OOF_REPLY rule with PidTagRuleState set to ST_ENABLED | ST_KEEP_OOF_HIST.

            ReplyActionData replyRuleActionData = new ReplyActionData
            {
                ReplyTemplateGUID = replyTemplateGUID,
                ReplyTemplateFID = this.InboxFolderID,
                ReplyTemplateMID = replyTemplateMessageID
            };

            RuleData ruleDataForReplyRule = AdapterHelper.GenerateValidRuleData(ActionType.OP_OOF_REPLY, TestRuleDataType.ForAdd, 1, RuleState.ST_ENABLED | RuleState.ST_KEEP_OOF_HIST, replyRuleActionData, ruleProperties, null);
            RopModifyRulesResponse ropModifyRulesResponse = this.OxoruleAdapter.RopModifyRules(this.InboxFolderHandle, ModifyRuleFlag.Modify_ReplaceAll, new RuleData[] { ruleDataForReplyRule });
            Site.Assert.AreEqual<uint>(0, ropModifyRulesResponse.ReturnValue, "Adding reply rule should succeed.");
            #endregion

            #region TestUser2 delivers a message to TestUser1 to trigger these rules.
            // Sleep enough time to wait for the rule to take effect.
            Thread.Sleep(this.WaitForTheRuleToTakeEffect);

            // TestUser2 log on to the server.
            this.LogonMailbox(TestUser.TestUser2);
            string mailSubject = Common.GenerateResourceName(this.Site, ruleProperties.ConditionSubjectName + "Title", 1);
            this.DeliverMessageToTriggerRule(this.User1Name, this.User1ESSDN, mailSubject, null);

            // Sleep enough time to wait for the rule to be executed on the delivered message.
            Thread.Sleep(this.WaitForTheRuleToTakeEffect);

            PropertyTag[] propertyTagList = new PropertyTag[2];
            propertyTagList[0].PropertyId = (ushort)PropertyId.PidTagBody;
            propertyTagList[0].PropertyType = (ushort)PropertyType.PtypString;
            propertyTagList[1].PropertyId = (ushort)PropertyId.PidTagSubject;
            propertyTagList[1].PropertyType = (ushort)PropertyType.PtypString;

            uint contentTableHandler = 0;
            int expectedMessageIndex = 0;
            RopQueryRowsResponse getNormalMailMessageContent = this.GetExpectedMessage(this.InboxFolderHandle, ref contentTableHandler, propertyTagList, ref expectedMessageIndex, mailSubject);
            string mailBody = AdapterHelper.PropertyValueConvertToString(getNormalMailMessageContent.RowData.PropertyRows[expectedMessageIndex].PropertyValues[0].Value);
            bool isBodyContainsReplyTemplateBody = mailBody.Contains(replyMessageBody);
            #endregion

            #region TestUser2 delivers a message to TestUser1 to trigger these rules again.
            // Sleep enough time to wait for the rule to take effect.
            Thread.Sleep(this.WaitForTheRuleToTakeEffect);
            mailSubject = Common.GenerateResourceName(this.Site, ruleProperties.ConditionSubjectName + "Title", 2);
            this.DeliverMessageToTriggerRule(this.User1Name, this.User1ESSDN, mailSubject, null);

            // Sleep enough time to wait for the rule to be executed on the delivered message.
            Thread.Sleep(this.WaitForTheRuleToTakeEffect);
            #endregion

            #region TestUser1 gets the message sent by TestUser2.
            // TestUser1 log on to the server.
            this.LogonMailbox(TestUser.TestUser1);
            uint inboxFolderContentsTableHandle = 0;
            PropertyTag[] propertyTags = new PropertyTag[1];
            propertyTags[0].PropertyId = (ushort)PropertyId.PidTagSubject;
            propertyTags[0].PropertyType = (ushort)PropertyType.PtypString;

            uint rowCount = 0;
            RopQueryRowsResponse getInboxMailMessageContent = this.GetExpectedMessage(this.InboxFolderHandle, ref inboxFolderContentsTableHandle, propertyTags, ref rowCount, 1, mailSubject);
            Site.Assert.AreEqual<uint>(0, getInboxMailMessageContent.ReturnValue, "getInboxMailMessageContent should succeed.");
            #endregion

            #region TestUser2 verifies whether can receive the replied message.
            
            // TestUser2 log on to the server.
            this.LogonMailbox(TestUser.TestUser2);
            bool doesUnexpectedMessageExist = this.CheckUnexpectedMessageExist(this.InboxFolderHandle, ref contentTableHandler, propertyTagList, mailSubject);

            #region Capture code
            if (Common.IsRequirementEnabled(5572, this.Site))
            {
                // Add the debug information
                Site.Log.Add(LogEntryKind.Debug, "Verify MS-OXORULE_R5572: the expected reply message index is {0} and the total message count is {1}, and whether the message body contains the reply template body is {2}", expectedMessageIndex, getNormalMailMessageContent.RowCount, isBodyContainsReplyTemplateBody);

                // Verify MS-OXORULE requirement: MS-OXORULE_R5572.
                // The above case shows the rule was not executed twice, which indirectly indicates server adds the normal user into 
                // History List after the rule was executed once.
                Site.CaptureRequirementIfIsFalse(
                    doesUnexpectedMessageExist,
                    5572,
                    @"[In Appendix A: Product Behavior] Implementation does not evaluate the rule (2) if the sender is on the list. (Exchange 2007, Exchange 2010 and Exchange 2016 follow this behavior.)");
            }
            // If R577 is verified, which means the sender was added to the list of recipients.
            this.Site.CaptureRequirement(
                558,
                @"[In Processing Out of Office Rules] If not [the sender is not on the list] and the rule (2) condition evaluates to ""TRUE"", the server MUST add the sender to the list of recipients (2) for the rule (2) in addition to executing the rule (2) action (2).");

            // Add the debug information
            Site.Log.Add(LogEntryKind.Debug, "Verify MS-OXORULE_R583");

            // Verify MS-OXORULE requirement: MS-OXORULE_R583
            // That is verified means when sending two messages, only the first one will execute the rule that has ST_KEEP_OOF_HIST flag, so the server must have kept a list for rules.
            bool isVerifyR583 = isBodyContainsReplyTemplateBody;

            Site.CaptureRequirementIfIsTrue(
                isVerifyR583,
                583,
                @"[In Entering and Exiting the Out of Office State] The server MUST also keep a list for rules (2) that have the ST_KEEP_OOF_HIST flag in the PidTagRuleState property specified in section 3.2.1.2.");
            #endregion
            #endregion

            #region Set TestUser1 back to normal state (not in OOF state)
            // Testuser1 logon to the server
            this.LogonMailbox(TestUser.TestUser1);

            isSetOOFSuccess = this.SUTSetOOFAdapter.SetUserOOFSettings(setOOFMailAddress, userPassword, false);
            Site.Assert.IsTrue(isSetOOFSuccess, "Cancelling Out of Office state for {0} should succeed.", this.User1Name);
            Thread.Sleep(this.WaitForSetOOFComplete);
            #endregion
        }