public async Task SearchDatasetVersion_GivenInvalidParameters_ShouldReturnBadRequestResult()
        {
            // Arrange
            SearchModel model = new SearchModel
            {
                PageNumber    = 0,
                Top           = 0,
                IncludeFacets = false
            };

            SearchResults <DatasetVersionIndex> mockSearchResults = new SearchResults <DatasetVersionIndex>();

            ISearchRepository <DatasetVersionIndex> mockDatasetVersionIndexRepository = CreateDatasetVersionSearchRepository();

            mockDatasetVersionIndexRepository.Search(Arg.Any <string>(), Arg.Any <SearchParameters>()).Returns(mockSearchResults);

            DatasetSearchService service = CreateDatasetSearchService(searchRepositoryDatasetVersion: mockDatasetVersionIndexRepository);

            // Act
            IActionResult actionResult = await service.SearchDatasetVersion(model);

            // Assert
            actionResult
            .Should().BeOfType <BadRequestObjectResult>()
            .Which
            .Value
            .Should().Be("An invalid search model was provided");
        }
        public async Task SearchDatasetVersion_GivenInvalidParameters_ShouldReturnBadRequestResult()
        {
            // Arrange
            SearchModel model = new SearchModel
            {
                PageNumber    = 0,
                Top           = 0,
                IncludeFacets = false
            };

            string json = JsonConvert.SerializeObject(model);

            byte[]       byteArray = Encoding.UTF8.GetBytes(json);
            MemoryStream stream    = new MemoryStream(byteArray);

            HttpRequest request = Substitute.For <HttpRequest>();

            request
            .Body
            .Returns(stream);

            SearchResults <DatasetVersionIndex> mockSearchResults = new SearchResults <DatasetVersionIndex>();

            ISearchRepository <DatasetVersionIndex> mockDatasetVersionIndexRepository = CreateDatasetVersionSearchRepository();

            mockDatasetVersionIndexRepository.Search(Arg.Any <string>(), Arg.Any <SearchParameters>()).Returns(mockSearchResults);

            DatasetSearchService service = CreateDatasetSearchService(searchRepositoryDatasetVersion: mockDatasetVersionIndexRepository);

            // Act
            IActionResult actionResult = await service.SearchDatasetVersion(request);

            // Assert
            actionResult
            .Should().BeOfType <BadRequestObjectResult>()
            .Which
            .Value
            .Should().Be("An invalid search model was provided");
        }
        public async Task SearchDatasetVersion_GivenValidParameters_ShouldReturnOkResult()
        {
            // Arrange
            SearchModel model = new SearchModel
            {
                PageNumber    = 10,
                Top           = 50,
                IncludeFacets = false
            };

            string blobName = "v1/Pe and sports Data.xlsx";

            SearchResults <DatasetVersionIndex> mockSearchResults = new SearchResults <DatasetVersionIndex>();

            mockSearchResults.Results = new List <Repositories.Common.Search.SearchResult <DatasetVersionIndex> >()
            {
                CreateDatasetVersionResult(new DatasetVersionIndex()
                {
                    Id                = "df073a02-bbc5-44ee-a84b-5931c6e7cf1e-v1",
                    Name              = "Pe and sports Data",
                    Version           = 1,
                    BlobName          = blobName,
                    DefinitionName    = "PSG",
                    DatasetId         = "df073a02-bbc5-44ee-a84b-5931c6e7cf1e",
                    Description       = "150 rows starting",
                    LastUpdatedByName = "James",
                    LastUpdatedDate   = new DateTime(2019, 1, 1),
                    FundingStreamId   = "DSG",
                    FundingStreamName = "Dedicated schools grant"
                })
            };

            ISearchRepository <DatasetVersionIndex> mockDatasetVersionIndexRepository = CreateDatasetVersionSearchRepository();

            mockDatasetVersionIndexRepository.Search(Arg.Any <string>(), Arg.Any <SearchParameters>()).Returns(mockSearchResults);
            DatasetSearchService service = CreateDatasetSearchService(searchRepositoryDatasetVersion: mockDatasetVersionIndexRepository);

            // Act
            IActionResult actionResult = await service.SearchDatasetVersion(model);

            // Assert
            actionResult.Should().BeOfType <OkObjectResult>();

            OkObjectResult objectResult = actionResult as OkObjectResult;
            DatasetVersionSearchResults datasetVersionSearchResults = objectResult.Value as DatasetVersionSearchResults;

            datasetVersionSearchResults.Results.Count().Should().Be(1);

            DatasetVersionSearchResult datasetVersionSearchResult = datasetVersionSearchResults.Results.First();

            datasetVersionSearchResult.Id.Should().Be("df073a02-bbc5-44ee-a84b-5931c6e7cf1e-v1");
            datasetVersionSearchResult.Name.Should().Be("Pe and sports Data");
            datasetVersionSearchResult.Version.Should().Be(1);
            datasetVersionSearchResult.BlobName.Should().Be(blobName);
            datasetVersionSearchResult.DefinitionName.Should().Be("PSG");
            datasetVersionSearchResult.DatasetId.Should().Be("df073a02-bbc5-44ee-a84b-5931c6e7cf1e");
            datasetVersionSearchResult.Description.Should().Be("150 rows starting");
            datasetVersionSearchResult.LastUpdatedByName.Should().Be("James");
            datasetVersionSearchResult.LastUpdatedDate.Should().Be(new DateTime(2019, 1, 1));
            datasetVersionSearchResult.FundingStreamId.Should().Be("DSG");
            datasetVersionSearchResult.FundingStreamName.Should().Be("Dedicated schools grant");
        }
        public async Task SearchDatasetVersion_GivenValidParameters_ShouldReturnOkResult()
        {
            // Arrange
            SearchModel model = new SearchModel
            {
                PageNumber    = 10,
                Top           = 50,
                IncludeFacets = false
            };

            string blobName = "v1/Pe and sports Data.xlsx";

            string json = JsonConvert.SerializeObject(model);

            byte[]       byteArray = Encoding.UTF8.GetBytes(json);
            MemoryStream stream    = new MemoryStream(byteArray);

            HttpRequest request = Substitute.For <HttpRequest>();

            request
            .Body
            .Returns(stream);

            SearchResults <DatasetVersionIndex> mockSearchResults = new SearchResults <DatasetVersionIndex>();

            mockSearchResults.Results = new List <Repositories.Common.Search.SearchResult <DatasetVersionIndex> >()
            {
                CreateDatasetVersionResult(new DatasetVersionIndex()
                {
                    Id                = "df073a02-bbc5-44ee-a84b-5931c6e7cf1e-v1",
                    Name              = "Pe and sports Data",
                    Version           = 1,
                    BlobName          = blobName,
                    DefinitionName    = "PSG",
                    DatasetId         = "df073a02-bbc5-44ee-a84b-5931c6e7cf1e",
                    Description       = "150 rows starting",
                    LastUpdatedByName = "James",
                    LastUpdatedDate   = new DateTime(2019, 1, 1)
                })
            };

            ISearchRepository <DatasetVersionIndex> mockDatasetVersionIndexRepository = CreateDatasetVersionSearchRepository();

            mockDatasetVersionIndexRepository.Search(Arg.Any <string>(), Arg.Any <SearchParameters>()).Returns(mockSearchResults);
            DatasetSearchService service = CreateDatasetSearchService(searchRepositoryDatasetVersion: mockDatasetVersionIndexRepository);

            // Act
            IActionResult actionResult = await service.SearchDatasetVersion(request);

            // Assert
            actionResult.Should().BeOfType <OkObjectResult>();

            OkObjectResult objectResult = actionResult as OkObjectResult;
            DatasetVersionSearchResults datasetVersionSearchResults = objectResult.Value as DatasetVersionSearchResults;

            datasetVersionSearchResults.Results.Count().Should().Be(1);

            DatasetVersionSearchResult datasetVersionSearchResult = datasetVersionSearchResults.Results.First();

            datasetVersionSearchResult.Id.Should().Be("df073a02-bbc5-44ee-a84b-5931c6e7cf1e-v1");
            datasetVersionSearchResult.Name.Should().Be("Pe and sports Data");
            datasetVersionSearchResult.Version.Should().Be(1);
            datasetVersionSearchResult.BlobName.Should().Be(blobName);
            datasetVersionSearchResult.DefinitionName.Should().Be("PSG");
            datasetVersionSearchResult.DatasetId.Should().Be("df073a02-bbc5-44ee-a84b-5931c6e7cf1e");
            datasetVersionSearchResult.Description.Should().Be("150 rows starting");
            datasetVersionSearchResult.LastUpdatedByName.Should().Be("James");
            datasetVersionSearchResult.LastUpdatedDate.Should().Be(new DateTime(2019, 1, 1));
        }