public void AddWordAllowsUserToAddWordIfUserIsCurrentEditor()
        {
            // Setup
            var store = Global.GetInMemoryStore();
            var userId = "users/1";
            var word = "New";
            var story = FakeEntityFactory.GetGenericStory(userId);
            story.Lock.UserId = userId;
            story.Lock.LockedDate = DateTime.Now.AddMinutes(-8);

            using (var session = store.OpenSession())
            {
                session.Store(story);
                session.SaveChanges();
                Global.UpdateIndex<Story>(session);
            }

            // Act
            StoryRepository repository = new StoryRepository(store);
            var result = repository.AddWord(story.Id, word, userId);

            // Assert
            Assert.AreEqual(result.ErrorCode, StoryErrorCode.Success);
            Assert.IsTrue(result.Story.Paragraphs.Last().EndsWith(word));
        }
        public void ReadStoryReturnsAStory()
        {
            // Setup
            IDocumentStore store = Global.GetInMemoryStore();
            User user = FakeEntityFactory.GetGenericUser();
            Story story;
            string userId = "users/1";

            using (var session = store.OpenSession())
            {
                session.Store(user);
                story = FakeEntityFactory.GetSpecificStoryWithText(userId, new List<string> { "123", "456", "789" });
                session.Store(story);
                session.SaveChanges();
            }

            IStoryRepository repo = new StoryRepository(store);
            StoryController controller = new StoryController(repo, new CurrentUser("users/1"));

            // Act
            Story result = (Story)controller.ReadStory(1).Data;

            // Assert
            Assert.AreEqual(result.Paragraphs[0], "123");
            Assert.AreEqual(result.Paragraphs[1], "456");
            Assert.AreEqual(result.Paragraphs[2], "789");
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var story = File.ReadLines("../../Story.txt");

            string storyID = "";

            foreach (var paragraph in story)
            {

                bool newParagraph = true;
                if (!string.IsNullOrEmpty(paragraph))
                {

                    Console.WriteLine("Saving Paragraph");
                    var words = paragraph.Split(' ');

                    foreach (string word in words)
                    {
                        if (!string.IsNullOrEmpty(word))
                        {
                            Console.WriteLine("Saving word");
                            StoryRepository repository = new StoryRepository();
                            var result = repository.AddWord(storyID, word, users.PickRandom<string>(), newParagraph);
                            newParagraph = false;
                            storyID = result.Story.Id;
                        }
                    }
                }
            }
        }
        public void UserCanLockAfterWordHasBeenAdded()
        {
            // Setup
            var user1 = "users/1";
            var user2 = "users/2";

            var story = FakeEntityFactory.GetGenericStory("users/23");

            IDocumentStore store = Global.GetInMemoryStore();

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

               }

            var repository = new StoryRepository(store);

            using (var session = store.OpenSession())
            {

                var firstLockResult = repository.LockStory(story.Id, user1);

                var saveStory = session.Load<Story>(story.Id);
                Assert.IsTrue(saveStory.HasEditor);
                Assert.AreEqual(saveStory.Lock.UserId, user1);

                var secondLockResult = repository.LockStory(story.Id, user2);
                saveStory = session.Load<Story>(story.Id);
                Assert.AreEqual(secondLockResult, StoryErrorCode.StoryLockedForEditing);
                Assert.AreEqual(saveStory.Lock.UserId, user1);

            }

            using (var session = store.OpenSession())
            {

                var firstAddWordResult = repository.AddWord(story.Id, "word", user1);
                var saveStory = session.Load<Story>(story.Id);
                Assert.AreEqual(firstAddWordResult.ErrorCode, StoryErrorCode.Success);
                Assert.IsFalse(saveStory.HasEditor);
            }

            using (var session = store.OpenSession())
            {
                var thirdLockResult = repository.LockStory(story.Id, user2);
                var saveStory = session.Load<Story>(story.Id);
                Assert.AreEqual(thirdLockResult, StoryErrorCode.Success);
                Assert.IsTrue(saveStory.HasEditor);
                Assert.AreEqual(saveStory.Lock.UserId, user2);
            }

            // Act

            // Assert
        }
        public void AddWordReturnsProperErrorCodeIfStoryIsNotFound()
        {
            // Setup
            IDocumentStore store = Global.GetInMemoryStore();
            StoryRepository repository = new StoryRepository(store);

            // Act
            var result = repository.AddWord("story/id", "Add", "users/1");

            // Assert
            Assert.AreEqual(result.ErrorCode, StoryErrorCode.StoryNotFoundInRepository);
        }
        public void AddWordTenMinutesAfterLockReturnsError()
        {
            // Setup
            var store = Global.GetInMemoryStore();
            var userId = "users/1";
            var story = FakeEntityFactory.GetGenericStory(userId);
            story.Lock.UserId = userId;
            story.Lock.LockedDate = DateTime.Now.AddMinutes(-15);

            using (var session = store.OpenSession())
            {
                session.Store(story);
                session.SaveChanges();
                Global.UpdateIndex<Story>(session);
            }

            StoryRepository repository = new StoryRepository(store);
            var result = repository.AddWord(story.Id, "New", userId);
            // Act

            // Assert
            Assert.AreEqual(result.ErrorCode, StoryErrorCode.TenMinuteLockWindowHasClosed);
        }
        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 GetStoriesByUserReturnsStories()
        {
            // Setup
            IDocumentStore store;
            User user;
            var stories = CreateFakeStories(out store, out user, 5);

            // Act
            IStoryRepository rep = new StoryRepository(store);
            List<Story> result = rep.GetStoriesByUser(user.Id).Stories;

            // Assert
            Assert.AreEqual(result.Count(), 5);

            foreach (var story in stories)
            {
                Assert.IsTrue(result.Where(s => s.Paragraphs.Contains(story.Paragraphs[0])).Count() > 0);
            }
        }
        public void GetStoriesByUserOrderTest()
        {
            // Setup
            IDocumentStore store;
            User user;
            var stories = CreateFakeStories(out store, out user, 10);

            // Act
            StoryRepository repository = new StoryRepository(store);
            var result = repository.GetStoriesByUser(user.Id);

            var sortedResult = result.Stories.OrderBy(s => s.DocumentId).ToList();

            // Assert
            for (int i = 0; i < result.Stories.Count; i++)
            {
                Assert.AreEqual(result.Stories[i].DocumentId, sortedResult[i].DocumentId);
            }
        }
        public void GetSecondPageTest()
        {
            // Setup
            IDocumentStore store = Global.GetInMemoryStore();

            string userId = "users/123";

            using (var session = store.OpenSession())
            {

                int i = 1;
                while (i <= 50)
                {
                    var story = FakeEntityFactory.GetGenericStory(userId);
                    story.Paragraphs[0] = story.Paragraphs[0].Insert(0, "Story" + i + " ");
                    session.Store(story);
                    i++;
                }

                session.SaveChanges();
                Global.UpdateIndex<Story>(session);

            }

            StoryRepository repo = new StoryRepository(store);

            // Act
            var result = repo.GetStoriesByUser(userId, 3, 10);

            // Assert
            Assert.IsTrue(result.Stories[0].Paragraphs[0].Contains("Story21"));
            Assert.IsTrue(result.Stories[1].Paragraphs[0].Contains("Story22"));
            Assert.IsTrue(result.Stories[2].Paragraphs[0].Contains("Story23"));
            Assert.IsTrue(result.Stories[3].Paragraphs[0].Contains("Story24"));
            Assert.IsTrue(result.Stories[4].Paragraphs[0].Contains("Story25"));
            Assert.IsTrue(result.Stories[5].Paragraphs[0].Contains("Story26"));
            Assert.IsTrue(result.Stories[6].Paragraphs[0].Contains("Story27"));
            Assert.IsTrue(result.Stories[7].Paragraphs[0].Contains("Story28"));
            Assert.IsTrue(result.Stories[8].Paragraphs[0].Contains("Story29"));
            Assert.IsTrue(result.Stories[9].Paragraphs[0].Contains("Story30"));
        }
        private static void RunLockTest()
        {
            var user1 = "users/1";
            var user2 = "users/2";

            var story = FakeEntityFactory.GetGenericStory("users/23");

            IDocumentStore store = Global.GetInMemoryStore();

            using (var session = store.OpenSession())
            {
                session.Store(story);
                session.SaveChanges();
                Global.UpdateIndex<Story>(session);
            }

            var repository = new StoryRepository(store);

            var tasks = new List<Task>();

            LockStoryRunner runner1 = new LockStoryRunner(story.Id, user1, repository);
            LockStoryRunner runner2 = new LockStoryRunner(story.Id, user2, repository);

            tasks.Add(Task.Factory.StartNew<StoryErrorCode>(runner1.LockStory));
            tasks.Add(Task.Factory.StartNew<StoryErrorCode>(runner2.LockStory));

            Task.WaitAll(tasks.ToArray());

            var result1 = (StoryErrorCode)tasks[0].Status;
            var result2 = (StoryErrorCode)tasks[1].Status;

            if (result1 == StoryErrorCode.Success)
                Assert.AreEqual(result2, StoryErrorCode.StoryLockedForEditing);

            if (result2 == StoryErrorCode.Success)
                Assert.AreEqual(result1, StoryErrorCode.StoryLockedForEditing);
        }
        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 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 AddWordToNewStory()
        {
            // Setup
            var store = Global.GetInMemoryStore();
            StoryRepository repository = new StoryRepository(store);

            string userId = "users/2";
            string word = "Once";

            // Act
            var result = repository.AddWord("", word, userId);

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

                Assert.AreEqual(story.LastEditorId, userId);
                Assert.IsNotNull(story.EditHistory);
                Assert.Greater(story.EditHistory[0].DateAdded, DateTime.Now.AddMinutes(-1));

                Assert.AreEqual(story.EditHistory[0].ParagraphIndex, 0);
                Assert.AreEqual(story.EditHistory[0].ParagraphNumber, 1);
                Assert.AreEqual(story.EditHistory[0].UserId, userId);

                Assert.AreEqual(story.EditHistory.Count, 1);

                Assert.AreEqual(story.Id, result.Story.Id);
                Assert.AreEqual(story.Paragraphs.Count, 1);
                Assert.AreEqual(story.Paragraphs[0], word);

            }
        }
        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");

            }
        }
        public void GetStoryByIdThrowsException()
        {
            // Setup

            IDocumentStore store = Global.GetInMemoryStore();

            StoryRepository repository = new StoryRepository(store);
            Assert.Throws<ArgumentNullException>(() => repository.GetStoryById(""));
            Assert.Throws<ArgumentNullException>(() => repository.GetStoryById(null));
        }
        public void PageSizeWithNoPageNoReturnsError()
        {
            // Setup
            IDocumentStore store = Global.GetInMemoryStore();
            StoryRepository repository = new StoryRepository(store);

            // Act
            Assert.Throws<ArgumentException>(() => repository.GetStoriesByUser("string", pageSize: 5), "Must include Page No with Page Size");
        }
 public LockStoryRunner(string storyId, string userId, StoryRepository repository)
 {
     _storyId = storyId;
     _userId = userId;
     _repository = repository;
 }
        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 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 StartNewParagraphTest()
        {
            // Setup
            IDocumentStore store = Global.GetInMemoryStore();

            var repository = new StoryRepository(store);

            var wordToAdd = "Hey";

            var story = FakeEntityFactory.GetGenericStory("users/1");

            var numberofParagraphs = story.Paragraphs.Count;

            using (var session = store.OpenSession())
            {

                session.Store(story);
                session.SaveChanges();
            }

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

            // Assert
            using (var session = store.OpenSession())
            {
                Global.UpdateIndex<Story>(session);
                var resultStory = session.Load<Story>(story.Id);
                Assert.AreEqual(resultStory.Paragraphs.Count, numberofParagraphs + 1);
                Assert.AreEqual(resultStory.Paragraphs[numberofParagraphs], wordToAdd);
            }
        }
        public void AllowsALockIfYouveAlreadyLockedIt()
        {
            // Setup
            var store = Global.GetInMemoryStore();

            var userId = "users/1";

            var story = FakeEntityFactory.GetGenericStory(userId);
            story.Lock.UserId = userId;

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

            // Act
            StoryRepository repo = new StoryRepository(store);
            var result = repo.LockStory("stories/1", userId);

            // Assert
            Assert.AreEqual(result, StoryErrorCode.Success);
        }
        public void AddWordThrowsExceptionsIfWordOrUserIdIsNullOrEmpty()
        {
            // Setup
            var store = Global.GetInMemoryStore();
            StoryRepository repository = new StoryRepository(store);

            // Assert

            Assert.Throws<ArgumentNullException>(() => repository.AddWord("story/1", "", "userId"), "word");
            Assert.Throws<ArgumentNullException>(() => repository.AddWord("story/2", null, "userId"), "word");
            Assert.Throws<ArgumentNullException>(() => repository.AddWord("story/1", "Once", ""), "userId");
            Assert.Throws<ArgumentNullException>(() => repository.AddWord("story/2", "Once", null), "userId");
        }
        public void GetRandomStoriesReturnsStories()
        {
            // Setup
            IDocumentStore store = Global.GetInMemoryStore();

            using (var session = store.OpenSession())
            {
                for(int i = 0; i < 20; i++)
                {
                    session.Store(FakeEntityFactory.GetGenericStory(string.Empty));
                }

                session.SaveChanges();
                Global.UpdateIndex<Story>(session);
            }

            IStoryRepository rep = new StoryRepository(store);

            // Act
            var result = rep.GetRandomStories(10);

            // Assert
            Assert.AreEqual(result.Count(), 10);
        }