public void InitializeService()
        {
            #region mock repository & categoryRepository

            Note expectedNote = new Note { NoteId = 1, Title = "Test" };
            IEnumerable<Category> expectedListOfCategories = new List<Category>();

            this.repository = new Mock<IWebNoteRepository>();
            this.repository.Setup(r => r.GetNote(expectedNote.NoteId)).Returns(expectedNote);

            this.categoryRepository = new Mock<IWebNoteCategoryRepository>();
            this.categoryRepository.Setup(s => s.GetCategories(expectedNote.NoteId)).Returns(expectedListOfCategories);

            #endregion

            this.sut = new WebNoteService
                {
                    WebNoteRepository = this.repository.Object,
                    WebNoteCategoryRepository = this.categoryRepository.Object
                };

            #region clean HttpRuntime.Cache

            IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator();
            while (enumerator.MoveNext())
            {
                HttpRuntime.Cache.Remove(enumerator.Key.ToString());
            }
            #endregion
        }
        /// <summary>
        /// Adds a new note to the DB.
        /// </summary>
        /// <param name="noteToAdd">The note to add.</param>
        public int AddNote(Note noteToAdd)
        {
            noteToAdd.Added = DateTime.Now;
            this.Context.Notes.AddObject(noteToAdd);
            this.Context.SaveChanges();

            return noteToAdd.NoteId;
        }
 public NoteWithCategories(Note note, IEnumerable<Category> categories)
 {
     this.NoteId = note.NoteId;
     this.Title = note.Title;
     this.Message = note.Message;
     this.Added = note.Added;
     this.Categories = categories;
 }
        public void CanAcceptCorrectData()
        {
            // Arrange
            Note wrongNote = new Note { Title = "Test", Message = "Test" };

            // Act
            TestDelegate createAction = () => this.webNoteService.Create(wrongNote, null);

            // Assert
            Assert.DoesNotThrow(createAction);
        }
        public void CanThrowDataNotValidException()
        {
            // Arrange
            Note wrongNote = new Note();

            // Act
            TestDelegate createAction = () => this.webNoteService.Create(wrongNote, null);

            // Assert
            Assert.Throws<DataNotValidException>(createAction, "+++ Do you really added the aspect to the service? +++");
        }
        /// <summary>
        /// Edits one note.
        /// </summary>
        /// <param name="noteData">note data.</param>
        public void EditNote(Note noteData)
        {
            Note noteToEdit = this.GetNote(noteData.NoteId);

            if (noteToEdit == null)
            {
                return;
            }

            noteToEdit.Title = noteData.Title;
            noteToEdit.Message = noteData.Message;
            this.Context.SaveChanges();
        }
        public void EditNoteTest()
        {
            // Arrange
            Note noteToChange = new Note { NoteId = 2, Title = "Micky", Message = "Maus" };

            // Act
            Repository.EditNote(noteToChange);
            Note changedNote = (from n in this.Context.Notes
                                where n.NoteId == noteToChange.NoteId
                                select n).First();

            // Assert
            Assert.That(changedNote.Title, Is.EquivalentTo(noteToChange.Title));
            Assert.That(changedNote.Message, Is.EquivalentTo(noteToChange.Message));
            Assert.That(this.Context.SavesChanged);
        }
        public void CanRestoreDefaultBehaviour()
        {
            // Arrange
            Note expected = new Note
            {
                NoteId = 4,
                Added = new DateTime(2011, 2, 5)
            };
            NewDateTime.DateTime = DateTime.MinValue;

            // Act
            Repository.AddNote(new Note());
            Note actual = (from n in Context.Notes select n).Last();

            // Assert
            Assert.AreNotEqual(expected, actual);
        }
        public void CanOverrideDateTimeNow()
        {
            // Arrange
            Note expected = new Note
                {
                    NoteId = 4,
                    Added = new DateTime(2011, 2, 5)
                };
            NewDateTime.DateTime = new DateTime(2011, 2, 5);

            // Act
            ////UNDONE: DateTimeNowOverrideAspectTest --> AddNote will use a overridden time
            Repository.AddNote(new Note());
            Note actual = (from n in Context.Notes select n).Last();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Esempio n. 10
0
        public ActionResult Create(Note noteToAdd, int[] newCategories)
        {
            #region using the validator
            //// UNDONE: ValidatorIntroduceAspect - using the interface
            #if false
            if (!this.ServiceAsValidator.IsValid(noteToAdd))
            {
                IValidationResults result = this.ServiceAsValidator.Validate(noteToAdd);
                this.ReplaceModelState(result);
                return View();
            }

            #endif
            #endregion

            this.WebNoteService.Create(noteToAdd, newCategories);

            return RedirectToAction("Index");
        }
Esempio n. 11
0
        public void UpdateWillRemoveItemFromCache()
        {
            // Arrange
            Note noteToChance = new Note { NoteId = 1, Title = "New", Message = "New" };

            // Act - this item will be cached
            this.webNoteService.Read(1);
            var cacheActual = this.cache.GetFirstItemFromCache();

            // Assert - the item should be in the cache now
            Assert.That(cacheActual, Is.Not.Null, "+++ Do you really added the aspect to the service? +++");

            /* --- */

            // Act - Update should force a remove
            this.webNoteService.Update(noteToChance, null);
            var cacheActual2 = this.cache.GetFirstItemFromCache();

            // Assert - is null when item was removed
            Assert.IsNull(cacheActual2);
        }
Esempio n. 12
0
        public void InitializeService()
        {
            #region mock repository & categoryRepository

            Note expectedNote = new Note { NoteId = 1, Title = "Test" };
            IEnumerable<Category> expectedListOfCategories = new List<Category>();

            this.repository = new Mock<IWebNoteRepository>();
            this.repository.Setup(r => r.GetNote(expectedNote.NoteId)).Returns(expectedNote);

            this.categoryRepository = new Mock<IWebNoteCategoryRepository>();
            this.categoryRepository.Setup(s => s.GetCategories(expectedNote.NoteId)).Returns(expectedListOfCategories);

            #endregion

            this.webNoteService = new WebNoteService
                {
                    WebNoteRepository = this.repository.Object,
                    WebNoteCategoryRepository = this.categoryRepository.Object
                };

            this.cache = new Cache("x");
            this.cache.CleanCompleteCache();
        }
Esempio n. 13
0
        /// <summary>
        /// Determines whether the specified <see cref="Note"/> is equal to this instance.
        /// </summary>
        /// <param name="other">The <see cref="Note"/> to compare with this instance.</param>
        /// <returns>
        ///     <c>true</c> if the specified <see cref="Note"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public bool Equals(Note other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }

            if (ReferenceEquals(this, other))
            {
                return true;
            }

            if (other.NoteId != this.NoteId)
            {
                return false;
            }

            if (other.Title != this.Title)
            {
                return false;
            }

            if (other.Message != this.Message)
            {
                return false;
            }

            if (other.Added != this.Added)
            {
                return false;
            }

            return true;
        }
Esempio n. 14
0
 /// <summary>
 /// Creates a new note and adds relations to the categories
 /// </summary>
 public void Create(Note noteToAdd, int[] newCategories)
 {
     int newNoteId = this.WebNoteRepository.AddNote(noteToAdd);
     this.WebNoteCategoryRepository.UpdateRelation(newNoteId, newCategories);
 }
Esempio n. 15
0
        public NoteWithCategories Update(Note noteToEdit, int[] newCategories)
        {
            if (noteToEdit == null ||
                string.IsNullOrEmpty(noteToEdit.Title) ||
                noteToEdit.NoteId == 0)
            {
                throw new Exception("Wrong data!"); // ugly code!
            }

            this.WebNoteRepository.EditNote(noteToEdit);
            this.WebNoteCategoryRepository.UpdateRelation(noteToEdit.NoteId, newCategories);

            return this.Read(noteToEdit.NoteId);
        }
        public void GetNoteShouldNotCallSaveChangesTest()
        {
            // Arrange
            var expected = new Note
                {
                    NoteId = 3,
                    Title = "Unit",
                    Message = "Test",
                    Added = DateTime.Parse("2010-10-29", CultureInfo.InvariantCulture)
                };

            // Act
            var actual = Repository.GetNote(expected.NoteId);

            // Assert
            Assert.That(actual, Is.EqualTo(expected));
            Assert.That(!this.Context.SavesChanged);
        }
Esempio n. 17
0
        public ActionResult Edit(Note noteToEdit, int[] newCategories)
        {
            #region using the validator
            //// UNDONE: ValidatorIntroduceAspect - using the interface
            #if false

            if (!this.ServiceAsValidator.IsValid(noteToEdit))
            {
                IValidationResults result = this.ServiceAsValidator.Validate(noteToEdit);
                this.ReplaceModelState(result);
                return this.Edit(noteToEdit.NoteId);
            }

            #endif
            #endregion

            NoteWithCategories changedNote = this.WebNoteService.Update(noteToEdit, newCategories);
            return View(changedNote);
        }
        public void UpdateWillRemoveItemFromCache()
        {
            // Arrange
            const string Prefix = "WebNoteAOP.Models.WebNoteService_";
            Note noteToChance = new Note { NoteId = 1, Title = "Neu" };

            // Act - this item will be cached
            this.sut.Read(1);
            var cacheActual = HttpRuntime.Cache[Prefix + 1];

            // Assert - the item should be in the cache now
            Assert.That(cacheActual, Is.Not.Null);

            /* --- */

            // Act - Update should force a remove
            this.sut.Update(noteToChance, null);
            var cacheActual2 = HttpRuntime.Cache[Prefix + 1];

            // Assert - is null when item was removed
            Assert.IsNull(cacheActual2);
        }