Skip to content

Commit

Permalink
Added last methode to mark active messages as toDelete
Browse files Browse the repository at this point in the history
  • Loading branch information
ramueSVA committed Dec 9, 2024
1 parent 4821557 commit d786161
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/highmed/numportal/domain/model/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ public class Message {

private MessageType type;

private boolean toDelete;

}
23 changes: 10 additions & 13 deletions src/main/java/org/highmed/numportal/service/MessageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,20 @@ public MessageDto updateUserMessage(Long id, MessageDto messageDto, String userI
return messageMapper.convertToDTO(savedMessage);
}

public MessageDto partialUpdateUserMessage(Long id, LocalDateTime endDate, String userId) {
userDetailsService.checkIsUserApproved(userId);
public void deleteActiveUserMessage(Long id, String userId) {
LocalDateTime now = LocalDateTime.now().minusMinutes(5);
Message messageToUpdate = messageRepository.findById(id)
userDetailsService.checkIsUserApproved(userId);
Message messageToDelete = messageRepository.findById(id)
.orElseThrow(() -> new ResourceNotFound(MessageService.class, MESSAGE_NOT_FOUND,
String.format(MESSAGE_NOT_FOUND, id)));
if (isInactiveMessage(messageToUpdate, now) && isPlannedMessage(messageToUpdate, now)) {
throw new BadRequestException(MessageService.class, CANNOT_UPDATE_MESSAGE_INVALID,
String.format(CANNOT_UPDATE_MESSAGE_INVALID, messageToUpdate, "Message is not active"));
// active messages should be marked as deleted
if (messageToDelete.getStartDate().isBefore(now) && messageToDelete.getEndDate().isAfter(now)) {
messageToDelete.setToDelete(true);
messageRepository.save(messageToDelete);
} else {
throw new BadRequestException(MessageService.class, CANNOT_DELETE_MESSAGE,
String.format(CANNOT_DELETE_MESSAGE, messageToDelete.getId()));
}
messageToUpdate.setEndDate(endDate);
Message savedMessage = messageRepository.save(messageToUpdate);
return messageMapper.convertToDTO(savedMessage);
}

public void deleteUserMessage(Long id, String userId) {
Expand All @@ -111,10 +112,6 @@ private static boolean isActiveMessage(MessageDto messageDto, Message messageToU
return messageToUpdate.getStartDate().isBefore(now) && messageDto.getEndDate().isAfter(now);
}

private static boolean isPlannedMessage(Message messageToUpdate, LocalDateTime now) {
return messageToUpdate.getStartDate().isAfter(now);
}

private void validateDates(LocalDateTime startDate, LocalDateTime endDate, LocalDateTime now) {
if (startDate.isBefore(now)) {
throw new BadRequestException(MessageService.class, CANNOT_HANDLE_DATE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ public ResponseEntity<MessageDto> updateUserMessage(
@Operation(
description = "Extend End date of a user message")
@PreAuthorize(Role.CONTENT_ADMIN)
public ResponseEntity<MessageDto> partialUpdateUserMessage(
public ResponseEntity<MessageDto> deleteActiveUserMessage(
@PathVariable("id") Long id,
@AuthenticationPrincipal @NotNull Jwt principal,
@Valid @NotNull @RequestParam LocalDateTime endDate) {
return ResponseEntity.ok(messageService.partialUpdateUserMessage(id, endDate, principal.getSubject()));
@AuthenticationPrincipal @NotNull Jwt principal) {
messageService.deleteActiveUserMessage(id, principal.getSubject());
return ResponseEntity.ok().build();
}

@ContextLog(type = "MessageManagement", description = "Delete a user message")
Expand Down
114 changes: 95 additions & 19 deletions src/test/java/org/highmed/numportal/service/MessageServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.highmed.numportal.domain.model.MessageType;
import org.highmed.numportal.domain.repository.MessageRepository;
import org.highmed.numportal.mapper.MessageMapper;
import org.highmed.numportal.service.exception.BadRequestException;
import org.highmed.numportal.service.exception.ResourceNotFound;

import org.junit.Assert;
Expand All @@ -19,7 +20,6 @@
import java.time.LocalDateTime;
import java.util.Optional;

import static org.highmed.numportal.domain.templates.ExceptionsTemplate.MESSAGE_NOT_FOUND;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -40,34 +40,46 @@ public class MessageServiceTest {
private MessageDto messageDto;
private MessageDto updateMessageDto;
private Message message;
private Message messageToEdit;
private Message messageToEditPlanned;
private Message messageToEditActive;

@Before
public void setup() {
messageDto = MessageDto.builder()
.title("Other title")
.text("Hier koennte deine Nachricht stehen")
.endDate(LocalDateTime.MAX)
.type(MessageType.INFO).build();
message = Message.builder()
.title("Other title")
.text("Hier koennte deine Nachricht stehen")
.endDate(LocalDateTime.MAX)
.type(MessageType.INFO).build();

updateMessageDto = MessageDto.builder()
.title("Neue Serverzeiten")
.text("Serverzeit: 00:00 Uhr - 24:00 Uhr")
messageDto = MessageDto.builder()
.title("Other title")
.text("Hier koennte deine Nachricht stehen")
.startDate(LocalDateTime.now())
.endDate(LocalDateTime.MAX)
.type(MessageType.INFO).build();
message = Message.builder()
.title("Other title")
.text("Hier koennte deine Nachricht stehen")
.startDate(LocalDateTime.now())
.endDate(LocalDateTime.MAX)
.type(MessageType.INFO).build();

messageToEdit = Message.builder()
updateMessageDto = MessageDto.builder()
.title("Neue Serverzeiten")
.text("Serverzeit: 00:00 Uhr - 24:00 Uhr")
.startDate(LocalDateTime.now())
.endDate(LocalDateTime.MAX)
.type(MessageType.INFO).build();

messageToEditPlanned = Message.builder()
.id(4L)
.title("Neue Serverzeiten")
.text("Serverzeit: 06:00 Uhr - 23:00 Uhr")
.startDate(LocalDateTime.now().plusHours(3))
.endDate(LocalDateTime.MAX)
.type(MessageType.INFO).build();


messageToEditActive = Message.builder()
.id(3L)
.title("Neue Serverzeiten")
.text("Serverzeit: 06:00 Uhr - 23:00 Uhr")
.startDate(LocalDateTime.now().minusHours(1))
.endDate(LocalDateTime.MAX.minusHours(1))
.type(MessageType.INFO).build();
}

@Test
Expand All @@ -87,9 +99,73 @@ public void createUserMessageTest() {

@Test
public void updateUserMessageTest() {
when(messageRepository.findById(2L)).thenReturn(Optional.ofNullable(messageToEdit));
when(messageRepository.findById(2L)).thenReturn(Optional.ofNullable(messageToEditPlanned));
messageService.updateUserMessage(2L, updateMessageDto, USER_ID);
Mockito.verify(userDetailsService, Mockito.times(1)).checkIsUserApproved(USER_ID);
Mockito.verify(messageRepository, Mockito.times(1)).save(Mockito.any(Message.class));
}

@Test
public void deleteActiveUserMessageTest() {
Long messageId = 2L;
String userId = "user456";
LocalDateTime now = LocalDateTime.now().minusMinutes(5);
Message message = new Message();
message.setId(messageId);
message.setStartDate(now.minusHours(10));
message.setEndDate(now.plusMinutes(20));

when(messageRepository.findById(messageId)).thenReturn(Optional.of(message));

messageService.deleteActiveUserMessage(messageId, userId);

Mockito.verify(messageRepository, Mockito.times(1)).save(message);
}

@Test
public void deleteActiveUserMessage_BadRequestTest() {
Long messageId = 2L;
String userId = "user456";
LocalDateTime now = LocalDateTime.now().minusMinutes(5);
Message message = new Message();
message.setId(messageId);
message.setStartDate(now.minusHours(10));
message.setEndDate(now.minusMinutes(20));

when(messageRepository.findById(messageId)).thenReturn(Optional.of(message));

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


@Test
public void deleteUserMessageTest() {
Long messageId = 1L;
String userId = "user123";
LocalDateTime now = LocalDateTime.now().minusMinutes(5);
Message message = new Message();
message.setId(messageId);
message.setStartDate(now.plusMinutes(10));

when(messageRepository.findById(messageId)).thenReturn(Optional.of(message));

messageService.deleteUserMessage(messageId, userId);

Mockito.verify(messageRepository, Mockito.times(1)).deleteById(messageId);
}

@Test
public void deleteUserMessage_CannotDeleteMessageTest() {
Long messageId = 1L;
String userId = "user123";
LocalDateTime now = LocalDateTime.now().minusMinutes(5);
Message message = new Message();
message.setId(messageId);
message.setStartDate(now.minusMinutes(10));

when(messageRepository.findById(messageId)).thenReturn(Optional.of(message));

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

0 comments on commit d786161

Please sign in to comment.