/// <summary>
        /// Get the Aggregate for the specified Id
        /// </summary>
        /// <param name="url">The Url of the aggregate requested</param>
        /// <returns>The requested Aggregate or NULL if nothing found</returns>
        /// <remarks>
        /// <para>
        /// This method is for use when the data held in the internal
        /// data store is a URL to the external source, which then 
        /// has to be called to get the actual data.
        /// </para>
        /// </remarks>
        public ImportAggregate Get(string url)
        {
            //  Deserialise the JSON object containing the Url
            var actualUrl = _helper.GetUrlFromJsonObject(url);
            if (actualUrl == null) return null;

            //  This Has to accept the Url and call to the external source.
            var importAggregate = new ImportAggregate();

            if (LoadData(out importAggregate, actualUrl))
                return importAggregate;
            return null;
        }
Ejemplo n.º 2
0
        public void MapImportToAggregateClass_AggregateWithMultipleData_OK()
        {
            //  Arrange:    Create an Import class & instantiate the class
            var importAggregate = new ImportAggregate()
            {
                Identifier = "FootPrintAggregate",
                ImportDate = _timeStamp,
                SourceId = 1,
                Data = new List<ImportData>()
                {
                    new ImportData()
                    {
                        Data = "{test data}",
                        DataType = DataTypeEnum.FootPrint
                    },
                    new ImportData()
                    {
                        Data = "{test data 2}",
                        DataType = DataTypeEnum.Style
                    },
                }
            };

            var mapper = new ImportAggregateToAggregateMapper();

            //  Act:        Call the map method
            var result = mapper.Map(importAggregate);
            var resultData = result.Data.ToArray();

            //  Assert
            Assert.AreEqual(0, result.Id, "Expected aggregate to have no Id, ie new aggregate");
            Assert.AreEqual("FootPrintAggregate", result.Name, "Expected the aggregate to be called 'FootprintAggregate");
            Assert.AreEqual(_timeStamp, result.LastUpdated, string.Format("Expected last updated to be set to {0}", _timeStamp));
            Assert.AreEqual(2, result.Data.Count, "Expected there to be 2 entry in the data collection");

            Assert.AreEqual(0, resultData[0].AggregateId, "Expected the data to have no aggregate Id either.");
            Assert.AreEqual(1, resultData[0].DataSourceId, "Expected a DataSourceid of 1");
            Assert.AreEqual(0, resultData[0].AggregateId, "Expected the aggregateId not to be set, ie 0 for a new aggregate");
            Assert.AreEqual("FootPrintAggregate", resultData[0].Name, "Expected the data.AggregateId to be 'FootprintAggregate'");
            Assert.AreEqual("{test data}", resultData[0].Data, "Expected '{test data}' to be set.");
            Assert.AreEqual(_timeStamp, resultData[0].LastUpdated, string.Format("Expected last updated to be set to '{0}'", _timeStamp));
            Assert.AreEqual(DataTypeEnum.FootPrint, resultData[0].DataType, string.Format("Expected the datatype to be '{0}'", DataTypeEnum.FootPrint));

            Assert.AreEqual(0, resultData[1].AggregateId, "Expected the data to have no aggregate Id either.");
            Assert.AreEqual(1, resultData[1].DataSourceId, "Expected a DataSourceid of 1");
            Assert.AreEqual(0, resultData[1].AggregateId, "Expected the aggregateId not to be set, ie 0 for a new aggregate");
            Assert.AreEqual("FootPrintAggregate", resultData[1].Name, "Expected the data.AggregateId to be 'FootprintAggregate'");
            Assert.AreEqual("{test data 2}", resultData[1].Data, "Expected '{test data 2}' to be set.");
            Assert.AreEqual(_timeStamp, resultData[1].LastUpdated, string.Format("Expected last updated to be set to '{0}'", _timeStamp));
            Assert.AreEqual(DataTypeEnum.Style, resultData[1].DataType, string.Format("Expected the datatype to be '{0}'", DataTypeEnum.Style));
        }
