public async Task<AeDeliveryLocation> GetOrAddAeDeliveryLocation(string approvalNumber, string operatorName)
        {
            // Replace empty strings with null
            operatorName = string.IsNullOrEmpty(operatorName) ? null : operatorName;

            if (cachedAeDeliveryLocations == null)
            {
                cachedAeDeliveryLocations =
                    await context.AeDeliveryLocations
                    .ToDictionaryAsync(ae => string.Format("{0}{1}", ae.ApprovalNumber, ae.OperatorName), StringComparer.OrdinalIgnoreCase);
            }

            var key = string.Format("{0}{1}", approvalNumber, operatorName);
            AeDeliveryLocation aeDeliveryLocation;
            if (!cachedAeDeliveryLocations.TryGetValue(key, out aeDeliveryLocation))
            {
                aeDeliveryLocation = new AeDeliveryLocation(approvalNumber, operatorName);

                cachedAeDeliveryLocations.Add(key, aeDeliveryLocation);
                context.AeDeliveryLocations.Add(aeDeliveryLocation);
            }

            return aeDeliveryLocation;
        }
        public async Task AddAeDeliveredAmount_CreatesAeDeliveredAmountDomainObject()
        {
            var helper = new DataReturnVersionBuilderHelper();
            A.CallTo(() => helper.DataAccess.FetchDataReturnOrDefault())
               .Returns((DataReturn)null);

            var aeDeliveryLocation = new AeDeliveryLocation("Approval Number", "Operator name");
            A.CallTo(() => helper.DataAccess.GetOrAddAeDeliveryLocation(A<string>._, A<string>._))
                .Returns(aeDeliveryLocation);

            var builder = helper.Create();
            await builder.AddAeDeliveredAmount("Approval Number", "Operator name", A.Dummy<WeeeCategory>(), ObligationType.B2C, A.Dummy<decimal>());

            var result = await builder.Build();

            Assert.Equal(1, result.DataReturnVersion.WeeeDeliveredReturnVersion.WeeeDeliveredAmounts.Count);
            Assert.Collection(result.DataReturnVersion.WeeeDeliveredReturnVersion.WeeeDeliveredAmounts,
                r => Assert.Equal("Approval Number", r.AeDeliveryLocation.ApprovalNumber));
            Assert.Same(aeDeliveryLocation, result.DataReturnVersion.WeeeDeliveredReturnVersion.WeeeDeliveredAmounts.Single().AeDeliveryLocation);
        }
            public static WeeeDeliveredAmount WithAeDeliveryLocation(AeDeliveryLocation aeDeliveryLocation)
            {
                var builder = new WeeeDeliveredAmountBuilder();
                builder.aeDeliveryLocation = aeDeliveryLocation;

                return builder.Build();
            }
        private static IEnumerable<WeeeDeliveredAmount> CreateDeliveredToAes(string approvalNumber)
        {
            var deliveredToAes = new List<WeeeDeliveredAmount>();

            string operatorName = string.Empty;
            if (RandomHelper.OneIn(2))
            {
                operatorName = RandomHelper.CreateRandomString("Operator", 0, 250);
            }

            var deliveryLocation = new AeDeliveryLocation(approvalNumber, operatorName);

            IEnumerable<IReturnItem> returnItems = CreateReturnItems(null);
            foreach (IReturnItem returnItem in returnItems)
            {
                deliveredToAes.Add(new WeeeDeliveredAmount(returnItem.ObligationType, returnItem.WeeeCategory, returnItem.Tonnage, deliveryLocation));
            }

            return deliveredToAes;
        }
        public async Task GetOrAddAeDeliveryLocation_NoMatchingOperatorName_ReturnsNewAeDeliveryLocation(string suppliedOperatorName, string resultingOperatorName)
        {
            // Arrange
            var dbContextHelper = new DbContextHelper();
            var context = A.Fake<WeeeContext>();

            var aeDeliveryLocationDb = new AeDeliveryLocation("AAA", "xxx");
            var aeDeliveryLocations = dbContextHelper.GetAsyncEnabledDbSet(new List<AeDeliveryLocation> { aeDeliveryLocationDb });
            A.CallTo(() => context.AeDeliveryLocations)
                .Returns(aeDeliveryLocations);

            var dataAccess = new DataReturnVersionBuilderDataAccess(A.Dummy<Scheme>(), A.Dummy<Quarter>(), context);

            // Act
            var result = await dataAccess.GetOrAddAeDeliveryLocation("AAA", suppliedOperatorName);

            // Assert
            Assert.NotNull(result);
            Assert.NotSame(aeDeliveryLocationDb, result);
            Assert.Equal("AAA", result.ApprovalNumber);
            Assert.Equal(resultingOperatorName, result.OperatorName);
            Assert.Contains(result, dataAccess.CachedAeDeliveryLocations.Values);
            A.CallTo(() => aeDeliveryLocations.Add(result))
                .MustHaveHappened();
        }
        public async Task GetOrAddAeDeliveryLocation_WithMatchingApprovalNumberAndOperatorName_DoesNotAddToCacheAndDatabase(string approvalNumber, string operatorName)
        {
            // Arrange
            var dbContextHelper = new DbContextHelper();
            var context = A.Fake<WeeeContext>();

            var aeDeliveryLocationDb = new AeDeliveryLocation(approvalNumber, operatorName);
            var aeDeliveryLocations = dbContextHelper.GetAsyncEnabledDbSet(new List<AeDeliveryLocation> { aeDeliveryLocationDb });
            A.CallTo(() => context.AeDeliveryLocations)
                .Returns(aeDeliveryLocations);

            var dataAccess = new DataReturnVersionBuilderDataAccess(A.Dummy<Scheme>(), A.Dummy<Quarter>(), context);

            // Act
            var result = await dataAccess.GetOrAddAeDeliveryLocation(approvalNumber, operatorName);

            // Assert
            Assert.Equal(1, dataAccess.CachedAeDeliveryLocations.Count);
            A.CallTo(() => aeDeliveryLocations.Add(result))
                .MustNotHaveHappened();
        }
        public async Task GetOrAddAeDeliveryLocation_PopulatesCacheWithDatabaseValues(string approvalNumber, string operatorName)
        {
            // Arrange
            var dbContextHelper = new DbContextHelper();
            var context = A.Fake<WeeeContext>();

            var aeDeliveryLocationDb = new AeDeliveryLocation(approvalNumber, operatorName);
            var aeDeliveryLocations = dbContextHelper.GetAsyncEnabledDbSet(new List<AeDeliveryLocation> { aeDeliveryLocationDb });
            A.CallTo(() => context.AeDeliveryLocations)
                .Returns(aeDeliveryLocations);

            var dataAccess = new DataReturnVersionBuilderDataAccess(A.Dummy<Scheme>(), A.Dummy<Quarter>(), context);

            // Act
            await dataAccess.GetOrAddAeDeliveryLocation(approvalNumber, operatorName);

            // Assert
            Assert.Equal(1, dataAccess.CachedAeDeliveryLocations.Count);
            Assert.Contains(aeDeliveryLocationDb, dataAccess.CachedAeDeliveryLocations.Values);
        }