Skip to content

Commit

Permalink
feat: provide new endpoint for hinttexts, extend alias object
Browse files Browse the repository at this point in the history
  • Loading branch information
mebo4b committed Jan 21, 2021
1 parent afccab6 commit b132691
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 94 deletions.
46 changes: 43 additions & 3 deletions api/messageservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,43 @@ paths:
- Bearer: []
x-codegen-request-body-name: Message

/messages/hinttext/new:
post:
tags:
- message-controller
summary: 'Add new Rocket.Chat hint text message chat by given eventType [Authorization:
Role: user, consultant,]'
operationId: createHintTextMessage
parameters:
- name: RCRoomId
in: header
required: true
schema:
type: string
- name: eventType
in: header
required: true
schema:
type: string
- name: username
in: header
required: true
schema:
type: string
responses:
201:
description: CREATED - hint message was successfully created
400:
description: BAD REQUEST - invalid/incomplete request or body object
401:
description: UNAUTHORIZED - no/invalid Keycloak token
403:
description: FORBIDDEN - no/invalid role/authorization
500:
description: INTERNAL SERVER ERROR - server encountered unexpected condition
security:
- Bearer: [ ]

/messages:
get:
tags:
Expand Down Expand Up @@ -208,7 +245,7 @@ paths:
content:
'application/json':
schema:
$ref: '#/components/schemas/ForwardMessageDTO'
$ref: '#/components/schemas/AliasMessageDTO'
required: true
responses:
201:
Expand Down Expand Up @@ -350,7 +387,7 @@ components:
type: string
example: "M73fE4WhYF4peYB3s"
alias:
$ref: '#/components/schemas/ForwardMessageDTO'
$ref: '#/components/schemas/AliasMessageDTO'
rid:
type: string
example: "fR2Rz7dmWmHdXE8uz"
Expand Down Expand Up @@ -384,7 +421,7 @@ components:
file:
$ref: '#/components/schemas/FileDTO'

ForwardMessageDTO:
AliasMessageDTO:
type: object
required:
- message
Expand All @@ -405,6 +442,9 @@ components:
rcUserId:
type: string
example: "ag89h3tjkerg94t"
hinttext:
type: string
example: "Your video call has been rejected"

