public static Story GetSpecificStoryWithText(string userId, List<string> paragraphs)
        {
            Story story = new Story
            {
                EditHistory = GetGenericEditHistory(userId),
                Paragraphs = paragraphs
            };

            return story;
        }
        public void HasEditor()
        {
            Story story = new Story();
            story.Lock.UserId = "blahs";

            Assert.IsTrue(story.HasEditor);

            story = new Story();

            Assert.IsFalse(story.HasEditor);
        }
        public AddWordResult AddWord(string storyId, string word, string userId, bool addParagraph = false)
        {
            if (string.IsNullOrEmpty(word)) throw new ArgumentNullException("word");
            if (string.IsNullOrEmpty(userId)) throw new ArgumentNullException("userId");

            var story = new Story();

            if (string.IsNullOrEmpty(storyId))
            {
                CreateNewStory(word, userId, story);
                return new AddWordResult() { ErrorCode = StoryErrorCode.Success, Story = story };

            }
            else
            {
                var result = AddToExistingStory(word, userId, storyId, addParagraph);
                return result;
            }
        }
        public static Story GetGenericStory(string userId)
        {
            if (string.IsNullOrEmpty(userId))
            {
                userId = "users/" + _random.Next(9999).ToString();
            }

            Story story = new Story
            {
                EditHistory = GetGenericEditHistory(userId),
                Paragraphs = new List<string>
                  {
                      Guid.NewGuid().ToString(),
                      Guid.NewGuid().ToString(),
                      Guid.NewGuid().ToString(),
                      Guid.NewGuid().ToString(),
                      Guid.NewGuid().ToString()
                  }
            };

            return story;
        }
