Example #1
0
        public void Get_WhenCalled_ReturnsOkResult()
        {
            //Test the Get Method
            var okResult = _controller.Get();

            //Make sure it returns a positive result
            Assert.IsType <OkObjectResult>(okResult.Result);
        }
        public async Task GetPeoplePaginated()
        {
            var databaseName = Guid.NewGuid().ToString();
            var context      = BuildContext(databaseName);
            var mapper       = BuildMap();

            context.People.Add(new Person()
            {
                Name = "Person 1"
            });
            context.People.Add(new Person()
            {
                Name = "Person 2"
            });
            context.People.Add(new Person()
            {
                Name = "Person 3"
            });
            context.SaveChanges();

            var context2 = BuildContext(databaseName);

            var controller = new PeopleController(context2, mapper, null);

            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            var responsePage1 = await controller.Get(new PaginationDTO()
            {
                Page = 1, RecordsPerPage = 2
            });

            var peoplePage1 = responsePage1.Value;

            Assert.AreEqual(2, peoplePage1.Count);

            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            var responsePage2 = await controller.Get(new PaginationDTO()
            {
                Page = 2, RecordsPerPage = 2
            });

            var peoplePage2 = responsePage2.Value;

            Assert.AreEqual(1, peoplePage2.Count);

            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            var responsePage3 = await controller.Get(new PaginationDTO()
            {
                Page = 3, RecordsPerPage = 2
            });

            var peoplePage3 = responsePage3.Value;

            Assert.AreEqual(0, peoplePage3.Count);
        }
Example #3
0
        public void GetNoParmsGetsAllPeople()
        {
            var expected = new List <Person>();

            _peopleRepositoryMock.Setup(p => p.GetAll()).Returns(expected);

            var actual = _controller.Get();

            Assert.That(actual, Is.SameAs(expected));
        }
Example #4
0
        public void GetReturnsListOfPeople()
        {
            //arrange
            moq.Setup(m => m.Get()).Returns(Task.FromResult(new List <Person>() as IEnumerable <Person>));

            //Act
            var result = controller.Get().Result;

            //Assert
            Assert.IsInstanceOfType(result, typeof(IEnumerable <Person>));
        }
Example #5
0
        public async Task GetPeoplePaginated()
        {
            var databaseName = Guid.NewGuid().ToString();
            var context      = BuildContext(databaseName);
            var mapper       = BuildMap();

            context.People.Add(new Person()
            {
                Name = "Person 1"
            });
            context.People.Add(new Person()
            {
                Name = "Person 2"
            });
            context.People.Add(new Person()
            {
                Name = "Person 3"
            });
            context.SaveChanges();

            var context2 = BuildContext(databaseName);

            var controller = new PeopleController(context2, mapper, null);

            //because we simple instanceate the conntroller we set a default context
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            var responsePage1 = await controller.Get(new PaginationDto { Page = 1, RecordsPerPage = 2 });

            var result1     = responsePage1.Result as OkObjectResult;
            var peoplePage1 = result1.Value as List <PersonDto>;

            Assert.AreEqual(2, peoplePage1.Count);

            //we add this again because if we have the same controller instance we try to add the same header
            //as a result we reseting this
            controller.ControllerContext.HttpContext = new DefaultHttpContext();
            var responsePage2 = await controller.Get(new PaginationDto { Page = 2, RecordsPerPage = 2 });

            var result2     = responsePage2.Result as OkObjectResult;
            var peoplePage2 = result2.Value as List <PersonDto>;

            Assert.AreEqual(1, peoplePage2.Count);

            controller.ControllerContext.HttpContext = new DefaultHttpContext();
            var responsePage3 = await controller.Get(new PaginationDto { Page = 3, RecordsPerPage = 2 });

            var result3     = responsePage3.Result as OkObjectResult;
            var peoplePage3 = result3.Value as List <PersonDto>;

            Assert.AreEqual(0, peoplePage3.Count);
        }
        public async Task Get_ReturnsPerson()
        {
            var id     = Guid.NewGuid();
            var person = PersonFaker.Get().Generate();

            getPersonUseCase.Setup(x => x.Execute(id))
            .ReturnsAsync(person.MapToResponse());

            var response = await instance.Get(id);

            response.Result.Should().BeOfType <OkObjectResult>()
            .Which.Value.Should().BeEquivalentTo(person.MapToViewModel());
        }
        public async Task PeopleControllerGet_WithoutParams_Should_ReturnAllPeople()
        {
            var expected = await _mockRepo.GetPersonAll(_cts.Token);

            var result = await _controller.Get(_cts.Token);

            var objResult = (ObjectResult)result;
            var response  = (SuccessResult)objResult.Value;

            var actual = response.Results;

            Assert.AreEqual(objResult.StatusCode, 200);
            Assert.IsTrue(response.Status == "Successful");
            Assert.AreEqual(expected.Length, actual.Length);
        }
