public async Task VerifyThatCloseModelWorks()
        {
            var siteDir     = new CDP4Common.SiteDirectoryData.SiteDirectory(Guid.NewGuid(), null, null);
            var modelRdlDto = new CDP4Common.DTO.ModelReferenceDataLibrary {
                Iid = Guid.NewGuid()
            };
            var siteDirDto = new CDP4Common.DTO.SiteDirectory {
                Iid = Guid.NewGuid()
            };
            var requiredPocoDto = new CDP4Common.DTO.SiteReferenceDataLibrary {
                Iid = Guid.NewGuid()
            };
            var containerEngModelSetupDto = new EngineeringModelSetup {
                Iid = Guid.NewGuid()
            };
            var containerEngModelSetup = new CDP4Common.SiteDirectoryData.EngineeringModelSetup {
                Iid = containerEngModelSetupDto.Iid
            };
            var iterationDto = new Iteration {
                Iid = Guid.NewGuid()
            };
            var iteration = new CDP4Common.EngineeringModelData.Iteration {
                Iid = iterationDto.Iid
            };
            var iterationSetupDto = new CDP4Common.DTO.IterationSetup {
                Iid = Guid.NewGuid(), IterationIid = iterationDto.Iid
            };

            iterationDto.IterationSetup = iterationSetupDto.IterationIid;
            siteDir.Model.Add(containerEngModelSetup);
            modelRdlDto.RequiredRdl = requiredPocoDto.Iid;

            var credentials = new Credentials("admin", "pass", new Uri("http://www.rheagroup.com"));
            var session2    = new Session(this.mockedDal.Object, credentials);

            var iterationSetup = new CDP4Common.SiteDirectoryData.IterationSetup {
                Iid = iterationSetupDto.Iid, Container = containerEngModelSetup, IterationIid = iteration.Iid
            };
            var thingsToAdd = new List <Thing> {
                siteDirDto, requiredPocoDto, containerEngModelSetupDto, modelRdlDto, iterationSetupDto
            };

            await session2.Assembler.Synchronize(thingsToAdd);

            var lazyiteration = new Lazy <CDP4Common.CommonData.Thing>(() => iteration);

            session2.Assembler.Cache.GetOrAdd(new CacheKey(iterationDto.Iid, null), lazyiteration);

            CDP4Common.CommonData.Thing changedObject = null;
            CDPMessageBus.Current.Listen <ObjectChangedEvent>(typeof(CDP4Common.EngineeringModelData.Iteration)).Subscribe(x => changedObject = x.ChangedThing);
            await session2.CloseIterationSetup(iterationSetup);

            Assert.NotNull(changedObject);
        }
        public async Task VerifyThatCloseModelRdlWorks()
        {
            var siteDir = new CDP4Common.SiteDirectoryData.SiteDirectory(Guid.NewGuid(), null, null);
            var JohnDoe = new CDP4Common.SiteDirectoryData.Person(this.person.Iid, this.session.Assembler.Cache, this.uri)
            {
                ShortName = "John"
            };
            var modelRdlDto = new CDP4Common.DTO.ModelReferenceDataLibrary()
            {
                Iid = Guid.NewGuid()
            };
            var siteDirDto = new CDP4Common.DTO.SiteDirectory()
            {
                Iid = Guid.NewGuid()
            };
            var requiredPocoDto = new CDP4Common.DTO.SiteReferenceDataLibrary()
            {
                Iid = Guid.NewGuid()
            };
            var requiredPocoRdl           = new SiteReferenceDataLibrary(Guid.NewGuid(), null, null);
            var containerEngModelSetupDto = new EngineeringModelSetup()
            {
                Iid = Guid.NewGuid()
            };
            var containerEngModelSetup = new CDP4Common.SiteDirectoryData.EngineeringModelSetup()
            {
                Iid = containerEngModelSetupDto.Iid
            };

            siteDir.Model.Add(containerEngModelSetup);
            modelRdlDto.RequiredRdl = requiredPocoDto.Iid;
            siteDir.Person.Add(JohnDoe);

            var credentials = new Credentials("admin", "pass", new Uri("http://www.rheagroup.com"));
            var session2    = new Session(this.mockedDal.Object, credentials);

            session2.GetType().GetProperty("ActivePerson").SetValue(session2, JohnDoe, null);

            var modelRdlPoco = new ModelReferenceDataLibrary {
                Iid = modelRdlDto.Iid, Name = modelRdlDto.Name, ShortName = modelRdlDto.ShortName, Container = containerEngModelSetup, RequiredRdl = requiredPocoRdl
            };
            var thingsToAdd = new List <Thing>()
            {
                siteDirDto, requiredPocoDto, containerEngModelSetupDto, modelRdlDto
            };

            await session2.Assembler.Synchronize(thingsToAdd);

            await session2.Read(modelRdlPoco);

            Assert.AreEqual(2, session2.OpenReferenceDataLibraries.ToList().Count());

            Lazy <CDP4Common.CommonData.Thing> rdlPocoToClose;

            session2.Assembler.Cache.TryGetValue(new CacheKey(modelRdlPoco.Iid, null), out rdlPocoToClose);
            Assert.NotNull(rdlPocoToClose);
            await session2.CloseModelRdl((ModelReferenceDataLibrary)rdlPocoToClose.Value);

            // Checkt that closing a modelRDL doesn't close it's required SiteRDL
            Assert.AreEqual(1, session2.OpenReferenceDataLibraries.ToList().Count());
            Assert.AreEqual(ClassKind.SiteReferenceDataLibrary, session2.OpenReferenceDataLibraries.First().ClassKind);
        }
        public async Task VerifyThatSiteRdlRequiredByModelRdlCannotBeClosed()
        {
            var rdlDto = new CDP4Common.DTO.SiteReferenceDataLibrary {
                Iid = Guid.NewGuid()
            };
            var siteDirDto = new CDP4Common.DTO.SiteDirectory {
                Iid = Guid.NewGuid()
            };
            var requiredRdlDto = new CDP4Common.DTO.SiteReferenceDataLibrary()
            {
                Iid = Guid.NewGuid()
            };

            rdlDto.RequiredRdl = requiredRdlDto.Iid;
            siteDirDto.SiteReferenceDataLibrary.Add(rdlDto.Iid);
            siteDirDto.SiteReferenceDataLibrary.Add(requiredRdlDto.Iid);

            siteDirDto.Person.Add(this.person.Iid);

            var mrdl = new CDP4Common.DTO.ModelReferenceDataLibrary(Guid.NewGuid(), 0)
            {
                RequiredRdl = requiredRdlDto.Iid
            };
            var modelsetup = new EngineeringModelSetup(Guid.NewGuid(), 0);

            modelsetup.RequiredRdl.Add(mrdl.Iid);

            var model = new EngineeringModel(Guid.NewGuid(), 0)
            {
                EngineeringModelSetup = modelsetup.Iid
            };
            var iteration = new Iteration(Guid.NewGuid(), 0);

            model.Iteration.Add(iteration.Iid);

            siteDirDto.Model.Add(modelsetup.Iid);

            var readReturn = new List <Thing>
            {
                siteDirDto,
                mrdl,
                modelsetup,
                model,
                iteration,
                this.person
            };

            var mrdlpoco       = new ModelReferenceDataLibrary(mrdl.Iid, null, null);
            var modelsetuppoco = new CDP4Common.SiteDirectoryData.EngineeringModelSetup(modelsetup.Iid, null, null);

            modelsetuppoco.RequiredRdl.Add(mrdlpoco);

            var participant = new CDP4Common.DTO.Participant(Guid.NewGuid(), 0)
            {
                Person = this.person.Iid
            };

            modelsetup.Participant.Add(participant.Iid);
            var modelPoco = new CDP4Common.EngineeringModelData.EngineeringModel(model.Iid, null, null)
            {
                EngineeringModelSetup = modelsetuppoco
            };
            var iterationPoco = new CDP4Common.EngineeringModelData.Iteration(iteration.Iid, null, null);

            modelPoco.Iteration.Add(iterationPoco);

            var readTaskCompletionSource = new TaskCompletionSource <IEnumerable <Thing> >();

            readTaskCompletionSource.SetResult(readReturn);
            this.mockedDal.Setup(
                x => x.Read(It.IsAny <Iteration>(), It.IsAny <CancellationToken>(), null))
            .Returns(readTaskCompletionSource.Task);

            var thingsToAdd = new List <Thing>()
            {
                siteDirDto, requiredRdlDto, rdlDto, this.person, participant, modelsetup
            };

            await this.session.Assembler.Synchronize(thingsToAdd);

            var JohnDoe = this.session.RetrieveSiteDirectory().Person.Single(x => x.Iid == this.person.Iid);

            this.session.GetType().GetProperty("ActivePerson").SetValue(this.session, JohnDoe, null);

            await this.session.Read(iterationPoco, null);

            Assert.AreEqual(2, this.session.OpenReferenceDataLibraries.Count());

            Lazy <CDP4Common.CommonData.Thing> requiredRdlToClose;

            this.session.Assembler.Cache.TryGetValue(new CacheKey(requiredRdlDto.Iid, null), out requiredRdlToClose);

            await this.session.CloseRdl((SiteReferenceDataLibrary)requiredRdlToClose.Value);

            Assert.AreEqual(2, this.session.OpenReferenceDataLibraries.Count());
        }