Beispiel #5
0
 public AddWordResult()
 {
     Story = new Story();
 }
 private static void SaveStory(Story story, IDocumentStore store)
 {
     using (var session = store.OpenSession())
     {
         session.Store(story);
         session.SaveChanges();
     }
 }
        public void StoryObjectPropertiesAreInitialized()
        {
            // Setup
            Story story = new Story();

            // Assert
            Assert.IsNotNull(story.Paragraphs);
            Assert.IsNotNull(story.LastEditorId);
            Assert.IsNotNull(story.EditHistory);
        }
        public void LockStoryReturnsTrueIfStoryIsNotLocked()
        {
            Story story = new Story();

            IDocumentStore store = Global.GetInMemoryStore();

            using (var session = store.OpenSession())
            {
                session.Store(story);
                session.SaveChanges();
            }

            StoryRepository repository = new StoryRepository(store);

            // Act
            StoryErrorCode result = repository.LockStory(story.Id, "users/1");

            using (var session = store.OpenSession())
            {
                var savedStory = session.Load<Story>(story.Id);
                Assert.AreEqual(savedStory.Lock.UserId, "users/1");
                Assert.GreaterOrEqual(savedStory.Lock.LockedDate, DateTime.Now.AddMinutes(-1));
            }

            // Assert
            Assert.AreEqual(result, StoryErrorCode.Success);
        }
        public void LockStoryReturnsFalseIfStoryIsAlreadyLocked()
        {
            // Setup
            Story story = new Story();
            story.Lock.UserId = "users/1";

            IDocumentStore store = Global.GetInMemoryStore();

            using (var session = store.OpenSession())
            {
                session.Store(story);
                session.SaveChanges();
            }

            StoryRepository repository = new StoryRepository(store);

            // Act
            StoryErrorCode result = repository.LockStory(story.Id, "users/2");

            // Assert
            Assert.AreEqual(result, StoryErrorCode.StoryLockedForEditing);
        }
        public void GetStoryByIdReturnsStory()
        {
            // Setup
            Story story = new Story()
            {

                EditHistory = new List<EditHistory>() {
                    new EditHistory() {
                        DateAdded = DateTime.Now.AddHours(-1),
                        ParagraphIndex = 45,
                        ParagraphNumber = 2,
                         UserId = "users/2"
                    },
                    new EditHistory() {
                        DateAdded = DateTime.Now.AddHours(-2),
                        ParagraphIndex = 25,
                        ParagraphNumber = 6,
                         UserId = "users/5"
                    },
                    new EditHistory() {
                        DateAdded = DateTime.Now.AddHours(-8),
                        ParagraphIndex = 25,
                        ParagraphNumber = 4,
                         UserId = "users/8"
                    }
                },
                Paragraphs = new List<string>() { "para1", "para2", "para3" }

            };

            IDocumentStore store = Global.GetInMemoryStore();

            using (var session = store.OpenSession())
            {
                session.Store(story);
                session.SaveChanges();
            }

            StoryRepository repository = new StoryRepository(store);

            // Act
            var savedStory = repository.GetStoryById(story.Id);

            // Assert
            Global.AreEqualByJson(story.EditHistory, savedStory.EditHistory);
            Assert.AreEqual(story.Id, savedStory.Id);
            Global.AreEqualByJson(story.Paragraphs, savedStory.Paragraphs);
        }
        public void AddWordWontAllowTheSameUserToAddTwoConsecutiveWords()
        {
            // Setup
            Story story = new Story();
            string userId = "users/1";
            story.EditHistory.Add(new EditHistory()
            {
                DateAdded = DateTime.Now.AddHours(-4),
                UserId = userId

            });

            IDocumentStore store = Global.GetInMemoryStore();
            using (var session = store.OpenSession())
            {
                session.Store(story);
                session.SaveChanges();
            }

            StoryRepository repository = new StoryRepository(store);

            // Act
            var result = repository.AddWord(story.Id, "Add", userId);

            // Assert
            Assert.AreEqual(result.ErrorCode, StoryErrorCode.UserAddedTheLastWordInThisStory);
        }
        public void AddWordToStoryWithCurrentEditorThrowsException()
        {
            // Setup
            Story story = new Story();

            story.Paragraphs.Add("Here is the first paragraph of the story. Content doesn't really matter at the moment");
            story.Lock.UserId = "users/2";
            story.Lock.LockedDate = DateTime.Now;

            IDocumentStore store = Global.GetInMemoryStore();
            StoryRepository repository = new StoryRepository(store);

            SaveStory(story, store);

            repository.AddWord(story.Id, "word", "users/1");
        }
        public void AddWordToOneParagraphExistingStory()
        {
            // Setup
            Story story = new Story();
            story.Paragraphs.Add("Here is the first paragraph of the story. Content doesn't really matter at the moment");

            var store = Global.GetInMemoryStore();

            SaveStory(story, store);

            var rep = new StoryRepository(store);

            // Act
            rep.AddWord(story.Id, "added.", "users/1");

            // Assert
            using (var session = store.OpenSession())
            {
                var savedStory = session.Load<Story>(story.Id);
                Assert.AreEqual(savedStory.Paragraphs[0], "Here is the first paragraph of the story. Content doesn't really matter at the moment added.");
            }
        }
        public void AddWordToMultiParagraphExistingStory()
        {
            // Setup
            Story story = new Story();
            story.Paragraphs.Add("Here is the first paragraph of the story. Content doesn't really matter at the moment");
            story.Paragraphs.Add("Here is the second paragraph of the story. Content doesn't really matter at the moment");

            var store = Global.GetInMemoryStore();

            SaveStory(story, store);

            StoryRepository repository = new StoryRepository(store);

            // Act
            repository.AddWord(story.Id, "Added", "users/1");

            // Assert
            using (var session = store.OpenSession())
            {
                var savedStory = session.Load<Story>(story.Id);
                Assert.IsTrue(savedStory.Paragraphs[1] == "Here is the second paragraph of the story. Content doesn't really matter at the moment Added");

            }
        }
        public void AddWordToExistingUpdatesEditHistory()
        {
            // Setup
            Story story = new Story();
            story.Paragraphs.Add("Here is the first paragraph of the story. Content doesn't really matter at the moment");

            var store = Global.GetInMemoryStore();

            SaveStory(story, store);

            StoryRepository repository = new StoryRepository(store);

            // Act
            repository.AddWord(story.Id, "Added", "users/1");

            // Assert
            using (var session = store.OpenSession())
            {
                var savedStory = session.Load<Story>(story.Id);

                Assert.Greater(savedStory.EditHistory[0].DateAdded, DateTime.Now.AddMinutes(-1));
                Assert.AreEqual(savedStory.EditHistory[0].ParagraphIndex, 86);
                Assert.AreEqual(savedStory.EditHistory[0].ParagraphNumber, 1);
                Assert.AreEqual(savedStory.EditHistory[0].UserId, "users/1");

            }
        }
        private void CreateNewStory(string word, string userId, Story story)
        {
            story.EditHistory.Add(new EditHistory()
            {
                DateAdded = DateTime.Now,
                ParagraphIndex = 0,
                ParagraphNumber = 1,
                UserId = userId

            });
            story.Paragraphs.Add(word);

            using (var session = _store.OpenSession())
            {
                session.Store(story);
                session.SaveChanges();

            }
        }