Exemple #1
0
        public AnimalController()
        {
            var context         = new AnimalHouseDbContext();
            var kennelProcessor = new KennelProcessor(context);

            _animalProcessor = new AnimalProcessor(context, kennelProcessor);
        }
Exemple #2
0
        public async Task RestructureFailsByOvercapacityAsyncTest()
        {
            var kennelProcessor = new KennelProcessor(_mockContext.Object);
            var animalProcessor = new AnimalProcessor(_mockContext.Object, kennelProcessor);

            //set capacity for each kennel to 1, which should force the business logic to fail
            _kennelData.ForEach(k => k.maxLimit = 1);

            var wasRestructureSuccess = await animalProcessor.ReorganizeAnimalsToAppropriateKennelsAsync();

            Assert.IsFalse(wasRestructureSuccess);
        }
Exemple #3
0
        public async Task GetAnimalsInEachKennelAsyncTest()
        {
            var kennelProcessor = new KennelProcessor(_mockContext.Object);
            var report          = await kennelProcessor.GetAnimalsInEachKennelAsync();

            var smallKennelReport  = report.Where(r => r.name == "small").FirstOrDefault();
            var mediumKennelReport = report.Where(r => r.name == "medium").FirstOrDefault();
            var largeKennelReport  = report.Where(r => r.name == "large").FirstOrDefault();

            Assert.IsTrue(smallKennelReport.animals.Count == 2);
            Assert.IsTrue(mediumKennelReport.animals.Count == 1);
            Assert.IsTrue(largeKennelReport.animals.Count == 2);
        }
Exemple #4
0
        public async Task RemoveAnimalByNameAndTypeAndSizeAsyncTest()
        {
            var kennelProcessor = new KennelProcessor(_mockContext.Object);
            var animalProcessor = new AnimalProcessor(_mockContext.Object, kennelProcessor);

            var wasAnimalRemoved = await animalProcessor.RemoveAnimalByNameAndTypeAndSizeAsync("Fido", "Dog", 12);

            Assert.IsTrue(wasAnimalRemoved);

            var shouldBeNull = _animalData.Where(a => a.animalId == 1).FirstOrDefault();

            Assert.IsTrue(shouldBeNull == null);
        }
Exemple #5
0
        public async Task NoAnimalLeftBehindAsyncTest()
        {
            var kennelProcessor = new KennelProcessor(_mockContext.Object);
            var animalProcessor = new AnimalProcessor(_mockContext.Object, kennelProcessor);

            //remove medium and large kennels, then try to restucture
            _kennelData.Remove(_kennelData.Where(k => k.kennelId == 2).First());
            _kennelData.Remove(_kennelData.Where(k => k.kennelId == 3).First());

            var wasRestructureSuccess = await animalProcessor.ReorganizeAnimalsToAppropriateKennelsAsync();

            Assert.IsFalse(wasRestructureSuccess);
        }
        public async Task ReorganizeAnimalsTest()
        {
            var kennelProcessor  = new KennelProcessor(_mockContext.Object);
            var animalProcessor  = new AnimalProcessor(_mockContext.Object, kennelProcessor);
            var animalController = new AnimalController(animalProcessor);

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

            var response = await animalController.ReorganizeAnimalsAsync();

            Assert.IsTrue(response.StatusCode == System.Net.HttpStatusCode.OK);
        }
Exemple #7
0
        public async Task AddAnimalToShelterAsyncTest()
        {
            var kennelProcessor = new KennelProcessor(_mockContext.Object);
            var animalProcessor = new AnimalProcessor(_mockContext.Object, kennelProcessor);

            var animalToAdd = new Animal()
            {
                name = "Bingo", type = "Dog", sizeInLbs = 26
            };

            var wasAminalAdded = await animalProcessor.AddAnimalToShelterAsync(animalToAdd);

            Assert.IsTrue(wasAminalAdded);
        }
        public async Task GetKennelReportTestAsync()
        {
            var kennelProcessor  = new KennelProcessor(_mockContext.Object);
            var kennelController = new KennelController(kennelProcessor);

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

            var response = await kennelController.ReportAsync();

            List <KennelAnimals> kennels;

            Assert.IsTrue(response.TryGetContentValue(out kennels));
            Assert.AreEqual(3, kennels.Count);
        }
        public async Task RemoveAnimalByIdTest()
        {
            var kennelProcessor  = new KennelProcessor(_mockContext.Object);
            var animalProcessor  = new AnimalProcessor(_mockContext.Object, kennelProcessor);
            var animalController = new AnimalController(animalProcessor);

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

            var goodResponse = await animalController.RemoveByIdAsync(1);

            Assert.IsTrue(goodResponse.StatusCode == System.Net.HttpStatusCode.OK);

            var badResponse = await animalController.RemoveByIdAsync(367); //does not exist

            Assert.IsFalse(badResponse.IsSuccessStatusCode);
        }
