private static void serializeDiagnosticResponse(BinaryMessages.VehicleMessage.Builder builder, DiagnosticResponse message) { builder.setType(BinaryMessages.VehicleMessage.Type.DIAGNOSTIC); BinaryMessages.DiagnosticResponse.Builder messageBuilder = BinaryMessages.DiagnosticResponse.newBuilder(); messageBuilder.setBus(message.getBusId()); messageBuilder.setMessageId(message.getId()); messageBuilder.setMode(message.getMode()); messageBuilder.setPid(message.getPid()); messageBuilder.setNegativeResponseCode(message.getNegativeResponseCode().code()); messageBuilder.setSuccess(message.isSuccessful()); if(message.hasValue()) { messageBuilder.setValue(message.getValue()); } if(message.hasPayload()) { messageBuilder.setPayload(ByteString.copyFrom(message.getPayload())); } builder.setDiagnosticResponse(messageBuilder); }
@Test public void getValueReturnsValue() { double value = 42.0; response = new DiagnosticResponse(bus, id, mode, pid, payload, DiagnosticResponse.NegativeResponseCode.NONE, value); assertTrue(response.hasValue()); assertThat(response.getValue(), equalTo(value)); }
@Override public String toString() { return MoreObjects.toStringHelper(this) .add("timestamp", getTimestamp()) .add("id", getId()) .add("mode", getMode()) .add("pid", getPid()) .add("payload", Arrays.toString(getPayload())) .add("value", getValue()) .add("negative_response_code", getNegativeResponseCode()) .add("extras", getExtras()) .toString(); }
busView.setText("" + message.getBusId()); idView.setText("0x" + Integer.toHexString(message.getId())); modeView.setText("0x" + Integer.toHexString(message.getMode())); if(message.hasPid()) { pidView.setText("0x" + Integer.toHexString(message.getPid())); } else { pidView.setText("None"); if(message.hasPayload()) { payloadView.setText("0x" + ByteAdapter.byteArrayToHexString( message.getPayload())); } else if(message.hasValue()) { payloadView.setText("Value: " + message.getValue()); } else { payloadView.setText("None");
@Test public void testDeserializeDiagnosticResponseFromJsonString() throws UnrecognizedMessageTypeException { String data = "{\"bus\":1,\"id\":2028,\"mode\":1,\"success\":true,\"pid\":64,\"payload\":\"0x40800020\"}"; DiagnosticResponse response = (DiagnosticResponse) JsonFormatter.deserialize(data); assertEquals(response.getBusId(), 1); assertEquals(response.getId(), 2028); assertEquals(response.getMode(), 1); assertEquals(response.isSuccessful(), true); assertEquals(response.getPid().intValue(), 64); assertEquals(ByteAdapter.byteArrayToHexString(response.getPayload()), "40800020"); }
@Test public void differentBusNotEqual() { DiagnosticResponse anotherResponse = new DiagnosticResponse( bus + 1, id, mode, pid, payload); anotherResponse.setPayload(payload); assertThat(response, not(equalTo(anotherResponse))); }
private static DiagnosticResponse deserializeDiagnosticResponse( BinaryMessages.VehicleMessage binaryMessage) throws UnrecognizedMessageTypeException { BinaryMessages.DiagnosticResponse serializedResponse = binaryMessage.getDiagnosticResponse(); if(!serializedResponse.hasBus() || !serializedResponse.hasMessageId() || !serializedResponse.hasMode()) { throw new UnrecognizedMessageTypeException( "Diagnostic response missing one or more required fields"); } DiagnosticResponse response = new DiagnosticResponse( serializedResponse.getBus(), serializedResponse.getMessageId(), serializedResponse.getMode()); if(serializedResponse.hasPid()) { response.setPid(serializedResponse.getPid()); } if(serializedResponse.hasPayload()) { response.setPayload(serializedResponse.getPayload().toByteArray()); } if(serializedResponse.hasNegativeResponseCode()) { response.setNegativeResponseCode( DiagnosticResponse.NegativeResponseCode.get( serializedResponse.getNegativeResponseCode())); } if(serializedResponse.hasValue()) { response.setValue(serializedResponse.getValue()); } return response; }
@Test public void nonEssentialFieldsNotEqual() { assertThat(response.getPid(), not(equalTo(simpleResponse.getPid()))); assertThat(response.getPayload(), not(equalTo(simpleResponse.getPayload()))); }
@Test public void nullNegativeResponseMeansSuccess() { double value = 42.0; response = new DiagnosticResponse(bus, id, mode, pid, payload, null, value); assertTrue(response.isSuccessful()); assertEquals(response.getNegativeResponseCode(), DiagnosticResponse.NegativeResponseCode.NONE); }
@Test public void parcelWithNoPayload() { response.setPayload(null); Parcel parcel = Parcel.obtain(); response.writeToParcel(parcel, 0); // Reset parcel for reading parcel.setDataPosition(0); VehicleMessage createdFromParcel = VehicleMessage.CREATOR.createFromParcel(parcel); assertThat(createdFromParcel, instanceOf(DiagnosticResponse.class)); assertEquals(response, createdFromParcel); assertThat(response.getPayload(), nullValue()); }
@Override public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeValue(getValue()); out.writeSerializable(getNegativeResponseCode()); }
@Before public void setup() { response = new DiagnosticResponse(bus, id, mode, pid, payload); simpleResponse = new DiagnosticResponse(bus, id, mode); }
@Test public void clearPayload() { response.setPayload(null); assertThat(response.getPayload(), nullValue()); }
@Test public void withNrcNotSuccessful() { response = new DiagnosticResponse(bus, id, mode, pid, payload, DiagnosticResponse.NegativeResponseCode.WRONG_BLOCK_SEQUENCE_COUNTER, 42); assertFalse(response.isSuccessful()); }
@Test public void getModeReturnsMode() { assertEquals(mode, response.getMode()); }
@Test public void getPayloadReturnsPayload() { assertArrayEquals(payload, response.getPayload()); }
@Test public void getBusReturnsBus() { assertEquals(bus, response.getBusId()); }
@Test public void getIdReturnsId() { assertEquals(id, response.getId()); }