Ejemplo n.º 3
0
        public void MapImportToAggregateClass_AggregateWithNoData_OK()
        {
            //  Arrange:    Create an Import class & instantiate the class
            var importAggregate = new ImportAggregate()
            {
                Identifier = "NoDataAggregate",
                ImportDate = _timeStamp,
                SourceId = 1,
                Data = new List<ImportData>()
            };

            var mapper = new ImportAggregateToAggregateMapper();

            //  Act:        Call the map method
            var result = mapper.Map(importAggregate);

            //  Assert
            Assert.AreEqual(0, result.Id, "Expected aggregate to have no Id, ie new aggregate");
            Assert.AreEqual("NoDataAggregate", result.Name, "Expected the aggregate to be called 'NoDataAggregate");
            Assert.AreEqual(_timeStamp, result.LastUpdated, string.Format("Expected last updated to be set to {0}", _timeStamp));
            Assert.AreEqual(0, result.Data.Count, "Expected there to be an empty data collection");
        }
Ejemplo n.º 4
0
        public void MapImportToAggregateClass_UnInitialisedAggregate_ReturnsEmptyAggregate()
        {
            //  Arrange:    Create an Import class & instantiate the class
            var importAggregate = new ImportAggregate();

            var mapper = new ImportAggregateToAggregateMapper();

            //  Act:        Call the map method
            var result = mapper.Map(importAggregate);

            //  Assert
            Assert.AreEqual(0, result.Id, "Expected aggregate to have no Id, ie new aggregate");
            Assert.AreEqual(string.Empty, result.Name, "Expected the aggregate to have no Name");
            Assert.AreEqual(new DateTime(), result.LastUpdated, string.Format("Expected last updated to be set to {0}", _timeStamp));
            Assert.AreEqual(0, result.Data.Count, "Expected there to be an empty data collection");
        }
        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");
        }
        /// <summary>
        /// Gets the resource for a specific url, coming from the Get operation
        /// </summary>
        /// <param name="sourceData">Output data scraped from the web site</param>
        /// <param name="url">The url of the resource</param>
        /// <returns>True if successfully scraped, otherwise false</returns>
        private bool LoadData(out ImportAggregate sourceData, string url)
        {
            sourceData = new ImportAggregate();

            //  Read the web page, return if not successful
            var doc = new HtmlDocument();
            if (_helper.LoadHtmlPage(out doc, url) != HttpStatusCode.OK)
                return false;

            var data = ScrapeDocument(doc);
            if (data == null)
                return false;

            sourceData = data;
            return true;
        }
        /// <summary>
        /// Creates and populates the importAggregate from the Name
        /// </summary>
        /// <param name="name">The name of the aggregate (AggregateIdentifier)</param>
        /// <param name="data">The data as a JSON object</param>
        /// <returns>Returns the new ImportAggregate</returns>
        private ImportAggregate CreateAggregate(string name, string data)
        {
            var importData = new ImportData()
            {
                DataType = Core.Model.DataTypeEnum.Statistics,
                Data = data
            };

            var importAggregate = new ImportAggregate()
            {
                Identifier = name,
                ImportDate = DateTime.Now,
                SourceId = 2,
                Data = new Collection<ImportData>()
            };
            importAggregate.Data.Add(importData);
            return importAggregate;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Create the Import model for the source, which contains all the
        /// data extracted from the source.
        /// </summary>
        /// <param name="name">Name of the aggregate</param>
        /// <param name="placemark">The placemark (data) for the aggregate</param>
        /// <param name="styles">The style, representing the status of the aggregat</param>
        /// <returns>Fully constructed instance of the aggregate containing all data extracted from the source</returns>
        private ImportAggregate CreateAggregate(string name, XElement placemark, IDictionary<string, XElement> styles)
        {
            var statusid = _helper.GetPlacemarkStatus(placemark);

            //  Get the style (Status)
            XElement styleKml = null;
            styles.TryGetValue(statusid, out styleKml);

            //  TODO: get the LOCATION information (from the NAMES folder) by name
            //  TODO: Refactor creation of ImportData and ImportAggregate classes using factory

            //  Format output object and add to results.
            //var strFootprintData = placemark.ToJson();
            var footprintData = new ImportData()
            {
                DataType = DataTypeEnum.FootPrint,
                Data = placemark.ToJson()
            };
            var statusData = new ImportData()
            {
                DataType = DataTypeEnum.Status,
                Data = (styleKml != null) ? styleKml.ToJson() : string.Empty
            };
            var aggregate = new ImportAggregate()
            {
                Identifier = name,
                SourceId = 1,
                Data = new Collection<ImportData>()
                                {
                                    statusData,
                                    footprintData
                                }
            };
            return aggregate;
        }