Example #8
0
        public async Task Get_ReturnsAOkObjectResult_WithAListOfPets(string petType, int noOfPetswithMale, int noOfPetswithFemale)
        {
            // Arrange
            var mockService = new Mock <IPeopleService>();

            mockService.Setup(service => service.RetrievePeopleListAsync(petType))
            .ReturnsAsync(GetPetsInAlphabetOrderGroupByWithGender(petType));
            var controller = new PeopleController(null, mockService.Object);

            // Act
            var result = await controller.Get(petType);

            // Assert
            var viewResult = Assert.IsType <OkObjectResult>(result);
            var model      = Assert.IsAssignableFrom <IEnumerable <PeopleDTO> >(viewResult.Value);

            // Result must have 2 items each one for Male/Female
            Assert.Equal(2, model.Count());

            // Result should match the pet count for Male
            var petCountWithMale = model.Where(c => c.Gender.Equals("male", StringComparison.OrdinalIgnoreCase)).FirstOrDefault()?.Pets.Count;

            Assert.Equal(noOfPetswithMale, petCountWithMale);

            // Result should match the pet count for Female
            var petCountWithFemale = model.Where(c => c.Gender.Equals("female", StringComparison.OrdinalIgnoreCase)).FirstOrDefault()?.Pets.Count;

            Assert.Equal(noOfPetswithFemale, petCountWithFemale);
        }
        public void TestingWithoutSearchClause()
        {
            var starWarsProvider = new Moq.Mock <IStarWarsProvider>();

            starWarsProvider.SetupAllProperties();
            var expectedList = new []
            {
                new Person()
                {
                    Name = "Ziutek", SkinColor = "black"
                }
            }.AsQueryable();

            starWarsProvider.Setup(element => element.GetPeople("Ziu")).Returns(expectedList);

            var httpContextAccessor = new Moq.Mock <IHttpContextAccessor>();

            httpContextAccessor.SetupAllProperties();
            httpContextAccessor.SetupGet(element => element.HttpContext.Request.Query).Returns(new QueryCollection(
                                                                                                   new Dictionary <string, StringValues>()
            {
                { "search", "Ziu" }
            }));

            var testee = new PeopleController(starWarsProvider.Object, httpContextAccessor.Object);
            var result = testee.Get();

            Assert.AreEqual(result.Count(), expectedList.Count());
        }
        public async void Task_Get_WhenCalled_ReturnsOkResult()
        {
            // Act
            var okResult = await _controller.Get();

            // Assert
            Assert.IsType <OkObjectResult>(okResult.Result);
        }
Example #11
0
        public async Task TestGetReturnsListOfPeople()
        {
            using (var context = await InitializeDatabase())
            {
                var controller = new PeopleController(context);
                var result     = controller.Get();

                Assert.Contains <Person>(result, p => p.FirstName == "Manuel" && p.LastName == "Riezebosch");
            }
        }
Example #12
0
        public async Task Get_given_non_existing_returns_NotFound()
        {
            var repo       = new Mock <IPersonRepository>();
            var controller = new PeopleController(repo.Object);

            var get = await controller.Get(42);

            var res = get.Result;

            Assert.IsType <NotFoundResult>(res);
        }
Example #13
0
        public void Get_PersonFound_ReturnsPerson()
        {
            // Arrange
            var foundPerson = new Person
            {
                Id        = 65,
                Name      = "Homer",
                LastName  = "Simpson",
                BirthDate = new DateTime(1976, 12, 24)
            };

            _repositoryMock.Setup(r => r.GetPerson(65)).Returns(foundPerson);

            // Act
            var result = _controller.Get(65) as OkObjectResult;

            // Assert
            Assert.AreEqual(200, result.StatusCode);
            Assert.AreEqual(foundPerson, result.Value);
        }
Example #14
0
        public async Task Get_existing_id_returns_dto()
        {
            var dto  = new PersonDTO();
            var repo = new Mock <IPersonRepository>();

            repo.Setup(s => s.FindAsync(42)).ReturnsAsync(dto);
            var controller = new PeopleController(repo.Object);

            var get = await controller.Get(42);

            Assert.Equal(dto, get.Value);
        }
