Skip to content

Commit

Permalink
feat: provide controller endpoint to retrieve saved draft message
Browse files Browse the repository at this point in the history
  • Loading branch information
mebo4b committed Oct 30, 2020
1 parent 524d916 commit fa3820e
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.caritas.cob.messageservice.api.controller;

import static java.util.Objects.nonNull;

import de.caritas.cob.messageservice.api.facade.PostGroupMessageFacade;
import de.caritas.cob.messageservice.api.helper.JSONHelper;
import de.caritas.cob.messageservice.api.model.ForwardMessageDTO;
Expand Down Expand Up @@ -124,4 +126,14 @@ public ResponseEntity<Void> saveDraftMessage(@RequestHeader String rcGroupId,
SavedDraftType savedDraftType = this.draftMessageService.saveDraftMessage(message, rcGroupId);
return new ResponseEntity<>(savedDraftType.getHttpStatus());
}

/**
* Returnes a saved draft message if present.
*/
@Override
public ResponseEntity<String> findDraftMessage(@RequestHeader String rcGroupId) {
String draftMessage = this.draftMessageService.findAndDecryptDraftMessage(rcGroupId);
return nonNull(draftMessage) ? ResponseEntity.ok(draftMessage) :
new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ public SavedDraftType saveDraftMessage(String message, String rcGroupId) {
return extractSavedDraftType(optionalDraftMessage);
}

/**
* Searches for a draft message by the authenticated user and given rocket chat group id.
*
* @param rcGroupId the rocket chat group id
* @return an {@link Optional} of the database query result
*/
public Optional<DraftMessage> findDraftMessage(String rcGroupId) {
private Optional<DraftMessage> findDraftMessage(String rcGroupId) {
return this.draftMessageRepository
.findByUserIdAndRcGroupId(this.authenticatedUser.getUserId(), rcGroupId);
}
Expand Down Expand Up @@ -90,4 +84,24 @@ private void deleteDraftMessage(DraftMessage draftMessage) {
this.draftMessageRepository.delete(draftMessage);
}

/**
* Searches for a draft message by the authenticated user and given rocket chat group id.
*
* @param rcGroupId the rocket chat group id
* @return an {@link Optional} of the database query result
*/
public String findAndDecryptDraftMessage(String rcGroupId) {
Optional<DraftMessage> message = findDraftMessage(rcGroupId);
return message.map(draftMessage -> decryptMessage(draftMessage.getMessage(), rcGroupId))
.orElse(null);
}

private String decryptMessage(String encryptedMessage, String rcGroupId) {
try {
return this.encryptionService.decrypt(encryptedMessage, rcGroupId);
} catch (CustomCryptoException e) {
throw new InternalServerErrorException(e, LogService::logInternalServerError);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import static de.caritas.cob.messageservice.testhelper.TestConstants.RC_TOKEN;
import static de.caritas.cob.messageservice.testhelper.TestConstants.RC_USER_ID;
import static de.caritas.cob.messageservice.testhelper.TestConstants.SEND_NOTIFICATION;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -94,7 +96,7 @@ RC_TIMESTAMP, new UserDTO(RC_USER_ID, "test", "name"), false, new String[0], new
private final String PATH_CREATE_FEEDBACK_MESSAGE = "/messages/feedback/new";
private final String PATH_GET_MESSAGES = "/messages";
private final String PATH_POST_FORWARD_MESSAGE = "/messages/forward";
private final String PATH_POST_DRAFT_MESSAGE = "/messages/draft";
private final String PATH_DRAFT_MESSAGE = "/messages/draft";
private final String QUERY_PARAM_OFFSET = "offset";
private final String QUERY_PARAM_COUNT = "count";
private final String QUERY_PARAM_RC_USER_ID = "rcUserId";
Expand Down Expand Up @@ -400,21 +402,21 @@ public void createMessage_Should_LogInternalServerError_When_InternalServerError

@Test
public void saveDraftMessage_Should_returnBadRequest_When_rcGroupIdAndMessageIsMissing() throws Exception {
mvc.perform(post(PATH_POST_DRAFT_MESSAGE).contentType(MediaType.APPLICATION_JSON))
mvc.perform(post(PATH_DRAFT_MESSAGE).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());
}

@Test
public void saveDraftMessage_Should_returnBadRequest_When_rcGroupIdIsMissing() throws Exception {
mvc.perform(post(PATH_POST_DRAFT_MESSAGE)
mvc.perform(post(PATH_DRAFT_MESSAGE)
.content("message")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());
}

@Test
public void saveDraftMessage_Should_returnBadRequest_When_messageIsMissing() throws Exception {
mvc.perform(post(PATH_POST_DRAFT_MESSAGE)
mvc.perform(post(PATH_DRAFT_MESSAGE)
.header(QUERY_PARAM_RC_GROUP_ID, RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());
Expand All @@ -424,7 +426,7 @@ public void saveDraftMessage_Should_returnBadRequest_When_messageIsMissing() thr
public void saveDraftMessage_Should_returnCreated_When_messageIsNew() throws Exception {
when(this.draftMessageService.saveDraftMessage(any(), any())).thenReturn(NEW_MESSAGE);

mvc.perform(post(PATH_POST_DRAFT_MESSAGE)
mvc.perform(post(PATH_DRAFT_MESSAGE)
.content("message")
.header(QUERY_PARAM_RC_GROUP_ID, RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON))
Expand All @@ -435,11 +437,40 @@ public void saveDraftMessage_Should_returnCreated_When_messageIsNew() throws Exc
public void saveDraftMessage_Should_returnOk_When_messageIsOverwritten() throws Exception {
when(this.draftMessageService.saveDraftMessage(any(), any())).thenReturn(OVERWRITTEN_MESSAGE);

mvc.perform(post(PATH_POST_DRAFT_MESSAGE)
mvc.perform(post(PATH_DRAFT_MESSAGE)
.content("message")
.header(QUERY_PARAM_RC_GROUP_ID, RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
public void findDraftMessage_Should_returnBadRequest_When_rcGroupIdIsMissing() throws Exception {
mvc.perform(get(PATH_DRAFT_MESSAGE).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());
}

@Test
public void findDraftMessage_Should_returnDraftMessage_When_messageExists() throws Exception {
when(this.draftMessageService.findAndDecryptDraftMessage(any())).thenReturn("message");

String message = mvc.perform(get(PATH_DRAFT_MESSAGE)
.header(QUERY_PARAM_RC_GROUP_ID, RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString();

assertThat(message, is("message"));
}

@Test
public void findDraftMessage_Should_returnNoContent_When_noDraftMessageExists() throws Exception {
mvc.perform(get(PATH_DRAFT_MESSAGE)
.header(QUERY_PARAM_RC_GROUP_ID, RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
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.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import de.caritas.cob.messageservice.api.exception.CustomCryptoException;
Expand Down Expand Up @@ -89,4 +91,44 @@ public void saveDraftMessage_Should_throwInternalServerError_When_encryptionServ
this.draftMessageService.saveDraftMessage("message", "rcGroupId");
}

@Test
public void findAndDecryptDraftMessage_Should_returnNull_When_noDraftMessageIsPresent() {
String draftMessage = this.draftMessageService.findAndDecryptDraftMessage("rcGroupId");

assertThat(draftMessage, nullValue());
verifyZeroInteractions(this.encryptionService);
}

@Test
public void findAndDecryptDraftMessage_Should_returnNull_When_rcGroupIdIsNull() {
String draftMessage = this.draftMessageService.findAndDecryptDraftMessage(null);

assertThat(draftMessage, nullValue());
verifyZeroInteractions(this.encryptionService);
}

@Test
public void findAndDecryptDraftMessage_Should_returnDecryptedMessage_When_draftMessageIsPresent()
throws CustomCryptoException {
DraftMessage draftMessage = DraftMessage.builder().message("encrypted").build();
when(this.draftMessageRepository.findByUserIdAndRcGroupId(any(), any()))
.thenReturn(Optional.of(draftMessage));
when(this.encryptionService.decrypt(any(), any())).thenReturn("decrypted");

String message = this.draftMessageService.findAndDecryptDraftMessage("rcGroupId");

assertThat(message, is("decrypted"));
verify(this.encryptionService, times(1)).decrypt("encrypted", "rcGroupId");
}

@Test(expected = InternalServerErrorException.class)
public void findAndDecryptDraftMessage_Should_throwInternalServerError_When_encryptionServiceThrowsCustomCryptoException()
throws CustomCryptoException {
when(this.draftMessageRepository.findByUserIdAndRcGroupId(any(), any()))
.thenReturn(Optional.of(new DraftMessage()));
when(this.encryptionService.decrypt(any(), any())).thenThrow(new CustomCryptoException(new Exception()));

this.draftMessageService.findAndDecryptDraftMessage("rcGroupId");
}

}

0 comments on commit fa3820e

Please sign in to comment.