public void PassQuestionForUser_ShouldPassQuestionsThatAreUnanswered() { var shouldPass = new PendingQuestion { Status = null, Question = new Question() { ID = 1 } }; var shouldPass2 = new PendingQuestion { Status = true, Question = new Question() { ID = 1 } }; var shouldNotPass = new PendingQuestion { Status = true, Answer = new Answer(), Question = new Question() { ID = 1 } }; var shouldNotPass2 = new PendingQuestion { Status = false, Question = new Question() { ID = 1 } }; var user = new User { PendingQuestions = new Collection<PendingQuestion> { shouldPass, shouldPass2, shouldNotPass, shouldNotPass2 } }; _dataFactory.Setup(x => x.UserRepository.GetUserByEmail(It.IsAny<string>())).Returns(user); _dataFactory.Setup(x => x.PendingQuestionRepository).Returns(new Mock<IPendingQuestionRepository>().Object); _advisorLocator.Setup(x => x.GetNextInLine(It.IsAny<int>())).Returns(new User()); _adviseManager.PassQuestionForUser("email"); Assert.That(shouldPass.Status, Is.False); Assert.That(shouldPass2.Status, Is.False); Assert.That(shouldNotPass.Status, Is.True); Assert.That(shouldNotPass2.Status, Is.False); }
public void QuestionAssigned(PendingQuestion pendingQuestion) { var client = _socketManager.GetClient(pendingQuestion.User.Email); if (client != null) { client.Send(SocketEvent.QuestionAssigned(pendingQuestion)); } }
public void QuestionAssigned_ShouldReturnCorrectJson() { //Arrange const string expectedJson = "[\"QuestionAssigned\",{\"ID\":1}]"; var pendingQuestion = new PendingQuestion {ID = 1}; //Act var result = SocketEvent.QuestionAssigned(pendingQuestion); //Assert Assert.That(result, Is.EqualTo(expectedJson)); }
public void StartAnswer_ShouldReturnTrue_WhenSuccessfull() { //Arrange var pending = new PendingQuestion { TimeStamp = DateTime.Now.AddSeconds(-Configuration.Timeout.Respond + 5) //5 seconds to spare }; var pendingQuestionRepository = new Mock<IPendingQuestionRepository>(); pendingQuestionRepository.Setup(x => x.GetByID(It.IsAny<int>())).Returns(pending); _dataFactory.Setup(x => x.PendingQuestionRepository).Returns(pendingQuestionRepository.Object); //Act var result = _adviseManager.StartAnswer(pending); //Assert Assert.That(result, Is.True); }
public void StartAnswer_ShouldNotifyEventManager_WhenSuccessfull() { //Arrange var pending = new PendingQuestion { TimeStamp = DateTime.Now.AddSeconds(-Configuration.Timeout.Respond + 5) //5 seconds to spare }; var pendingQuestionRepository = new Mock<IPendingQuestionRepository>(); pendingQuestionRepository.Setup(x => x.GetByID(It.IsAny<int>())).Returns(pending); _dataFactory.Setup(x => x.PendingQuestionRepository).Returns(pendingQuestionRepository.Object); //Act _adviseManager.StartAnswer(pending); //Assert _eventManager.Verify(x => x.AnswerStarted(pending), Times.Once); }
public void GetDeadLine_ShouldReturnCorrectDateTime_ForRespond() { //Arrange var now = DateTime.Now; var expected = now.AddSeconds(Configuration.Timeout.Respond); var pending = new PendingQuestion { Status = null, TimeStamp = now }; //Act var result = _adviseManager.GetDeadline(pending); //Arrange Assert.That(result, Is.EqualTo(expected)); }
public void StartAnswer_ShouldNotNotifyEventManagerOrUpdateStatus_AndReturnFalse_IfUserWasTooLate() { var pending = new PendingQuestion { TimeStamp = DateTime.Now.AddSeconds(-Configuration.Timeout.Respond - 5) //5 seconds too late }; var pendingQuestionRepository = new Mock<IPendingQuestionRepository>(); pendingQuestionRepository.Setup(x => x.GetByID(It.IsAny<int>())).Returns(pending); _dataFactory.Setup(x => x.PendingQuestionRepository).Returns(pendingQuestionRepository.Object); //Act var result = _adviseManager.StartAnswer(pending); //Assert Assert.That(result, Is.False); pendingQuestionRepository.Verify(x => x.Update(pending), Times.Never); _dataFactory.Verify(x => x.Commit(), Times.Never); _eventManager.Verify(x => x.AnswerStarted(pending), Times.Never); }
public void GetAvailableUsers_ShouldNotReturnUsers_WhoHasAcceptedToAnswer_ButHasNotCompletedIt() { //Arrange var dbMock = new Mock<IRadvillContext>(); var userOne = new User { ID = 1, Connected = true }; var userDbSet = new FakeDbSet<User> { userOne }; dbMock.Setup(x => x.Users).Returns(userDbSet); var pendingQuestion = new PendingQuestion { ID = 1, User = userOne, Answer = null, Status = true }; var pendingQuestionDbSet = new FakeDbSet<PendingQuestion> { pendingQuestion }; dbMock.Setup(x => x.PendingQuestions).Returns(pendingQuestionDbSet); dbMock.Setup(x => x.Set<User>()).Returns(userDbSet); var userRepository = new UserRepository(dbMock.Object); //Act var result = userRepository.GetAvailableUsers(); //Assert Assert.That(result, Is.Not.Null); Assert.That(result.Count(), Is.EqualTo(0)); }
public void PassQuestion_ShouldAddNewPendingQuestion_IfAdvisorIsAvailable() { var pending = new PendingQuestion { Question = new Question() }; //Arrange var pendingQuestionRepository = new Mock<IPendingQuestionRepository>(); pendingQuestionRepository.Setup(x => x.GetByID(It.IsAny<int>())).Returns(pending); _dataFactory.Setup(x => x.PendingQuestionRepository).Returns(pendingQuestionRepository.Object); _dataFactory.Setup(x => x.QuestionRepository.GetByID(It.IsAny<int>())).Returns(new Question()); _advisorLocator.Setup(x => x.GetNextInLine(It.IsAny<int>())).Returns(new User()); //Act _adviseManager.PassQuestion(pending); //Assert pendingQuestionRepository.Verify(x => x.Update(pending), Times.Once); pendingQuestionRepository.Verify(x => x.Insert(It.IsAny<PendingQuestion>()), Times.Once); _dataFactory.Verify(x => x.Commit(), Times.Exactly(2)); _eventManager.Verify(x => x.QuestionAssigned(It.IsAny<PendingQuestion>()), Times.Once); }
public void SubmitAnswer_ShouldReturnFalse_WhenDeadlineHasPassed() { //Arrange var pending = new PendingQuestion { TimeStamp = DateTime.Now.AddSeconds(-Configuration.Timeout.Respond - 5) //5 seconds over due }; const string answer = "Answer"; var answerRepo = new Mock<IAnswerRepository>(); _dataFactory.Setup(x => x.AnswerRepository).Returns(answerRepo.Object); //Act var result = _adviseManager.SubmitAnswer(pending, answer); //Assert Assert.That(result, Is.False); answerRepo.Verify(x => x.Insert(It.IsAny<Answer>()), Times.Never); _dataFactory.Verify(x => x.Commit(), Times.Never); _eventManager.Verify(x => x.AnswerSubmitted(It.IsAny<Answer>()), Times.Never); }
public void GetNextInLine_ShouldNotReturnUser_WhoHavePassedTheQuestionBefore() { //Arrange var question = new Question{ID = 1}; var question2 = new Question{ID = 2}; var user = new User { Answers = new Collection<Answer>(), Questions = new Collection<Question>(),Connected = true}; var pending = new PendingQuestion {Question = question, Status = false, User = user}; var pending2 = new PendingQuestion {Question = question2, Status = false, User = user}; user.PendingQuestions = new Collection<PendingQuestion>{pending, pending2}; var users = new List<User> {user}; _userRepositoryMock.Setup(x => x.GetAvailableUsers()).Returns(users); _dataFactoryMock.Setup(x => x.UserRepository).Returns(_userRepositoryMock.Object); _advisorLocator = new AdvisorLocator(_dataFactoryMock.Object); //Act var result = _advisorLocator.GetNextInLine(question2.ID); //Assert Assert.That(result, Is.Null); }
public void PassQuestion(PendingQuestion pendingQuestion) { try { //Set pass status pendingQuestion.Status = false; _dataFactory.PendingQuestionRepository.Update(pendingQuestion); _dataFactory.Commit(); var reciever = _advisorLocator.GetNextInLine(pendingQuestion.Question.ID); if (reciever == null) { AllRecipientsPassed(pendingQuestion.Question); return; } SendQuestionToNewUser(pendingQuestion.Question); } catch (Exception e) { Logger.Log.Fatal("Exception during Pass Question", e); throw; } }
public AnswerStartedEvent(PendingQuestion pendingQuestion) { ID = pendingQuestion.Question.ID; }
public bool StartAnswer(PendingQuestion pending) { try { if (DateTime.Now > GetDeadline(pending)) { return false; } pending.Status = true; _dataFactory.PendingQuestionRepository.Update(pending); _dataFactory.Commit(); _eventManager.AnswerStarted(pending); return true; } catch (Exception e) { Logger.Log.Fatal("Exception during start answer", e); throw; } }
public DateTime GetDeadline(PendingQuestion pending) { return pending.Status == true ? pending.TimeStamp.AddSeconds(Configuration.Timeout.Respond + Configuration.Timeout.Answer) : pending.TimeStamp.AddSeconds(Configuration.Timeout.Respond); }
private bool SendQuestionToNewUser(Question question) { var reciever = _advisorLocator.GetNextInLine(question.ID); if (reciever == null) { AllRecipientsPassed(question); return false; } var newPending = new PendingQuestion { Question = question, Status = null, TimeStamp = DateTime.Now, User = reciever }; _dataFactory.PendingQuestionRepository.Insert(newPending); _dataFactory.Commit(); _eventManager.QuestionAssigned(newPending); return true; }
public void PassQuestion_ShouldSetPassStatus_ToPendingQuestion() { //Arrange var pending = new PendingQuestion { Question = new Question(), Status = null }; _dataFactory.Setup(x => x.PendingQuestionRepository.GetByID(It.IsAny<int>())).Returns(pending); _dataFactory.Setup(x => x.QuestionRepository).Returns(new Mock<IQuestionRepository>().Object); //Act _adviseManager.PassQuestion(pending); //Assert Assert.That(pending.Status, Is.False); _dataFactory.Verify(x => x.PendingQuestionRepository.Update(pending), Times.Once); _dataFactory.Verify(x => x.Commit(), Times.Exactly(2)); }
public QuestionAssignedEvent(PendingQuestion pendingQuestion) { ID = pendingQuestion.ID; }
public void SubmitAnswer_ShouldReturnTrue_WhenSuccessfull() { //Arrange var pending = new PendingQuestion { TimeStamp = DateTime.Now.AddSeconds(-Configuration.Timeout.Respond + 5), //5 seconds to spare }; const string answer = "Answer"; var answerRepo = new Mock<IAnswerRepository>(); var pendingQuestionRepo = new Mock<IPendingQuestionRepository>(); _dataFactory.Setup(x => x.AnswerRepository).Returns(answerRepo.Object); _dataFactory.Setup(x => x.PendingQuestionRepository).Returns(pendingQuestionRepo.Object); //Act var result = _adviseManager.SubmitAnswer(pending, answer); //Assert Assert.That(result, Is.True); Assert.That(pending.Answer, Is.Not.Null); answerRepo.Verify(x => x.Insert(It.IsAny<Answer>()), Times.Once); pendingQuestionRepo.Verify(x => x.Update(pending), Times.Once); _dataFactory.Verify(x => x.Commit(), Times.Once); _eventManager.Verify(x => x.AnswerSubmitted(It.IsAny<Answer>()), Times.Once); }
public bool SubmitAnswer(PendingQuestion pending, string answer) { try { var now = DateTime.Now; if (now > GetDeadline(pending) || pending.Answer != null) { return false; } var answerEntity = new Answer { Accepted = null, Question = pending.Question, Text = answer, User = pending.User, TimeStamp = now }; pending.Answer = answerEntity; _dataFactory.PendingQuestionRepository.Update(pending); _dataFactory.AnswerRepository.Insert(answerEntity); _dataFactory.Commit(); _eventManager.AnswerSubmitted(answerEntity); return true; } catch (Exception e) { Logger.Log.Fatal("Exception during submit answer", e); throw; } }
public bool SubmitQuestion(int userid, int categoryId, string question) { try { var user = _dataFactory.UserRepository.GetByID(userid); var category = _dataFactory.CategoryRepository.GetByID(categoryId); var timeStamp = DateTime.Now; var questionEntity = new Question { Text = question, Category = category, User = user, TimeStamp = timeStamp }; _dataFactory.QuestionRepository.Insert(questionEntity); _dataFactory.Commit(); var reciever = _advisorLocator.GetNextInLine(questionEntity.ID); if (reciever == null) { _dataFactory.QuestionRepository.Delete(questionEntity); _dataFactory.Commit(); return false; } var pendingEntity = new PendingQuestion { Question = questionEntity, Status = null, User = reciever, TimeStamp = timeStamp }; _dataFactory.PendingQuestionRepository.Insert(pendingEntity); _dataFactory.Commit(); _eventManager.QuestionAssigned(pendingEntity); return true; } catch (Exception e) { Logger.Log.Fatal("Exception during Submit Question", e); throw; } }
public static string QuestionAssigned(PendingQuestion pendingQuestion) { var eventData = new QuestionAssignedEvent(pendingQuestion); var json = string.Format(JsonBody, "QuestionAssigned", JsonConvert.SerializeObject(eventData)); return json; }
public void GetAvailableUsers_ShouldReturnUsers_WhoIsntAnswereringAQuestion() { //Arrange var dbMock = new Mock<IRadvillContext>(); var userOne = new User { ID = 1, Connected = true }; var userTwo = new User { ID = 2, Connected = true }; var userDbSet = new FakeDbSet<User> { userOne, userTwo }; dbMock.Setup(x => x.Users).Returns(userDbSet); var pendingQuestion = new PendingQuestion { ID = 1, User = userOne }; var pendingQuestionDbSet = new FakeDbSet<PendingQuestion> { pendingQuestion }; dbMock.Setup(x => x.PendingQuestions).Returns(pendingQuestionDbSet); dbMock.Setup(x => x.Set<User>()).Returns(userDbSet); var userRepository = new UserRepository(dbMock.Object); //Act var result = userRepository.GetAvailableUsers(); //Assert Assert.That(result, Is.Not.Null); Assert.That(result.Count(), Is.EqualTo(1)); Assert.That(result.First().ID, Is.EqualTo(userTwo.ID)); }