public async Task StatisticsHomePage_Per_Package_ValidateReportStructureAndAvailabilityInvalidGroupBy()
        {
            string PackageId = "A";

            JObject report = new JObject
            {
                { "Downloads", 603 },
                { "Items", new JArray
                  {
                      new JObject
                      {
                          { "Version", "1.0" },
                          { "Downloads", 101 },
                          { "Items", new JArray
                            {
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "Install" },
                                    { "Downloads", 101 }
                                },
                            } }
                      },
                      new JObject
                      {
                          { "Version", "2.0" },
                          { "Downloads", 502 },
                          { "Items", new JArray
                            {
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "Install" },
                                    { "Downloads", 201 }
                                },
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "unknown" },
                                    { "Downloads", 301 }
                                }
                            } }
                      },
                  } }
            };

            var fakeReport = report.ToString();

            var fakeReportService = new Mock <IReportService>();

            string reportName = "recentpopularity/RecentPopularityDetail_" + PackageId + ".json";

            reportName = reportName.ToLowerInvariant();

            var updatedUtc = new DateTime(2001, 01, 01, 10, 20, 30);

            fakeReportService.Setup(x => x.Load(reportName)).Returns(Task.FromResult(new StatisticsReport(fakeReport, updatedUtc)));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            TestUtility.SetupUrlHelperForUrlGeneration(controller);

            var invalidDimension = "this_dimension_does_not_exist";

            var actualReport = (StatisticsPackagesReport)((JsonResult)await controller.PackageDownloadsByVersionReport(PackageId, new[] { Constants.StatisticsDimensions.Version, invalidDimension })).Data;

            int sum = 0;

            foreach (var row in actualReport.Table)
            {
                sum += int.Parse(row[row.GetLength(0) - 1].Data);
            }

            Assert.Equal(603, sum);
            Assert.Equal(603, actualReport.Total);
            Assert.True(actualReport.LastUpdatedUtc.HasValue);
            Assert.Equal(updatedUtc, actualReport.LastUpdatedUtc.Value);
            Assert.DoesNotContain(invalidDimension, actualReport.Columns);
        }
        public async void Statistics_By_Client_Operation_ValidateReportStructureAndAvailability()
        {
            string PackageId      = "A";
            string PackageVersion = "2.0";

            JObject report = new JObject
            {
                { "Downloads", 603 },
                { "Items", new JArray
                  {
                      new JObject
                      {
                          { "Version", "1.0" },
                          { "Downloads", 101 },
                          { "Items", new JArray
                            {
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "Install" },
                                    { "Downloads", 101 }
                                },
                            } }
                      },
                      new JObject
                      {
                          { "Version", "2.0" },
                          { "Downloads", 502 },
                          { "Items", new JArray
                            {
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "Install" },
                                    { "Downloads", 201 }
                                },
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "unknow" },
                                    { "Downloads", 301 }
                                }
                            } }
                      },
                  } }
            };

            var fakeReport = report.ToString();

            var fakeReportService = new Mock <IReportService>();

            string reportName = "recentpopularity/RecentPopularityDetail_" + PackageId + ".json";

            reportName = reportName.ToLowerInvariant();

            var updatedUtc = new DateTime(2001, 01, 01, 10, 20, 30);

            fakeReportService.Setup(x => x.Load(reportName)).Returns(Task.FromResult(new StatisticsReport(fakeReport, updatedUtc)));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            TestUtility.SetupUrlHelperForUrlGeneration(controller, new Uri("http://nuget.org"));

            var model = (StatisticsPackagesViewModel)((ViewResult)await controller.PackageDownloadsDetail(PackageId, PackageVersion, new string[] { "ClientName" })).Model;

            int sum = 0;

            foreach (var row in model.Report.Table)
            {
                sum += int.Parse(row[row.GetLength(0) - 1].Data);
            }

            Assert.Equal(502, sum);
            Assert.Equal("502", model.Report.Total);
            Assert.True(model.LastUpdatedUtc.HasValue);
            Assert.Equal(updatedUtc, model.LastUpdatedUtc.Value);
        }
            public async void VerifyRecentPopularityStatsDownloads()
            {
                JArray report = new JArray
                {
                    new JObject
                    {
                        { "PackageId", "A" },
                        { "PackageVersion", "1.0" },
                        { "PackageTitle", "Package A Title" },
                        { "PackageDescription", "Package A Description" },
                        { "PackageIconUrl", "Package A IconUrl" },
                        { "Downloads", 3 }
                    },
                    new JObject
                    {
                        { "PackageId", "A" },
                        { "PackageVersion", "1.1" },
                        { "PackageTitle", "Package A Title" },
                        { "PackageDescription", "Package A Description" },
                        { "PackageIconUrl", "Package A IconUrl" },
                        { "Downloads", 4 }
                    },
                    new JObject
                    {
                        { "PackageId", "B" },
                        { "PackageVersion", "1.0" },
                        { "PackageTitle", "Package B Title" },
                        { "PackageDescription", "Package B Description" },
                        { "PackageIconUrl", "Package B IconUrl" },
                        { "Downloads", 5 }
                    },
                    new JObject
                    {
                        { "PackageId", "B" },
                        { "PackageVersion", "1.1" },
                        { "PackageTitle", "Package B Title" },
                        { "PackageDescription", "Package B Description" },
                        { "PackageIconUrl", "Package B IconUrl" },
                        { "Downloads", 6 }
                    },
                };

                var fakePackageVersionReport = report.ToString();

                var fakeReportService = new Mock <IReportService>();

                fakeReportService.Setup(x => x.Load("RecentPopularityDetail.json")).Returns(Task.FromResult(fakePackageVersionReport));

                var controller = new ApiController(null, null, null, null, null, null, new JsonStatisticsService(fakeReportService.Object));

                TestUtility.SetupUrlHelperForUrlGeneration(controller, new Uri("http://nuget.org"));

                ActionResult actionResult = await controller.GetStatsDownloads(null);

                ContentResult contentResult = (ContentResult)actionResult;

                JArray result = JArray.Parse(contentResult.Content);

                Assert.True((string)result[3]["Gallery"] == "http://nuget.org/packages/B/1.1", "unexpected content result[3].Gallery");
                Assert.True((int)result[2]["Downloads"] == 5, "unexpected content result[2].Downloads");
                Assert.True((string)result[1]["PackageDescription"] == "Package A Description", "unexpected content result[1].PackageDescription");
            }