Skip to content

Commit c1a8768

Browse files
authored
T2 ServiceBus - Add byte array constructor (Azure#17173)
* added byte array constructor
1 parent 6276a8c commit c1a8768

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

sdk/servicebus/azure-messaging-servicebus/src/main/java/com/azure/messaging/servicebus/ServiceBusMessage.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ public class ServiceBusMessage {
5757

5858
private Context context;
5959

60+
/**
61+
* Creates a {@link ServiceBusMessage} with given byte array body.
62+
*
63+
* @param body The content of the Service bus message.
64+
*
65+
* @throws NullPointerException if {@code body} is null.
66+
*/
67+
public ServiceBusMessage(byte[] body) {
68+
this(BinaryData.fromBytes(Objects.requireNonNull(body, "'body' cannot be null.")));
69+
}
70+
6071
/**
6172
* Creates a {@link ServiceBusMessage} with a {@link java.nio.charset.StandardCharsets#UTF_8 UTF_8} encoded body.
6273
*
@@ -80,6 +91,7 @@ public ServiceBusMessage(String body) {
8091
* @see BinaryData
8192
*/
8293
public ServiceBusMessage(BinaryData body) {
94+
Objects.requireNonNull(body, "'body' cannot be null.");
8395
this.context = Context.NONE;
8496
this.amqpAnnotatedMessage = new AmqpAnnotatedMessage(
8597
new AmqpDataBody(Collections.singletonList(body.toBytes())));

sdk/servicebus/azure-messaging-servicebus/src/main/java/com/azure/messaging/servicebus/ServiceBusReceivedMessage.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Collections;
2929
import java.util.Date;
3030
import java.util.Map;
31+
import java.util.Objects;
3132
import java.util.UUID;
3233

3334
/**
@@ -52,6 +53,7 @@ public AmqpAnnotatedMessage getAmqpAnnotatedMessage() {
5253
}
5354

5455
ServiceBusReceivedMessage(BinaryData body) {
56+
Objects.requireNonNull(body, "'body' cannot be null.");
5557
amqpAnnotatedMessage = new AmqpAnnotatedMessage(new AmqpDataBody(Collections.singletonList(body.toBytes())));
5658
}
5759

@@ -84,6 +86,15 @@ public BinaryData getBody() {
8486
}
8587
}
8688

89+
/**
90+
* Gets the actual payload/data wrapped by the {@link ServiceBusReceivedMessage}.
91+
*
92+
* @return A byte array representing the data.
93+
*/
94+
public byte[] getBodyAsBytes() {
95+
return getBody().toBytes();
96+
}
97+
8798
/**
8899
* Gets the content type of the message.
89100
*

sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/ServiceBusMessageTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,29 @@ void bodyAsString() {
172172
assertArrayEquals(encoded, message.getBody().toBytes());
173173
}
174174

175+
/**
176+
* Verify body is created.
177+
*/
178+
@Test
179+
void bodyAsBytes() {
180+
// Arrange
181+
byte[] expected = "some-contents".getBytes(UTF_8);
182+
183+
// Act
184+
ServiceBusMessage message = new ServiceBusMessage(expected);
185+
186+
// Assert
187+
assertArrayEquals(expected, message.getBody().toBytes());
188+
}
189+
175190
/**
176191
* Verify that expected exceptions are thrown.
177192
*/
178193
@Test
179194
void bodyNotNull() {
180195
assertThrows(NullPointerException.class, () -> new ServiceBusMessage((String) null));
181196
assertThrows(NullPointerException.class, () -> new ServiceBusMessage((BinaryData) null));
197+
assertThrows(NullPointerException.class, () -> new ServiceBusMessage((byte[]) null));
182198
}
183199

184200
@Test

sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/ServiceBusReceivedMessageTest.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import static com.azure.core.amqp.AmqpMessageConstant.LOCKED_UNTIL_KEY_ANNOTATION_NAME;
2424
import static com.azure.core.amqp.AmqpMessageConstant.SEQUENCE_NUMBER_ANNOTATION_NAME;
2525
import static java.nio.charset.StandardCharsets.UTF_8;
26+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2627
import static org.junit.jupiter.api.Assertions.assertEquals;
2728
import static org.junit.jupiter.api.Assertions.assertNotNull;
2829
import static org.junit.jupiter.api.Assertions.assertNull;
29-
import static org.junit.jupiter.api.Assertions.assertThrows;
3030
import static org.mockito.Mockito.mock;
3131
import static org.mockito.Mockito.when;
3232

@@ -37,11 +37,6 @@ public class ServiceBusReceivedMessageTest {
3737
private static final byte[] PAYLOAD_BYTES = PAYLOAD.getBytes(UTF_8);
3838
private static final BinaryData PAYLOAD_BINARY = BinaryData.fromString(PAYLOAD);
3939

40-
@Test
41-
public void byteArrayNotNull() {
42-
assertThrows(NullPointerException.class, () -> new ServiceBusReceivedMessage(null));
43-
}
44-
4540
@Test
4641
public void messagePropertiesShouldNotBeNull() {
4742
// Act
@@ -52,7 +47,6 @@ public void messagePropertiesShouldNotBeNull() {
5247
assertNotNull(receivedMessage.getApplicationProperties());
5348
}
5449

55-
5650
/**
5751
* Verify that we can create an Message with an empty byte array.
5852
*/
@@ -74,7 +68,7 @@ public void canCreateWithEmptyArray() {
7468
* Verify that we can create an Message with the correct body contents.
7569
*/
7670
@Test
77-
public void canCreateWithBytePayload() {
71+
public void canCreateWithBinaryDataPayload() {
7872
// Act
7973
final ServiceBusReceivedMessage serviceBusMessageData = new ServiceBusReceivedMessage(PAYLOAD_BINARY);
8074

@@ -83,6 +77,19 @@ public void canCreateWithBytePayload() {
8377
assertEquals(PAYLOAD, serviceBusMessageData.getBody().toString());
8478
}
8579

80+
/**
81+
* Verify that we can create an Message with the correct byte array contents.
82+
*/
83+
@Test
84+
public void canCreateWithByteArrayPayload() {
85+
// Act
86+
final ServiceBusReceivedMessage serviceBusMessageData = new ServiceBusReceivedMessage(PAYLOAD_BINARY);
87+
88+
// Assert
89+
assertNotNull(serviceBusMessageData.getBody());
90+
assertArrayEquals(PAYLOAD_BYTES, serviceBusMessageData.getBodyAsBytes());
91+
}
92+
8693
@Test
8794
public void toServiceBusMessageTest() {
8895
//Arrange

0 commit comments

Comments
 (0)