Skip to content

Commit

Permalink
Fix bytes message read (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
juancgalvis authored Mar 27, 2024
1 parent faa1672 commit 6c018cd
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import lombok.experimental.UtilityClass;
import lombok.extern.log4j.Log4j2;

import java.nio.charset.StandardCharsets;

@Log4j2
@UtilityClass
public class Utils {
Expand All @@ -18,7 +20,10 @@ public static JmsMessage fromMessage(Message message) {
if (message instanceof TextMessage) {
body = ((TextMessage) message).getText();
} else if (message instanceof BytesMessage) {
body = ((BytesMessage) message).readUTF();
BytesMessage bytesMessage = (BytesMessage) message;
byte[] byteArray = new byte[(int) bytesMessage.getBodyLength()];
bytesMessage.readBytes(byteArray);
body = new String(byteArray, StandardCharsets.UTF_8);
} else {
log.warn("Message type not identified getting response as String");
body = message.getBody(String.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package co.com.bancolombia.commons.jms.http.replier;

import co.com.bancolombia.commons.jms.api.model.JmsMessage;
import jakarta.jms.BytesMessage;
import jakarta.jms.JMSException;
import jakarta.jms.ObjectMessage;
import jakarta.jms.TextMessage;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class UtilsTest {

@Mock
private TextMessage textMessage;
@Mock
private BytesMessage bytesMessage;
@Mock
private ObjectMessage objectMessage;

private final String expected = "message body";

@Test
void shouldGetStringMessage() throws JMSException {
// Arrange
when(textMessage.getText()).thenReturn(expected);
when(textMessage.getJMSMessageID()).thenReturn("message-id");
when(textMessage.getJMSCorrelationID()).thenReturn("correlation-id");
when(textMessage.getJMSTimestamp()).thenReturn(1L);
// Act
JmsMessage result = Utils.fromMessage(textMessage);
// Assert
assertEquals(expected, result.getBody());
assertEquals("message-id", result.getMessageID());
assertEquals("correlation-id", result.getCorrelationID());
assertEquals(1L, result.getTimestamp());
}

@Test
void shouldGetBytesMessage() throws JMSException {
// Arrange
byte[] bytes = expected.getBytes(StandardCharsets.UTF_8);
when(bytesMessage.getBodyLength()).thenReturn((long) bytes.length);
when(bytesMessage.readBytes(any())).thenAnswer(invocation -> {
byte[] bytesCall = (byte[]) invocation.getArguments()[0];
System.arraycopy(bytes, 0, bytesCall, 0, bytesCall.length);
return bytesCall.length;
});
// Act
JmsMessage result = Utils.fromMessage(bytesMessage);
// Assert
assertEquals(expected, result.getBody());
}

@Test
void shouldGetObjectMessage() throws JMSException {
// Arrange
when(objectMessage.getBody(any())).thenReturn(expected);
// Act
JmsMessage result = Utils.fromMessage(objectMessage);
// Assert
assertEquals(expected, result.getBody());
}

}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=2.2.0
version=2.2.1
springBootVersion=3.2.3
gradleVersionsVersion=0.47.0
owaspDependencyCheckVersion=9.0.9
Expand Down

0 comments on commit 6c018cd

Please sign in to comment.