Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T2 ServiceBus - Add byte array constructor #17173

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public class ServiceBusMessage {

private Context context;

/**
hemanttanwar marked this conversation as resolved.
Show resolved Hide resolved
* Creates a {@link ServiceBusMessage} with given byte array body.
*
* @param body The content of the Service bus message.
*
* @throws NullPointerException if {@code body} is null.
*/
public ServiceBusMessage(byte[] body) {
this(BinaryData.fromBytes(Objects.requireNonNull(body, "'body' cannot be null.")));
hemanttanwar marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Creates a {@link ServiceBusMessage} with a {@link java.nio.charset.StandardCharsets#UTF_8 UTF_8} encoded body.
*
Expand All @@ -80,6 +91,7 @@ public ServiceBusMessage(String body) {
* @see BinaryData
*/
public ServiceBusMessage(BinaryData body) {
Objects.requireNonNull(body, "'body' cannot be null.");
this.context = Context.NONE;
this.amqpAnnotatedMessage = new AmqpAnnotatedMessage(
new AmqpDataBody(Collections.singletonList(body.toBytes())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
Expand All @@ -52,9 +53,14 @@ public AmqpAnnotatedMessage getAmqpAnnotatedMessage() {
}

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

ServiceBusReceivedMessage(byte[] body) {
this(BinaryData.fromBytes(Objects.requireNonNull(body, "'body' cannot be null.")));
}
hemanttanwar marked this conversation as resolved.
Show resolved Hide resolved

/**
* Gets the actual payload/data wrapped by the {@link ServiceBusReceivedMessage}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,29 @@ void bodyAsString() {
assertArrayEquals(encoded, message.getBody().toBytes());
}

/**
* Verify body is created.
*/
@Test
void bodyAsBytes() {
// Arrange
byte[] expected = "some-contents".getBytes(UTF_8);

// Act
ServiceBusMessage message = new ServiceBusMessage(expected);

// Assert
assertArrayEquals(expected, message.getBody().toBytes());
}

/**
* Verify that expected exceptions are thrown.
*/
@Test
void bodyNotNull() {
assertThrows(NullPointerException.class, () -> new ServiceBusMessage((String) null));
assertThrows(NullPointerException.class, () -> new ServiceBusMessage((BinaryData) null));
assertThrows(NullPointerException.class, () -> new ServiceBusMessage((byte[]) null));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class ServiceBusReceivedMessageTest {

@Test
public void byteArrayNotNull() {
assertThrows(NullPointerException.class, () -> new ServiceBusReceivedMessage(null));
final byte[] data = null;
assertThrows(NullPointerException.class, () -> new ServiceBusReceivedMessage(data));
}

@Test
Expand Down