Search in sources :

Example 6 with BytesMessage

use of jakarta.jms.BytesMessage in project spring-framework by spring-projects.

the class MappingJackson2MessageConverterTests method fromBytesMessage.

@Test
void fromBytesMessage() throws Exception {
    BytesMessage bytesMessageMock = mock(BytesMessage.class);
    Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar");
    byte[] bytes = "{\"foo\":\"bar\"}".getBytes();
    final ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
    given(bytesMessageMock.getStringProperty("__typeid__")).willReturn(Object.class.getName());
    given(bytesMessageMock.propertyExists("__encoding__")).willReturn(false);
    given(bytesMessageMock.getBodyLength()).willReturn(Long.valueOf(bytes.length));
    given(bytesMessageMock.readBytes(any(byte[].class))).willAnswer((Answer<Integer>) invocation -> byteStream.read((byte[]) invocation.getArguments()[0]));
    Object result = converter.fromMessage(bytesMessageMock);
    assertThat(unmarshalled).as("Invalid result").isEqualTo(result);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) JsonView(com.fasterxml.jackson.annotation.JsonView) BytesMessage(jakarta.jms.BytesMessage) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JMSException(jakarta.jms.JMSException) HashMap(java.util.HashMap) Session(jakarta.jms.Session) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Answer(org.mockito.stubbing.Answer) ByteArrayInputStream(java.io.ByteArrayInputStream) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) TextMessage(jakarta.jms.TextMessage) MethodParameter(org.springframework.core.MethodParameter) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Method(java.lang.reflect.Method) Collections(java.util.Collections) ArgumentMatchers.isA(org.mockito.ArgumentMatchers.isA) Mockito.mock(org.mockito.Mockito.mock) ByteArrayInputStream(java.io.ByteArrayInputStream) BytesMessage(jakarta.jms.BytesMessage) Test(org.junit.jupiter.api.Test)

Example 7 with BytesMessage

use of jakarta.jms.BytesMessage in project spring-framework by spring-projects.

the class MappingJackson2MessageConverterTests method toBytesMessage.

@Test
void toBytesMessage() throws Exception {
    BytesMessage bytesMessageMock = mock(BytesMessage.class);
    Date toBeMarshalled = new Date();
    given(sessionMock.createBytesMessage()).willReturn(bytesMessageMock);
    converter.toMessage(toBeMarshalled, sessionMock);
    verify(bytesMessageMock).setStringProperty("__encoding__", "UTF-8");
    verify(bytesMessageMock).setStringProperty("__typeid__", Date.class.getName());
    verify(bytesMessageMock).writeBytes(isA(byte[].class));
}
Also used : BytesMessage(jakarta.jms.BytesMessage) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

BytesMessage (jakarta.jms.BytesMessage)7 Test (org.junit.jupiter.api.Test)6 Session (jakarta.jms.Session)3 TextMessage (jakarta.jms.TextMessage)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 JMSException (jakarta.jms.JMSException)2 Message (jakarta.jms.Message)2 ObjectMessage (jakarta.jms.ObjectMessage)2 Date (java.util.Date)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 BDDMockito.given (org.mockito.BDDMockito.given)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.verify (org.mockito.Mockito.verify)2 Answer (org.mockito.stubbing.Answer)2 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)2 JsonView (com.fasterxml.jackson.annotation.JsonView)1 InvalidDestinationException (jakarta.jms.InvalidDestinationException)1 MapMessage (jakarta.jms.MapMessage)1 MessageProducer (jakarta.jms.MessageProducer)1