public async Task GetSearch_ReturnsSearchView()
        {
            // Arrange
            BreadcrumbService breadcrumb = A.Dummy<BreadcrumbService>();
            ISearcher<ProducerSearchResult> producerSearcher = A.Dummy<ISearcher<ProducerSearchResult>>();
            Func<IWeeeClient> weeeClient = A.Dummy<Func<IWeeeClient>>();

            ProducersController controller = new ProducersController(breadcrumb, producerSearcher, weeeClient, cache);

            // Act
            ActionResult result = await controller.Search();

            // Assert
            ViewResult viewResult = result as ViewResult;
            Assert.NotNull(viewResult);

            Assert.True(string.IsNullOrEmpty(viewResult.ViewName) || viewResult.ViewName.ToLowerInvariant() == "search");
        }
        public async Task PostSearch_WithInvalidModel_ReturnsSearchView()
        {
            // Arrange
            BreadcrumbService breadcrumb = A.Dummy<BreadcrumbService>();
            ISearcher<ProducerSearchResult> producerSearcher = A.Dummy<ISearcher<ProducerSearchResult>>();
            Func<IWeeeClient> weeeClient = A.Dummy<Func<IWeeeClient>>();

            ProducersController controller = new ProducersController(breadcrumb, producerSearcher, weeeClient, cache);

            SearchViewModel viewModel = new SearchViewModel();
            controller.ModelState.AddModelError("SomeProperty", "Exception");

            // Act
            ActionResult result = await controller.Search(viewModel);

            // Assert
            ViewResult viewResult = result as ViewResult;
            Assert.NotNull(viewResult);

            Assert.True(string.IsNullOrEmpty(viewResult.ViewName) || viewResult.ViewName.ToLowerInvariant() == "search");
        }
        public async Task PostSearch_WithSearchTermAndNoSelectedPRN_RedirectsToSearchResultsAction()
        {
            // Arrange
            BreadcrumbService breadcrumb = A.Dummy<BreadcrumbService>();
            ISearcher<ProducerSearchResult> producerSearcher = A.Dummy<ISearcher<ProducerSearchResult>>();
            Func<IWeeeClient> weeeClient = A.Dummy<Func<IWeeeClient>>();

            ProducersController controller = new ProducersController(breadcrumb, producerSearcher, weeeClient, cache);

            SearchViewModel viewModel = new SearchViewModel();
            viewModel.SearchTerm = "testSearchTerm";
            viewModel.SelectedRegistrationNumber = null;

            // Act
            ActionResult result = await controller.Search(viewModel);

            // Assert
            RedirectToRouteResult redirectResult = result as RedirectToRouteResult;
            Assert.NotNull(redirectResult);

            Assert.Equal("SearchResults", redirectResult.RouteValues["action"]);
            Assert.Equal("testSearchTerm", redirectResult.RouteValues["SearchTerm"]);
        }
        public async void HttpGet_DownloadProducerEeeHistoryCsv_ShouldReturnFileContentType()
        {
            // Arrange
            BreadcrumbService breadcrumb = A.Dummy<BreadcrumbService>();
            ISearcher<ProducerSearchResult> producerSearcher = A.Dummy<ISearcher<ProducerSearchResult>>();
            IWeeeClient weeeClient = A.Fake<IWeeeClient>();
            CSVFileData csvData = A.Dummy<CSVFileData>();

            A.CallTo(() => weeeClient.SendAsync(A<string>._, A<GetProducerEeeDataHistoryCsv>._))
                .Returns(new CSVFileData
                {
                    FileName = "test.csv",
                    FileContent = "123,abc"
                });

            Func<IWeeeClient> weeeClientFunc = A.Fake<Func<IWeeeClient>>();
            A.CallTo(() => weeeClientFunc())
                .Returns(weeeClient);

            ProducersController controller = new ProducersController(breadcrumb, producerSearcher, weeeClientFunc, cache);

            //Act
            var result = await controller.DownloadProducerEeeDataHistoryCsv("WEE/AA1111AA");

            //Assert
            Assert.IsType<FileContentResult>(result);
        }
        public async Task PostConfirmRemoval_WithInvalidModel_CallsApiAndPopulatesViewModel()
        {
            // Arrange
            Guid registeredProducerId = new Guid("9F253FE4-B644-4EA1-B58E-19C735512449");

            ProducerDetailsScheme producerDetailsScheme = A.Dummy<ProducerDetailsScheme>();

            IWeeeClient weeeClient = A.Fake<IWeeeClient>();
            A.CallTo(() => weeeClient.SendAsync(A<string>._, A<GetProducerDetailsByRegisteredProducerId>._))
                .WhenArgumentsMatch(a => a.Get<GetProducerDetailsByRegisteredProducerId>("request").RegisteredProducerId == registeredProducerId)
                .Returns(producerDetailsScheme);

            ProducersController controller = new ProducersController(
                A.Dummy<BreadcrumbService>(),
                A.Dummy<ISearcher<ProducerSearchResult>>(),
                () => weeeClient,
                A.Dummy<IWeeeCache>());

            controller.ModelState.AddModelError("SomeProperty", "Exception");

            // Act
            ActionResult result = await controller.ConfirmRemoval(registeredProducerId, A.Dummy<ConfirmRemovalViewModel>());

            // Assert
            ViewResult viewResult = result as ViewResult;
            Assert.NotNull(viewResult);

            ConfirmRemovalViewModel viewModel = viewResult.Model as ConfirmRemovalViewModel;
            Assert.NotNull(viewModel);

            Assert.Equal(producerDetailsScheme, viewModel.Producer);
        }
        public async Task PostConfirmRemoval_WithInvalidModel_ReturnsConfirmRemovalView()
        {
            // Arrange
            ProducersController controller = new ProducersController(
                A.Dummy<BreadcrumbService>(),
                A.Dummy<ISearcher<ProducerSearchResult>>(),
                () => A.Dummy<IWeeeClient>(),
                A.Dummy<IWeeeCache>());

            controller.ModelState.AddModelError("SomeProperty", "Exception");

            // Act
            ActionResult result = await controller.ConfirmRemoval(A.Dummy<Guid>(), A.Dummy<ConfirmRemovalViewModel>());

            // Assert
            ViewResult viewResult = result as ViewResult;
            Assert.NotNull(viewResult);

            Assert.True(string.IsNullOrEmpty(viewResult.ViewName) || viewResult.ViewName.ToLowerInvariant() == "confirmremoval");
        }
        public async void GetConfirmRemoval_ReturnsHttpForbiddenResult_WhenCanRemoveProducerIsFalse()
        {
            // Arrange
            IWeeeClient weeeClient = A.Fake<IWeeeClient>();

            ProducersController controller = new ProducersController(
                A.Dummy<BreadcrumbService>(),
                A.Dummy<ISearcher<ProducerSearchResult>>(),
                () => weeeClient,
                A.Dummy<IWeeeCache>());

            A.CallTo(() => weeeClient.SendAsync(A<string>._, A<GetProducerDetailsByRegisteredProducerId>._))
                .Returns(
                new ProducerDetailsScheme
                {
                    CanRemoveProducer = false
                });

            // Act
            var result = await controller.ConfirmRemoval(A.Dummy<Guid>());

            // Assert
            Assert.IsType<HttpForbiddenResult>(result);
        }
        public async void GetConfirmRemoval_ReturnsConfirmRemovalView_WhenCanRemoveProducerIsTrue()
        {
            // Arrange
            IWeeeClient weeeClient = A.Fake<IWeeeClient>();

            ProducersController controller = new ProducersController(
                A.Dummy<BreadcrumbService>(),
                A.Dummy<ISearcher<ProducerSearchResult>>(),
                () => weeeClient,
                A.Dummy<IWeeeCache>());

            A.CallTo(() => weeeClient.SendAsync(A<string>._, A<GetProducerDetailsByRegisteredProducerId>._))
                .Returns(
                new ProducerDetailsScheme
                {
                    CanRemoveProducer = true
                });

            // Act
            ActionResult result = await controller.ConfirmRemoval(A.Dummy<Guid>());

            // Assert
            ViewResult viewResult = result as ViewResult;
            Assert.NotNull(viewResult);

            Assert.True(string.IsNullOrEmpty(viewResult.ViewName) || viewResult.ViewName.ToLowerInvariant() == "confirmremoval");
        }
        public async Task FetchDetails_FetchesDetailsFromApiAndReturnsDetailsView()
        {
            // Arrange
            BreadcrumbService breadcrumb = A.Dummy<BreadcrumbService>();
            ISearcher<ProducerSearchResult> producerSearcher = A.Dummy<ISearcher<ProducerSearchResult>>();

            IWeeeClient weeeClient = A.Fake<IWeeeClient>();

            A.CallTo(() => weeeClient.SendAsync(A<string>._, A<GetProducerComplianceYear>._))
                .WhenArgumentsMatch(a => ((GetProducerComplianceYear)a[1]).RegistrationNumber == "WEE/AA1111AA")
                .Returns(new List<int> { 2018, 2017, 2016 });

            ProducerDetails producerDetails = A.Dummy<ProducerDetails>();

            A.CallTo(() => weeeClient.SendAsync(A<string>._, A<GetProducerDetails>._))
                .WhenArgumentsMatch(a => ((GetProducerDetails)a[1]).RegistrationNumber == "WEE/AA1111AA")
                .Returns(producerDetails);

            Func<IWeeeClient> weeeClientFunc = A.Fake<Func<IWeeeClient>>();
            A.CallTo(() => weeeClientFunc())
                .Returns(weeeClient);

            ProducersController controller = new ProducersController(breadcrumb, producerSearcher, weeeClientFunc, cache);

            // Act
            ActionResult result = await controller.FetchDetails("WEE/AA1111AA", 2015);

            // Assert
            A.CallTo(() => weeeClient.SendAsync(A<string>._, A<GetProducerDetails>._))
                .WhenArgumentsMatch(a => ((GetProducerDetails)a[1]).RegistrationNumber == "WEE/AA1111AA")
                .MustHaveHappened();

            PartialViewResult viewResult = result as PartialViewResult;
            Assert.NotNull(viewResult);

            Assert.True(string.IsNullOrEmpty(viewResult.ViewName) || viewResult.ViewName.ToLowerInvariant() == "_detailsresults");

            Assert.Equal(producerDetails, viewResult.Model);
        }
        public async Task GetDetails_FetchesComplianceYearsFromApiAndReturnsViewWithYearValues()
        {
            // Arrange
            BreadcrumbService breadcrumb = A.Dummy<BreadcrumbService>();
            ISearcher<ProducerSearchResult> producerSearcher = A.Dummy<ISearcher<ProducerSearchResult>>();

            IWeeeClient weeeClient = A.Fake<IWeeeClient>();
            A.CallTo(() => weeeClient.SendAsync(A<string>._, A<GetProducerComplianceYear>._))
                .WhenArgumentsMatch(a => ((GetProducerComplianceYear)a[1]).RegistrationNumber == "WEE/AA1111AA")
                .Returns(new List<int> { 2018, 2017, 2016 });

            Func<IWeeeClient> weeeClientFunc = A.Fake<Func<IWeeeClient>>();
            A.CallTo(() => weeeClientFunc())
                .Returns(weeeClient);

            ProducersController controller = new ProducersController(breadcrumb, producerSearcher, weeeClientFunc, cache);

            // Act
            ActionResult result = await controller.Details("WEE/AA1111AA");

            // Assert
            A.CallTo(() => weeeClient.SendAsync(A<string>._, A<GetProducerComplianceYear>._))
                .WhenArgumentsMatch(a => ((GetProducerComplianceYear)a[1]).RegistrationNumber == "WEE/AA1111AA")
                .MustHaveHappened();

            ViewResult viewResult = result as ViewResult;
            Assert.NotNull(viewResult);

            Assert.True(string.IsNullOrEmpty(viewResult.ViewName) || viewResult.ViewName.ToLowerInvariant() == "details");

            DetailsViewModel resultsViewModel = viewResult.Model as DetailsViewModel;
            Assert.NotNull(resultsViewModel);

            Assert.Equal(2018, resultsViewModel.SelectedYear);
            Assert.Collection(resultsViewModel.ComplianceYears,
                r1 => Assert.Equal("2018", r1.Text),
                r2 => Assert.Equal("2017", r2.Text),
                r3 => Assert.Equal("2016", r3.Text));
        }
        public async Task PostSearchResults_WithSelectedPRN_RedirectsToDetailsAction()
        {
            // Arrange
            BreadcrumbService breadcrumb = A.Dummy<BreadcrumbService>();
            ISearcher<ProducerSearchResult> producerSearcher = A.Dummy<ISearcher<ProducerSearchResult>>();

            Func<IWeeeClient> weeeClient = A.Dummy<Func<IWeeeClient>>();

            ProducersController controller = new ProducersController(breadcrumb, producerSearcher, weeeClient, cache);

            SearchResultsViewModel viewModel = new SearchResultsViewModel()
            {
                SelectedRegistrationNumber = "WEE/AA1111AA"
            };

            // Act
            ActionResult result = await controller.SearchResults(viewModel);

            // Assert
            RedirectToRouteResult redirectResult = result as RedirectToRouteResult;
            Assert.NotNull(redirectResult);

            Assert.Equal("Details", redirectResult.RouteValues["action"]);
            Assert.Equal("WEE/AA1111AA", redirectResult.RouteValues["RegistrationNumber"]);
        }
        public async Task PostSearchResults_WithInvalidModel_DoesSearchForTenResultsAndReturnsSearchReturnsView()
        {
            // Arrange
            BreadcrumbService breadcrumb = A.Dummy<BreadcrumbService>();

            List<ProducerSearchResult> fakeResults = new List<ProducerSearchResult>()
            {
                new ProducerSearchResult()
                {
                    Name = "Producer1",
                    RegistrationNumber = "WEE/AA1111AA"
                }
            };

            ISearcher<ProducerSearchResult> producerSearcher = A.Fake<ISearcher<ProducerSearchResult>>();
            A.CallTo(() => producerSearcher.Search("testSearchTerm", 10, false))
                .Returns(fakeResults);

            Func<IWeeeClient> weeeClient = A.Dummy<Func<IWeeeClient>>();

            ProducersController controller = new ProducersController(breadcrumb, producerSearcher, weeeClient, cache);

            SearchResultsViewModel viewModel = new SearchResultsViewModel();
            viewModel.SearchTerm = "testSearchTerm";
            controller.ModelState.AddModelError("SomeProperty", "Exception");

            // Act
            ActionResult result = await controller.SearchResults(viewModel);

            // Assert
            ViewResult viewResult = result as ViewResult;
            Assert.NotNull(viewResult);

            Assert.True(string.IsNullOrEmpty(viewResult.ViewName) || viewResult.ViewName.ToLowerInvariant() == "searchresults");

            SearchResultsViewModel resultsViewModel = viewResult.Model as SearchResultsViewModel;
            Assert.NotNull(resultsViewModel);

            Assert.Contains(resultsViewModel.Results, r => r.RegistrationNumber == "WEE/AA1111AA");
        }