Skip to content

Commit

Permalink
feat: provide controller endpoint to save draft messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mebo4b committed Oct 29, 2020
1 parent f8f7988 commit 2668490
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import de.caritas.cob.messageservice.api.model.MasterKeyDTO;
import de.caritas.cob.messageservice.api.model.MessageDTO;
import de.caritas.cob.messageservice.api.model.MessageStreamDTO;
import de.caritas.cob.messageservice.api.model.draftmessage.SavedDraftType;
import de.caritas.cob.messageservice.api.service.DraftMessageService;
import de.caritas.cob.messageservice.api.service.EncryptionService;
import de.caritas.cob.messageservice.api.service.LogService;
import de.caritas.cob.messageservice.api.service.RocketChatService;
Expand All @@ -23,19 +25,20 @@
import org.springframework.web.bind.annotation.RestController;

/**
* Controller for message requests
* Controller for message requests.
*/
@RestController
@RequiredArgsConstructor
@Api(tags = "message-controller")
public class MessageController implements MessagesApi {

private @NonNull RocketChatService rocketChatService;
private @NonNull EncryptionService encryptionService;
private @NonNull PostGroupMessageFacade postGroupMessageFacade;
private final @NonNull RocketChatService rocketChatService;
private final @NonNull EncryptionService encryptionService;
private final @NonNull PostGroupMessageFacade postGroupMessageFacade;
private final @NonNull DraftMessageService draftMessageService;

/**
* Returns a list of {@link MessageStreamDTO}s from the specified Rocket.Chat group
* Returns a list of {@link MessageStreamDTO}s from the specified Rocket.Chat group.
*/
@Override
public ResponseEntity<MessageStreamDTO> getMessageStream(@RequestHeader String rcToken,
Expand All @@ -50,10 +53,7 @@ public ResponseEntity<MessageStreamDTO> getMessageStream(@RequestHeader String r
}

/**
* Upates the Master-Key Fragment for the en-/decryption of messages
*
* @param masterKey
* @return
* Upates the Master-Key Fragment for the en-/decryption of messages.
*/
@Override
public ResponseEntity<Void> updateKey(@Valid @RequestBody MasterKeyDTO masterKey) {
Expand All @@ -68,7 +68,7 @@ public ResponseEntity<Void> updateKey(@Valid @RequestBody MasterKeyDTO masterKey
}

/**
* Posts a message in the specified Rocket.Chat group
* Posts a message in the specified Rocket.Chat group.
*/
@Override
public ResponseEntity<Void> createMessage(@RequestHeader String rcToken,
Expand Down Expand Up @@ -102,7 +102,7 @@ public ResponseEntity<Void> forwardMessage(@RequestHeader String rcToken,
}

/**
* Posts a message in the specified Feedback Rocket.Chat group
* Posts a message in the specified Feedback Rocket.Chat group.
*/
@Override
public ResponseEntity<Void> createFeedbackMessage(@RequestHeader String rcToken,
Expand All @@ -114,4 +114,14 @@ public ResponseEntity<Void> createFeedbackMessage(@RequestHeader String rcToken,

return new ResponseEntity<>(HttpStatus.CREATED);
}

/**
* Saves a draft message identified by current authenticated user and Rocket.Chat group.
*/
@Override
public ResponseEntity<Void> saveDraftMessage(@RequestHeader String rcGroupId,
@Valid @RequestBody String message) {
SavedDraftType savedDraftType = this.draftMessageService.saveDraftMessage(message, rcGroupId);
return new ResponseEntity<>(savedDraftType.getHttpStatus());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.caritas.cob.messageservice.api.controller;

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 de.caritas.cob.messageservice.testhelper.TestConstants.DONT_SEND_NOTIFICATION;
import static de.caritas.cob.messageservice.testhelper.TestConstants.MESSAGE;
import static de.caritas.cob.messageservice.testhelper.TestConstants.RC_ATTACHMENT_DESCRIPTION;
Expand Down Expand Up @@ -42,6 +44,7 @@
import de.caritas.cob.messageservice.api.model.MessageStreamDTO;
import de.caritas.cob.messageservice.api.model.rocket.chat.message.MessagesDTO;
import de.caritas.cob.messageservice.api.model.rocket.chat.message.UserDTO;
import de.caritas.cob.messageservice.api.service.DraftMessageService;
import de.caritas.cob.messageservice.api.service.EncryptionService;
import de.caritas.cob.messageservice.api.service.LogService;
import de.caritas.cob.messageservice.api.service.RocketChatService;
Expand Down Expand Up @@ -91,6 +94,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 QUERY_PARAM_OFFSET = "offset";
private final String QUERY_PARAM_COUNT = "count";
private final String QUERY_PARAM_RC_USER_ID = "rcUserId";
Expand All @@ -114,6 +118,9 @@ RC_TIMESTAMP, new UserDTO(RC_USER_ID, "test", "name"), false, new String[0], new
@MockBean
private PostGroupMessageFacade postGroupMessageFacade;

@MockBean
private DraftMessageService draftMessageService;

@Mock
private Logger logger;

Expand Down Expand Up @@ -390,4 +397,49 @@ public void createMessage_Should_LogInternalServerError_When_InternalServerError

verify(logger, atLeastOnce()).error(eq("{}{}"), eq("Internal Server Error: "), anyString());
}

@Test
public void saveDraftMessage_Should_returnBadRequest_When_rcGroupIdAndMessageIsMissing() throws Exception {
mvc.perform(post(PATH_POST_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)
.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)
.header(QUERY_PARAM_RC_GROUP_ID, RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());
}

@Test
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)
.content("message")
.header(QUERY_PARAM_RC_GROUP_ID, RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated());
}

@Test
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)
.content("message")
.header(QUERY_PARAM_RC_GROUP_ID, RC_GROUP_ID)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

}

0 comments on commit 2668490

Please sign in to comment.