public void UpsertSocSkillMatrixTest(bool skillAvailable)
        {
            //Arrange
            var dummySocSkill = new SocSkillMatrix {
                Title = "title test", SocCode = nameof(SocSkillMatrix.SocCode), Skill = nameof(SocSkillMatrix.Skill)
            };
            var dummyDynamicContent = A.Dummy <DynamicContent>();

            // Dummies and fakes
            SetupRepositoryCalls(dummyDynamicContent, skillAvailable);

            var socSkillMatrixRepository = new SocSkillMatrixRepository(fakeFrameworkSkillRepository, fakeSocMatrixRepository, fakeDynamicContentExtensions, fakeSocCodeRepository, fakeSocSkillConverter);

            // Act
            socSkillMatrixRepository.UpsertSocSkillMatrix(dummySocSkill);

            // Assert
            if (!skillAvailable)
            {
                A.CallTo(() => fakeSocMatrixRepository.Create()).MustHaveHappenedOnceExactly();
                A.CallTo(() => fakeSocMatrixRepository.Add(dummyDynamicContent)).MustHaveHappenedOnceExactly();
            }
            else
            {
                A.CallTo(() => fakeSocMatrixRepository.GetMaster(dummyDynamicContent))
                .MustHaveHappenedOnceExactly();
                A.CallTo(() => fakeSocMatrixRepository.GetTemp(dummyDynamicContent)).MustHaveHappenedOnceExactly();
                A.CallTo(() => fakeSocMatrixRepository.CheckinTemp(dummyDynamicContent)).MustHaveHappenedOnceExactly();
                A.CallTo(() => fakeSocMatrixRepository.Publish(dummyDynamicContent, A <string> ._))
                .MustHaveHappenedOnceExactly();
                A.CallTo(() =>
                         fakeDynamicContentExtensions.SetRelatedFieldValue(dummyDynamicContent, dummyDynamicContent, A <string> ._, A <float> ._)).MustHaveHappened(2, Times.OrLess);
                A.CallTo(() =>
                         fakeDynamicContentExtensions.DeleteRelatedFieldValues(dummyDynamicContent, A <string> ._))
                .MustHaveHappened(2, Times.OrLess);
                A.CallTo(() =>
                         fakeDynamicContentExtensions.SetFieldValue(dummyDynamicContent, A <string> ._, A <string> ._))
                .MustHaveHappened();
                A.CallTo(() =>
                         fakeDynamicContentExtensions.SetFieldValue(dummyDynamicContent, A <string> ._, A <decimal?> ._))
                .MustHaveHappened(2, Times.Exactly);
                A.CallTo(() => fakeSocCodeRepository.Get(A <Expression <Func <DynamicContent, bool> > > .That.Matches(m =>
                                                                                                                      LinqExpressionsTestHelper.IsExpressionEqual(m, d =>
                                                                                                                                                                  d.Status == ContentLifecycleStatus.Master &&
                                                                                                                                                                  d.GetValue <string>(nameof(SocCode.SOCCode)) == dummySocSkill.SocCode))))
                .MustHaveHappened();

                A.CallTo(() => fakeSocMatrixRepository.Get(A <Expression <Func <DynamicContent, bool> > > .That.Matches(m =>
                                                                                                                        LinqExpressionsTestHelper.IsExpressionEqual(m, item =>
                                                                                                                                                                    item.Visible && item.Status == ContentLifecycleStatus.Live && item.UrlName == dummySocSkill.SfUrlName)))).MustHaveHappened();
                A.CallTo(() => fakeSocMatrixRepository.Commit()).MustHaveHappened();
            }
        }
        public RepoActionResult UpdateSocSkillMatrices(JobProfileOverloadForWhatItTakes jobProfile, IEnumerable <SocSkillMatrix> socSkillMatrices)
        {
            var jobprofile = repository.Get(item =>
                                            item.UrlName == jobProfile.UrlName && item.Status == ContentLifecycleStatus.Live && item.Visible);

            var skillMatrices = socSkillMatrices as IList <SocSkillMatrix> ?? socSkillMatrices.ToList();

            if (jobprofile != null && skillMatrices.Any())
            {
                var master = repository.GetMaster(jobprofile);

                dynamicContentExtensions.DeleteRelatedFieldValues(master, RelatedSkillField);

                float ordinal = 1;
                foreach (var socSkillMatrix in skillMatrices)
                {
                    var relatedSocSkillItem = socSkillRepository.Get(d => d.Status == ContentLifecycleStatus.Master && d.UrlName == socSkillMatrix.SfUrlName);

                    if (relatedSocSkillItem != null)
                    {
                        dynamicContentExtensions.SetRelatedFieldValue(master, relatedSocSkillItem, RelatedSkillField, socSkillMatrix.Rank.HasValue ? (float)socSkillMatrix.Rank.Value : ordinal);
                    }

                    ordinal++;
                }

                dynamicContentExtensions.SetFieldValue(master, nameof(JobProfile.DigitalSkillsLevel), jobProfile.DigitalSkillsLevel);

                repository.Commit();

                repository.Update(master, UpdateComment);

                repository.Commit();
            }

            return(new RepoActionResult {
                Success = true
            });
        }
        public void UpsertSocSkillMatrix(SocSkillMatrix socSkillMatrix)
        {
            var repoSocMatrix = socMatrixRepository.Get(item =>
                                                        item.Visible && item.Status == ContentLifecycleStatus.Live && item.UrlName == socSkillMatrix.SfUrlName);

            if (repoSocMatrix != null)
            {
                var master = socMatrixRepository.GetMaster(repoSocMatrix);

                if (!string.IsNullOrWhiteSpace(socSkillMatrix.ONetElementId))
                {
                    dynamicContentExtensions.DeleteRelatedFieldValues(master, RelatedSkillField);
                    var relatedSkillItem = frameworkSkillRepository.Get(d =>
                                                                        d.Status == ContentLifecycleStatus.Master &&
                                                                        d.GetValue <string>(nameof(FrameworkSkill.ONetElementId)) == socSkillMatrix.ONetElementId);
                    if (relatedSkillItem != null)
                    {
                        dynamicContentExtensions.SetRelatedFieldValue(master, relatedSkillItem, RelatedSkillField, 1);
                    }
                }

                if (!string.IsNullOrWhiteSpace(socSkillMatrix.SocCode))
                {
                    dynamicContentExtensions.DeleteRelatedFieldValues(master, RelatedSocField);
                    var relatedSocItem = socCodeRepository.Get(d =>
                                                               d.Status == ContentLifecycleStatus.Master &&
                                                               d.GetValue <string>(nameof(SocCode.SOCCode)) == socSkillMatrix.SocCode);
                    if (relatedSocItem != null)
                    {
                        dynamicContentExtensions.SetRelatedFieldValue(master, relatedSocItem, RelatedSocField, 1);
                    }
                }

                // Save related on live version
                socMatrixRepository.Commit();

                var temp = socMatrixRepository.GetTemp(master);

                dynamicContentExtensions.SetFieldValue(temp, nameof(SocSkillMatrix.Title), socSkillMatrix.Title);
                dynamicContentExtensions.SetFieldValue(temp, nameof(SocSkillMatrix.Contextualised), socSkillMatrix.Contextualised);
                dynamicContentExtensions.SetFieldValue(temp, nameof(SocSkillMatrix.ONetAttributeType), socSkillMatrix.ONetAttributeType);
                dynamicContentExtensions.SetFieldValue(temp, nameof(SocSkillMatrix.ONetRank), socSkillMatrix.ONetRank);
                dynamicContentExtensions.SetFieldValue(temp, nameof(SocSkillMatrix.Rank), socSkillMatrix.Rank);

                var updatedMaster = socMatrixRepository.CheckinTemp(temp);

                socMatrixRepository.Publish(updatedMaster, UpdateComment);
                socMatrixRepository.Commit();
            }
            else
            {
                var newSocMatrix = socMatrixRepository.Create();
                dynamicContentExtensions.SetFieldValue(newSocMatrix, UrlNameField, (Lstring)socSkillMatrix.SfUrlName);
                socMatrixRepository.Add(newSocMatrix);

                socMatrixRepository.Commit();
                var newlyCreated = socMatrixRepository.Get(item =>
                                                           item.Visible && item.Status == ContentLifecycleStatus.Live &&
                                                           item.UrlName == socSkillMatrix.SfUrlName);

                if (newlyCreated != null)
                {
                    UpsertSocSkillMatrix(socSkillMatrix);
                }
            }
        }
        public void UpdateSocSkillMatricesTest(bool jobProfileAvailable)
        {
            // Arrange
            var urlname      = "test-url";
            var digitalSkill = "digiSkill";
            var jobProfile   = new JobProfileOverloadForWhatItTakes {
                UrlName = urlname, DigitalSkillsLevel = digitalSkill
            };
            var dummyDynamicContent = A.Dummy <DynamicContent>();
            var socSkill            = new SocSkillMatrix {
                Title = "test soc"
            };
            var socSkills = new List <SocSkillMatrix> {
                socSkill
            };

            // Fakes Setup
            A.CallTo(() => fakeRepository.Get(A <Expression <Func <DynamicContent, bool> > > ._))
            .Returns(jobProfileAvailable ? dummyDynamicContent : null);
            A.CallTo(() => fakeRepository.GetMaster(dummyDynamicContent)).Returns(dummyDynamicContent);
            A.CallTo(() => fakeRepository.Update(dummyDynamicContent, digitalSkill)).DoesNothing();
            A.CallTo(() => fakeRepository.Commit()).DoesNothing();
            A.CallTo(() =>
                     fakeDynamicContentExtensions.SetRelatedFieldValue(dummyDynamicContent, dummyDynamicContent, A <string> ._, A <float> ._)).DoesNothing();
            A.CallTo(() =>
                     fakeDynamicContentExtensions.DeleteRelatedFieldValues(dummyDynamicContent, A <string> ._)).DoesNothing();
            A.CallTo(() => fakeRepository.Get(A <Expression <Func <DynamicContent, bool> > > ._))
            .Returns(jobProfileAvailable ? dummyDynamicContent : null);
            A.CallTo(() => fakeSocSkillRepo.Get(A <Expression <Func <DynamicContent, bool> > > ._))
            .Returns(dummyDynamicContent);

            var jobProfileRepository = new JobProfileRepository(fakeRepository, fakeJobProfileConverter, fakeDynamicContentExtensions, fakeSocSkillRepo, fakeWitConverter, fakeJobProfileSearchConverter);

            // Act
            jobProfileRepository.UpdateSocSkillMatrices(jobProfile, socSkills);

            // Assert
            A.CallTo(() => fakeRepository.Get(A <Expression <Func <DynamicContent, bool> > > .That.Matches(m =>
                                                                                                           LinqExpressionsTestHelper.IsExpressionEqual(m, item => item.UrlName == jobProfile.UrlName && item.Status == ContentLifecycleStatus.Live &&
                                                                                                                                                       item.Visible))))
            .MustHaveHappened();

            if (jobProfileAvailable)
            {
                A.CallTo(() => fakeRepository.GetMaster(dummyDynamicContent)).MustHaveHappened();
                A.CallTo(() => fakeRepository.Update(dummyDynamicContent, A <string> ._)).MustHaveHappened();
                A.CallTo(() => fakeRepository.Commit()).MustHaveHappened();
                A.CallTo(() =>
                         fakeDynamicContentExtensions.SetRelatedFieldValue(A <DynamicContent> ._, A <DynamicContent> ._, "RelatedSkills", A <float> ._)).MustHaveHappened();
                A.CallTo(() =>
                         fakeDynamicContentExtensions.DeleteRelatedFieldValues(A <DynamicContent> ._, A <string> ._))
                .MustHaveHappened();
                A.CallTo(() => fakeSocSkillRepo.Get(A <Expression <Func <DynamicContent, bool> > > .That.Matches(m =>
                                                                                                                 LinqExpressionsTestHelper.IsExpressionEqual(m, d => d.Status == ContentLifecycleStatus.Master && d.UrlName == socSkill.SfUrlName)))).MustHaveHappened();
            }
            else
            {
                A.CallTo(() => fakeRepository.GetMaster(dummyDynamicContent)).MustNotHaveHappened();
                A.CallTo(() => fakeRepository.Update(dummyDynamicContent, A <string> ._)).MustNotHaveHappened();
                A.CallTo(() => fakeRepository.Commit()).MustNotHaveHappened();
                A.CallTo(() => fakeRepository.CheckinTemp(dummyDynamicContent)).MustNotHaveHappened();
                A.CallTo(() =>
                         fakeDynamicContentExtensions.SetRelatedFieldValue(A <DynamicContent> ._, A <DynamicContent> ._, A <string> ._, A <float> ._)).MustNotHaveHappened();
                A.CallTo(() =>
                         fakeDynamicContentExtensions.DeleteRelatedFieldValues(A <DynamicContent> ._, A <string> ._)).MustNotHaveHappened();
                A.CallTo(() => fakeSocSkillRepo.Get(A <Expression <Func <DynamicContent, bool> > > ._))
                .MustNotHaveHappened();
            }
        }