use of com.canoo.dp.impl.remoting.legacy.communication.Command in project dolphin-platform by canoo.
the class TestOptimizedJsonCodec method shouldEncodeTwoCustomCodecCommands.
@Test
public void shouldEncodeTwoCustomCodecCommands() {
final Command command = createCPMCommand();
final String actual = OptimizedJsonCodec.getInstance().encode(Arrays.asList(command, command));
final String expected = createCPMCommandString();
assertThat(actual, is("[" + expected + "," + expected + "]"));
}
use of com.canoo.dp.impl.remoting.legacy.communication.Command in project dolphin-platform by canoo.
the class TestOptimizedJsonCodec method shouldDecodeValueChangedCommandWithBooleans.
@Test
public void shouldDecodeValueChangedCommandWithBooleans() {
final List<Command> commands = OptimizedJsonCodec.getInstance().decode("[{\"a_id\":\"3357S\",\"v\":false,\"id\":\"ValueChanged\"}]");
final ValueChangedCommand command = new ValueChangedCommand();
command.setNewValue(false);
command.setAttributeId("3357S");
assertThat(commands, hasSize(1));
assertThat(commands.get(0), Matchers.<Command>samePropertyValuesAs(command));
}
use of com.canoo.dp.impl.remoting.legacy.communication.Command in project dolphin-platform by canoo.
the class TestOptimizedJsonCodec method shouldDecodeValueChangedCommandWithStrings.
@Test
public void shouldDecodeValueChangedCommandWithStrings() {
final List<Command> commands = OptimizedJsonCodec.getInstance().decode("[{\"a_id\":\"3357S\",\"v\":\"Good Bye\",\"id\":\"ValueChanged\"}]");
final ValueChangedCommand command = new ValueChangedCommand();
command.setNewValue("Good Bye");
command.setAttributeId("3357S");
assertThat(commands, hasSize(1));
assertThat(commands.get(0), Matchers.<Command>samePropertyValuesAs(command));
}
use of com.canoo.dp.impl.remoting.legacy.communication.Command in project dolphin-platform by canoo.
the class TestOptimizedJsonCodec method shouldEncodeCustomCodecCommandAndStandardCodecCommand.
@Test
public void shouldEncodeCustomCodecCommandAndStandardCodecCommand() {
final Command customCodecCommand = createCPMCommand();
final Command standardCodecCommand = createCommand();
final String actual = OptimizedJsonCodec.getInstance().encode(Arrays.asList(customCodecCommand, standardCodecCommand));
final String customCodecCommandString = createCPMCommandString();
final String standardCodecCommandString = createCommandJsonString();
assertThat(actual, is("[" + customCodecCommandString + "," + standardCodecCommandString + "]"));
}
use of com.canoo.dp.impl.remoting.legacy.communication.Command in project dolphin-platform by canoo.
the class OptimizedJsonCodec method encode.
@Override
@SuppressWarnings("unchecked")
public String encode(final List<? extends Command> commands) {
Assert.requireNonNull(commands, "commands");
LOG.debug("Encoding command list with {} commands", commands.size());
final StringBuilder builder = new StringBuilder("[");
for (final Command command : commands) {
if (command == null) {
throw new IllegalArgumentException("Command list contains a null command: " + command);
} else {
LOG.trace("Encoding command of type {}", command.getClass());
final CommandTranscoder encoder = transcoders.get(command.getId());
if (encoder == null) {
throw new RuntimeException("No encoder for command type " + command.getClass() + " found");
}
final JsonObject jsonObject = encoder.encode(command);
GSON.toJson(jsonObject, builder);
builder.append(",");
}
}
if (!commands.isEmpty()) {
final int length = builder.length();
builder.delete(length - 1, length);
}
builder.append("]");
if (LOG.isTraceEnabled()) {
LOG.trace("Encoded message: {}", builder.toString());
}
return builder.toString();
}
Aggregations