Skip to content

Commit

Permalink
T2 ServiceBus - Add byte array constructor (#17173)
Browse files Browse the repository at this point in the history
* added byte array constructor
  • Loading branch information
hemanttanwar authored Nov 4, 2020
1 parent 6276a8c commit c1a8768
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public class ServiceBusMessage {

private Context context;

/**
* 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.")));
}

/**
* 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,6 +53,7 @@ public AmqpAnnotatedMessage getAmqpAnnotatedMessage() {
}

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

Expand Down Expand Up @@ -84,6 +86,15 @@ public BinaryData getBody() {
}
}

/**
* Gets the actual payload/data wrapped by the {@link ServiceBusReceivedMessage}.
*
* @return A byte array representing the data.
*/
public byte[] getBodyAsBytes() {
return getBody().toBytes();
}

/**
* Gets the content type of the message.
*
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 @@ -23,10 +23,10 @@
import static com.azure.core.amqp.AmqpMessageConstant.LOCKED_UNTIL_KEY_ANNOTATION_NAME;
import static com.azure.core.amqp.AmqpMessageConstant.SEQUENCE_NUMBER_ANNOTATION_NAME;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

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

@Test
public void byteArrayNotNull() {
assertThrows(NullPointerException.class, () -> new ServiceBusReceivedMessage(null));
}

@Test
public void messagePropertiesShouldNotBeNull() {
// Act
Expand All @@ -52,7 +47,6 @@ public void messagePropertiesShouldNotBeNull() {
assertNotNull(receivedMessage.getApplicationProperties());
}


/**
* Verify that we can create an Message with an empty byte array.
*/
Expand All @@ -74,7 +68,7 @@ public void canCreateWithEmptyArray() {
* Verify that we can create an Message with the correct body contents.
*/
@Test
public void canCreateWithBytePayload() {
public void canCreateWithBinaryDataPayload() {
// Act
final ServiceBusReceivedMessage serviceBusMessageData = new ServiceBusReceivedMessage(PAYLOAD_BINARY);

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

/**
* Verify that we can create an Message with the correct byte array contents.
*/
@Test
public void canCreateWithByteArrayPayload() {
// Act
final ServiceBusReceivedMessage serviceBusMessageData = new ServiceBusReceivedMessage(PAYLOAD_BINARY);

// Assert
assertNotNull(serviceBusMessageData.getBody());
assertArrayEquals(PAYLOAD_BYTES, serviceBusMessageData.getBodyAsBytes());
}

@Test
public void toServiceBusMessageTest() {
//Arrange
Expand Down

0 comments on commit c1a8768

Please sign in to comment.