Microsoft.Protocols.TestSuites.MS_WOPI.MS_WOPI_S17_FileLevelItems.MSWOPI_S17_TC26_UnlockAndRelock C# (CSharp) Method

MSWOPI_S17_TC26_UnlockAndRelock() private method

private MSWOPI_S17_TC26_UnlockAndRelock ( ) : void
return void
        public void MSWOPI_S17_TC26_UnlockAndRelock()
        {
            // Get the file URL.
            string fileUrl = this.AddFileToSUT();

            // Get the WOPI URL.
            string wopiTargetFileUrl = WopiSutManageCodeControlAdapter.GetWOPIRootResourceUrl(fileUrl, WOPIRootResourceUrlType.FileLevel, TokenAndRequestUrlHelper.DefaultUserName, TokenAndRequestUrlHelper.DefaultPassword, TokenAndRequestUrlHelper.DefaultDomain);

            // Get the common header.
            WebHeaderCollection commonHeaders = HeadersHelper.GetCommonHeaders(wopiTargetFileUrl);

            string lockIdentifierValue = Guid.NewGuid().ToString("N");

            // Take a lock for editing a file.
            WopiAdapter.Lock(wopiTargetFileUrl, commonHeaders, lockIdentifierValue);

            string unlockAndRelockIdentifierValue = Guid.NewGuid().ToString("N");
            bool isDeleteFileSuccessful = false;
            bool isRelockSuccessful = false;
            try
            {
                // Get the common header.
                commonHeaders = HeadersHelper.GetCommonHeaders(wopiTargetFileUrl);

                int statusCodeOfUnlockAndRelock = 0;

                // Release and retake a lock for editing a file.
                WOPIHttpResponse httpWebResponseForUnLockAndRelock = WopiAdapter.UnlockAndRelock(wopiTargetFileUrl, commonHeaders, unlockAndRelockIdentifierValue, lockIdentifierValue);
                statusCodeOfUnlockAndRelock = httpWebResponseForUnLockAndRelock.StatusCode;
                isRelockSuccessful = true;

                // Verify MS-WOPI requirement: MS-WOPI_R462
                this.Site.CaptureRequirementIfAreEqual(
                              200,
                              statusCodeOfUnlockAndRelock,
                              462,
                              @"[In UnlockAndRelock] Status code ""200"" means ""Success"".");

                // Get the common header.
                commonHeaders = HeadersHelper.GetCommonHeaders(wopiTargetFileUrl);

                bool isWebExceptionRaise = false;
                try
                {
                    // Delete this file which is refresh locked.
                    WopiAdapter.DeleteFile(wopiTargetFileUrl, commonHeaders);

                    // The file has been deleted, so remove it from the clean up list.
                    this.ExcludeFileFromTheCleanUpProcess(fileUrl);
                    isDeleteFileSuccessful = true;
                }
                catch (WebException webEx)
                {
                    isWebExceptionRaise = true;
                    HttpWebResponse errorResponse = this.GetErrorResponseFromWebException(webEx);
                    this.GetStatusCodeFromHTTPResponse(errorResponse);
                }

                // Verify MS-WOPI requirement: MS-WOPI_R259
                this.Site.CaptureRequirementIfIsTrue(
                              isWebExceptionRaise,
                              259,
                              @"[In HTTP://server/<...>/wopi*/files/<id>] Operation ""UnlockAndRelock"" is used for ""Releases and then retakes a lock for editing a file"".");

                // Verify MS-WOPI requirement: MS-WOPI_R449
                this.Site.CaptureRequirementIfIsTrue(
                              isWebExceptionRaise,
                              449,
                              @"[In UnlockAndRelock] Release and retake a lock for editing a file.");

                // The URI in "UnlockAndRelock" WOPI request follow the "HTTP://server/<...>/wopi*/files/<id>?access_token=<token>" pattern, if the operation execute successfully, capture R432
                // Verify MS-WOPI requirement: MS-WOPI_R432
                this.Site.CaptureRequirement(
                              450,
                              @"[In UnlockAndRelock] HTTP Verb: POST
                              URI: HTTP://server/<...>/wopi*/files/<id>?access_token=<token>");

                // If the "UnlockAndRelock" execute successfully with specified new lock indentifier in header "X-WOPI-Lock" and the file could not be deleted, then capture R455 and R458
                // Verify MS-WOPI requirement: MS-WOPI_R455
                this.Site.CaptureRequirement(
                              455,
                              @"[In UnlockAndRelock] X-WOPI-Lock is a string provided by the WOPI client that the WOPI server MUST use to identify the lock on the file.");

                // Verify MS-WOPI requirement: MS-WOPI_R458
                this.Site.CaptureRequirement(
                              458,
                              @"[In UnlockAndRelock] X-WOPI-OldLock is a string previously provided by the WOPI client that the WOPI server MUST have used to identify the lock on the file.");
            }
            finally
            {
                if (!isDeleteFileSuccessful)
                {
                    // Release a lock for editing a file.
                    commonHeaders = HeadersHelper.GetCommonHeaders(wopiTargetFileUrl);
                    string currentLockIndentifier = isRelockSuccessful ? unlockAndRelockIdentifierValue : lockIdentifierValue;
                    WopiAdapter.UnLock(wopiTargetFileUrl, commonHeaders, currentLockIndentifier);
                }
            }
        }