Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: updated MessageNotificationResponseDto changed lastMessage fiel… #64

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.chat.yourway.dto.response;

import java.time.LocalDateTime;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@NoArgsConstructor
@Getter
@Setter
@ToString
public class LastMessageResponseDto {

private static final int MAX_LENGTH = 20;

private LocalDateTime timestamp;
private String sentFrom;
private String lastMessage;

public LastMessageResponseDto(LocalDateTime timestamp, String sentFrom, String lastMessage) {
this.timestamp = timestamp;
this.sentFrom = sentFrom;
this.lastMessage = lastMessage;
}

public void setLastMessage(String lastMessage) {
if (lastMessage.length() <= MAX_LENGTH) {
this.lastMessage = lastMessage;
} else {
this.lastMessage = lastMessage.substring(0, MAX_LENGTH) + "...";
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public class MessageNotificationResponseDto {
private EventType status;
private Integer unreadMessages;
private LocalDateTime lastRead;
private String lastMessage;
private LastMessageResponseDto lastMessage;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.chat.yourway.model.event.EventType.UNSUBSCRIBED;

import com.chat.yourway.config.websocket.WebsocketProperties;
import com.chat.yourway.dto.response.LastMessageResponseDto;
import com.chat.yourway.model.event.ContactEvent;
import com.chat.yourway.service.interfaces.ChatNotificationService;
import com.chat.yourway.service.interfaces.ContactEventService;
Expand All @@ -29,7 +30,7 @@ public class StompSubscriptionListener {
private final ContactEventService contactEventService;
private final ChatNotificationService chatNotificationService;

private static String lastMessage;
private static LastMessageResponseDto lastMessageDto;
private static final String USER_DESTINATION = "/user";
private static final String SLASH = "/";

Expand All @@ -40,10 +41,10 @@ public void handleWebSocketSubscribeListener(SessionSubscribeEvent event) {

try {
if (isTopicDestination(destination)) {
lastMessage = contactEventService.getByTopicIdAndEmail(getTopicId(event), email)
lastMessageDto = contactEventService.getByTopicIdAndEmail(getTopicId(event), email)
.getLastMessage();
var contactEvent = new ContactEvent(email, getTopicId(event), SUBSCRIBED,
getTimestamp(event), lastMessage);
getTimestamp(event), lastMessageDto);
contactEventService.save(contactEvent);
}

Expand All @@ -64,7 +65,7 @@ public void handleWebSocketUnsubscribeListener(SessionUnsubscribeEvent event) {
try {
if (isTopicDestination(destination)) {
var contactEvent = new ContactEvent(email, getTopicId(event), UNSUBSCRIBED,
getTimestamp(event), lastMessage);
getTimestamp(event), lastMessageDto);
contactEventService.save(contactEvent);
}

Expand Down
15 changes: 4 additions & 11 deletions src/main/java/com/chat/yourway/model/event/ContactEvent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.chat.yourway.model.event;

import com.chat.yourway.dto.response.LastMessageResponseDto;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -25,11 +26,11 @@ public class ContactEvent {
private Integer topicId;
private EventType eventType;
private LocalDateTime timestamp;
private String lastMessage;
private LastMessageResponseDto lastMessage;

private static final int MAX_LENGTH = 20;

public ContactEvent(String email, Integer topicId, EventType eventType, LocalDateTime timestamp, String lastMessage) {
public ContactEvent(String email, Integer topicId, EventType eventType, LocalDateTime timestamp,
LastMessageResponseDto lastMessage) {
this.id = email + "_" + topicId;
this.email = email;
this.topicId = topicId;
Expand All @@ -38,12 +39,4 @@ public ContactEvent(String email, Integer topicId, EventType eventType, LocalDat
this.lastMessage = lastMessage;
}

public void setLastMessage(String lastMessage) {
if (lastMessage.length() <= MAX_LENGTH) {
this.lastMessage = lastMessage;
} else {
this.lastMessage = lastMessage.substring(0, MAX_LENGTH) + "...";
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.chat.yourway.dto.request.MessagePrivateRequestDto;
import com.chat.yourway.dto.request.MessagePublicRequestDto;
import com.chat.yourway.dto.request.PageRequestDto;
import com.chat.yourway.dto.response.LastMessageResponseDto;
import com.chat.yourway.dto.response.MessageResponseDto;
import com.chat.yourway.service.interfaces.ChatMessageService;
import com.chat.yourway.service.interfaces.ChatNotificationService;
Expand Down Expand Up @@ -67,7 +68,12 @@ public List<MessageResponseDto> sendMessageHistoryByTopicId(Integer topicId,
}

private void sendToTopic(Integer topicId, MessageResponseDto messageDto) {
contactEventService.setLastMessageToAllTopicSubscribers(topicId, messageDto.getContent());
var lastMessageDto = new LastMessageResponseDto();
lastMessageDto.setTimestamp(messageDto.getTimestamp());
lastMessageDto.setSentFrom(messageDto.getSentFrom());
lastMessageDto.setLastMessage(messageDto.getContent());

contactEventService.setLastMessageToAllTopicSubscribers(topicId, lastMessageDto);
simpMessagingTemplate.convertAndSend(toTopicDestination(topicId), messageDto);
chatNotificationService.notifyTopicSubscribers(topicId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.chat.yourway.model.event.EventType.ONLINE;

import com.chat.yourway.dto.response.LastMessageResponseDto;
import com.chat.yourway.model.event.ContactEvent;
import com.chat.yourway.model.event.EventType;
import com.chat.yourway.repository.ContactEventRedisRepository;
Expand Down Expand Up @@ -30,7 +31,7 @@ public ContactEvent getByTopicIdAndEmail(Integer topicId, String email) {
log.trace("Started getByTopicIdAndEmail, topicId [{}], email [{}]", topicId, email);

return contactEventRedisRepository.findById(email + "_" + topicId)
.orElse(new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), ""));
.orElse(new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), null));
}

@Override
Expand Down Expand Up @@ -61,7 +62,7 @@ public List<ContactEvent> getAllByTopicId(Integer topicId) {
}

@Override
public void setLastMessageToAllTopicSubscribers(Integer topicId, String message) {
public void setLastMessageToAllTopicSubscribers(Integer topicId, LastMessageResponseDto message) {
log.trace("Started setLastMessageToAllTopicSubscribers, topic id [{}], last message [{}]",
topicId, message);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.chat.yourway.service.interfaces;

import com.chat.yourway.dto.response.LastMessageResponseDto;
import com.chat.yourway.model.event.ContactEvent;
import com.chat.yourway.model.event.EventType;
import java.util.List;
Expand Down Expand Up @@ -44,8 +45,8 @@ public interface ContactEventService {
* Set last message to all topic subscribers events.
*
* @param topicId topic id.
* @param message last message.
* @param lastMessageDto last message Dto.
*/
void setLastMessageToAllTopicSubscribers(Integer topicId, String message);
void setLastMessageToAllTopicSubscribers(Integer topicId, LastMessageResponseDto lastMessageDto);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.chat.yourway.dto.request.MessagePrivateRequestDto;
import com.chat.yourway.dto.request.MessagePublicRequestDto;
import com.chat.yourway.dto.request.PageRequestDto;
import com.chat.yourway.dto.response.LastMessageResponseDto;
import com.chat.yourway.dto.response.MessageNotificationResponseDto;
import com.chat.yourway.dto.response.MessageResponseDto;
import com.chat.yourway.integration.controller.websocketclient.TestStompFrameHandler;
Expand Down Expand Up @@ -196,7 +197,12 @@ void getMessages_shouldReturnReceivedMessagesHistoryFromTopic() {
void notifyTopicSubscribers_shouldNotifyTopicSubscribersIfSubscribeEvent() {
// Given
int topicId = 12;
var event = new ContactEvent("[email protected]", topicId, ONLINE, LocalDateTime.now(), "Hi");
var lastMessageDto = new LastMessageResponseDto();
lastMessageDto.setTimestamp(LocalDateTime.now());
lastMessageDto.setSentFrom("[email protected]");
lastMessageDto.setLastMessage("Hi");

var event = new ContactEvent("[email protected]", topicId, ONLINE, LocalDateTime.now(), lastMessageDto);
saveContactEvent(event);
//Stored subscription results for testing
CompletableFuture<MessageNotificationResponseDto[]> resultKeeper = new CompletableFuture<>();
Expand All @@ -223,7 +229,12 @@ void notifyTopicSubscribers_shouldNotifyTopicSubscribersIfSubscribeEvent() {
void notifyTopicSubscribers_shouldNotifyTopicSubscribersIfAnyContactSubscribedToTopic() {
// Given
int topicId = 12;
var event = new ContactEvent("[email protected]", topicId, ONLINE, LocalDateTime.now(), "Hi");
var lastMessageDto = new LastMessageResponseDto();
lastMessageDto.setTimestamp(LocalDateTime.now());
lastMessageDto.setSentFrom("[email protected]");
lastMessageDto.setLastMessage("Hi");

var event = new ContactEvent("[email protected]", topicId, ONLINE, LocalDateTime.now(), lastMessageDto);
saveContactEvent(event);
//Stored subscription results for testing
CompletableFuture<MessageNotificationResponseDto[]> resultKeeper = new CompletableFuture<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.springframework.messaging.simp.stomp.StompCommand.UNSUBSCRIBE;

import com.chat.yourway.config.websocket.WebsocketProperties;
import com.chat.yourway.dto.response.LastMessageResponseDto;
import com.chat.yourway.listener.StompSubscriptionListener;
import com.chat.yourway.model.event.ContactEvent;
import com.chat.yourway.service.interfaces.ChatNotificationService;
Expand Down Expand Up @@ -68,9 +69,13 @@ public void handleWebSocketSubscribeListener_shouldSaveEvent() {
int topicId = 1;
String destination = "/topic/" + topicId;

String lastMessage = "Hello";
var lastMessageDto = new LastMessageResponseDto();
lastMessageDto.setTimestamp(LocalDateTime.now());
lastMessageDto.setSentFrom("[email protected]");
lastMessageDto.setLastMessage("Hello");

ContactEvent contactEvent = new ContactEvent();
contactEvent.setLastMessage(lastMessage);
contactEvent.setLastMessage(lastMessageDto);

var event = createSubscribeEvent(destination, getPrincipal(email, password));

Expand All @@ -87,7 +92,7 @@ public void handleWebSocketSubscribeListener_shouldSaveEvent() {
assertThat(capturedEvent.getEmail()).isEqualTo(email);
assertThat(capturedEvent.getTimestamp()).isInstanceOfAny(LocalDateTime.class);
assertThat(capturedEvent.getEventType()).isEqualTo(SUBSCRIBED);
assertThat(capturedEvent.getLastMessage()).isEqualTo(lastMessage);
assertThat(capturedEvent.getLastMessage()).isEqualTo(lastMessageDto);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.chat.yourway.dto.response.LastMessageResponseDto;
import com.chat.yourway.model.event.ContactEvent;
import com.chat.yourway.model.event.EventType;
import com.chat.yourway.repository.ContactEventRedisRepository;
Expand Down Expand Up @@ -40,7 +41,7 @@ void save_shouldSaveToRepository() {
// Given
String email = "[email protected]";
int topicId = 1;
ContactEvent contactEvent = new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), "");
ContactEvent contactEvent = new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), null);

// When
contactEventService.save(contactEvent);
Expand All @@ -55,8 +56,7 @@ void getByTopicIdAndEmail_shouldReturnContactEventFromRepository() {
// Given
Integer topicId = 1;
String email = "[email protected]";
ContactEvent expectedContactEvent = new ContactEvent(email, topicId, ONLINE,
LocalDateTime.now(), "");
ContactEvent expectedContactEvent = new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), null);
when(contactEventRedisRepository.findById(email + "_" + topicId))
.thenReturn(Optional.of(expectedContactEvent));

Expand Down Expand Up @@ -91,8 +91,8 @@ void getAllByEmail_shouldReturnContactEventsFromRepository() {
// Given
String email = "[email protected]";
List<ContactEvent> expectedContactEvents = Arrays.asList(
new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), ""),
new ContactEvent(email, 2, OFFLINE, LocalDateTime.now(), "")
new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), null),
new ContactEvent(email, 2, OFFLINE, LocalDateTime.now(), null)
);
when(contactEventRedisRepository.findAllByEmail(email)).thenReturn(expectedContactEvents);

Expand All @@ -110,8 +110,8 @@ void updateEventTypeByEmail_shouldUpdateEventTypesInRepository() {
String email = "[email protected]";
EventType newEventType = OFFLINE;
List<ContactEvent> events = Arrays.asList(
new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), ""),
new ContactEvent(email, 2, ONLINE, LocalDateTime.now(), "")
new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), null),
new ContactEvent(email, 2, ONLINE, LocalDateTime.now(), null)
);
when(contactEventRedisRepository.findAllByEmail(email)).thenReturn(events);

Expand All @@ -129,8 +129,8 @@ void getAllByTopicId_shouldReturnContactEventsFromRepository() {
// Given
Integer topicId = 1;
List<ContactEvent> expectedContactEvents = Arrays.asList(
new ContactEvent("[email protected]", topicId, ONLINE, LocalDateTime.now(), ""),
new ContactEvent("[email protected]", topicId, OFFLINE, LocalDateTime.now(), "")
new ContactEvent("[email protected]", topicId, ONLINE, LocalDateTime.now(), null),
new ContactEvent("[email protected]", topicId, OFFLINE, LocalDateTime.now(), null)
);
when(contactEventRedisRepository.findAllByTopicId(topicId)).thenReturn(expectedContactEvents);

Expand All @@ -146,18 +146,22 @@ void getAllByTopicId_shouldReturnContactEventsFromRepository() {
void setLastMessageToAllTopicSubscribers_shouldUpdateLastMessagesInRepository() {
// Given
Integer topicId = 1;
String newMessage = "New message";
var lastMessageDto = new LastMessageResponseDto();
lastMessageDto.setTimestamp(LocalDateTime.now());
lastMessageDto.setSentFrom("[email protected]");
lastMessageDto.setLastMessage("New message");

List<ContactEvent> events = Arrays.asList(
new ContactEvent("[email protected]", topicId, ONLINE, LocalDateTime.now(), ""),
new ContactEvent("[email protected]", topicId, OFFLINE, LocalDateTime.now(), "")
new ContactEvent("[email protected]", topicId, ONLINE, LocalDateTime.now(), null),
new ContactEvent("[email protected]", topicId, OFFLINE, LocalDateTime.now(), null)
);
when(contactEventRedisRepository.findAllByTopicId(topicId)).thenReturn(events);

// When
contactEventService.setLastMessageToAllTopicSubscribers(topicId, newMessage);
contactEventService.setLastMessageToAllTopicSubscribers(topicId, lastMessageDto);

// Then
events.forEach(e -> e.setLastMessage(newMessage));
events.forEach(e -> e.setLastMessage(lastMessageDto));
verify(contactEventRedisRepository, times(1)).saveAll(events);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.chat.yourway.dto.response.LastMessageResponseDto;
import com.chat.yourway.dto.response.MessageNotificationResponseDto;
import com.chat.yourway.mapper.MessageNotificationMapper;
import com.chat.yourway.model.event.ContactEvent;
Expand Down Expand Up @@ -46,9 +47,14 @@ class NotificationServiceImplTest {
void notifyTopicSubscribers_shouldReturnListOfNotificationsWithUnreadMessageCounts() {
// Given
Integer topicId = 1;
var lastMessageDto = new LastMessageResponseDto();
lastMessageDto.setTimestamp(LocalDateTime.now());
lastMessageDto.setSentFrom("[email protected]");
lastMessageDto.setLastMessage("Hello");

List<ContactEvent> events = Arrays.asList(
new ContactEvent("[email protected]", topicId, ONLINE, LocalDateTime.now(), "Hello"),
new ContactEvent("[email protected]", topicId, OFFLINE, LocalDateTime.now(), "Hello")
new ContactEvent("[email protected]", topicId, ONLINE, LocalDateTime.now(), lastMessageDto),
new ContactEvent("[email protected]", topicId, OFFLINE, LocalDateTime.now(), lastMessageDto)
);

var expectedNotifications = events.stream()
Expand Down
Loading