@Before public void setup() { response = new DiagnosticResponse(bus, id, mode, pid, payload); simpleResponse = new DiagnosticResponse(bus, id, mode); }
@Test public void sameValueEqual() { double value = 42.0; response = new DiagnosticResponse(bus, id, mode, pid, payload, null, value); DiagnosticResponse anotherResponse = new DiagnosticResponse( bus, id, mode, pid, payload, null, value); assertThat(response, equalTo(anotherResponse)); }
@Test public void differentNrcNotEqual() { double value = 42.0; response = new DiagnosticResponse(bus, id, mode, pid, payload, null, value); DiagnosticResponse anotherResponse = new DiagnosticResponse(bus, id, mode, pid, payload, DiagnosticResponse.NegativeResponseCode.WRONG_BLOCK_SEQUENCE_COUNTER, value); assertThat(response, not(equalTo(anotherResponse))); }
@Test public void differentValueNotEqual() { double value = 42.0; response = new DiagnosticResponse(bus, id, mode, pid, payload, null, value); DiagnosticResponse anotherResponse = new DiagnosticResponse( bus, id, mode, pid, payload, null, value + 1); assertThat(response, not(equalTo(anotherResponse))); }
@Test public void differentIdNotEqual() { DiagnosticResponse anotherResponse = new DiagnosticResponse( bus, id + 1, mode, pid, payload); anotherResponse.setPayload(payload); assertThat(response, not(equalTo(anotherResponse))); }
@Test public void differentModeNotEqual() { DiagnosticResponse anotherResponse = new DiagnosticResponse( bus, id, mode + 1, pid, payload); anotherResponse.setPayload(payload); assertThat(response, not(equalTo(anotherResponse))); }
@Test public void differentBusNotEqual() { DiagnosticResponse anotherResponse = new DiagnosticResponse( bus + 1, id, mode, pid, payload); anotherResponse.setPayload(payload); assertThat(response, not(equalTo(anotherResponse))); }
@Test public void differentPayloadNotEqual() { payload[1] = (byte) (payload[1] + 1); DiagnosticResponse anotherResponse = new DiagnosticResponse( bus, id, mode, pid, payload); anotherResponse.setPayload(payload); assertThat(response, not(equalTo(anotherResponse))); }
@Test public void serializeFailedDiagnosticResponse() { int id = 42; int bus = 1; int mode = 2; int pid = 4; byte[] payload = new byte[] { 1, 2, 3, 4, 5, 6, 7 }; double value = 42.0; serializeDeserializeAndCheckEqual(new DiagnosticResponse(bus, id, mode, pid, payload, null, value)); }
@Test public void testSerializeAndDeserializeDiagnosticResponseNoPayload() throws UnrecognizedMessageTypeException { serializeDeserializeAndCheckEqual(new DiagnosticResponse( 1, 2028, 1, 64, null, null, 12 )); }
@Test public void serializeDiagnosticResponseWithValue() { serializeDeserializeAndCheckEqual(new DiagnosticResponse( 1, 2, 3, 4, new byte[]{1,2,3,4}, null, 42.0)); }
@Test public void serializeDiagnosticResponse() { serializeDeserializeAndCheckEqual(new DiagnosticResponse( 1, 2, 3, 4, new byte[]{1,2,3,4})); }
@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)); }
@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 testSerializeAndDeserializeDiagnosticResponse() throws UnrecognizedMessageTypeException { serializeDeserializeAndCheckEqual(new DiagnosticResponse( 1, 2028, 1, 64, ByteAdapter.hexStringToByteArray("40800020"), null, 12 )); }
@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); }
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; }