public async Task VerifyThatReadRdlWorks()
        {
            var siteDir = new CDP4Common.SiteDirectoryData.SiteDirectory(Guid.NewGuid(), this.session.Assembler.Cache, this.uri);
            var JohnDoe = new CDP4Common.SiteDirectoryData.Person(this.person.Iid, this.session.Assembler.Cache, this.uri)
            {
                ShortName = "John"
            };

            this.session.Assembler.Cache.TryAdd(new CacheKey(siteDir.Iid, null),
                                                new Lazy <CDP4Common.CommonData.Thing>(() => siteDir));

            var sitedirDto = new SiteDirectory(siteDir.Iid, 1);
            var rdl        = new CDP4Common.DTO.SiteReferenceDataLibrary(Guid.NewGuid(), 1);

            sitedirDto.SiteReferenceDataLibrary.Add(rdl.Iid);

            var readOutput = new List <Thing>
            {
                sitedirDto,
                rdl
            };

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

            readTaskCompletionSource.SetResult(readOutput);
            this.mockedDal.Setup(x => x.Read(It.IsAny <Thing>(), It.IsAny <CancellationToken>(), It.IsAny <IQueryAttributes>())).Returns(readTaskCompletionSource.Task);

            var srdl = new SiteReferenceDataLibrary(rdl.Iid, null, null);

            srdl.Container = siteDir;

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

            Assert.AreEqual(1, this.session.OpenReferenceDataLibraries.Count());
            Assert.IsTrue(siteDir.SiteReferenceDataLibrary.Any());
        }
        public async Task VerifyThatReadIterationWorks()
        {
            var siteDir = new CDP4Common.SiteDirectoryData.SiteDirectory(Guid.NewGuid(), this.session.Assembler.Cache, this.uri);
            var JohnDoe = new CDP4Common.SiteDirectoryData.Person(this.person.Iid, this.session.Assembler.Cache, this.uri)
            {
                ShortName = "John"
            };
            var modelSetup     = new CDP4Common.SiteDirectoryData.EngineeringModelSetup(Guid.NewGuid(), this.session.Assembler.Cache, this.uri);
            var iterationSetup = new CDP4Common.SiteDirectoryData.IterationSetup(Guid.NewGuid(), this.session.Assembler.Cache, this.uri)
            {
                FrozenOn = DateTime.Now, IterationIid = Guid.NewGuid()
            };
            var mrdl         = new ModelReferenceDataLibrary(Guid.NewGuid(), this.session.Assembler.Cache, this.uri);
            var srdl         = new SiteReferenceDataLibrary(Guid.NewGuid(), this.session.Assembler.Cache, this.uri);
            var activeDomain = new DomainOfExpertise(Guid.NewGuid(), this.session.Assembler.Cache, this.uri);

            mrdl.RequiredRdl = srdl;
            modelSetup.RequiredRdl.Add(mrdl);
            modelSetup.IterationSetup.Add(iterationSetup);
            siteDir.Model.Add(modelSetup);
            siteDir.SiteReferenceDataLibrary.Add(srdl);
            siteDir.Domain.Add(activeDomain);
            siteDir.Person.Add(JohnDoe);

            this.session.Assembler.Cache.TryAdd(new CacheKey(siteDir.Iid, null), new Lazy <CDP4Common.CommonData.Thing>(() => siteDir));
            this.session.Assembler.Cache.TryAdd(new CacheKey(JohnDoe.Iid, null), new Lazy <CDP4Common.CommonData.Thing>(() => JohnDoe));
            this.session.Assembler.Cache.TryAdd(new CacheKey(modelSetup.Iid, null), new Lazy <CDP4Common.CommonData.Thing>(() => modelSetup));
            this.session.Assembler.Cache.TryAdd(new CacheKey(mrdl.Iid, null), new Lazy <CDP4Common.CommonData.Thing>(() => mrdl));
            this.session.Assembler.Cache.TryAdd(new CacheKey(srdl.Iid, null), new Lazy <CDP4Common.CommonData.Thing>(() => srdl));
            this.session.Assembler.Cache.TryAdd(new CacheKey(siteDir.Iid, null), new Lazy <CDP4Common.CommonData.Thing>(() => siteDir));
            this.session.Assembler.Cache.TryAdd(new CacheKey(iterationSetup.Iid, null), new Lazy <CDP4Common.CommonData.Thing>(() => iterationSetup));

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

            var participant = new CDP4Common.SiteDirectoryData.Participant(Guid.NewGuid(), this.session.Assembler.Cache, this.uri)
            {
                Person = this.session.ActivePerson
            };

            modelSetup.Participant.Add(participant);

            var model     = new EngineeringModel(Guid.NewGuid(), 1);
            var iteration = new Iteration(iterationSetup.IterationIid, 10)
            {
                IterationSetup = iterationSetup.Iid
            };

            model.Iteration.Add(iteration.Iid);
            model.EngineeringModelSetup = modelSetup.Iid;

            var readOutput = new List <Thing>
            {
                model,
                iteration
            };

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

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

            var iterationToOpen = new CDP4Common.EngineeringModelData.Iteration(iteration.Iid, null, null);
            var modelToOpen     = new CDP4Common.EngineeringModelData.EngineeringModel(model.Iid, null, null);

            iterationToOpen.Container = modelToOpen;

            await this.session.Read(iterationToOpen, activeDomain);

            this.mockedDal.Verify(x => x.Read(It.Is <Iteration>(i => i.Iid == iterationToOpen.Iid), It.IsAny <CancellationToken>(), It.IsAny <IQueryAttributes>()), Times.Once);

            var pair = this.session.OpenIterations.Single();

            Assert.AreEqual(pair.Value.Item1, activeDomain);

            await this.session.Read(iterationToOpen, activeDomain);

            this.mockedDal.Verify(x => x.Read(It.Is <Iteration>(i => i.Iid == iterationToOpen.Iid), It.IsAny <CancellationToken>(), It.IsAny <IQueryAttributes>()), Times.Exactly(2));

            pair = this.session.OpenIterations.Single();
            Assert.AreEqual(pair.Value.Item1, activeDomain);

            var selectedDomain = this.session.QuerySelectedDomainOfExpertise(iterationToOpen);

            Assert.AreEqual(activeDomain.Iid, selectedDomain.Iid);

            this.mockedDal.Setup(x => x.Read(It.IsAny <Thing>(), It.IsAny <CancellationToken>(), It.IsAny <IQueryAttributes>())).Returns <Thing, CancellationToken, IQueryAttributes>(
                (x, y, z) =>
            {
                // the method with iteration is called
                var xvariable = x;
                return(readTaskCompletionSource.Task);
            });

            await this.session.Refresh();

            this.mockedDal.Verify(x => x.Read <Thing>(It.IsAny <Thing>(), It.IsAny <CancellationToken>(), It.IsAny <IQueryAttributes>()), Times.Exactly(1));

            Assert.ThrowsAsync <InvalidOperationException>(async() => await this.session.Read(iterationToOpen, null));
        }
        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);
        }