Exemplo n.º 1
0
        public async void GetTrustAcademies_TrustRepoFailure()
        {
            //Arrange
            var trustId = Guid.Parse("a16e9020-9123-4420-8055-851d1b672fa9");

            //Set up trusts repo to return a failure
            var trustsRepoMock = new Mock <ITrustsRepository>();

            trustsRepoMock.Setup(m => m.GetTrustById(trustId))
            .ReturnsAsync(new RepositoryResult <GetTrustsD365Model>
            {
                Error = new RepositoryResultBase.RepositoryError
                {
                    StatusCode   = HttpStatusCode.BadRequest,
                    ErrorMessage = "Bad Request"
                }
            });

            var mapperMock = new Mock <IMapper <GetTrustsD365Model, GetTrustsModel> >();

            //Set up error handler to return a random result
            var errorHandlerMock = new Mock <IRepositoryErrorResultHandler>();

            errorHandlerMock.Setup(r => r.LogAndCreateResponse(It.IsAny <RepositoryResultBase>()))
            .Returns(new ObjectResult("Some error message")
            {
                StatusCode = 499
            });

            var trustsController = new TrustsController(trustsRepoMock.Object,
                                                        _academiesRepository.Object,
                                                        mapperMock.Object,
                                                        _getAcademiesMapper.Object,
                                                        errorHandlerMock.Object);


            //Execute
            var result = await trustsController.GetTrustAcademies(trustId);

            var castedResult = (ObjectResult)result.Result;

            //Assert

            //Error handler should be called with result from repository
            errorHandlerMock.Verify(h => h.LogAndCreateResponse(
                                        It.Is <RepositoryResultBase>(r =>
                                                                     r.Error.ErrorMessage == "Bad Request" &&
                                                                     r.Error.StatusCode == HttpStatusCode.BadRequest)),
                                    Times.Once);
            //Mapper not to be called when repo fails
            mapperMock.Verify(m => m.Map(It.IsAny <GetTrustsD365Model>()), Times.Never);

            //Final result should be what the error handler returns
            Assert.Equal("Some error message", castedResult.Value);
            Assert.Equal(499, castedResult.StatusCode);
        }
Exemplo n.º 2
0
        public async void SearchTrusts_InvalidRepoResult()
        {
            var searchTerm = "Some search term";

            //Set up mock repository to return a failed D365 call
            var trustsRepoMock = new Mock <ITrustsRepository>();

            trustsRepoMock.Setup(m => m.SearchTrusts(searchTerm))
            .ReturnsAsync(new RepositoryResult <List <GetTrustsD365Model> >
            {
                Error = new RepositoryResultBase.RepositoryError
                {
                    StatusCode   = HttpStatusCode.BadRequest,
                    ErrorMessage = "Bad Request"
                }
            });

            var mapperMock = new Mock <IMapper <GetTrustsD365Model, GetTrustsModel> >();

            //Set up error handler to return a random result
            var errorHandlerMock = new Mock <IRepositoryErrorResultHandler>();

            errorHandlerMock.Setup(r => r.LogAndCreateResponse(It.IsAny <RepositoryResultBase>()))
            .Returns(new ObjectResult("Some error message")
            {
                StatusCode = 499
            });

            var trustsController = new TrustsController(trustsRepoMock.Object,
                                                        _academiesRepository.Object,
                                                        mapperMock.Object,
                                                        _getAcademiesMapper.Object,
                                                        errorHandlerMock.Object);

            //Execute
            var result = await trustsController.SearchTrusts(searchTerm);

            var castedResult = (ObjectResult)result.Result;

            //Assert

            //Error handler should be called with repository result
            errorHandlerMock.Verify(h => h.LogAndCreateResponse(
                                        It.Is <RepositoryResultBase>(r =>
                                                                     r.Error.ErrorMessage == "Bad Request" &&
                                                                     r.Error.StatusCode == HttpStatusCode.BadRequest)),
                                    Times.Once);
            //Mapper not to be called when repo fails
            mapperMock.Verify(m => m.Map(It.IsAny <GetTrustsD365Model>()), Times.Never);

            //Final result should be what the error handler returns
            Assert.Equal("Some error message", castedResult.Value);
            Assert.Equal(499, castedResult.StatusCode);
        }