Exemple #10
0
        public async Task RejectAnimalIfKennelFullAsyncTest()
        {
            var kennelProcessor = new KennelProcessor(_mockContext.Object);
            var animalProcessor = new AnimalProcessor(_mockContext.Object, kennelProcessor);

            _animalData.Add(new Animal {
                kennelId = 2
            });
            _animalData.Add(new Animal {
                kennelId = 2
            });
            _animalData.Add(new Animal {
                kennelId = 2
            });
            _animalData.Add(new Animal {
                kennelId = 2
            });
            _animalData.Add(new Animal {
                kennelId = 2
            });
            _animalData.Add(new Animal {
                kennelId = 2
            });
            _animalData.Add(new Animal {
                kennelId = 2
            });
            _animalData.Add(new Animal {
                kennelId = 2
            });
            _animalData.Add(new Animal {
                kennelId = 2
            });
            _animalData.Add(new Animal {
                kennelId = 2
            });

            var animalToAdd = new Animal()
            {
                name = "Bingo", type = "Dog", sizeInLbs = 26
            };

            var wasAminalAdded = await animalProcessor.AddAnimalToShelterAsync(animalToAdd);

            Assert.IsFalse(wasAminalAdded);
        }
        public async Task AddAnimalTest()
        {
            var kennelProcessor  = new KennelProcessor(_mockContext.Object);
            var animalProcessor  = new AnimalProcessor(_mockContext.Object, kennelProcessor);
            var animalController = new AnimalController(animalProcessor);

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

            var animalToAdd = new AnimalModel {
                name = "fluffy", type = "dog", sizeInLbs = 32.1
            };

            var goodResponse = await animalController.AddAsync(animalToAdd);

            Assert.IsTrue(goodResponse.StatusCode == System.Net.HttpStatusCode.OK);

            var badResponse = await animalController.AddAsync(null);

            Assert.IsFalse(badResponse.IsSuccessStatusCode);
        }
Exemple #12
0
        public async Task RestructureAsyncTest()
        {
            var kennelProcessor = new KennelProcessor(_mockContext.Object);
            var animalProcessor = new AnimalProcessor(_mockContext.Object, kennelProcessor);

            //test that the count of animals in small kennel is 2 before the restructure
            var smallAnimalsBeforeRestructure = await animalProcessor.GetAnimalsInKennelAsync(1);

            //reorganizing the kennel sizes
            //increase small size definition to be less than than 30 lbs
            //increase medium size definition to be greater than 30 lbs
            _kennelData.Where(k => k.name == "small").ToList().ForEach(k => k.maxAminalSize  = 30);
            _kennelData.Where(k => k.name == "medium").ToList().ForEach(k => k.minAnimalSize = 30);

            var wasRestructureSuccess = await animalProcessor.ReorganizeAnimalsToAppropriateKennelsAsync();

            var smallAnimalsAfterRestructure = await animalProcessor.GetAnimalsInKennelAsync(1);

            Assert.IsTrue(wasRestructureSuccess);
            Assert.IsTrue(smallAnimalsBeforeRestructure.Count == 2);
            Assert.IsTrue(smallAnimalsAfterRestructure.Count == 3);
        }
Exemple #13
0
 public KennelController(KennelProcessor kennelProcessor)
 {
     _kennelProcessor = kennelProcessor;
 }
Exemple #14
0
 public KennelController()
 {
     _kennelProcessor = new KennelProcessor(new AnimalHouseDbContext());
 }
Exemple #15
0
        public async Task GetCorrectKennelSizeForAnimalAsync()
        {
            var kennelData = new List <Kennel>()
            {
                new Kennel {
                    kennelId = 1, name = "small", maxLimit = 16, minAnimalSize = 0, maxAminalSize = 20
                },
                new Kennel {
                    kennelId = 2, name = "medium", maxLimit = 10, minAnimalSize = 20, maxAminalSize = 50
                },
                new Kennel {
                    kennelId = 3, name = "large", maxLimit = 8, minAnimalSize = 50, maxAminalSize = int.MaxValue
                }
            }.AsQueryable();


            var mockSet = new Mock <DbSet <Kennel> >();

            mockSet.As <IDbAsyncEnumerable <Kennel> >()
            .Setup(k => k.GetAsyncEnumerator())
            .Returns(new TestDbAsyncEnumerator <Kennel>(kennelData.GetEnumerator()));

            mockSet.As <IQueryable <Kennel> >()
            .Setup(k => k.Provider)
            .Returns(new TestDbAsyncQueryProvider <Kennel>(kennelData.Provider));

            mockSet.As <IQueryable <Kennel> >().Setup(k => k.Expression).Returns(kennelData.Expression);
            mockSet.As <IQueryable <Kennel> >().Setup(k => k.ElementType).Returns(kennelData.ElementType);
            mockSet.As <IQueryable <Kennel> >().Setup(k => k.GetEnumerator()).Returns(kennelData.GetEnumerator());


            var mockContext = new Mock <AnimalHouseDbContext>();

            mockContext.Setup(k => k.Kennels).Returns(mockSet.Object);

            var kennelProcessor = new KennelProcessor(mockContext.Object);

            var smallKennel = await kennelProcessor.GetKennelByAnimalSizeAsync(12);

            var mediumKennel = await kennelProcessor.GetKennelByAnimalSizeAsync(35);

            var largeKennel = await kennelProcessor.GetKennelByAnimalSizeAsync(64);

            Assert.IsTrue(smallKennel.name == "small");
            Assert.IsTrue(mediumKennel.name == "medium");
            Assert.IsTrue(largeKennel.name == "large");

            //edge cases
            smallKennel = await kennelProcessor.GetKennelByAnimalSizeAsync(.001);

            Assert.IsTrue(smallKennel.name == "small");
            smallKennel = await kennelProcessor.GetKennelByAnimalSizeAsync(20);

            Assert.IsTrue(smallKennel.name == "small");

            mediumKennel = await kennelProcessor.GetKennelByAnimalSizeAsync(20.01);

            Assert.IsTrue(mediumKennel.name == "medium");
            mediumKennel = await kennelProcessor.GetKennelByAnimalSizeAsync(50);

            Assert.IsTrue(mediumKennel.name == "medium");

            largeKennel = await kennelProcessor.GetKennelByAnimalSizeAsync(50.01);

            Assert.IsTrue(largeKennel.name == "large");
            largeKennel = await kennelProcessor.GetKennelByAnimalSizeAsync(99999);

            Assert.IsTrue(largeKennel.name == "large");
        }