Skip to content

Commit

Permalink
feat: provide service to save, overwrite and delete draft messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mebo4b committed Oct 29, 2020
1 parent e60c87a commit f8f7988
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.caritas.cob.messageservice.api.model.draftmessage;

import org.springframework.http.HttpStatus;

public enum SavedDraftType {

NEW_MESSAGE(HttpStatus.CREATED),
OVERWRITTEN_MESSAGE(HttpStatus.OK);

private final HttpStatus httpStatus;

SavedDraftType(HttpStatus httpStatus) {
this.httpStatus = httpStatus;
}

public HttpStatus getHttpStatus() {
return this.httpStatus;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.caritas.cob.messageservice.api.model.entity;
package de.caritas.cob.messageservice.api.model.draftmessage.entity;

import java.time.LocalDateTime;
import javax.persistence.Column;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package de.caritas.cob.messageservice.api.repository;

import de.caritas.cob.messageservice.api.model.entity.DraftMessage;
import de.caritas.cob.messageservice.api.model.draftmessage.entity.DraftMessage;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;

public interface DraftMessageRepository extends CrudRepository<DraftMessage, Long> {

Optional<DraftMessage> findByUserIdAndRcGroupId(String userId, String rcGroupId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package de.caritas.cob.messageservice.api.service;

import static de.caritas.cob.messageservice.api.model.draftmessage.SavedDraftType.NEW_MESSAGE;
import static de.caritas.cob.messageservice.api.model.draftmessage.SavedDraftType.OVERWRITTEN_MESSAGE;

import de.caritas.cob.messageservice.api.exception.CustomCryptoException;
import de.caritas.cob.messageservice.api.exception.InternalServerErrorException;
import de.caritas.cob.messageservice.api.helper.AuthenticatedUser;
import de.caritas.cob.messageservice.api.model.draftmessage.SavedDraftType;
import de.caritas.cob.messageservice.api.model.draftmessage.entity.DraftMessage;
import de.caritas.cob.messageservice.api.repository.DraftMessageRepository;
import java.time.LocalDateTime;
import java.util.Optional;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
* Service class to provide creation, updating and deletion of draft messages.
*/
@Service
@RequiredArgsConstructor
public class DraftMessageService {

private final @NonNull DraftMessageRepository draftMessageRepository;
private final @NonNull EncryptionService encryptionService;
private final @NonNull AuthenticatedUser authenticatedUser;

/**
* Encrypts and saves a draft message. The message will be overwritten if a message for the given
* user and rocket chat group id already exists.
*
* @param message the message to encrypt and persist
* @param rcGroupId the rocket chat group id
* @return a {@link SavedDraftType} for the created type
*/
public SavedDraftType saveDraftMessage(String message, String rcGroupId) {

Optional<DraftMessage> optionalDraftMessage = this.draftMessageRepository
.findByUserIdAndRcGroupId(this.authenticatedUser.getUserId(), rcGroupId);

DraftMessage draftMessage = optionalDraftMessage.orElse(buildNewDraftMessage(rcGroupId));
updateMessage(message, rcGroupId, draftMessage);

this.draftMessageRepository.save(draftMessage);
return extractSavedDraftType(optionalDraftMessage);
}

private SavedDraftType extractSavedDraftType(Optional<DraftMessage> optionalDraftMessage) {
return optionalDraftMessage.isPresent() ? OVERWRITTEN_MESSAGE : NEW_MESSAGE;
}

private DraftMessage buildNewDraftMessage(String rcGroupId) {
return DraftMessage.builder()
.createDate(LocalDateTime.now())
.userId(this.authenticatedUser.getUserId())
.rcGroupId(rcGroupId)
.build();
}

private void updateMessage(String message, String rcGroupId, DraftMessage draftMessage) {
try {
String encryptedMessage = this.encryptionService.encrypt(message, rcGroupId);
draftMessage.setMessage(encryptedMessage);
} catch (CustomCryptoException e) {
throw new InternalServerErrorException(e, LogService::logInternalServerError);
}
}

/**
* Deletes a draft message if exists.
*
* @param rcGroupId the rocket chat group id
*/
public void deleteDraftMessageIfExist(String rcGroupId) {
this.draftMessageRepository.findByUserIdAndRcGroupId(this.authenticatedUser.getUserId(),
rcGroupId).ifPresent(this::deleteDraftMessage);
}

private void deleteDraftMessage(DraftMessage draftMessage) {
this.draftMessageRepository.delete(draftMessage);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package de.caritas.cob.messageservice.api.service;

import static de.caritas.cob.messageservice.api.model.draftmessage.SavedDraftType.NEW_MESSAGE;
import static de.caritas.cob.messageservice.api.model.draftmessage.SavedDraftType.OVERWRITTEN_MESSAGE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import de.caritas.cob.messageservice.api.exception.CustomCryptoException;
import de.caritas.cob.messageservice.api.exception.InternalServerErrorException;
import de.caritas.cob.messageservice.api.helper.AuthenticatedUser;
import de.caritas.cob.messageservice.api.model.draftmessage.SavedDraftType;
import de.caritas.cob.messageservice.api.model.draftmessage.entity.DraftMessage;
import de.caritas.cob.messageservice.api.repository.DraftMessageRepository;
import java.util.Optional;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class DraftMessageServiceTest {

@InjectMocks
private DraftMessageService draftMessageService;

@Mock
private DraftMessageRepository draftMessageRepository;

@Mock
private AuthenticatedUser authenticatedUser;

@Mock
private EncryptionService encryptionService;

@Test
public void saveDraftMessage_Should_returnNewMessageType_When_noMessageForUserAndRcGroupExists()
throws CustomCryptoException {

SavedDraftType savedDraftType = this.draftMessageService
.saveDraftMessage("message", "rcGroupId");

assertThat(savedDraftType, is(NEW_MESSAGE));
verify(this.draftMessageRepository, times(1)).save(any());
verify(this.encryptionService, times(1)).encrypt(any(), any());
}

@Test
public void saveDraftMessage_Should_returnOverwrittenMessageType_When_messageForUserAndRcGroupExists()
throws CustomCryptoException {
when(this.draftMessageRepository.findByUserIdAndRcGroupId(any(), any()))
.thenReturn(Optional.of(new DraftMessage()));

SavedDraftType savedDraftType = this.draftMessageService
.saveDraftMessage("message", "rcGroupId");

assertThat(savedDraftType, is(OVERWRITTEN_MESSAGE));
verify(this.draftMessageRepository, times(1)).save(any());
verify(this.encryptionService, times(1)).encrypt(any(), any());
}

@Test
public void deleteDraftMessageIfExist_Should_notCallDelete_When_noMessageForUserAndGroupExists() {
this.draftMessageService.deleteDraftMessageIfExist("rcGroupId");

verify(this.draftMessageRepository, times(0)).delete(any());
}

@Test
public void deleteDraftMessageIfExist_Should_callDelete_When_messageForUserAndGroupExists() {
when(this.draftMessageRepository.findByUserIdAndRcGroupId(any(), any()))
.thenReturn(Optional.of(new DraftMessage()));

this.draftMessageService.deleteDraftMessageIfExist("rcGroupId");

verify(this.draftMessageRepository, times(1)).delete(any());
}

@Test(expected = InternalServerErrorException.class)
public void saveDraftMessage_Should_throwInternalServerError_When_encryptionServiceThrowsCustomCryptoException()
throws CustomCryptoException {
when(this.encryptionService.encrypt(any(), any()))
.thenThrow(new CustomCryptoException(new Exception()));

this.draftMessageService.saveDraftMessage("message", "rcGroupId");
}

}

0 comments on commit f8f7988

Please sign in to comment.