Exemplo n.º 3
0
        public async void GetById_TrustFound_Returns200OkResult()
        {
            //Arrange
            var trustId = Guid.Parse("a16e9020-9123-4420-8055-851d1b672fa9");

            //Set up mock repository to return a valid and set result
            var trustsRepoMock = new Mock <ITrustsRepository>();

            trustsRepoMock.Setup(m => m.GetTrustById(trustId))
            .ReturnsAsync(new RepositoryResult <GetTrustsD365Model>
            {
                Result = new GetTrustsD365Model
                {
                    Id = trustId
                }
            });

            var mapperMock = new Mock <IMapper <GetTrustsD365Model, GetTrustsModel> >();

            //Set up mapper to return a slim result when called with the expected input
            mapperMock.Setup(m => m.Map(It.Is <GetTrustsD365Model>(p => p.Id == trustId)))
            .Returns(new GetTrustsModel
            {
                Id      = trustId,
                Address = "Some address"
            });

            var errorHandlerMock = new Mock <IRepositoryErrorResultHandler>();

            var trustsController = new TrustsController(trustsRepoMock.Object,
                                                        _academiesRepository.Object,
                                                        mapperMock.Object,
                                                        _getAcademiesMapper.Object,
                                                        errorHandlerMock.Object);


            //Execute
            var result = await trustsController.GetById(trustId);

            var castedResult = (ObjectResult)result.Result;

            //Assert

            //Error handler should not be called when repo returns valid result
            errorHandlerMock.Verify(h => h.LogAndCreateResponse(It.IsAny <RepositoryResultBase>()), Times.Never);
            //Mapper to be called once with the expected input
            mapperMock.Verify(m => m.Map(It.Is <GetTrustsD365Model>(p => p.Id == trustId)), Times.Once);

            //Final result should be a 200 OK with the result of the mapping operation
            Assert.Equal(trustId, ((GetTrustsModel)castedResult.Value).Id);
            Assert.Equal("Some address", ((GetTrustsModel)castedResult.Value).Address);
            Assert.Equal(200, castedResult.StatusCode);
        }
Exemplo n.º 4
0
        public async void GetTrustAcademies_TrustFound_NoAcademiesFound()
        {
            //Arrange
            var trustId = Guid.Parse("a16e9020-9123-4420-8055-851d1b672fa9");

            //Set up mock trust repository to return a valid and set result
            var trustsRepoMock = new Mock <ITrustsRepository>();

            trustsRepoMock.Setup(m => m.GetTrustById(trustId))
            .ReturnsAsync(new RepositoryResult <GetTrustsD365Model>
            {
                Result = new GetTrustsD365Model
                {
                    Id = trustId
                }
            });

            //Set up mock academies repo to return a failed result
            var academiesRepoMock = new Mock <IAcademiesRepository>();

            academiesRepoMock.Setup(m => m.GetAcademiesByTrustId(trustId))
            .ReturnsAsync(new RepositoryResult <List <GetAcademiesD365Model> >
            {
                Result = null
            });

            var getAcademiesMapper = new Mock <IMapper <GetAcademiesD365Model, GetAcademiesModel> >();
            var errorHandlerMock   = new Mock <IRepositoryErrorResultHandler>();

            var trustsController = new TrustsController(trustsRepoMock.Object,
                                                        academiesRepoMock.Object,
                                                        _getTrustMapper.Object,
                                                        getAcademiesMapper.Object,
                                                        errorHandlerMock.Object);


            //Execute
            var result = await trustsController.GetTrustAcademies(trustId);

            var castedResult = (ObjectResult)result.Result;

            //Assert

            //Error handler should be called when a repository fails
            errorHandlerMock.Verify(h => h.LogAndCreateResponse(It.IsAny <RepositoryResultBase>()), Times.Never);

            //GetAcademiesMapper should not be called when no academies are found
            getAcademiesMapper.Verify(m => m.Map(It.IsAny <GetAcademiesD365Model>()), Times.Never);
            //Final result should be a 200 wrapped around a null list
            Assert.Null((List <GetAcademiesModel>)castedResult.Value);
            Assert.Equal(200, castedResult.StatusCode);
        }
