Skip to content

Commit

Permalink
Message Read methos tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ramueSVA committed Dec 11, 2024
1 parent d013d56 commit 0480c31
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/highmed/numportal/domain/model/Message.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.highmed.numportal.domain.model;

import org.highmed.numportal.domain.model.admin.User;
import org.highmed.numportal.domain.model.admin.UserDetails;

import jakarta.persistence.Entity;
Expand All @@ -18,6 +17,7 @@

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
Expand Down Expand Up @@ -51,6 +51,6 @@ public class Message implements Serializable {
name = "read_message_by_users",
joinColumns = @JoinColumn(name = "message_id"),
inverseJoinColumns = @JoinColumn(name = "user_details_id"))
private List<UserDetails> readByUsers;
private List<UserDetails> readByUsers = new ArrayList<>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.highmed.numportal.domain.dto.MessageDto;
import org.highmed.numportal.domain.model.Message;
import org.highmed.numportal.domain.model.MessageType;
import org.highmed.numportal.domain.model.admin.UserDetails;
import org.highmed.numportal.domain.repository.MessageRepository;
import org.highmed.numportal.integrationtesting.security.WithMockNumUser;

Expand Down Expand Up @@ -166,4 +167,19 @@ public void noAccessApiWithWrongRole() {
mockMvc.perform(delete(MESSAGE_PATH + "/{id}", 3).with(csrf())).andExpect(status().isForbidden());
}

@Test
@SneakyThrows
public void markUserMessageAsReadTest(){
mockMvc.perform(post(MESSAGE_PATH + "/read/{id}", 2).with(csrf()))
.andExpect(status().isNoContent());
}

@Test
@SneakyThrows
public void getAllDisplayedUserMessagesTest(){
MvcResult result = mockMvc.perform(get(MESSAGE_PATH + "/read").with(csrf()))
.andExpect(status().isOk()).andReturn();
List<UserDetails> readUserMessageList = mapper.readValue(result.getResponse().getContentAsString(), List.class);
Assert.assertEquals(1, readUserMessageList.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import org.highmed.numportal.domain.dto.MessageDto;
import org.highmed.numportal.domain.model.Message;
import org.highmed.numportal.domain.model.MessageType;
import org.highmed.numportal.domain.model.Organization;
import org.highmed.numportal.domain.model.admin.UserDetails;
import org.highmed.numportal.domain.repository.MessageRepository;
import org.highmed.numportal.domain.repository.UserDetailsRepository;
import org.highmed.numportal.mapper.MessageMapper;
import org.highmed.numportal.service.exception.BadRequestException;

Expand All @@ -17,6 +20,7 @@
import org.mockito.junit.MockitoJUnitRunner;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Optional;

import static org.mockito.Mockito.when;
Expand All @@ -29,6 +33,8 @@ public class MessageServiceTest {
@Mock
private MessageRepository messageRepository;
@Mock
private UserDetailsRepository userDetailsRepository;
@Mock
private MessageMapper messageMapper;
@Mock
private UserDetailsService userDetailsService;
Expand All @@ -54,7 +60,8 @@ public void setup() {
.text("Hier koennte deine Nachricht stehen")
.startDate(LocalDateTime.now())
.endDate(LocalDateTime.MAX)
.type(MessageType.INFO).build();
.type(MessageType.INFO)
.readByUsers(new ArrayList<>()).build();

updateMessageDto = MessageDto.builder()
.title("Neue Serverzeiten")
Expand Down Expand Up @@ -157,5 +164,26 @@ public void deleteUserMessage_CannotDeleteMessageTest() {

Assert.assertThrows(BadRequestException.class, () -> messageService.deleteUserMessage(messageId, userId));
}

@Test
public void markMessageAsReadTest() {
Message readMessage = Message.builder()
.title("Other title")
.text("Hier koennte deine Nachricht stehen")
.startDate(message.getStartDate())
.endDate(LocalDateTime.MAX)
.type(MessageType.INFO)
.readByUsers(new ArrayList<>()).build();
UserDetails userDetails = new UserDetails();
readMessage.getReadByUsers().add(userDetails);
when(messageRepository.findById(1L)).thenReturn(Optional.ofNullable(message));
when(userDetailsRepository.findByUserId(USER_ID)).thenReturn(Optional.of(userDetails));
when(messageRepository.save(readMessage)).thenReturn(readMessage);
messageService.markUserMessageAsRead(1L, USER_ID);

Mockito.verify(messageRepository, Mockito.times(1)).findById(1L);
Mockito.verify(userDetailsRepository, Mockito.times(1)).findByUserId(USER_ID);
Mockito.verify(messageRepository, Mockito.times(1)).save(readMessage);
}
}

0 comments on commit 0480c31

Please sign in to comment.