// PUT: api/Hobbit/5
        public Hobbit Put(long id, Hobbit hobbit)
        {
            long hobbitId;
            if (!_hobbitDataManager.TryGet(hobbit, out hobbitId))
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("The Hobbit provided does not already exist.")
                });
            }

            if(hobbitId != id)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("The Hobbit provided already exists as another Hobbit.")
                });
            }

            Hobbit updatedHobbit = _hobbitDataManager.Update(hobbit, id);

            if (updatedHobbit == null)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("The body did not contain a valid Hobbit data type.")
                });
            }

            return updatedHobbit;
        }
        public void TryGet_ReturnsFalse_ForUnknownHobbit()
        {
            Hobbit testHobbit = new Hobbit("TryGet_ReturnsFalse hobbit", FAMILY_NAME, BIRTH_YEAR, DEATH_YEAR, ID);

            HobbitDataManager dataManager = new HobbitDataManager();

            long hobbitId;
            bool foundHobbit = dataManager.TryGet(testHobbit, out hobbitId);

            Assert.False(foundHobbit);
        }
        public void Insert_ReturnsHobbit()
        {
            Hobbit testHobbit = new Hobbit("Insert hobbit", FAMILY_NAME, BIRTH_YEAR, DEATH_YEAR, ID);

            HobbitDataManager dataManager = new HobbitDataManager();

            Hobbit hobbit = dataManager.Insert(testHobbit);

            Assert.NotNull(hobbit);
            Assert.True(CompareHobbits(testHobbit, hobbit));
        }
        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 TryGet_ReturnsTrue_ForKnownHobbit()
        {
            Hobbit testHobbit = new Hobbit("TryGet_ReturnsTrue hobbit", FAMILY_NAME, BIRTH_YEAR, DEATH_YEAR, ID);

            HobbitDataManager dataManager = new HobbitDataManager();

            Hobbit insertedHobbit = dataManager.Insert(testHobbit);

            long hobbitId;
            bool foundHobbit = dataManager.TryGet(insertedHobbit, out hobbitId);

            Assert.True(foundHobbit);
            Assert.True(hobbitId != default(long));  // SQLite ids/rownums start at 1
        }
        public void Insert_ReturnsHobbit_WhenUsingDefaultValues(
            string name,
            string familyName,
            int birthYear,
            int deathYear,
            long id)
        {
            Hobbit testHobbit = new Hobbit("Insert " + name, familyName, birthYear, deathYear, id);

            HobbitDataManager dataManager = new HobbitDataManager();

            Hobbit hobbit = dataManager.Insert(testHobbit);

            Assert.NotNull(hobbit);
            Assert.True(CompareHobbits(testHobbit, hobbit));
        }
        // POST: api/Hobbit
        public Hobbit Post(Hobbit hobbit)
        {
            long hobbitId;
            if (_hobbitDataManager.TryGet(hobbit, out hobbitId))
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("The Hobbit provided already exists.")
                });
            }

            Hobbit insertedHobbit = _hobbitDataManager.Insert(hobbit);

            if (insertedHobbit == null)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("The body did not contain a valid Hobbit data type.")
                });
            }

            return insertedHobbit;
        }
        public void Insert_ReturnsNull_WhenNameIsNull()
        {
            Hobbit testHobbit = new Hobbit(null, FAMILY_NAME, BIRTH_YEAR, DEATH_YEAR, ID);

            HobbitDataManager dataManager = new HobbitDataManager();

            Hobbit hobbit = dataManager.Insert(testHobbit);

            Assert.Null(hobbit);
        }
        private bool CompareHobbits(Hobbit baselineHobbit, Hobbit comparisonHobbit, bool compareId = false)
        {
            if (baselineHobbit.Name != comparisonHobbit.Name)
            {
                return false;
            }

            if (baselineHobbit.FamilyName != comparisonHobbit.FamilyName)
            {
                return false;
            }

            if (baselineHobbit.BirthYear != comparisonHobbit.BirthYear)
            {
                return false;
            }

            if (baselineHobbit.DeathYear != comparisonHobbit.DeathYear)
            {
                return false;
            }

            if (compareId && baselineHobbit.Id != comparisonHobbit.Id)
            {
                return false;
            }

            return true;
        }
        public void Delete_ReturnsTrue_ForKnownHobbit()
        {
            Hobbit testHobbit = new Hobbit("Deletable Hobbit", FAMILY_NAME, BIRTH_YEAR, DEATH_YEAR, ID);

            HobbitDataManager dataManager = new HobbitDataManager();

            Hobbit insertedHobbit = dataManager.Insert(testHobbit);
            bool deleted = dataManager.Delete(insertedHobbit.Id);
            Hobbit hobbit;
            bool foundHobbit = dataManager.TryGet(insertedHobbit.Id, out hobbit);

            Assert.True(deleted);
            Assert.False(foundHobbit);
        }
        public void Update_ReturnsHobbit_WhenUsingDefaultValues(
            string name,
            string familyName,
            int birthYear,
            int deathYear,
            long id)
        {
            Hobbit testHobbit = new Hobbit("Update " + name, familyName, birthYear, deathYear, id);

            HobbitDataManager dataManager = new HobbitDataManager();
            Hobbit insertedHobbit = dataManager.Insert(testHobbit);

            Hobbit updatedHobbit = new Hobbit(
                           "Updated and updated " + insertedHobbit.Name,
                           insertedHobbit.FamilyName,
                           insertedHobbit.BirthYear,
                           insertedHobbit.DeathYear,
                           insertedHobbit.Id);
            Hobbit hobbit = dataManager.Update(updatedHobbit, updatedHobbit.Id);

            Assert.NotNull(hobbit);
            Assert.True(CompareHobbits(updatedHobbit, hobbit));
        }
        public void Update_ReturnsUpdatedHobbit()
        {
            Hobbit testHobbit = new Hobbit("Insert hobbit", FAMILY_NAME, BIRTH_YEAR, DEATH_YEAR, ID);

            HobbitDataManager dataManager = new HobbitDataManager();
            Hobbit insertedHobbit = dataManager.Insert(testHobbit);

            Hobbit updatedHobbit = new Hobbit(
                "Inserted and updated" + insertedHobbit.Name,
                insertedHobbit.FamilyName,
                insertedHobbit.BirthYear,
                insertedHobbit.DeathYear,
                insertedHobbit.Id);
            Hobbit hobbit = dataManager.Update(updatedHobbit, updatedHobbit.Id);

            Assert.True(CompareHobbits(updatedHobbit, hobbit));
        }
示例#13
0
        public void DeathYear_StoresProvidedValue(int year)
        {
            Hobbit hobbit = new Hobbit(null, null, 0, year, 0);

            Assert.Equal(year, hobbit.DeathYear);
        }
示例#14
0
        public void FamilyName_StoresProvidedValue(string name)
        {
            Hobbit hobbit = new Hobbit(name, null, 0, 0, 0);

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