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 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 static WeeeDeliveredAmount WithAeDeliveryLocation(AeDeliveryLocation aeDeliveryLocation)
            {
                var builder = new WeeeDeliveredAmountBuilder();

                builder.aeDeliveryLocation = aeDeliveryLocation;

                return(builder.Build());
            }
        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);
        }
        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_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);
        }
        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 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);
        }
Example #9
0
        public void GenerateXml_CreatesValidDataReturnsXmlFile()
        {
            // Arrange
            var scheme = A.Fake <Scheme>();

            A.CallTo(() => scheme.ApprovalNumber)
            .Returns("WEE/SC0001ST/SCH");

            var dataReturn        = new DataReturn(scheme, new Quarter(2016, QuarterType.Q2));
            var dataReturnVersion = new DataReturnVersion(dataReturn);

            // WEEE collected
            dataReturnVersion.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(
                new WeeeCollectedAmount(WeeeCollectedAmountSourceType.Dcf, ObligationType.B2C, WeeeCategory.ElectricalAndElectronicTools, 100));
            dataReturnVersion.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(
                new WeeeCollectedAmount(WeeeCollectedAmountSourceType.Dcf, ObligationType.B2B, WeeeCategory.DisplayEquipment, 200));

            dataReturnVersion.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(
                new WeeeCollectedAmount(WeeeCollectedAmountSourceType.Distributor, ObligationType.B2C, WeeeCategory.ElectricalAndElectronicTools, 100));
            dataReturnVersion.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(
                new WeeeCollectedAmount(WeeeCollectedAmountSourceType.Distributor, ObligationType.B2C, WeeeCategory.ITAndTelecommsEquipment, 50));

            dataReturnVersion.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(
                new WeeeCollectedAmount(WeeeCollectedAmountSourceType.FinalHolder, ObligationType.B2C, WeeeCategory.ElectricalAndElectronicTools, 100));
            dataReturnVersion.WeeeCollectedReturnVersion.AddWeeeCollectedAmount(
                new WeeeCollectedAmount(WeeeCollectedAmountSourceType.FinalHolder, ObligationType.B2C, WeeeCategory.MedicalDevices, 2));

            // WEEE delivered
            var aatfDeliveryLocation1 = new AatfDeliveryLocation("WEE/AA0001AA/ATF", "TestAATF1");

            dataReturnVersion.WeeeDeliveredReturnVersion.AddWeeeDeliveredAmount(
                new WeeeDeliveredAmount(ObligationType.B2C, WeeeCategory.LargeHouseholdAppliances, 200, aatfDeliveryLocation1));
            dataReturnVersion.WeeeDeliveredReturnVersion.AddWeeeDeliveredAmount(
                new WeeeDeliveredAmount(ObligationType.B2C, WeeeCategory.PhotovoltaicPanels, 200, aatfDeliveryLocation1));

            var aatfDeliveryLocation2 = new AatfDeliveryLocation("WEE/AA0002AA/ATF", "TestAATF2");

            dataReturnVersion.WeeeDeliveredReturnVersion.AddWeeeDeliveredAmount(
                new WeeeDeliveredAmount(ObligationType.B2B, WeeeCategory.PhotovoltaicPanels, 200, aatfDeliveryLocation2));

            var aeDeliveryLocation1 = new AeDeliveryLocation("WEE/AA0001AA/AE", "TestAE1");

            dataReturnVersion.WeeeDeliveredReturnVersion.AddWeeeDeliveredAmount(
                new WeeeDeliveredAmount(ObligationType.B2C, WeeeCategory.LargeHouseholdAppliances, 200, aeDeliveryLocation1));
            dataReturnVersion.WeeeDeliveredReturnVersion.AddWeeeDeliveredAmount(
                new WeeeDeliveredAmount(ObligationType.B2B, WeeeCategory.LightingEquipment, 20, aeDeliveryLocation1));

            var aeDeliveryLocation2 = new AeDeliveryLocation("WEE/AA0002AA/AE", "TestAE2");

            dataReturnVersion.WeeeDeliveredReturnVersion.AddWeeeDeliveredAmount(
                new WeeeDeliveredAmount(ObligationType.B2B, WeeeCategory.LightingEquipment, 10, aeDeliveryLocation2));

            // EEE output
            var registeredProducer1 = CreateRegisteredProducer(scheme, 2016, "WEE/AA0001RP", "Test Organisation1");

            dataReturnVersion.EeeOutputReturnVersion.AddEeeOutputAmount(
                new EeeOutputAmount(ObligationType.B2C, WeeeCategory.LightingEquipment, 3000, registeredProducer1));
            dataReturnVersion.EeeOutputReturnVersion.AddEeeOutputAmount(
                new EeeOutputAmount(ObligationType.B2C, WeeeCategory.PhotovoltaicPanels, 100, registeredProducer1));

            var registeredProducer2 = CreateRegisteredProducer(scheme, 2016, "WEE/AA0002RP", "Test Organisation2");

            dataReturnVersion.EeeOutputReturnVersion.AddEeeOutputAmount(
                new EeeOutputAmount(ObligationType.B2C, WeeeCategory.PhotovoltaicPanels, 100, registeredProducer2));

            var xmlGenerator = new XmlGenerator();

            // Act
            var generatedXml = xmlGenerator.GenerateXml(dataReturnVersion);

            // Assert
            var xmlSchemaHelper  = new XmlSchemaHelper(@"DataReturns\v3schema.xsd");
            var validationResult = xmlSchemaHelper.ValidateXml(generatedXml);

            Assert.Empty(validationResult);
        }
        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 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);
        }