Beispiel #1
0
        public async Task GetObservationAsync_ReturnsNotFound_WhenObservationIsNotFound(int id)
        {
            //Arrange
            var requestingUser = GetUser("Any");

            var mockUnitOfWork  = new Mock <IUnitOfWork>();
            var mockBirdRepo    = new Mock <IBirdRepository>();
            var mockUserManager = SharedFunctions.InitialiseMockUserManager();
            var mockObsRepo     = new Mock <IObservationRepository>();

            mockObsRepo.Setup(o => o.GetObservationAsync(It.IsAny <int>(), It.IsAny <bool>()))
            .Returns(Task.FromResult <Observation>(null));
            var mockObsPositionRepo = new Mock <IObservationPositionRepository>();
            var mockObsNotesRepo    = new Mock <IObservationNoteRepository>();

            var controller = new ObservationController(
                _mapper
                , _systemClock
                , mockUnitOfWork.Object
                , mockBirdRepo.Object
                , _logger.Object
                , mockUserManager.Object
                , mockObsRepo.Object
                , mockObsPositionRepo.Object
                , mockObsNotesRepo.Object);

            controller.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = SharedFunctions.GetTestClaimsPrincipal(requestingUser.UserName)
                }
            };

            // Act
            var result = await controller.GetObservationAsync(id);

            // Assert
            string expectedMessage = $"Observation with id '{id}' was not found.";

            var objectResult = Assert.IsType <ObjectResult>(result);

            Assert.Equal(StatusCodes.Status500InternalServerError, objectResult.StatusCode);
            var actual = Assert.IsType <string>(objectResult.Value);

            Assert.Equal($"Observation with id '{id}' was not found.", actual);
        }
Beispiel #2
0
        public async Task GetObservationAsync_ReturnsBadRequest_OnException(int id)
        {
            //Arrange
            var requestingUser = GetUser("Any");

            var mockUnitOfWork  = new Mock <IUnitOfWork>();
            var mockBirdRepo    = new Mock <IBirdRepository>();
            var mockUserManager = SharedFunctions.InitialiseMockUserManager();
            var mockObsRepo     = new Mock <IObservationRepository>();

            mockObsRepo.Setup(o => o.GetObservationAsync(It.IsAny <int>(), It.IsAny <bool>()))
            .ThrowsAsync(new InvalidOperationException());
            var mockObsPositionRepo = new Mock <IObservationPositionRepository>();
            var mockObsNotesRepo    = new Mock <IObservationNoteRepository>();

            var controller = new ObservationController(
                _mapper
                , _systemClock
                , mockUnitOfWork.Object
                , mockBirdRepo.Object
                , _logger.Object
                , mockUserManager.Object
                , mockObsRepo.Object
                , mockObsPositionRepo.Object
                , mockObsNotesRepo.Object);

            controller.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = SharedFunctions.GetTestClaimsPrincipal(requestingUser.UserName)
                }
            };

            // Act
            var result = await controller.GetObservationAsync(id);

            // Assert
            var objectResult = Assert.IsType <ObjectResult>(result);

            Assert.Equal(StatusCodes.Status500InternalServerError, objectResult.StatusCode);
            var actual = Assert.IsType <string>(objectResult.Value);

            Assert.Equal("an unexpected error occurred", actual);
        }
Beispiel #3
0
        public async Task GetObservationAsync_ReturnsOkWithObservation_OnSuccessfulRequest(int id)
        {
            //Arrange
            var requestingUser = GetUser("Any");

            var mockUnitOfWork  = new Mock <IUnitOfWork>();
            var mockBirdRepo    = new Mock <IBirdRepository>();
            var mockUserManager = SharedFunctions.InitialiseMockUserManager();
            var mockObsRepo     = new Mock <IObservationRepository>();

            mockObsRepo.Setup(o => o.GetObservationAsync(It.IsAny <int>(), It.IsAny <bool>()))
            .ReturnsAsync(GetTestObservation(id, requestingUser));
            var mockObsPositionRepo = new Mock <IObservationPositionRepository>();
            var mockObsNotesRepo    = new Mock <IObservationNoteRepository>();

            var controller = new ObservationController(
                _mapper
                , _systemClock
                , mockUnitOfWork.Object
                , mockBirdRepo.Object
                , _logger.Object
                , mockUserManager.Object
                , mockObsRepo.Object
                , mockObsPositionRepo.Object
                , mockObsNotesRepo.Object);

            controller.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = SharedFunctions.GetTestClaimsPrincipal(requestingUser.UserName)
                }
            };

            // Act
            var result = await controller.GetObservationAsync(id);

            // Assert
            var objectResult = Assert.IsType <OkObjectResult>(result);
            var actualObs    = Assert.IsType <ObservationDto>(objectResult.Value);

            Assert.Equal(id, actualObs.ObservationId);
            Assert.Equal(requestingUser.UserName, actualObs.User.UserName);
        }