public async Task ThenItShouldReturnMappedLearningProviders(bool readFromLive, DateTime?pointInTime)
        {
            var learningProviders = _fixture.Create <LearningProvider[]>();
            var urns = learningProviders.Select(x => x.Urn.Value).ToArray();

            for (var i = 0; i < learningProviders.Length; i++)
            {
                var establishment = new PointInTimeEstablishment()
                {
                    Urn = learningProviders[i].Urn.Value,
                };

                _giasApiClientMock.Setup(c => c.GetEstablishmentAsync(establishment.Urn, _cancellationToken))
                .ReturnsAsync(establishment);
                _establishmentRepository.Setup(c => c.GetEstablishmentAsync(establishment.Urn, pointInTime, _cancellationToken))
                .ReturnsAsync(establishment);
                _mapperMock.Setup(m => m.MapAsync <LearningProvider>(It.IsAny <Establishment>(), _cancellationToken))
                .ReturnsAsync((Establishment e, CancellationToken ct) => learningProviders.Single(x => x.Urn == e.Urn));
            }

            var actual = await _manager.GetLearningProvidersAsync(urns.Select(x => x.ToString()).ToArray(), null, readFromLive, pointInTime, _cancellationToken);

            Assert.AreEqual(learningProviders.Length, actual.Length);
            for (var i = 0; i < learningProviders.Length; i++)
            {
                Assert.AreSame(learningProviders[i], actual[i],
                               $"Expected {i} to be same");
            }
        }
        private PointInTimeEstablishment CloneWithChanges(PointInTimeEstablishment group, string establishmentName = null, bool?isCurrent = null)
        {
            var clone = Clone(group);

            if (establishmentName != null)
            {
                clone.EstablishmentName = establishmentName;
            }

            if (isCurrent.HasValue)
            {
                clone.IsCurrent = isCurrent.Value;
            }

            return(clone);
        }
Esempio n. 3
0
        private async Task StoreEstablishmentAndRaiseEventAsync(
            PointInTimeEstablishment staging,
            bool isUpdate,
            CancellationToken cancellationToken)
        {
            var current = await _establishmentRepository.GetEstablishmentAsync(staging.Urn, cancellationToken);

            staging.IsCurrent = current == null || staging.PointInTime > current.PointInTime;
            if (current != null && staging.IsCurrent)
            {
                current.IsCurrent = false;
            }

            var toStore = current == null || current.IsCurrent
                ? new[] { staging }
                : new[] { current, staging };

            await _establishmentRepository.StoreAsync(toStore, cancellationToken);

            _logger.Debug($"Stored establishment {staging.Urn} in repository");

            var learningProvider = await _mapper.MapAsync <LearningProvider>(staging, cancellationToken);

            learningProvider._Lineage = null;

            if (isUpdate)
            {
                await _eventPublisher.PublishLearningProviderUpdatedAsync(learningProvider, staging.PointInTime, cancellationToken);
            }
            else
            {
                await _eventPublisher.PublishLearningProviderCreatedAsync(learningProvider, staging.PointInTime, cancellationToken);
            }

            _logger.Debug($"Sent event for establishment {staging.Urn}");
        }
        public async Task ThenItShouldRaiseEventForEstablishmentIfAlreadyExistsNotUpdatedButGroupIsUpdated(PointInTimeGroup stagingGroup, PointInTimeEstablishment stagingEstablishment)
        {
            // Arrange
            var learningProvider = new LearningProvider {
                Urn = stagingEstablishment.Urn
            };

            _mapperMock.Setup(m => m.MapAsync <LearningProvider>(It.IsAny <PointInTimeEstablishment>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(learningProvider);
            _groupRepositoryMock.Setup(r => r.GetGroupFromStagingAsync(stagingGroup.Uid, stagingGroup.PointInTime, _cancellationToken))
            .ReturnsAsync(stagingGroup);
            _groupRepositoryMock.Setup(r => r.GetGroupAsync(stagingGroup.Uid, stagingGroup.PointInTime, _cancellationToken))
            .ReturnsAsync(CloneWithChanges(stagingGroup, stagingGroup.GroupName + "-updated", false));
            _establishmentRepositoryMock.Setup(r =>
                                               r.GetEstablishmentFromStagingAsync(stagingEstablishment.Urn, It.IsAny <DateTime>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(stagingEstablishment);
            _establishmentRepositoryMock.Setup(r => r.GetEstablishmentAsync(stagingEstablishment.Urn, It.IsAny <DateTime>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(Clone(stagingEstablishment));

            // Act
            await _manager.ProcessGroupAsync(stagingGroup.Uid, new[] { stagingEstablishment.Urn }, stagingGroup.PointInTime, _cancellationToken);

            // Assert
            _eventPublisherMock.Verify(p => p.PublishLearningProviderUpdatedAsync(
                                           learningProvider,
                                           stagingEstablishment.PointInTime,
                                           _cancellationToken),
                                       Times.Once);
        }
        public async Task ThenItShouldStoreEstablishmentIfAlreadyExistsAndUpdated(PointInTimeGroup stagingGroup, PointInTimeEstablishment stagingEstablishment)
        {
            // Arrange
            _groupRepositoryMock.Setup(r => r.GetGroupFromStagingAsync(stagingGroup.Uid, stagingGroup.PointInTime, _cancellationToken))
            .ReturnsAsync(stagingGroup);
            _establishmentRepositoryMock.Setup(r =>
                                               r.GetEstablishmentFromStagingAsync(stagingEstablishment.Urn, It.IsAny <DateTime>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(stagingEstablishment);
            _establishmentRepositoryMock.Setup(r => r.GetEstablishmentAsync(stagingEstablishment.Urn, It.IsAny <DateTime>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(CloneWithChanges(stagingEstablishment, stagingEstablishment.EstablishmentName + "-updated", false));

            // Act
            await _manager.ProcessGroupAsync(stagingGroup.Uid, new[] { stagingEstablishment.Urn }, stagingGroup.PointInTime, _cancellationToken);

            // Assert
            _establishmentRepositoryMock.Verify(r => r.StoreAsync(
                                                    It.Is <PointInTimeEstablishment[]>(toStore =>
                                                                                       toStore.Length == 1 &&
                                                                                       toStore[0] == stagingEstablishment),
                                                    _cancellationToken),
                                                Times.Once);
        }
        public async Task ThenItShouldRaiseEventForEstablishmentIfAlreadyExistsAndUpdated(PointInTimeLocalAuthority stagingLocalAuthority, PointInTimeEstablishment stagingEstablishment)
        {
            // Arrange
            var learningProvider = new LearningProvider {
                Urn = stagingEstablishment.Urn
            };

            _mapperMock.Setup(m => m.MapAsync <LearningProvider>(It.IsAny <PointInTimeEstablishment>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(learningProvider);
            _localAuthorityRepositoryMock.Setup(r => r.GetLocalAuthorityFromStagingAsync(stagingLocalAuthority.Code, stagingLocalAuthority.PointInTime, _cancellationToken))
            .ReturnsAsync(stagingLocalAuthority);
            _establishmentRepositoryMock.Setup(r =>
                                               r.GetEstablishmentFromStagingAsync(stagingEstablishment.Urn, It.IsAny <DateTime>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(stagingEstablishment);
            _establishmentRepositoryMock.Setup(r => r.GetEstablishmentAsync(stagingEstablishment.Urn, It.IsAny <DateTime>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(CloneWithChanges(stagingEstablishment, stagingEstablishment.EstablishmentName + "-updated", false));

            // Act
            await _manager.ProcessLocalAuthorityAsync(stagingLocalAuthority.Code, new[] { stagingEstablishment.Urn }, stagingLocalAuthority.PointInTime, _cancellationToken);

            // Assert
            _eventPublisherMock.Verify(p => p.PublishLearningProviderUpdatedAsync(
                                           learningProvider,
                                           stagingEstablishment.PointInTime,
                                           _cancellationToken),
                                       Times.Once);
        }
        public async Task ThenItShouldStoreEstablishmentIfDoesNotAlreadyExist(PointInTimeLocalAuthority stagingLocalAuthority, PointInTimeEstablishment stagingEstablishment)
        {
            // Arrange
            _localAuthorityRepositoryMock.Setup(r => r.GetLocalAuthorityFromStagingAsync(stagingLocalAuthority.Code, stagingLocalAuthority.PointInTime, _cancellationToken))
            .ReturnsAsync(stagingLocalAuthority);
            _establishmentRepositoryMock.Setup(r =>
                                               r.GetEstablishmentFromStagingAsync(stagingEstablishment.Urn, It.IsAny <DateTime>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(stagingEstablishment);

            // Act
            await _manager.ProcessLocalAuthorityAsync(stagingLocalAuthority.Code, new[] { stagingEstablishment.Urn }, stagingLocalAuthority.PointInTime, _cancellationToken);

            // Assert
            _establishmentRepositoryMock.Verify(r => r.StoreAsync(
                                                    It.Is <PointInTimeEstablishment[]>(toStore =>
                                                                                       toStore.Length == 1 &&
                                                                                       toStore[0] == stagingEstablishment),
                                                    _cancellationToken),
                                                Times.Once);
        }