UserDTO:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import de.caritas.cob.messageservice.api.facade.PostGroupMessageFacade;
import de.caritas.cob.messageservice.api.helper.JSONHelper;
import de.caritas.cob.messageservice.api.model.ForwardMessageDTO;
import de.caritas.cob.messageservice.api.model.AliasMessageDTO;
import de.caritas.cob.messageservice.api.model.MasterKeyDTO;
import de.caritas.cob.messageservice.api.model.MessageDTO;
import de.caritas.cob.messageservice.api.model.MessageStreamDTO;
Expand Down Expand Up @@ -89,16 +89,16 @@ public ResponseEntity<Void> createMessage(@RequestHeader String rcToken,
@Override
public ResponseEntity<Void> forwardMessage(@RequestHeader String rcToken,
@RequestHeader String rcUserId, @RequestHeader String rcGroupId,
@Valid @RequestBody ForwardMessageDTO forwardMessageDTO) {
@Valid @RequestBody AliasMessageDTO aliasMessageDTO) {

Optional<String> alias = JSONHelper.convertForwardMessageDTOToString(forwardMessageDTO);
Optional<String> alias = JSONHelper.convertAliasMessageDTOToString(aliasMessageDTO);

if (!alias.isPresent()) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

postGroupMessageFacade.postFeedbackGroupMessage(rcToken, rcUserId,
rcGroupId, forwardMessageDTO.getMessage(), alias.get());
rcGroupId, aliasMessageDTO.getMessage(), alias.get());

return new ResponseEntity<>(HttpStatus.CREATED);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,83 +1,53 @@
package de.caritas.cob.messageservice.api.helper;

import java.io.IOException;
import java.util.Optional;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.caritas.cob.messageservice.api.model.ForwardMessageDTO;
import de.caritas.cob.messageservice.api.model.AliasMessageDTO;
import de.caritas.cob.messageservice.api.service.LogService;
import java.io.IOException;
import java.util.Optional;

/**
* Helper class for JSON specific tasks
*
* Helper class for JSON specific tasks.
*/
public class JSONHelper {

private static LogService logService;
@Autowired
private LogService autowiredLogService;

@PostConstruct
public void init() {
JSONHelper.logService = autowiredLogService;
}
private JSONHelper() {}

/**
* Converts a {@link ForwardMessageDTO} to a JSON formatted String
*
* @param forwardMessageDTO
* Converts a {@link AliasMessageDTO} into an optional of an json string.
*
* @param aliasMessageDTO the message to be converted
* @return Optional String as JSON
*/
/**
*
* @param forwardMessageDTO
* @return
*/
public static Optional<String> convertForwardMessageDTOToString(
ForwardMessageDTO forwardMessageDTO) {

ObjectMapper mapper = new ObjectMapper();
public static Optional<String> convertAliasMessageDTOToString(AliasMessageDTO aliasMessageDTO) {
try {
return Optional
.ofNullable(Helper.urlEncodeString(mapper.writeValueAsString(forwardMessageDTO)));

.ofNullable(
Helper.urlEncodeString(new ObjectMapper().writeValueAsString(aliasMessageDTO)));
} catch (JsonProcessingException jsonEx) {
logService.logInternalServerError("Could not convert ForwardMessageDTO to alias String",
LogService.logInternalServerError("Could not convert ForwardMessageDTO to alias String",
jsonEx);
return Optional.empty();
}
}

/**
* Maps a given String to a {@link ForwardMessageDTO}
*
* Maps a given String to a {@link AliasMessageDTO}.
*
* @param alias String
* @return Optional of {@link ForwardMessageDTO}
* @return Optional of {@link AliasMessageDTO}
*/
public static Optional<ForwardMessageDTO> convertStringToForwardMessageDTO(String alias) {
ObjectMapper mapper = new ObjectMapper();
public static Optional<AliasMessageDTO> convertStringToForwardMessageDTO(String alias) {
try {
return Optional
.ofNullable(mapper.readValue(Helper.urlDecodeString(alias), ForwardMessageDTO.class));
.ofNullable(
new ObjectMapper().readValue(Helper.urlDecodeString(alias), AliasMessageDTO.class));

} catch (JsonParseException jsonParseEx) {
logService.logInternalServerError("Could not convert alias String to ForwardMessageDTO",
} catch (IOException jsonParseEx) {
LogService.logInternalServerError("Could not convert alias String to ForwardMessageDTO",
jsonParseEx);
return Optional.empty();

} catch (JsonMappingException jsonMappingEx) {
logService.logInternalServerError("Could not convert alias String to ForwardMessageDTO",
jsonMappingEx);
return Optional.empty();

} catch (IOException ioEx) {
logService.logInternalServerError("Could not convert alias String to ForwardMessageDTO",
ioEx);
return Optional.empty();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package de.caritas.cob.messageservice.api.model.jsonDeserializer;
package de.caritas.cob.messageservice.api.model.jsondeserializer;

import static java.util.Objects.nonNull;

import java.io.IOException;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import de.caritas.cob.messageservice.api.helper.JSONHelper;
import de.caritas.cob.messageservice.api.helper.UserHelper;
import de.caritas.cob.messageservice.api.model.ForwardMessageDTO;
import de.caritas.cob.messageservice.api.model.AliasMessageDTO;
import java.io.IOException;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;

/**
*
* Json Deserializer for the alias
*
* Json Deserializer for the alias.
*/
public class AliasJsonDeserializer extends JsonDeserializer<ForwardMessageDTO> {
public class AliasJsonDeserializer extends JsonDeserializer<AliasMessageDTO> {

private UserHelper userHelper;
private final UserHelper userHelper;

public AliasJsonDeserializer() {
this.userHelper = new UserHelper();
Expand All @@ -29,19 +28,18 @@ public AliasJsonDeserializer(UserHelper userHelper) {
}

@Override
public ForwardMessageDTO deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
public AliasMessageDTO deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException {

String aliasValue = p.getValueAsString();
if (aliasValue == null || aliasValue.equals(StringUtils.EMPTY)) {
if (StringUtils.isBlank(aliasValue)) {
return null;
}

ForwardMessageDTO alias = null;
Optional<ForwardMessageDTO> forwardMessageDTO =
Optional<AliasMessageDTO> aliasMessageDTO =
JSONHelper.convertStringToForwardMessageDTO(aliasValue);
alias = forwardMessageDTO.isPresent() ? forwardMessageDTO.get() : null;
if (alias.getUsername() != null) {
AliasMessageDTO alias = aliasMessageDTO.orElse(null);
if (nonNull(alias) && nonNull(alias.getUsername())) {
alias.setUsername(userHelper.decodeUsername(alias.getUsername()));
}
return alias;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.caritas.cob.messageservice.api.model.jsonDeserializer;
package de.caritas.cob.messageservice.api.model.jsondeserializer;

import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import de.caritas.cob.messageservice.api.model.AliasMessageDTO;
import de.caritas.cob.messageservice.api.model.AttachmentDTO;
import de.caritas.cob.messageservice.api.model.FileDTO;
import de.caritas.cob.messageservice.api.model.ForwardMessageDTO;
import de.caritas.cob.messageservice.api.model.jsonDeserializer.AliasJsonDeserializer;
import de.caritas.cob.messageservice.api.model.jsondeserializer.AliasJsonDeserializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
Expand All @@ -32,7 +32,7 @@ public class MessagesDTO {

@ApiModelProperty(required = false, example = "%7B%0A%20%20%22message%22%3A%20", position = 1)
@JsonDeserialize(using = AliasJsonDeserializer.class)
private ForwardMessageDTO alias;
private AliasMessageDTO alias;

@ApiModelProperty(required = true, example = "fR2Rz7dmWmHdXE8uz", position = 2)
private String rid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.caritas.cob.messageservice.api.model.rocket.chat.message;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import de.caritas.cob.messageservice.api.model.jsonDeserializer.DecodeUsernameJsonDeserializer;
import de.caritas.cob.messageservice.api.model.jsondeserializer.DecodeUsernameJsonDeserializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.caritas.cob.messageservice.api.model.jsonDeserializer;
package de.caritas.cob.messageservice.api.model.jsondeserializer;

import static de.caritas.cob.messageservice.testhelper.TestConstants.MESSAGE_FORWARD_ALIAS_JSON_WITH_DECODED_USERNAME;
import static de.caritas.cob.messageservice.testhelper.TestConstants.MESSAGE_FORWARD_ALIAS_JSON_WITH_ENCODED_USERNAME;
Expand All @@ -16,7 +16,7 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.caritas.cob.messageservice.api.helper.UserHelper;
import de.caritas.cob.messageservice.api.model.ForwardMessageDTO;
import de.caritas.cob.messageservice.api.model.AliasMessageDTO;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -48,38 +48,38 @@ public void aliasJsonDeserializer_Schould_haveNoArgsConstructor() {

@Test
public void deserialize_Schould_convertAliasWithEncodedUsernameToForwardMessageDTO()
throws JsonParseException, IOException {
ForwardMessageDTO result = deserializeAlias(MESSAGE_FORWARD_ALIAS_JSON_WITH_ENCODED_USERNAME);
throws IOException {
AliasMessageDTO result = deserializeAlias(MESSAGE_FORWARD_ALIAS_JSON_WITH_ENCODED_USERNAME);
assertEquals(RC_USER_ID, result.getRcUserId());
assertEquals(TIMESTAMP, result.getTimestamp());
assertEquals(USERNAME_DECODED, result.getUsername());
}

@Test
public void deserialize_Schould_convertAliasWithDecodedUsernameToForwardMessageDTO()
throws JsonParseException, IOException {
ForwardMessageDTO result = deserializeAlias(MESSAGE_FORWARD_ALIAS_JSON_WITH_DECODED_USERNAME);
throws IOException {
AliasMessageDTO result = deserializeAlias(MESSAGE_FORWARD_ALIAS_JSON_WITH_DECODED_USERNAME);
assertEquals(RC_USER_ID, result.getRcUserId());
assertEquals(TIMESTAMP, result.getTimestamp());
assertEquals(USERNAME_DECODED, result.getUsername());
}

@Test
public void deserialize_Schould_ReturnNull_IfAliasIsEmpty()
throws JsonParseException, IOException {
ForwardMessageDTO result = deserializeAlias(MESSAGE_FORWARD_EMPTY_ALIAS_JSON);
throws IOException {
AliasMessageDTO result = deserializeAlias(MESSAGE_FORWARD_EMPTY_ALIAS_JSON);
assertNull(result);
}

@Test
public void deserialize_Schould_ReturnNull_IfAliasIsNull()
throws JsonParseException, IOException {
ForwardMessageDTO result = deserializeAlias(MESSAGE_FORWARD_NULL_ALIAS_JSON);
throws IOException {
AliasMessageDTO result = deserializeAlias(MESSAGE_FORWARD_NULL_ALIAS_JSON);
assertNull(result);
}


private ForwardMessageDTO deserializeAlias(String json) throws JsonParseException, IOException {
private AliasMessageDTO deserializeAlias(String json) throws IOException {
InputStream stream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
JsonParser jsonParser = objectMapper.getFactory().createParser(stream);
jsonParser.nextToken();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.caritas.cob.messageservice.api.model.jsonDeserializer;
package de.caritas.cob.messageservice.api.model.jsondeserializer;

import static de.caritas.cob.messageservice.testhelper.TestConstants.USERNAME_DECODED;
import static de.caritas.cob.messageservice.testhelper.TestConstants.USERNAME_ENCODED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import de.caritas.cob.messageservice.api.exception.RocketChatBadRequestException;
import de.caritas.cob.messageservice.api.exception.RocketChatPostMessageException;
import de.caritas.cob.messageservice.api.exception.RocketChatUserNotInitializedException;
import de.caritas.cob.messageservice.api.model.ForwardMessageDTO;
import de.caritas.cob.messageservice.api.model.AliasMessageDTO;
import de.caritas.cob.messageservice.api.model.MessageStreamDTO;
import de.caritas.cob.messageservice.api.model.rocket.chat.RocketChatCredentials;
import de.caritas.cob.messageservice.api.model.rocket.chat.StandardResponseDTO;
Expand Down Expand Up @@ -354,7 +354,7 @@ public void getGroupMessages_Should_UpdateToFirstMessage_When_SeveralMessagesAre
private MessagesDTO createMessagesDto(String id) {
return new MessagesDTO(
id,
new ForwardMessageDTO(),
new AliasMessageDTO(),
"rid",
"message",
"ts",
Expand Down

0 comments on commit b132691

Please sign in to comment.