forked from Onlineberatung/onlineBeratung-messageService
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide service to save, overwrite and delete draft messages
- Loading branch information
Showing
5 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/de/caritas/cob/messageservice/api/model/draftmessage/SavedDraftType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...ervice/api/model/entity/DraftMessage.java → ...del/draftmessage/entity/DraftMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
src/main/java/de/caritas/cob/messageservice/api/repository/DraftMessageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
src/main/java/de/caritas/cob/messageservice/api/service/DraftMessageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
src/test/java/de/caritas/cob/messageservice/api/service/DraftMessageServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |