public async Task ThenItShouldRaiseEventForGroupIfAlreadyExistsAndUpdated(PointInTimeLocalAuthority stagingLocalAuthority) { // Arrange var managementGroup = new ManagementGroup { Identifier = stagingLocalAuthority.Code.ToString() }; _mapperMock.Setup(m => m.MapAsync <ManagementGroup>(It.IsAny <PointInTimeLocalAuthority>(), It.IsAny <CancellationToken>())) .ReturnsAsync(managementGroup); _localAuthorityRepositoryMock.Setup(r => r.GetLocalAuthorityFromStagingAsync(stagingLocalAuthority.Code, stagingLocalAuthority.PointInTime, _cancellationToken)) .ReturnsAsync(stagingLocalAuthority); _localAuthorityRepositoryMock.Setup(r => r.GetLocalAuthorityAsync(stagingLocalAuthority.Code, stagingLocalAuthority.PointInTime, _cancellationToken)) .ReturnsAsync(CloneWithChanges(stagingLocalAuthority, stagingLocalAuthority.Name + "-updated", false)); _establishmentRepositoryMock.Setup(r => r.GetEstablishmentFromStagingAsync(It.IsAny <long>(), It.IsAny <DateTime>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new PointInTimeEstablishment()); // Act await _manager.ProcessLocalAuthorityAsync(stagingLocalAuthority.Code, new[] { 1000001L }, stagingLocalAuthority.PointInTime, _cancellationToken); // Assert _eventPublisherMock.Verify(p => p.PublishManagementGroupUpdatedAsync( managementGroup, stagingLocalAuthority.PointInTime, _cancellationToken), Times.Once); }
private PointInTimeLocalAuthority CloneWithChanges(PointInTimeLocalAuthority localAuthority, string name = null, bool?isCurrent = null) { var clone = Clone(localAuthority); if (name != null) { clone.Name = name; } if (isCurrent.HasValue) { clone.IsCurrent = isCurrent.Value; } return(clone); }
public async Task ThenItShouldStoreTheGroupIfDoesNotAlreadyExist(PointInTimeLocalAuthority stagingLocalAuthority) { // Arrange _localAuthorityRepositoryMock.Setup(r => r.GetLocalAuthorityFromStagingAsync(stagingLocalAuthority.Code, stagingLocalAuthority.PointInTime, _cancellationToken)) .ReturnsAsync(stagingLocalAuthority); _establishmentRepositoryMock.Setup(r => r.GetEstablishmentFromStagingAsync(It.IsAny <long>(), It.IsAny <DateTime>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new PointInTimeEstablishment()); // Act await _manager.ProcessLocalAuthorityAsync(stagingLocalAuthority.Code, new[] { 1000001L }, stagingLocalAuthority.PointInTime, _cancellationToken); // Assert _localAuthorityRepositoryMock.Verify(r => r.StoreAsync( It.Is <PointInTimeLocalAuthority[]>(toStore => toStore.Length == 1 && toStore[0] == stagingLocalAuthority), _cancellationToken), Times.Once); }
public async Task ThenItShouldMapManagementGroupToLAIfNoLinks(Establishment source, int laCode) { EnsureManagmentGroupCodes(source, laCode: laCode); var localAuthority = new PointInTimeLocalAuthority(); var managementGroup = new ManagementGroup(); _localAuthorityRepositoryMock.Setup(r => r.GetLocalAuthorityAsync(It.IsAny <int>(), It.IsAny <CancellationToken>())) .ReturnsAsync(localAuthority); _mapperMock.Setup(m => m.MapAsync <ManagementGroup>(It.IsAny <LocalAuthority>(), It.IsAny <CancellationToken>())) .ReturnsAsync(managementGroup); var actual = await _mapper.MapAsync <LearningProvider>(source, _cancellationToken); Assert.IsNotNull(actual.ManagementGroup); Assert.AreSame(managementGroup, actual.ManagementGroup); _localAuthorityRepositoryMock.Verify(r => r.GetLocalAuthorityAsync(laCode, _cancellationToken), Times.Once); _mapperMock.Verify(m => m.MapAsync <ManagementGroup>(localAuthority, _cancellationToken), Times.Once); }
public async Task ThenItShouldStoreEstablishmentIfAlreadyExistsAndUpdated(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); _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 _establishmentRepositoryMock.Verify(r => r.StoreAsync( It.Is <PointInTimeEstablishment[]>(toStore => toStore.Length == 1 && toStore[0] == stagingEstablishment), _cancellationToken), Times.Once); }
private async Task StoreLocalAuthorityAndRaiseEventAsync( PointInTimeLocalAuthority staging, bool isUpdate, CancellationToken cancellationToken) { var current = await _localAuthorityRepository.GetLocalAuthorityAsync(staging.Code, 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 _localAuthorityRepository.StoreAsync(toStore, cancellationToken); _logger.Debug($"Stored local authority {staging.Code} in repository"); var managementGroup = await _mapper.MapAsync <ManagementGroup>(staging, cancellationToken); managementGroup._Lineage = null; if (isUpdate) { await _eventPublisher.PublishManagementGroupUpdatedAsync(managementGroup, staging.PointInTime, cancellationToken); } else { await _eventPublisher.PublishManagementGroupCreatedAsync(managementGroup, staging.PointInTime, cancellationToken); } _logger.Debug($"Sent event for local authority {staging.Code}"); }
public async Task ThenItShouldRaiseEventForEstablishmentIfAlreadyExistsNotUpdatedButGroupIsUpdated(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); _localAuthorityRepositoryMock.Setup(r => r.GetLocalAuthorityAsync(stagingLocalAuthority.Code, stagingLocalAuthority.PointInTime, _cancellationToken)) .ReturnsAsync(CloneWithChanges(stagingLocalAuthority, stagingLocalAuthority.Name + "-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.ProcessLocalAuthorityAsync(stagingLocalAuthority.Code, new[] { stagingEstablishment.Urn }, stagingLocalAuthority.PointInTime, _cancellationToken); // Assert _eventPublisherMock.Verify(p => p.PublishLearningProviderUpdatedAsync( learningProvider, stagingEstablishment.PointInTime, _cancellationToken), Times.Once); }
public Task StoreAsync(PointInTimeLocalAuthority localAuthority, CancellationToken cancellationToken) { throw new NotImplementedException(); }