Exemplo n.º 5
0
        public async void GetById_NotFound_ReturnsNotFoundResult()
        {
            //Arrange
            var trustId = Guid.Parse("a16e9020-9123-4420-8055-851d1b672fa9");

            //Set up mock repository to return a null result (not found)
            var trustsRepoMock = new Mock <ITrustsRepository>();

            trustsRepoMock.Setup(m => m.GetTrustById(trustId))
            .ReturnsAsync(new RepositoryResult <GetTrustsD365Model>
            {
                Result = null
            });

            var mapperMock       = new Mock <IMapper <GetTrustsD365Model, GetTrustsModel> >();
            var errorHandlerMock = new Mock <IRepositoryErrorResultHandler>();

            var trustsController = new TrustsController(trustsRepoMock.Object,
                                                        _academiesRepository.Object,
                                                        mapperMock.Object,
                                                        _getAcademiesMapper.Object,
                                                        errorHandlerMock.Object);


            //Execute
            var result = await trustsController.GetById(trustId);

            var castedResult = (ObjectResult)result.Result;

            //Assert

            //Error handler should not be called when repo returns valid result
            errorHandlerMock.Verify(h => h.LogAndCreateResponse(It.IsAny <RepositoryResultBase>()), Times.Never);
            //Mapper not to be called when repo returns null but valid result
            mapperMock.Verify(m => m.Map(It.IsAny <GetTrustsD365Model>()), Times.Never);

            //Final result should be a 404 - Not Found with the correct message
            Assert.Equal("The trust with the id: 'a16e9020-9123-4420-8055-851d1b672fa9' was not found", castedResult.Value);
            Assert.Equal(404, castedResult.StatusCode);
        }
Exemplo n.º 6
0
        public async void SearchTrusts_EmptyButValidSearchResult()
        {
            var searchTerm = "Some search term";

            //Set up mock repository to return a failed D365 call
            var trustsRepoMock = new Mock <ITrustsRepository>();

            trustsRepoMock.Setup(m => m.SearchTrusts(searchTerm))
            .ReturnsAsync(new RepositoryResult <List <GetTrustsD365Model> >
            {
                Result = null
            });

            var mapperMock       = new Mock <IMapper <GetTrustsD365Model, GetTrustsModel> >();
            var errorHandlerMock = new Mock <IRepositoryErrorResultHandler>();

            var trustsController = new TrustsController(trustsRepoMock.Object,
                                                        _academiesRepository.Object,
                                                        mapperMock.Object,
                                                        _getAcademiesMapper.Object,
                                                        errorHandlerMock.Object);

            //Execute
            var result = await trustsController.SearchTrusts(searchTerm);

            var castedResult = (ObjectResult)result.Result;

            //Assert

            //Error handler should not be called when repo returns valid result
            errorHandlerMock.Verify(h => h.LogAndCreateResponse(It.IsAny <RepositoryResultBase>()), Times.Never);
            //Mapper not to be called becasue there are no results
            mapperMock.Verify(m => m.Map(It.IsAny <GetTrustsD365Model>()), Times.Never);

            //Final result should be a 200 OK wrapped around a null list
            Assert.Null((List <GetTrustsModel>)castedResult.Value);
            Assert.Equal(200, castedResult.StatusCode);
        }
