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

            var fakeStatisticsService = new Mock <IStatisticsService>();

            fakeStatisticsService
            .Setup(service => service.GetPackageDownloadsByVersion(PackageId))
            .Throws <StatisticsReportNotFoundException>();

            var controller = new StatisticsController(fakeStatisticsService.Object);

            TestUtility.SetupUrlHelperForUrlGeneration(controller);

            await controller.PackageDownloadsByVersionReport(PackageId, It.IsAny <string[]>());

            Assert.Equal(404, controller.Response.StatusCode);
        }
        public async Task StatisticsDownloadByVersionAction_ReturnsHttpOkIfPackageExists()
        {
            const string PackageId = "A";

            var fakeStatisticsService = new Mock <IStatisticsService>();

            fakeStatisticsService
            .Setup(service => service.GetPackageDownloadsByVersion(PackageId))
            .Returns(Task.FromResult(new StatisticsPackagesReport()));

            var controller = new StatisticsController(fakeStatisticsService.Object);

            TestUtility.SetupUrlHelperForUrlGeneration(controller);

            await controller.PackageDownloadsByVersionReport(PackageId, It.IsAny <string[]>());

            Assert.Equal(200, controller.Response.StatusCode);
        }
        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[] { GalleryConstants.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);
        }