public async Task VerifyRecentPopularityStatsDownloads()
            {
                JArray report = new JArray
                {
                    new JObject
                    {
                        { "PackageId", "A" },
                        { "PackageVersion", "1.0" },
                        { "Downloads", 3 }
                    },
                    new JObject
                    {
                        { "PackageId", "A" },
                        { "PackageVersion", "1.1" },
                        { "Downloads", 4 }
                    },
                    new JObject
                    {
                        { "PackageId", "B" },
                        { "PackageVersion", "1.0" },
                        { "Downloads", 5 }
                    },
                    new JObject
                    {
                        { "PackageId", "B" },
                        { "PackageVersion", "1.1" },
                        { "Downloads", 6 }
                    },
                };

                var fakePackageVersionReport = report.ToString();

                var fakeReportService = new Mock <IReportService>();

                fakeReportService.Setup(x => x.Load("recentpopularitydetail.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageVersionReport, DateTime.UtcNow)));

                var controller = new TestableApiController
                {
                    StatisticsService = 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");
            }
            public async Task VerifyStatsDownloadsReturnsNotFoundWhenStatsNotAvailable()
            {
                var controller = new TestableApiController();

                controller.MockStatisticsService.Setup(x => x.LoadDownloadPackageVersions()).Returns(Task.FromResult(StatisticsReportResult.Failed));

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

                ActionResult actionResult = await controller.GetStatsDownloads(null);

                HttpStatusCodeResult httpStatusResult = (HttpStatusCodeResult)actionResult;

                Assert.True(httpStatusResult.StatusCode == (int)HttpStatusCode.NotFound, "unexpected StatusCode");
            }
        public async void StatisticsHomePage_Per_Package_ValidateModel()
        {
            string PackageId = "A";

            var fakeReportService = new Mock <IReportService>();

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

            TestUtility.SetupUrlHelperForUrlGeneration(controller);

            var model = (StatisticsPackagesViewModel)((ViewResult)await controller.PackageDownloadsByVersion(PackageId, new[] { GalleryConstants.StatisticsDimensions.Version })).Model;

            Assert.Equal(PackageId, model.PackageId);
        }
        public async void Statistics_By_Client_Operation_ValidateModel()
        {
            string PackageId      = "A";
            string PackageVersion = "2.0";

            var fakeReportService = new Mock <IReportService>();

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

            TestUtility.SetupUrlHelperForUrlGeneration(controller);

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

            Assert.Equal(PackageId, model.PackageId);
            Assert.Equal(PackageVersion, model.PackageVersion);
        }
Exemple #5
0
            public async void VerifyStatsDownloadsReturnsNotFoundWhenStatsNotAvailable()
            {
                var fakeStatisticsService = new Mock <IStatisticsService>();

                fakeStatisticsService.Setup(x => x.LoadDownloadPackageVersions()).Returns(Task.FromResult(false));

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

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

                ActionResult actionResult = await controller.GetStatsDownloads(null);

                HttpStatusCodeResult httpStatusResult = (HttpStatusCodeResult)actionResult;

                Assert.True(httpStatusResult.StatusCode == (int)HttpStatusCode.NotFound, "unexpected StatusCode");
            }
            public async void VerifyRecentPopularityStatsDownloadsCount()
            {
                JArray report = new JArray
                {
                    new JObject {
                        { "PackageId", "A" }, { "PackageVersion", "1.0" }, { "Downloads", 3 }
                    },
                    new JObject {
                        { "PackageId", "A" }, { "PackageVersion", "1.1" }, { "Downloads", 4 }
                    },
                    new JObject {
                        { "PackageId", "B" }, { "PackageVersion", "1.0" }, { "Downloads", 5 }
                    },
                    new JObject {
                        { "PackageId", "B" }, { "PackageVersion", "1.1" }, { "Downloads", 6 }
                    },
                    new JObject {
                        { "PackageId", "C" }, { "PackageVersion", "1.0" }, { "Downloads", 7 }
                    },
                    new JObject {
                        { "PackageId", "C" }, { "PackageVersion", "1.1" }, { "Downloads", 8 }
                    },
                };

                var fakePackageVersionReport = report.ToString();

                var fakeReportService = new Mock <IReportService>();

                fakeReportService.Setup(x => x.Load("RecentPopularityDetail.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageVersionReport, DateTime.UtcNow)));

                var controller = new TestableApiController
                {
                    StatisticsService = new JsonStatisticsService(fakeReportService.Object),
                };

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

                T4MVCHelpers.ProcessVirtualPath = s => s;

                ActionResult actionResult = await controller.GetStatsDownloads(3);

                ContentResult contentResult = (ContentResult)actionResult;

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

                Assert.True(result.Count == 3, "unexpected content");
            }
        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 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 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 StatisticsHomePage_Per_Package_ValidateReportStructureAndAvailability()
        {
            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", "unknow" },
                                    { "Downloads", 301 }
                                }
                            } }
                      },
                  } }
            };

            var fakeReport = report.ToString();

            var fakeReportService = new Mock <IReportService>();

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

            reportName = reportName.ToLowerInvariant();

            fakeReportService.Setup(x => x.Load(reportName)).Returns(Task.FromResult(fakeReport));

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

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

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

            int sum = 0;

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

            Assert.Equal <int>(603, sum);
            Assert.Equal <string>("603", model.Report.Total);
        }
        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);
        }
Exemple #12
0
            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");
            }