Exemplo n.º 1
0
		public override async Task<bool> Handle(string element)
		{
			string username, content;
			if (!CanHandle(element, out username, out content))
				return false;

			// Handle
			var user = await _userRepository.Get(username);
			if (user == null)
			{
				user = new User()
				{
					Id = Guid.NewGuid(),
					Username = username
				};
				user = await _userRepository.Create(user);

				if (user == null)
				{
					Errors.Add("The user could not be created.");
					return true;
				}
			}

			var message = new Message()
			{
				Id = Guid.NewGuid(),
				TimeStamp = DateTime.UtcNow,
				UserId = user.Id,
				Content = content
			};
			message = await _messageRepository.Create(message);

			if (message == null)
			{
				Errors.Add("The message could not be created.");
				return true;
			}

			return true;
		}
		public void HandleTest()
		{
			var user1 = new User() { Id = Guid.NewGuid(), Username = "******" };
			var user2 = new User() { Id = Guid.NewGuid(), Username = "******" };
			var message1 = new Message() { Id = Guid.NewGuid(), UserId = user1.Id, User = user1, Content = "Test1", TimeStamp = DateTime.UtcNow.AddMinutes(-2) };
			var message2 = new Message() { Id = Guid.NewGuid(), UserId = user2.Id, User = user2, Content = "Test2", TimeStamp = DateTime.UtcNow.AddMinutes(-1) };

			user1.Following = new List<User>() { user2 };
			user1.Messages = new List<Message>() { message1 };
			user2.Messages = new List<Message>() { message2 };

			_userRepositoryMock.CachedUsers = new List<User>() { user1, user2 };
			_messageRepositoryMock.CachedMessages = new List<Message>() { message1, message2 };

			Assert.AreEqual(true, _wallCommandHandler.Handle("User1 wall").Result);
			Assert.DoesNotThrow(() => _userRepositoryMock.Verify(x => x.Get(It.IsAny<string>()), Times.AtLeast(1)));
			Assert.DoesNotThrow(() => _messageRepositoryMock.Verify(x => x.Get(), Times.AtLeast(1)));
			Assert.DoesNotThrow(() => _consoleMock.Verify(x => x.WriteLine(It.IsAny<string>(), It.IsAny<object[]>()), Times.Exactly(2)));
			Assert.IsTrue(_consoleMock.WriteLineBuffer.Count == 2);
			Assert.AreEqual("User1 - Test1 (2 minutes ago)", _consoleMock.WriteLineBuffer[0]);
			Assert.AreEqual("User2 - Test2 (1 minutes ago)", _consoleMock.WriteLineBuffer[1]);
		}