public void Post_ThrowsException_WhenHobbitAlreadyExists()
        {
            long someId;
            Mock<IDataManager<Hobbit>> mockHobbitManager = new Mock<IDataManager<Hobbit>>();
            mockHobbitManager.Setup(m => m.TryGet(_testHobbit, out someId)).Returns(true);

            HobbitController controller = new HobbitController(mockHobbitManager.Object);

            Assert.Throws<HttpResponseException>(() => controller.Post(_testHobbit));
        }
        public void Get_ThrowsException_ForUnknownId()
        {
            Hobbit nullHobbit = null;
            Mock<IDataManager<Hobbit>> mockHobbitManager = new Mock<IDataManager<Hobbit>>();
            mockHobbitManager.Setup(m => m.TryGet(ID, out nullHobbit)).Returns(false);

            HobbitController controller = new HobbitController(mockHobbitManager.Object);

            Assert.Throws<HttpResponseException>(() => controller.Get(ID));
        }
        public void Get_ReturnsSingleHobbit()
        {
            Mock<IDataManager<Hobbit>> mockHobbitManager = new Mock<IDataManager<Hobbit>>();
            mockHobbitManager.Setup(m => m.TryGet(ID, out _testHobbit)).Returns(true);

            HobbitController controller = new HobbitController(mockHobbitManager.Object);

            Hobbit hobbit = controller.Get(ID);

            Assert.Equal(_testHobbit.Name, hobbit.Name);
        }
        public void Post_ReturnsHobbit()
        {
            long someId;
            Mock<IDataManager<Hobbit>> mockHobbitManager = new Mock<IDataManager<Hobbit>>();
            mockHobbitManager.Setup(m => m.TryGet(_testHobbit, out someId)).Returns(false);
            mockHobbitManager.Setup(m => m.Insert(_testHobbit)).Returns(_testHobbit);

            HobbitController controller = new HobbitController(mockHobbitManager.Object);

            Hobbit hobbit = controller.Post(_testHobbit);

            Assert.Equal(_testHobbit.Name, hobbit.Name);
        }
        public void Get_ReturnsAllHobbits()
        {
            Hobbit[] _singleHobbitArray = new Hobbit[] { _testHobbit };

            Mock<IDataManager<Hobbit>> mockHobbitManager = new Mock<IDataManager<Hobbit>>();
            mockHobbitManager.Setup(m => m.GetAll()).Returns(_singleHobbitArray);

            HobbitController controller = new HobbitController(mockHobbitManager.Object);

            IEnumerable<Hobbit> hobbit = controller.Get();

            Assert.Equal(_singleHobbitArray.Count(), hobbit.Count());
            Assert.Equal(NAME, hobbit.First().Name);
        }
        public void Post_ThrowsException_WhenInsertFails()
        {
            long someId;
            Mock<IDataManager<Hobbit>> mockHobbitManager = new Mock<IDataManager<Hobbit>>();
            mockHobbitManager.Setup(m => m.TryGet(_testHobbit, out someId)).Returns(false);
            mockHobbitManager.Setup(m => m.Insert(_testHobbit));    // returns null

            HobbitController controller = new HobbitController(mockHobbitManager.Object);

            Assert.Throws<HttpResponseException>(() => controller.Post(_testHobbit));
        }
        public void Delete_ThrowsException_WhenDeleteFails()
        {
            Hobbit someHobbit;
            Mock<IDataManager<Hobbit>> mockHobbitManager = new Mock<IDataManager<Hobbit>>();
            mockHobbitManager.Setup(m => m.TryGet(ID, out someHobbit)).Returns(true);
            mockHobbitManager.Setup(m => m.Delete(ID)).Returns(false);

            HobbitController controller = new HobbitController(mockHobbitManager.Object);

            Assert.Throws<HttpResponseException>(() => controller.Delete(ID));
        }
        public void Delete_ThrowsException_WhenHobbitDoesNotExist()
        {
            long someId;
            Mock<IDataManager<Hobbit>> mockHobbitManager = new Mock<IDataManager<Hobbit>>();
            mockHobbitManager.Setup(m => m.TryGet(_testHobbit, out someId)).Returns(false);

            HobbitController controller = new HobbitController(mockHobbitManager.Object);

            Assert.Throws<HttpResponseException>(() => controller.Delete(ID));
        }
        public void Delete_CompletesSuccessfully()
        {
            Hobbit someHobbit;
            Mock<IDataManager<Hobbit>> mockHobbitManager = new Mock<IDataManager<Hobbit>>();
            mockHobbitManager.Setup(m => m.TryGet(ID, out someHobbit)).Returns(true);
            mockHobbitManager.Setup(m => m.Delete(ID)).Returns(true);

            HobbitController controller = new HobbitController(mockHobbitManager.Object);

            controller.Delete(ID);

            mockHobbitManager.Verify(m => m.Delete(ID), Times.Once());
        }
        public void Put_ThrowsExceptio_WhenExistingHobbitIdMismatchesIdProvided()
        {
            long someId = 1;
            Mock<IDataManager<Hobbit>> mockHobbitManager = new Mock<IDataManager<Hobbit>>();
            mockHobbitManager.Setup(m => m.TryGet(_testHobbit, out someId)).Returns(false);

            HobbitController controller = new HobbitController(mockHobbitManager.Object);

            Assert.Throws<HttpResponseException>(() => controller.Post(_testHobbit));
        }
        public void Put_ReturnsHobbit()
        {
            long someId = ID;
            Mock<IDataManager<Hobbit>> mockHobbitManager = new Mock<IDataManager<Hobbit>>();
            mockHobbitManager.Setup(m => m.TryGet(_testHobbit, out someId)).Returns(true);
            mockHobbitManager.Setup(m => m.Update(_testHobbit, ID)).Returns(_testHobbit);

            HobbitController controller = new HobbitController(mockHobbitManager.Object);

            Hobbit hobbit = controller.Put(ID, _testHobbit);

            Assert.Equal(_testHobbit.Name, hobbit.Name);
        }