Example #15
0
        public async Task GetPersons_returns_all()
        {
            IList <PersonDTO> dtos = new List <PersonDTO>()
            {
                new PersonDTO()
            };
            var repo = new Mock <IPersonRepository>();

            repo.Setup(s => s.ReadAsync()).ReturnsAsync(dtos);
            var controller = new PeopleController(repo.Object);

            var get = await controller.Get();
        }
        public void findByProviderTest()
        {
            // Arrange
            var testPeopleRepository = new TestPeopleRepository();
            PeopleController testPeopleController = new PeopleController(testPeopleRepository);
            var expected = typeof(JsonResult);

            // Act
            var actual = testPeopleController.Get("Provder Name");

            // Assert
            Assert.IsType(expected, actual);
        }
        public void DefaultTest()
        {
            // Arrange
            var testPeopleRepository = new TestPeopleRepository();
            PeopleController testPeopleController = new PeopleController(testPeopleRepository);
            var expected = typeof(OkObjectResult);

            // Act
            var actual = testPeopleController.Get();

            // Assert
            Assert.IsType(expected, actual);
        }
        public void findByAddressTest()
        {
            // Arrange
            var testPeopleRepository = new TestPeopleRepository();
            PeopleController testPeopleController = new PeopleController(testPeopleRepository);
            var expected = typeof(JsonResult);

            // Act
            var actual = testPeopleController.Get("336 FAKE ST UNIT 114 HENRY'S HOUSE");

            // Assert
            Assert.IsType(expected, actual);
        }
        /// <summary>
        /// Simulate HTTP Get operation using actual controller returning actual results
        /// </summary>
        /// <param name="guid">GUID of image to Get</param>
        /// <returns>FileContentsResult</returns>
        private ActionResult <Person[]> DoHttpGet()
        {
            using (PeopleSearchContext context = new PeopleSearchContext(PeopleDbOptions))
            {
                PeopleController controller = new PeopleController(context);

                // TEST GET

                ActionResult <Person[]> results = null;
                Task.Run(async() => results = await controller.Get()).Wait();
                return(results);
            }
        }
Example #20
0
        public void PeopleTest()
        {
            var mockPeopleRepository = new MockPeopleRepository();

            var controller = new PeopleController(mockPeopleRepository);

            var result = controller.Get() as OkObjectResult;

            var response = (List <Person>)result.Value;

            Assert.Equal(result.StatusCode, 200);

            Assert.Equal(response[0].FirstName, "Unit");
        }
Example #21
0
        public async Task Get_InternalServerError(string petType)
        {
            // Arrange
            var mockLogger  = new Mock <ILogger <PeopleController> >();
            var mockService = new Mock <IPeopleService>();

            mockService.Setup(service => service.RetrievePeopleListAsync(petType)).Throws(new Exception(errorMessage));

            var controller = new PeopleController(mockLogger.Object, mockService.Object);

            // Act
            var ex = await Assert.ThrowsAsync <Exception>(() => controller.Get(petType));

            // Assert
            Assert.Equal(errorMessage, ex.Message);
        }
Example #22
0
        public void GetPeople()
        {
            var repo = new Mock <IRepository <Person> >();
            // Arrange
            PeopleRepository rep        = new PeopleRepository();
            PeopleController controller = new PeopleController(rep.Repo);

            // Act
            IEnumerable <PersonDTO> result = controller.Get();

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(7, result.Count());
            Assert.AreEqual("Escobar Gaviria", result.ElementAt(2).LastName);
            Assert.AreEqual("White", result.ElementAt(3).LastName);
        }
Example #23
0
        public void Get_Returns_NotFound()
        {
            // Arrange
            var mockRepo = new Mock <IPersonRepository>();
            var personId = 100;

            mockRepo.Setup(repo => repo.Get(personId))
            .Returns(null as Person);

            var controller = new PeopleController(mockRepo.Object);

            // Act
            var result = controller.Get(personId);

            // Assert
            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
        }
Example #24
0
        public async Task Get_ReturnsBadRequestResult_WhenPetTypeIsEmptyOrNull(string petType)
        {
            // Arrange
            var mockService = new Mock <IPeopleService>();

            mockService.Setup(service => service.RetrievePeopleListAsync(petType))
            .ReturnsAsync(GetPetsInAlphabetOrderGroupByWithGender(petType));
            var controller = new PeopleController(null, mockService.Object);

            // Act
            var result = await controller.Get(petType);

            // Assert
            var badRequestResult = Assert.IsType <BadRequestObjectResult>(result);

            Assert.Equal(badRequestResult.StatusCode, StatusCodes.Status400BadRequest);
        }