Exemplo n.º 7
0
        public async void GetTrustAcademies_TrustFound_FailedAcademyRepo()
        {
            //Arrange
            var trustId = Guid.Parse("a16e9020-9123-4420-8055-851d1b672fa9");

            //Set up mock trust repository to return a valid and set result
            var trustsRepoMock = new Mock <ITrustsRepository>();

            trustsRepoMock.Setup(m => m.GetTrustById(trustId))
            .ReturnsAsync(new RepositoryResult <GetTrustsD365Model>
            {
                Result = new GetTrustsD365Model
                {
                    Id = trustId
                }
            });

            //Set up mock academies repo to return a failed result
            var academiesRepoMock = new Mock <IAcademiesRepository>();

            academiesRepoMock.Setup(m => m.GetAcademiesByTrustId(trustId))
            .ReturnsAsync(new RepositoryResult <List <GetAcademiesD365Model> >
            {
                Error = new RepositoryResultBase.RepositoryError
                {
                    StatusCode   = HttpStatusCode.BadRequest,
                    ErrorMessage = "Bad Request"
                }
            });

            var mapperMock = new Mock <IMapper <GetTrustsD365Model, GetTrustsModel> >();

            //Set up mapper to return a slim result when called with the expected input
            mapperMock.Setup(m => m.Map(It.Is <GetTrustsD365Model>(p => p.Id == trustId)))
            .Returns(new GetTrustsModel
            {
                Id      = trustId,
                Address = "Some address"
            });

            //Set up error handler to return a random result
            var errorHandlerMock = new Mock <IRepositoryErrorResultHandler>();

            errorHandlerMock.Setup(r => r.LogAndCreateResponse(It.IsAny <RepositoryResultBase>()))
            .Returns(new ObjectResult("Some error message")
            {
                StatusCode = 499
            });

            var trustsController = new TrustsController(trustsRepoMock.Object,
                                                        academiesRepoMock.Object,
                                                        mapperMock.Object,
                                                        _getAcademiesMapper.Object,
                                                        errorHandlerMock.Object);


            //Execute
            var result = await trustsController.GetTrustAcademies(trustId);

            var castedResult = (ObjectResult)result.Result;

            //Assert

            //Error handler should be called when a repository fails
            errorHandlerMock.Verify(h => h.LogAndCreateResponse(It.IsAny <RepositoryResultBase>()), Times.Once);
            //Final result should be what the error handler returns
            Assert.Equal("Some error message", castedResult.Value);
            Assert.Equal(499, castedResult.StatusCode);
        }
Exemplo n.º 8
0
        public async void SearchTrusts_ThreeResults()
        {
            var searchTerm = "Some search term";

            //Set up mock repository to return a failed D365 call
            var trustsRepoMock = new Mock <ITrustsRepository>();

            trustsRepoMock.Setup(m => m.SearchTrusts(searchTerm))
            .ReturnsAsync(new RepositoryResult <List <GetTrustsD365Model> >
            {
                Result = new List <GetTrustsD365Model>
                {
                    new GetTrustsD365Model {
                        TrustName = "Trust 001"
                    },
                    new GetTrustsD365Model {
                        TrustName = "Trust 002"
                    },
                    new GetTrustsD365Model {
                        TrustName = "Trust 003"
                    }
                }
            });

            //Set up mock mapper to return slim models
            var mapperMock = new Mock <IMapper <GetTrustsD365Model, GetTrustsModel> >();

            mapperMock.Setup(m => m.Map(It.Is <GetTrustsD365Model>(t => t.TrustName == "Trust 001")))
            .Returns(new GetTrustsModel {
                TrustName = "Mapped Trust 001"
            });

            mapperMock.Setup(m => m.Map(It.Is <GetTrustsD365Model>(t => t.TrustName == "Trust 002")))
            .Returns(new GetTrustsModel {
                TrustName = "Mapped Trust 002"
            });

            mapperMock.Setup(m => m.Map(It.Is <GetTrustsD365Model>(t => t.TrustName == "Trust 003")))
            .Returns(new GetTrustsModel {
                TrustName = "Mapped Trust 003"
            });

            var errorHandlerMock = new Mock <IRepositoryErrorResultHandler>();

            var trustsController = new TrustsController(trustsRepoMock.Object,
                                                        _academiesRepository.Object,
                                                        mapperMock.Object,
                                                        _getAcademiesMapper.Object,
                                                        errorHandlerMock.Object);

            //Execute
            var result = await trustsController.SearchTrusts(searchTerm);

            var castedResult = (ObjectResult)result.Result;

            //Assert

            //Error handler should not be called when repo returns valid result
            errorHandlerMock.Verify(h => h.LogAndCreateResponse(It.IsAny <RepositoryResultBase>()), Times.Never);
            //Mapper to be called three times, once for each result
            mapperMock.Verify(m => m.Map(It.IsAny <GetTrustsD365Model>()), Times.Exactly(3));

            //Final result should be a 200OK wrapped around a a list with three results
            Assert.Equal(3, ((List <GetTrustsModel>)castedResult.Value).Count);
            Assert.Equal("Mapped Trust 001", ((List <GetTrustsModel>)castedResult.Value)[0].TrustName);
            Assert.Equal("Mapped Trust 002", ((List <GetTrustsModel>)castedResult.Value)[1].TrustName);
            Assert.Equal("Mapped Trust 003", ((List <GetTrustsModel>)castedResult.Value)[2].TrustName);
            Assert.Equal(200, castedResult.StatusCode);
        }