public void VerifyException1()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         var helper = new CopyPermissionHelper(null, false);
     });
 }
        /// <summary>
        /// Perform the copy operation of an <see cref="ElementDefinition"/>
        /// </summary>
        /// <param name="elementDefinition">The <see cref="ElementDefinition"/> to copy</param>
        /// <param name="targetIteration">The target container</param>
        /// <param name="keyStates">The <see cref="DragDropKeyStates"/> used in the drag-and-drop operation</param>
        public async Task Copy(ElementDefinition elementDefinition, Iteration targetIteration, DragDropKeyStates keyStates)
        {
            // copy the payload to this iteration
            var ownedIsChanged       = keyStates == Constants.DryCopy || keyStates == Constants.CtrlCopy;
            var copyOperationHelper  = new CopyPermissionHelper(this.session, ownedIsChanged);
            var copyPermissionResult = copyOperationHelper.ComputeCopyPermission(elementDefinition, targetIteration);

            var operationKind = keyStates.GetCopyOperationKind();

            if (copyPermissionResult.ErrorList.Any())
            {
                // show permission information
                var copyConfirmationDialog = new CopyConfirmationDialogViewModel(copyPermissionResult.CopyableThings, copyPermissionResult.ErrorList);

                var dialogResult = this.dialogNavigationService.NavigateModal(copyConfirmationDialog);
                if (dialogResult != null && dialogResult.Result == true)
                {
                    await this.WriteCopyOperation(elementDefinition, targetIteration, operationKind.Value);
                }
            }
            else if (copyPermissionResult.CopyableThings.Any())
            {
                await this.WriteCopyOperation(elementDefinition, targetIteration, operationKind.Value);
            }
        }
        public void VerifyException2()
        {
            var helper = new CopyPermissionHelper(this.session.Object, false);
            var target = this.iteration2.Clone(false);

            Assert.Throws <ArgumentNullException>(() =>
            {
                var res = helper.ComputeCopyPermission(null, target);
            });
        }
        //[ExpectedException(typeof(InvalidOperationException))]
        public void VerifyException5()
        {
            this.session.Setup(x => x.OpenIterations)
            .Returns(new Dictionary <Iteration, Tuple <DomainOfExpertise, Participant> > {
                { this.iteration2, new Tuple <DomainOfExpertise, Participant>(this.domain1, null) }
            });

            var helper = new CopyPermissionHelper(this.session.Object, false);
            var copy   = this.def1.Clone(false);
            var target = this.iteration1.Clone(false);

            Assert.Throws <InvalidOperationException>(() =>
            {
                var res = helper.ComputeCopyPermission(copy, target);
            });
        }
        public void VerifyThatCanCopyAll()
        {
            this.permissionService.Setup(x => x.CanWrite(It.IsAny <ClassKind>(), It.IsAny <Thing>())).Returns(true);
            this.session.Setup(x => x.OpenIterations)
            .Returns(new Dictionary <Iteration, Tuple <DomainOfExpertise, Participant> > {
                { this.iteration2, new Tuple <DomainOfExpertise, Participant>(this.domain1, null) }
            });

            var copy   = this.def1.Clone(false);
            var target = this.iteration2.Clone(false);

            var helper     = new CopyPermissionHelper(this.session.Object, false);
            var canCopyRes = helper.ComputeCopyPermission(copy, target);

            Assert.AreEqual(0, canCopyRes.ErrorList.Count);
            Assert.AreEqual(6, canCopyRes.CopyableThings.Count());
        }
        public void VerifyThatComputationWorksIfMissingRdls()
        {
            this.def2.Category.Add(this.category);
            this.permissionService.Setup(x => x.CanWrite(It.IsAny <ClassKind>(), It.IsAny <Thing>())).Returns(true);
            this.session.Setup(x => x.OpenIterations)
            .Returns(new Dictionary <Iteration, Tuple <DomainOfExpertise, Participant> > {
                { this.iteration2, new Tuple <DomainOfExpertise, Participant>(this.domain1, null) }
            });

            var copy   = this.def1.Clone(false);
            var target = this.iteration2.Clone(false);

            var helper     = new CopyPermissionHelper(this.session.Object, false);
            var canCopyRes = helper.ComputeCopyPermission(copy, target);

            // element def for usage cant be copied - missing rdl
            // usage cant be copied as element def cannot
            Assert.AreEqual(2, canCopyRes.ErrorList.Count);
            Assert.AreEqual(1, canCopyRes.CopyableThings.Count());
        }
        /// <summary>
        /// Compute a set of <see cref="IEnumerable{Operation}"/> from a copy <see cref="Operation"/>
        /// </summary>
        /// <param name="copyOperation">The copy <see cref="Operation"/></param>
        private void ComputeOperations(Operation copyOperation)
        {
            this.copyThingMap = new Dictionary <Poco, Poco>();
            this.copyableIds  = new List <Guid>();
            this.operations   = new List <Operation>();

            var copyDto  = copyOperation.ModifiedThing;
            var copyPoco = copyDto.QuerySourceThing();

            var originalDto  = copyOperation.OriginalThing;
            var originalPoco = originalDto.QuerySourceThing();

            if (copyPoco.TopContainer.ClassKind != ClassKind.EngineeringModel)
            {
                throw new InvalidOperationException("The copy operation on WSP is only implemented for things contained by EngineeringModel.");
            }

            this.copyThingMap.Add(originalPoco, copyPoco);

            // compute the things to copy
            var copyPermissionHelper = new CopyPermissionHelper(this.session, copyOperation.OperationKind.IsCopyChangeOwnerOperation());
            var copyPermissionResult = copyPermissionHelper.ComputeCopyPermission(originalPoco, copyPoco.Container);

            // Add all contained objects
            this.copyableIds.AddRange(copyPermissionResult.CopyableThings.Select(c => c.Iid).ToList());
            if (this.copyableIds.Contains(originalPoco.Iid))
            {
                var updatedIteration = copyPoco.GetContainerOfType <Iteration>();

                this.CreatePocoCopy(copyPoco, updatedIteration);

                // modify the references to point to the copy thing
                this.ModifyReferences();
                if (copyOperation.OperationKind.IsCopyChangeOwnerOperation())
                {
                    this.ChangeOwner(updatedIteration);
                }

                this.CreateOperations();
            }
        }
        public void VerifyThatComputationWorksIfPermissionDenied()
        {
            this.permissionService.Setup(x => x.CanWrite(It.Is <ClassKind>(cls =>
                                                                           cls == ClassKind.ElementDefinition ||
                                                                           cls == ClassKind.ElementUsage ||
                                                                           cls == ClassKind.Parameter ||
                                                                           cls == ClassKind.ParameterSubscription), It.IsAny <Thing>())).Returns(true);
            this.session.Setup(x => x.OpenIterations)
            .Returns(new Dictionary <Iteration, Tuple <DomainOfExpertise, Participant> > {
                { this.iteration2, new Tuple <DomainOfExpertise, Participant>(this.domain1, null) }
            });
            // permission denied for Override

            var copy   = this.def1.Clone(false);
            var target = this.iteration2.Clone(false);

            var helper     = new CopyPermissionHelper(this.session.Object, false);
            var canCopyRes = helper.ComputeCopyPermission(copy, target);

            Assert.AreEqual(1, canCopyRes.ErrorList.Count);
            Assert.AreEqual(4, canCopyRes.CopyableThings.Count());
        }