@Override public boolean equals(Object obj) { if (!super.equals(obj) || !(obj instanceof CustomCommand)) { return false; } final CustomCommand other = (CustomCommand) obj; return Objects.equal(getCommands(), other.getCommands()); }
@Override public String toString() { MoreObjects.ToStringHelper finalString = toStringHelper(COMMAND_KEY); finalString.add("timestamp", getTimestamp()); for (Map.Entry<String, String> entry : commands.entrySet()) { finalString.add(entry.getKey(), entry.getValue()); } return finalString.toString(); }
protected CustomCommand(Parcel in) { readFromParcel(in); }
@Before public void setup() { customCommand = new CustomCommand(inputCommand); }
@Test public void writeAndReadFromParcelWithDiagnostic() { customCommand = new CustomCommand(inputCommand); Parcel parcel = Parcel.obtain(); customCommand.writeToParcel(parcel, 0); // Reset parcel for reading parcel.setDataPosition(0); VehicleMessage createdFromParcel = VehicleMessage.CREATOR.createFromParcel(parcel); assertThat(createdFromParcel, instanceOf(CustomCommand.class)); assertEquals(customCommand, createdFromParcel); }
@Override public MessageKey getKey() { if (super.getKey() == null) { HashMap<String, Object> key = new HashMap<>(); key.put(COMMAND_KEY, commands.get(COMMAND_KEY)); setKey(new MessageKey(key)); } return super.getKey(); }
@Test public void toStringNotNull() { assertThat(customCommand.toString(), notNullValue()); }
@Test public void keyNotNull() { assertThat(customCommand.getKey(), notNullValue()); }
@Test public void writeAndReadFromParcel() { Parcel parcel = Parcel.obtain(); customCommand.writeToParcel(parcel, 0); // Reset parcel for reading parcel.setDataPosition(0); VehicleMessage createdFromParcel = VehicleMessage.CREATOR.createFromParcel(parcel); assertThat(createdFromParcel, instanceOf(CustomCommand.class)); assertEquals(customCommand, createdFromParcel); }
@Test public void sameCommandEquals() { CustomCommand anotherMessage = new CustomCommand(inputCommand); assertEquals(customCommand, anotherMessage); }
/*** * A custom serializer for Custom Command. The custom commands are received in * HashMap and then each key value is mapped as separate properties rather than a single * JsonArray. This allows us to add any number of custom commands and then send it in openxc * standard message format. * @param customCommand * @param typeOfSrc * @param context * @return */ @Override public JsonElement serialize (CustomCommand customCommand, Type typeOfSrc, JsonSerializationContext context) { JsonObject jObject = new JsonObject(); HashMap<String,String> commands = customCommand.getCommands(); for (Map.Entry<String, String> entry : commands.entrySet()) { jObject.addProperty(entry.getKey(),entry.getValue()); } return jObject; } }
mCustomInput.setError(getResources().getString(R.string.input_json_error)); else request = new CustomCommand(inputCommand); break; default:
@Test public void getCommandReturnsCommand() { assertEquals(inputCommand, customCommand.getCommands()); }