public async Task AddTwoMessagesWithTheSameMessageId() { string participant1 = RandomString(); string participant2 = RandomString(); var createConversationDto = new CreateConversationDto { Participants = new[] { participant1, participant2 } }; await Task.WhenAll( chatServiceClient.CreateProfile(new CreateProfileDto { Username = participant1, FirstName = "Participant", LastName = "1" }), chatServiceClient.CreateProfile(new CreateProfileDto { Username = participant2, FirstName = "Participant", LastName = "2" }) ); var conversationDto = await chatServiceClient.AddConversation(createConversationDto); var messageId = RandomString(); var message1 = new SendMessageDtoV2("Hello", participant1, messageId); var message2 = new SendMessageDtoV2("Hello", participant1, messageId); await chatServiceClient.SendMessage(conversationDto.Id, message1); await chatServiceClient.SendMessage(conversationDto.Id, message2); ListMessagesDto listMessagesDto = await chatServiceClient.ListMessages(conversationDto.Id); Assert.AreEqual(1, listMessagesDto.Messages.Count); }
public async Task <Message> PostMessage(string conversationId, SendMessageDtoV2 messageDto) { var matchingMessage = await conversationsStore.TryGetMessage(conversationId, messageDto.MessageId); if (matchingMessage.found) // if the message was found in storage { return(matchingMessage.message); } var currentTime = DateTime.Now; var message = new Message(messageDto.Text, messageDto.SenderUsername, currentTime); await conversationsStore.AddMessage(conversationId, messageDto.MessageId, message); logger.LogInformation(Events.ConversationMessageAdded, "Message {MessageId} has been added to conversation {conversationId}, sender: {senderUsername}", messageDto.MessageId, conversationId, messageDto.SenderUsername); var conversation = await conversationsStore.GetConversation(messageDto.SenderUsername, conversationId); var usersToNotify = conversation.Participants; var newMessagePayload = new NotificationPayload(currentTime, "MessageAdded", conversationId, usersToNotify); await notificationService.SendNotificationAsync(newMessagePayload); return(message); }
public async Task <Message> PostMessage(string conversationId, SendMessageDto messageDto) { var currentTime = DateTime.Now; string messageId = GenerateMessageId(messageDto, currentTime); var messageDtoV2 = new SendMessageDtoV2(messageDto.Text, messageDto.SenderUsername, messageId); return(await PostMessage(conversationId, messageDtoV2)); }
public async Task PostMessageV2Returns503WhenStorageIsUnavailable() { conversationsStoreMock.Setup(store => store.TryGetMessage(It.IsAny <string>(), It.IsAny <string>())) .ThrowsAsync(new StorageErrorException("Test Failure")); SendMessageDtoV2 newMessage = new SendMessageDtoV2(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); IActionResult result = await conversationController.PostMessage( Guid.NewGuid().ToString(), newMessage); TestUtils.AssertStatusCode(HttpStatusCode.ServiceUnavailable, result); }
public async Task PostMessageV2Returns500WhenUnknownExceptionIsThrown() { conversationsStoreMock.Setup(store => store.TryGetMessage(It.IsAny <string>(), It.IsAny <string>())) .ThrowsAsync(new UnknownException()); SendMessageDtoV2 newMessage = new SendMessageDtoV2(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); IActionResult result = await conversationController.PostMessage( Guid.NewGuid().ToString(), newMessage); TestUtils.AssertStatusCode(HttpStatusCode.InternalServerError, result); }
public async Task <IActionResult> PostMessage(string id, [FromBody] SendMessageDtoV2 messageDto) { try { var message = await conversationService.PostMessage(id, messageDto); return(Ok(message)); } catch (StorageErrorException e) { logger.LogError(Events.StorageError, e, "Could not reach storage to add message, conversationId {conversationId}", id); return(StatusCode(503, $"Could not reach storage to add message, conversationId {id}")); } catch (Exception e) { logger.LogError(Events.InternalError, e, "Failed to add message to conversation, conversationId: {conversationId}", id); return(StatusCode(500, $"Failed to add message to conversation, conversationId: {id}")); } }
public async Task SendMessage(string conversationId, SendMessageDtoV2 messageDto) { try { HttpResponseMessage response = await httpClient.PostAsync($"api/v2/conversation/{conversationId}", new StringContent(JsonConvert.SerializeObject(messageDto), Encoding.UTF8, "application/json")); if (!response.IsSuccessStatusCode) { throw new ChatServiceException("Failed to retrieve user profile", response.ReasonPhrase, response.StatusCode); } } catch (JsonException e) { throw new ChatServiceException("Failed to deserialize the response", e, "Serialization Exception", HttpStatusCode.InternalServerError); } catch (Exception e) { throw new ChatServiceException("Failed to reach chat service", e, "Internal Server Error", HttpStatusCode.InternalServerError); } }
public async Task AddListMessagesV2() { string participant1 = RandomString(); string participant2 = RandomString(); var createConversationDto = new CreateConversationDto { Participants = new[] { participant1, participant2 } }; await Task.WhenAll( chatServiceClient.CreateProfile(new CreateProfileDto { Username = participant1, FirstName = "Participant", LastName = "1" }), chatServiceClient.CreateProfile(new CreateProfileDto { Username = participant2, FirstName = "Participant", LastName = "2" }) ); var conversationDto = await chatServiceClient.AddConversation(createConversationDto); var message1 = new SendMessageDtoV2("Hello", participant1, RandomString()); var message2 = new SendMessageDtoV2("What's up?", participant1, RandomString()); var message3 = new SendMessageDtoV2("Not much!", participant2, RandomString()); await chatServiceClient.SendMessage(conversationDto.Id, message1); await chatServiceClient.SendMessage(conversationDto.Id, message2); await chatServiceClient.SendMessage(conversationDto.Id, message3); ListMessagesDto listMessagesDto = await chatServiceClient.ListMessages(conversationDto.Id); Assert.AreEqual(3, listMessagesDto.Messages.Count); Assert.AreEqual(message3.Text, listMessagesDto.Messages[0].Text); Assert.AreEqual(message2.Text, listMessagesDto.Messages[1].Text); Assert.AreEqual(message1.Text, listMessagesDto.Messages[2].Text); }
public Task <Message> PostMessage(string conversationId, SendMessageDtoV2 messageDto) { return(postMessageControllerTimeMetric.TrackTime(() => service.PostMessage(conversationId, messageDto))); }