Example #25
0
        public void Get_Returns_Ok()
        {
            // Arrange
            var mockRepo = new Mock <IPersonRepository>();
            var personId = 1;

            mockRepo.Setup(repo => repo.Get(personId))
            .Returns(GetTestPeople().FirstOrDefault(p => p.Id == personId));

            var controller = new PeopleController(mockRepo.Object);

            // Act
            var result = controller.Get(personId);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
        }
Example #26
0
        public void Get_Calls_Repository()
        {
            // Arrange
            var mockRepo = new Mock <IPersonRepository>();
            var personId = 1;

            mockRepo.Setup(repo => repo.Get(personId))
            .Returns(GetTestPeople().FirstOrDefault(p => p.Id == personId));

            var controller = new PeopleController(mockRepo.Object);

            // Act
            controller.Get(personId);

            // Assert
            mockRepo.Verify(mock => mock.Get(personId), Times.Once());
        }
        public async Task TestGet()
        {
            const int id = 1;

            repositoryContext = new Mock <IPersonRepository>();
            repositoryContext.Setup(mock => mock.FindAsync(It.IsAny <int>())).Returns(Task.Run(() => new Person
            {
                Id = id
            }));
            subject = new PeopleController(repositoryContext.Object);

            var result = await subject.Get(1) as OkObjectResult;

            Assert.IsNotNull(result?.Value);
            var person = result?.Value as Person;

            Assert.IsNotNull(person);
            Assert.AreEqual(id, person?.Id);
        }
Example #28
0
        public void GetPeopleById()
        {
            var repo = new Mock <IRepository <Person> >();
            // Arrange
            PeopleRepository rep        = new PeopleRepository();
            PeopleController controller = new PeopleController(rep.Repo);

            controller.Request       = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            // Act
            IHttpActionResult result = controller.Get("*****@*****.**");
            var contentResult        = result as OkNegotiatedContentResult <PersonExtendedDTO>;

            // Assert
            Assert.IsNotNull(contentResult);
            Assert.IsNotNull(contentResult.Content);
            Assert.AreEqual("Albuquerque", contentResult.Content.City);
        }
        public async Task GetPeoplePaginated_ShouldReturnPaginatedResult()
        {
            // Arrange
            var databaseName = Guid.NewGuid().ToString();

            BuildPeopleTestDatabase(databaseName);

            var testContext = BuildContext(databaseName);
            var mapper      = BuildMapper();

            var httpContext1 = BuildHttpContext("GET");
            var controller1  = new PeopleController(testContext, mapper, null);

            controller1.ControllerContext.HttpContext = httpContext1;

            var httpContext2 = BuildHttpContext("GET");
            var controller2  = new PeopleController(testContext, mapper, null);

            controller2.ControllerContext.HttpContext = httpContext2;

            // Act
            var responsePage1 = await controller1.Get(new PaginationDTO()
            {
                Page = 1, RecordsPerPage = 2
            });

            var peoplePage1 = responsePage1.Value;

            var responsePage2 = await controller2.Get(new PaginationDTO()
            {
                Page = 2, RecordsPerPage = 2
            });

            var peoplePage2 = responsePage2.Value;

            // Assert
            Assert.AreEqual(2, peoplePage1.Count);
            Assert.AreEqual(2, Int32.Parse(controller1.Response.Headers["page-count"]));
            Assert.AreEqual(1, peoplePage2.Count);
        }
Example #30
0
        public void Get_Returns_Person()
        {
            // Arrange
            var mockRepo   = new Mock <IPersonRepository>();
            var personId   = 1;
            var testPerson = GetTestPeople().FirstOrDefault(p => p.Id == personId);

            mockRepo.Setup(repo => repo.Get(personId))
            .Returns(testPerson);

            var controller = new PeopleController(mockRepo.Object);

            // Act
            var result       = controller.Get(personId);
            var objectResult = result as OkObjectResult;
            var value        = objectResult?.Value as Person;

            // Assert
            Assert.IsInstanceOfType(objectResult?.Value, typeof(Person));
            Assert.AreEqual(testPerson?.Id, value?.Id);
            Assert.AreEqual(testPerson?.FirstName, value?.FirstName);
            Assert.AreEqual(testPerson?.LastName, value?.LastName);
        }