public async Task V2FeedPackagesCollectionDoesNotContainDeletedPackages(string filter)
                {
                    // Arrange
                    var repo = FeedServiceHelpers.SetupTestPackageRepository();

                    var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");
                    configuration.Setup(c => c.Features).Returns(new FeatureConfiguration() { FriendlyLicenses = true });

                    var searchService = new Mock<ISearchService>(MockBehavior.Strict);
                    searchService.Setup(s => s.Search(It.IsAny<SearchFilter>())).Returns
                        <IQueryable<Package>, string>((_, __) => Task.FromResult(new SearchResults(_.Count(), DateTime.UtcNow, _)));
                    searchService.Setup(s => s.ContainsAllVersions).Returns(false);

                    var v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/api/v2/Packages?$filter=" + filter);

                    // Act
                    var result = (await v2Service.Get(new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request)))
                        .ExpectQueryResult<V2FeedPackage>()
                        .GetInnerResult()
                        .ExpectOkNegotiatedContentResult<IQueryable<V2FeedPackage>>()
                        .ToArray();

                    // Assert
                    Assert.False(result.Any(p => p.Id == "Baz"));
                }
                public async Task V2FeedPackagesDoesNotUseSearchHijackForFunkyQueries(string filter)
                {
                    // Arrange
                    var repo = FeedServiceHelpers.SetupTestPackageRepository();

                    var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");
                    configuration.Setup(c => c.Features).Returns(new FeatureConfiguration() { FriendlyLicenses = true });

                    bool called = false;
                    var searchService = new Mock<ExternalSearchService>(MockBehavior.Loose);
                    searchService.CallBase = true;
                    searchService.Setup(s => s.RawSearch(It.IsAny<SearchFilter>())).Returns
                        <IQueryable<Package>, string>((_, __) => Task.FromResult(new SearchResults(_.Count(), DateTime.UtcNow, _)))
                            .Callback(() => called = true);

                    string rawUrl = "https://localhost:8081/api/v2/Packages?$filter=" + filter + "&$top=10";

                    var v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, rawUrl);
                    v2Service.RawUrl = rawUrl;

                    // Act
                    var result = (await v2Service.Get(new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request)))
                        .ExpectQueryResult<V2FeedPackage>()
                        .GetInnerResult()
                        .ExpectOkNegotiatedContentResult<IQueryable<V2FeedPackage>>()
                        .ToArray();

                    // Assert
                    Assert.NotNull(result);
                    Assert.False(called); // Hijack was performed and it should not have been.
                    searchService.Verify();
                }
                public async Task V2FeedPackagesByIdAndVersionReturnsNotFoundWhenPackageNotFound(string expectedId, string expectedVersion)
                {
                    // Arrange
                    var repo = FeedServiceHelpers.SetupTestPackageRepository();

                    var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");
                    configuration.Setup(c => c.Features).Returns(new FeatureConfiguration() { FriendlyLicenses = true });

                    var searchService = new Mock<ISearchService>(MockBehavior.Strict);
                    searchService.Setup(s => s.Search(It.IsAny<SearchFilter>())).Returns
                        <IQueryable<Package>, string>((_, __) => Task.FromResult(new SearchResults(_.Count(), DateTime.UtcNow, _)));
                    searchService.Setup(s => s.ContainsAllVersions).Returns(false);

                    var v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/api/v2/Packages(Id='" + expectedId + "', Version='" + expectedVersion + "')");

                    // Act
                    (await v2Service.Get(new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request), expectedId, expectedVersion))
                        .ExpectResult<NotFoundResult>();
                }
                public async Task V2FeedPackagesReturnsCollection(string filter, int top, int expectedNumberOfPackages, string[] expectedIds, string[] expectedVersions)
                {
                    // Arrange
                    var repo = FeedServiceHelpers.SetupTestPackageRepository();

                    var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");
                    configuration.Setup(c => c.Features).Returns(new FeatureConfiguration() { FriendlyLicenses = true });

                    var searchService = new Mock<ISearchService>(MockBehavior.Strict);
                    searchService.Setup(s => s.Search(It.IsAny<SearchFilter>())).Returns
                        <IQueryable<Package>, string>((_, __) => Task.FromResult(new SearchResults(_.Count(), DateTime.UtcNow, _)));
                    searchService.Setup(s => s.ContainsAllVersions).Returns(false);

                    var v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/api/v2/Packages?$filter=" + filter + "&$top=" + top);

                    // Act
                    var result = (await v2Service.Get(new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request)))
                        .ExpectQueryResult<V2FeedPackage>()
                        .GetInnerResult()
                        .ExpectOkNegotiatedContentResult<IQueryable<V2FeedPackage>>()
                        .ToArray();

                    // Assert
                    Assert.Equal(expectedNumberOfPackages, result.Length);
                    for (int i = 0; i < expectedIds.Length; i++)
                    {
                        var expectedId = expectedIds[i];
                        var expectedVersion = expectedVersions[i];

                        Assert.True(result.Any(p => p.Id == expectedId && p.Version == expectedVersion), string.Format("Search results did not contain {0} {1}", expectedId, expectedVersion));
                    }
                }