Skip to content

Commit

Permalink
Remove the unnecessary DeltaExtras constructor on MessageExtras.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quintin Willison committed Jun 12, 2020
1 parent 99e3407 commit d6ff228
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 78 deletions.
7 changes: 0 additions & 7 deletions lib/src/main/java/io/ably/lib/types/MessageExtras.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ public MessageExtras(final JsonObject jsonObject) {
this(jsonObject, null);
}

/**
* @since 1.2.0
*/
public MessageExtras(final DeltaExtras delta) {
this(Serializer.wrapDelta(delta), delta);
}

private MessageExtras(final JsonObject jsonObject, final DeltaExtras delta) {
if (null == jsonObject) {
throw new NullPointerException("jsonObject cannot be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.Objects;

import com.google.gson.JsonObject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
Expand Down Expand Up @@ -133,19 +134,27 @@ public ITransport getTransport(ITransport.TransportParams transportParams, Conne
* Special transport class that corrupts the order bookkeeping of delta messages to allow testing delta recovery.
*/
private static class OutOfOrderDeltasWebsocketTransportMock extends WebSocketTransport {

private static final String DELTA = "delta";

private OutOfOrderDeltasWebsocketTransportMock(TransportParams transportParams, ConnectionManager connectionManager) {
super(transportParams, connectionManager);
}

@Override
protected void preProcessReceivedMessage(ProtocolMessage message) {
if(message.action == ProtocolMessage.Action.message &&
message.messages[0].extras != null &&
message.messages[0].extras.getDelta() != null) {
final String format = message.messages[0].extras.getDelta().getFormat();
message.messages[0].extras = new MessageExtras(new DeltaExtras(format, ""));
protected void preProcessReceivedMessage(ProtocolMessage protocolMessage) {
if(protocolMessage.action == ProtocolMessage.Action.message) {
for (final Message message : protocolMessage.messages) {
final MessageExtras extras = message.extras;
if (extras != null) {
final JsonObject json = message.extras.asJsonObject();

if (json.has(DELTA)) {
// This MessageExtras (json) has DeltaExtras.
// Corrupt it by replacing the value at the from key with an empty string.
json.getAsJsonObject(DELTA).addProperty("from", "");
}
}
}
}
}
}
Expand Down
64 changes: 0 additions & 64 deletions lib/src/test/java/io/ably/lib/types/MessageExtrasTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,68 +62,4 @@ public void rawViaMessagePack() throws IOException {
public void rawNullArgument() {
new MessageExtras((JsonObject)null);
}

/**
* Construct an instance with DeltaExtras and validate that the
* serialised JSON is as expected. Also validate that the DeltaExtras
* retrieved is the same.
*/
@Test
public void delta() {
final DeltaExtras deltaExtrasA = new DeltaExtras("someFormat", "someSource");
final DeltaExtras deltaExtrasB = new DeltaExtras("someFormat", "someOtherSource");

final MessageExtras messageExtras = new MessageExtras(deltaExtrasA);
assertEquals(deltaExtrasA, messageExtras.getDelta());
assertNotEquals(deltaExtrasB, messageExtras.getDelta());
assertNotEquals(deltaExtrasB, deltaExtrasA);

final JsonObject expectedJsonElement = deltaExtrasJsonObject("someFormat", "someSource");

final MessageExtras.Serializer serializer = new MessageExtras.Serializer();
final JsonElement serialised = serializer.serialize(messageExtras, null, null);

assertEquals(expectedJsonElement, serialised);
}

/**
* Construct an instance with DeltaExtras and validate that it can be encoded
* to MessagePack and then decoded back again from MessagePack.
*/
@Test
public void deltaViaMessagePack() throws IOException {
final DeltaExtras deltaExtras = new DeltaExtras("tamrof", "morf");
final MessageExtras messageExtras = new MessageExtras(deltaExtras);
final JsonObject expectedMessageExtrasJsonObject = deltaExtrasJsonObject("tamrof", "morf");
assertEquals(expectedMessageExtrasJsonObject, messageExtras.asJsonObject());

// Encode to MessagePack
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final MessagePacker packer = Serialisation.msgpackPackerConfig.newPacker(out);
messageExtras.write(packer);
packer.flush();

// Decode from MessagePack
System.out.println("len: " + out.toByteArray().length);
MessageUnpacker unpacker = Serialisation.msgpackUnpackerConfig.newUnpacker(out.toByteArray());
final MessageExtras unpacked = MessageExtras.read(unpacker);

assertEquals(messageExtras.getDelta(), unpacked.getDelta());
assertEquals(messageExtras, unpacked);
assertEquals(expectedMessageExtrasJsonObject, unpacked.asJsonObject());
}

@Test(expected = NullPointerException.class)
public void deltaNullArgument() {
new MessageExtras((DeltaExtras)null);
}

private static JsonObject deltaExtrasJsonObject(final String format, final String from) {
final JsonObject deltaExtrasJsonElement = new JsonObject();
deltaExtrasJsonElement.addProperty("format", format);
deltaExtrasJsonElement.addProperty("from", from);
final JsonObject jsonElement = new JsonObject();
jsonElement.add("delta", deltaExtrasJsonElement);
return jsonElement;
}
}

0 comments on commit d6ff228

Please sign in to comment.