private void SeedDatabaseContext()
        {
            using var context = new ChargesDatabaseContext(_dbContextOptions);
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            var chargeTypes = new List <ChargeType> {
                new ChargeType {
                    Code = KnownChargeType, Id = 1,
                }
            };

            context.AddRange(chargeTypes);

            var resolutionTypes = new List <ResolutionType> {
                new ResolutionType {
                    Name = KnownResolutionType, Id = 1,
                }
            };

            context.AddRange(resolutionTypes);

            var vatPayerTypes = new List <VatPayerType> {
                new VatPayerType {
                    Name = KnownVatPayer, Id = 1,
                }
            };

            context.AddRange(vatPayerTypes);

            var chargeOwners = new List <Ingestion.Infrastructure.ChangeOfCharges.Context.Model.MarketParticipant>
            {
                new Ingestion.Infrastructure.ChangeOfCharges.Context.Model.MarketParticipant
                {
                    MRid = KnownChargeOwner,
                    Id   = 1,
                },
            };

            context.AddRange(chargeOwners);

            var ruleConfigurations = new List <RuleConfiguration>
            {
                new RuleConfiguration(ValidationRuleNames.StartOfValidIntervalFromNowInDays, "31"),
                new RuleConfiguration(ValidationRuleNames.EndOfValidIntervalFromNowInDays, "1095"),
            };

            context.AddRange(ruleConfigurations);

            context.SaveChanges();
        }
        public async Task StoreChargeAsync_WhenChargeIsSaved_ThenSuccessReturnedAsync()
        {
            // Arrange
            var message = GetValidChangeOfChargesMessage();

            SeedDatabase();
            await using var chargesDatabaseContext = new ChargesDatabaseContext(_dbContextOptions);
            var sut = new ChargeRepository(chargesDatabaseContext);

            // Act
            var result = await sut.StoreChargeAsync(message).ConfigureAwait(false);

            // Assert
            Assert.True(result.Success);
        }
        private void SeedDatabase()
        {
            using var context = new ChargesDatabaseContext(_dbContextOptions);
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            var chargeTypes = new List <ChargeType> {
                new ChargeType {
                    Code = KnownChargeType, Id = 1,
                }
            };

            context.AddRange(chargeTypes);

            var resolutionTypes = new List <ResolutionType> {
                new ResolutionType {
                    Name = KnownResolutionType, Id = 1,
                }
            };

            context.AddRange(resolutionTypes);

            var vatPayerTypes = new List <VatPayerType> {
                new VatPayerType {
                    Name = KnownVatPayer, Id = 1,
                }
            };

            context.AddRange(vatPayerTypes);

            var chargeOwners = new List <MarketParticipant>
            {
                new MarketParticipant
                {
                    MRid = KnownChargeOwner,
                    Id   = 1,
                },
            };

            context.AddRange(chargeOwners);

            context.SaveChanges();
        }
        public async Task StoreChargeAsync_WhenValueNotFoundInDbContext_ThenFailureStatusReturnedAsync(string chargeType, string resolutionType, string vatPayerType, string chargeOwner, string failureReason)
        {
            // Arrange
            var message = GetValidChangeOfChargesMessage();

            message.Type = chargeType;
            message.ChargeTypeOwnerMRid = chargeOwner;
            message !.MktActivityRecord !.ChargeType !.VatPayer = vatPayerType;
            message !.Period !.Resolution = resolutionType;

            SeedDatabase();
            await using var chargesDatabaseContext = new ChargesDatabaseContext(_dbContextOptions);
            var sut = new ChargeRepository(chargesDatabaseContext);

            // Act
            var result = await sut.StoreChargeAsync(message).ConfigureAwait(false);

            // Assert
            Assert.False(result.Success);
            Assert.Equal(failureReason, result.Reason);
        }
        public async Task StoreChargeAsync_WhenValuesInMessageUsedForDbContextLookupsAreInvalid_ThenExceptionThrownAsync(string chargeType, string resolutionType, string vatPayerType, string chargeOwner, string exceptionMessage)
        {
            // Arrange
            var message = GetValidChangeOfChargesMessage();

            message.Type = chargeType;
            message.ChargeTypeOwnerMRid = chargeOwner;
#pragma warning disable CS8602 // Dereference of a possibly null reference.
            message.MktActivityRecord.ChargeType.VatPayer = vatPayerType;
            message.Period.Resolution = resolutionType;
#pragma warning restore CS8602 // Dereference of a possibly null reference.

            SeedDatabase();
            await using var context = new ChargesDatabaseContext(_dbContextOptions);
            var sut = new ChargeRepository(context);

            // Act
            var exception = await Record.ExceptionAsync(async() => await sut.StoreChargeAsync(message).ConfigureAwait(false)).ConfigureAwait(false);

            // Assert
            Assert.IsType <ArgumentException>(exception);
            Assert.Contains(exceptionMessage, exception.Message);
        }
        public async Task StoreChargeAsync_WhenValuesInMessageAreInvalid_ThenExceptionThrownAsync(string chargeTypeMRid, string correlationId, string lastUpdatedBy, string shortDescription, string longDescription, string argumentThatFails)
        {
            // Arrange
            var message = GetValidChangeOfChargesMessage();

            message.ChargeTypeMRid = chargeTypeMRid;
            message.CorrelationId  = correlationId;
            message.LastUpdatedBy  = lastUpdatedBy;
#pragma warning disable CS8602 // Dereference of a possibly null reference.
            message.MktActivityRecord.ChargeType.Name        = shortDescription;
            message.MktActivityRecord.ChargeType.Description = longDescription;
#pragma warning restore CS8602 // Dereference of a possibly null reference.

            SeedDatabase();
            await using var context = new ChargesDatabaseContext(_dbContextOptions);
            var sut = new ChargeRepository(context);

            // Act
            var exception = await Record.ExceptionAsync(async() => await sut.StoreChargeAsync(message).ConfigureAwait(false)).ConfigureAwait(false);

            // Assert
            Assert.IsType <ArgumentException>(exception);
            Assert.Contains($"{argumentThatFails} must have value", exception.Message);
        }