public void GetAggregates_Successful_ReturnsCollection_InternalDataStoreValues()
        {
            //////IUnitOfWork uow = new AggregateContext();
            //////IAggregateRepository aggregateRepository = new AggregateRepository(uow);

            //  Arrange
            //  Configure the AggregateRepository
            _mockAggregateRepository.Setup(r => r.GetAll()).Returns(_snhAggregates);
            //  Configure the DataSourceRepository
            _mockDatasourceRepository.Setup(d => d.Get(1)).Returns(_dataSource1);
            _mockDatasourceRepository.Setup(d => d.Get(2)).Returns(_dataSource2);

            var dataService = new DataService(_mockAggregateRepository.Object,
                _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
                _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);
            //////var dataService = new DataService(aggregateRepository,
            //////    _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
            //////    _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);

            //  Act
            var results = dataService.GetAggregates(string.Empty).ToArray();

            //  Assert
            Assert.AreEqual(2, results.Count(), "Expected 2 aggregates to be returned.");
            Assert.AreEqual("Achany Estate", results[0].Name, "Expected 'Achany Estate' to be returned as first object.");
            Assert.AreEqual("{achany estate}", results[0].Data.FirstOrDefault(t => t.DataType == DataTypeEnum.FootPrint).Data, "Expected original data '{achany estate}' to be returned");
            Assert.AreEqual("Whitelee Windfarm", results[1].Name, "Expected 'Whitelee Windfarm' to be returned as second object.");
            Assert.AreEqual("{whitelee windfarm}", results[1].Data.FirstOrDefault(t => t.DataType == DataTypeEnum.FootPrint).Data, "Expected original data '{whitelee windfarm}' to be returned");
        }
        public void GetAggregateWithDataType_Aikengall_Status_Successful()
        {
            //  Arrange:
            var inputDoc = _htmlSingleAggregateDoc;

            var dataService = new DataService(_repository, _dataSourceRepository, _dataTypeRepository, _importServiceResolver, _dataSourceResolver);

            //  Mock the external call to the internet.
            using (ShimsContext.Create())
            {
                //  Fake the call to the internet
                HtmlAgilityPack.Fakes.ShimHtmlWeb.AllInstances.LoadString =
                    (htmlWeb, s) => { return inputDoc; };
                //  Fake the statue code returned from HTTP response
                HtmlAgilityPack.Fakes.ShimHtmlWeb.AllInstances.StatusCodeGet =
                    (web => { return System.Net.HttpStatusCode.OK; });

                //  Act
                var result = dataService.GetAggregateWithDataType(AikengallAggregate,DataTypeEnum.Status);

                //  Assert
                var resultData = result.Data.ToArray();
                var minsegments = (resultData.Count() >= 1);

                Assert.IsNotNull(result, "Expected aggregate 'Aikengall' to be returned");
                Assert.AreEqual("Aikengall", result.Name, "Expected Name to be 'Aikengall'.");
                Assert.IsTrue(minsegments, "Expected at least one data segment 'Status' to be returned.");

                Assert.AreEqual(DataTypeEnum.Status, resultData[0].DataType, "Expected the first data segment to be a 'Turbine' segment.");
                Assert.AreEqual(DataTypeEnum.Turbine, resultData[1].DataType, "Expected the second data segment to be a 'Status' segment.");
            }
        }
        public void GetAggregates_Successful()
        {
            //  Arrange:
            var dataService = new DataService(_repository, _dataSourceRepository, _dataTypeRepository, _importServiceResolver, _dataSourceResolver);

            //  Act
            var result = dataService.GetAggregates(string.Empty).ToArray();     // Get all aggregates

            //  Assert
            var minCount = result.Count() >= 10;
            Assert.IsTrue(minCount, "Expected at least 10 aggregates to be returned");
        }
        public void GetAggregatesWithDataType_Status_Successful()
        {
            //  Arrange:
            var dataService = new DataService(_repository, _dataSourceRepository, _dataTypeRepository, _importServiceResolver, _dataSourceResolver);

            //  Act
            var result = dataService.GetAggregatesWithDataType(DataTypeEnum.Status).ToArray();     // Get all aggregates

            //  Assert
            var minCount = result.Count() >= 1; //  Aikengall can guarantee a Status segment
            Assert.IsTrue(minCount, "Expected at least 1 aggregates to be returned");
        }
        public void GetAggregates_Successful_ReturnsUnaltered_InvalidDataSource()
        {
            //  Arrange
            //  Configure the AggregateRepository
            _mockAggregateRepository.Setup(r => r.GetAll()).Returns(_snhAggregates);
            //  Configure the DataSourceRepository
            _mockDatasourceRepository.Setup(d => d.Get(It.IsAny<int>())).Returns(() => null);

            var dataService = new DataService(_mockAggregateRepository.Object,
                _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
                _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);

            //  Act
            var results = dataService.GetAggregates(string.Empty).ToArray();

            //  Assert
            Assert.AreEqual(2, results.Count(), "Expected 2 aggregates to be returned.");
            Assert.AreEqual("Achany Estate", results[0].Name, "Expected 'Achany Estate' to be returned as first object.");
            Assert.AreEqual("{achany estate}", results[0].Data.FirstOrDefault(t => t.DataType == DataTypeEnum.FootPrint).Data, "Expected original data '{achany estate}' to be returned");
            Assert.AreEqual("Whitelee Windfarm", results[1].Name, "Expected 'Whitelee Windfarm' to be returned as second object.");
            Assert.AreEqual("{whitelee windfarm}", results[1].Data.FirstOrDefault(t => t.DataType == DataTypeEnum.FootPrint).Data, "Expected original data '{whitelee windfarm}' to be returned");
        }
        public void GetAggregates_UnSuccessful_NoAggregatesWithDataType_MixedSourceValues()
        {
            //  Arrange
            var aggregatesNone = new List<Aggregate>().AsQueryable();

            //  Configure the AggregateRepository
            _mockAggregateRepository.Setup(r => r.GetAllWithDataType(It.IsAny<DataTypeEnum>())).Returns(aggregatesNone);

            var dataService = new DataService(_mockAggregateRepository.Object,
                _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
                _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);

            //  Act
            var results = dataService.GetAggregatesWithDataType(DataTypeEnum.Statistics).ToArray();

            //  Assert
            Assert.AreEqual(0, results.Count(), "Expected empty collection of aggregates to be returned.");
        }
        public void ImportRenUk_Successful()
        {
            //  Arrange
            var dataService = new DataService(_repository, _dataSourceRepository, _dataTypeRepository,_importServiceResolver, _dataSourceResolver);

            var inputDoc = _htmlDoc;

            using (ShimsContext.Create())
            {
                //  Fake the call to the internet
                HtmlAgilityPack.Fakes.ShimHtmlWeb.AllInstances.LoadString =
                    (htmlWeb, s) => { return inputDoc; };
                //  Fake the statue code returned from HTTP response
                HtmlAgilityPack.Fakes.ShimHtmlWeb.AllInstances.StatusCodeGet =
                    (web => { return System.Net.HttpStatusCode.OK; });

                //  Act
                var result = dataService.ImportDataSource(2, baseUrl);

                //  Assert
                Assert.IsTrue(result, "Expected a successful import to return True.");
            }
        }
        public void GetInvalidDataSource_Unsuccessful()
        {
            //  Arrange:
            var dataService = new DataService(_repository, _dataSourceRepository, _dataTypeRepository, _importServiceResolver, _dataSourceResolver);

            //  Act
            var result = dataService.GetDataSource(NonExistentDataSource);

            //  Assert
            Assert.IsNull(result, "Expected no datasource to be returned.");
        }
        public void GetDataSource_Successful_ReturnsDatasource2()
        {
            //  Arrange
            _mockDatasourceRepository.Setup(r => r.Get(It.IsAny<int>())).Returns(_dataSource2);

            var dataService = new DataService(_mockAggregateRepository.Object,
                _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
                _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);

            //  Act
            var result = dataService.GetDataSource(2);

            //  Assert
            Assert.AreEqual("Test Service 2", result.Title, "Expected datasource title to be 'Test Service 2'.");
        }
        public void GetDataSources_Successful_Returns2()
        {
            //  Arrange
            _mockDatasourceRepository.Setup(r => r.GetAll()).Returns(_datasources.AsQueryable());

            var dataService = new DataService(_mockAggregateRepository.Object,
                _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
                _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);

            //  Act
            var results = dataService.GeDataSources().ToArray();

            //  Assert
            Assert.AreEqual(2, results.Count(), "Expected 2 results to be returned.");
            Assert.AreEqual("Test Service 1", results[0].Title, "Expected first datasource title to be 'Test Service 1'.");
            Assert.AreEqual("Test Service 2", results[1].Title, "Expected second datasource title to be 'Test Service 2'.");
        }
        public void GetAggregate_UnSuccessful_NoSuchAggregate_MixedSourceValues()
        {
            //  Arrange
            //  Configure the AggregateRepository
            _mockAggregateRepository.Setup(r => r.Get(It.IsAny<int>(), It.IsAny<DataTypeEnum>())).Returns(() => null);

            var dataService = new DataService(_mockAggregateRepository.Object,
                _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
                _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);

            //  Act
            var result = dataService.GetAggregateWithDataType(4, DataTypeEnum.Statistics);

            //  Assert
            Assert.IsNull(result, "Expected NULL reference to be returned, Aggregate does not exist.");
        }
        public void GetAggregate_UnSuccessful_NoAggregateFound()
        {
            //  Arrange
            //  Setup the mock datasource instance
            var mockDataSource = new Mock<IRenUkDataSource>();
            mockDataSource.Setup(d => d.Get(It.IsAny<string>())).Returns(() => null);

            var dataService = new DataService(_mockAggregateRepository.Object,
                _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
                _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);

            //  Act
            var result = dataService.GetAggregate(_whiteleeWindfarm.Id);

            //  Assert
            Assert.IsNull(result, "Expected a NULL object to be returned");
        }
        public void GetAggregate_Successful_ReturnsDataTypeStatistics_MixedSourceValues()
        {
            //  Arrange
            var importAggregate = new ImportAggregate
            {
                Identifier = "Whitelee Windfarm",
                Data = new Collection<ImportData>
                {
                    new ImportData
                    {
                        DataType = DataTypeEnum.Statistics,
                        Data = "External JSON Object"
                    }
                },
                ImportDate = DateTime.Now,
                SourceId = 2,
            };

            //  Setup the mock datasource instance
            var mockDataSource = new Mock<IRenUkDataSource>();
            mockDataSource.Setup(d => d.Get(It.IsAny<string>())).Returns(importAggregate);
            //  Configure the DataSourceResolver
            _mockDataSourceResolver.Setup(r => r.Resolve(It.IsAny<int>())).Returns(mockDataSource.Object);
            //  Configure the AggregateRepository
            _mockAggregateRepository.Setup(r => r.Get(It.IsAny<int>(), It.IsAny<DataTypeEnum>())).Returns(_blacklawWindfarm);
            //  Configure the DataSourceRepository
            _mockDatasourceRepository.Setup(d => d.Get(1)).Returns(_dataSource1);
            _mockDatasourceRepository.Setup(d => d.Get(2)).Returns(_dataSource2);

            var dataService = new DataService(_mockAggregateRepository.Object,
                _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
                _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);

            //  Act
            var results = dataService.GetAggregateWithDataType(4, DataTypeEnum.Statistics);

            //  Assert
            Assert.AreEqual("Blacklaw Windfarm", results.Name, "Expected 'Blacklaw Windfarm' to be returned as first object.");
            Assert.AreEqual("External JSON Object", results.Data.FirstOrDefault(t => t.DataType == DataTypeEnum.Statistics).Data, "Expected the data to have been overridden as external source");
            Assert.AreEqual(DataTypeEnum.Statistics, results.Data.FirstOrDefault().DataType, "Expected original data '{achany estate}' to be returned");
            Assert.AreEqual(1, results.Data.Count, "Expected only one data segment");
        }
        public void GetAggregate_Successful_ReturnsAggregate1()
        {
            //  Arrange
            var importAggregate = new ImportAggregate
            {
                Identifier = "Whitelee Windfarm",
                Data = new Collection<ImportData>
                {
                    new ImportData
                    {
                        DataType = DataTypeEnum.Statistics,
                        Data = "External JSON Object"
                    }
                },
                ImportDate = DateTime.Now,
                SourceId = 2,
            };

            //  Setup the mock datasource instance
            var mockDataSource = new Mock<IRenUkDataSource>();
            mockDataSource.Setup(d => d.Get(It.IsAny<string>())).Returns(importAggregate);
            //  Configure the DataSourceResolver
            _mockDataSourceResolver.Setup(r => r.Resolve(It.IsAny<int>())).Returns(mockDataSource.Object);
            //  Configure the AggregateRepository
            _mockAggregateRepository.Setup(r => r.Get(2)).Returns(_whiteleeWindfarm);
            //  Configure the DataSourceRepository
            _mockDatasourceRepository.Setup(d => d.Get(1)).Returns(_dataSource1);
            _mockDatasourceRepository.Setup(d => d.Get(2)).Returns(_dataSource2);

            var dataService = new DataService(_mockAggregateRepository.Object,
                _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
                _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);

            //  Act
            var result = dataService.GetAggregate(_whiteleeWindfarm.Id);

            //  Assert
            Assert.AreEqual("Whitelee Windfarm", result.Name, "Expected 'Whitelee Windfarm' to be returned as first object.");
            Assert.AreEqual("{whitelee windfarm}", result.Data.FirstOrDefault(t => t.DataType == DataTypeEnum.FootPrint).Data, "Expected original data '{whitelee windfarm}' to be returned");
            Assert.AreEqual("External JSON Object", result.Data.FirstOrDefault(t => t.DataType == DataTypeEnum.Statistics).Data, "Expected the data to have been overridden as external source");
        }
        public void GetAggregates_UnSuccessful_ReturnsEmpty_NoSourceValues()
        {
            //  Arrange
            _mockAggregateRepository.Setup(r => r.GetAll()).Returns(new List<Aggregate>().AsQueryable);

            var dataService = new DataService(_mockAggregateRepository.Object,
                _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
                _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);

            //  Act
            var results = dataService.GetAggregates(string.Empty).ToArray();

            //  Assert
            Assert.AreEqual(0, results.Count(), "Expected zero aggregates to be returned.");
        }
        public void GetAggregate_NonExistantAggregate_Unsuccessful()
        {
            //  Arrange:
            var inputDoc = _htmlSingleAggregateDoc;

            var dataService = new DataService(_repository, _dataSourceRepository, _dataTypeRepository, _importServiceResolver, _dataSourceResolver);

            //  Mock the external call to the internet.
            using (ShimsContext.Create())
            {
                //  Fake the call to the internet
                HtmlAgilityPack.Fakes.ShimHtmlWeb.AllInstances.LoadString =
                    (htmlWeb, s) => { return inputDoc; };
                //  Fake the statue code returned from HTTP response
                HtmlAgilityPack.Fakes.ShimHtmlWeb.AllInstances.StatusCodeGet =
                    (web => { return System.Net.HttpStatusCode.OK; });

                //  Act
                var result = dataService.GetAggregate(NonExistentAggregate);

                //  Assert
                Assert.IsNull(result, "Expected no aggregate to be returned");
            }
        }
        public void GetDataTypes_Successful()
        {
            //  Arrange:
            var dataService = new DataService(_repository, _dataSourceRepository, _dataTypeRepository, _importServiceResolver, _dataSourceResolver);

            //  Act
            var result = dataService.GetDataTypes().ToArray();

            //  Assert
            var minCount = result.Count() >= 2;
            Assert.IsTrue(minCount, "Expected at least 2 data types to be returned, 'Status' and 'Turbine'");

            var containsStatus = result.Contains("Status");
            var containsTurbine = result.Contains("Turbine");

            Assert.IsTrue(containsStatus, "Expected 'Status' to be in the returned datatypes.");
            Assert.IsTrue(containsTurbine, "Expected 'turbine' to be in the returned datatypes.");
        }
        public void GetDataTypes_Successful_ReturnsCollection()
        {
            //  Arrange
            var mockImportService = new Mock<IRenUkImportService>();
            mockImportService.Setup(i => i.ImportData(It.IsAny<int>(), It.IsAny<string>())).Returns(false);
            _mockDataTypeRepository.Setup(t => t.GetAll()).Returns(_dataTypes);

            var dataService = new DataService(_mockAggregateRepository.Object,
                _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
                _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);

            //  Act
            var results = dataService.GetDataTypes().ToArray();

            //  Assert
            Assert.AreEqual(2, results.Count(), "Expected 2 datatypes to be returned.");
            Assert.AreEqual("Footprint", results[0], "Expected first DataType to be 'Footprint'.");
            Assert.AreEqual("Status", results[1], "Expected second DataType to be 'Status'.");
        }
        public void GetSNHDataSource_Successful()
        {
            //  Arrange:
            var dataService = new DataService(_repository, _dataSourceRepository, _dataTypeRepository, _importServiceResolver, _dataSourceResolver);

            //  Act
            var result = dataService.GetDataSource(SnhDataSourceId);

            //  Assert
            Assert.IsNotNull(result, "Expected 'SNH' datasource to be returned.");
            Assert.AreEqual("Scottish Natural Heritage KML Source", result.Title, "Expected 'SNH' as the first datasource");
        }
        public void ImportDataSource_UnSuccessful_ReturnsFALSE()
        {
            //  Arrange
            var mockImportService = new Mock<IRenUkImportService>();
            mockImportService.Setup(i => i.ImportData(It.IsAny<int>(), It.IsAny<string>())).Returns(false);

            _mockImportServiceResolver.Setup(r => r.Resolve(It.IsAny<int>())).Returns(mockImportService.Object);

            var dataService = new DataService(_mockAggregateRepository.Object,
                _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
                _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);

            //  Act
            var result = dataService.ImportDataSource(2, string.Empty);

            //  Assert
            Assert.IsFalse(result, "Expected Import to be unsuccessfull and return FALSE.");
        }
        public void GeAlltDataSources_Successful()
        {
            //  Arrange:
            //  Accessing data direct from data store, so keep to base data.
            //  Always check for the minimum test data, as other data could be
            //  there because of the imports, so do not check for any cases
            //  where the import data could be included.  Don't check for things
            //  like import or updated dates as these cannot be guaranteed and
            //  result in a false negative test.
            var dataService = new DataService(_repository, _dataSourceRepository, _dataTypeRepository, _importServiceResolver, _dataSourceResolver);

            //  Act
            var result = dataService.GeDataSources().ToArray();
            var minDatasources = (result.Count() >= 2) ? true : false;

            //  Assert
            Assert.IsNotNull(result, "Expected some datasources to be returned.");
            Assert.IsTrue(minDatasources, "Expected at least 2 datasources to be returned 'SNH & RenUK'.");
            Assert.AreEqual("Scottish Natural Heritage KML Source", result[0].Title, "Expected 'SNH' as the first datasource");
            Assert.AreEqual("RenewableUk WebSite", result[1].Title, "Expected 'RenUk' as the second datasource");
        }
        public void GetAggregates_Successful_ReturnsUnaltered_InvalidResolver()
        {
            //  Arrange
            //  Configure the DataSourceResolver
            _mockDataSourceResolver.Setup(r => r.Resolve(It.IsAny<int>())).Returns(() => null);
            //  Configure the AggregateRepository
            _mockAggregateRepository.Setup(r => r.GetAll()).Returns(_renUkAggregates);
            //  Configure the DataSourceRepository
            _mockDatasourceRepository.Setup(d => d.Get(1)).Returns(_dataSource1);
            _mockDatasourceRepository.Setup(d => d.Get(2)).Returns(_dataSource2);

            var dataService = new DataService(_mockAggregateRepository.Object,
                _mockDatasourceRepository.Object, _mockDataTypeRepository.Object,
                _mockImportServiceResolver.Object, _mockDataSourceResolver.Object);

            //  Act
            var results = dataService.GetAggregates(string.Empty).ToArray();

            //  Assert
            Assert.AreEqual(2, results.Count(), "Expected 2 aggregates to be returned.");
            Assert.AreEqual("Whitelee Windfarm", results[0].Name, "Expected 'Whitelee Windfarm' to be returned as first object.");
            Assert.AreEqual("{URL to Whiltelee Windfarm}", results[0].Data.FirstOrDefault(t => t.DataType == DataTypeEnum.Statistics).Data, "Expected the original data '{URL to Whiltelee Windfarm}' to be returned");
            Assert.AreEqual("Blacklaw Windfarm", results[1].Name, "Expected 'Blacklaw Windfarm' to be returned as first object.");
            Assert.AreEqual("{URL to Blacklaw Windfarm}", results[1].Data.FirstOrDefault(t => t.DataType == DataTypeEnum.Statistics).Data, "Expected the original data '{URL to Blacklaw Windfarm}' to be returned");
        }