private void assertValid(final Decoder decoder) { final boolean isValid = decoder.validate(); assertTrue(String.format( "Decoder fails validation due to: %s for tag: %d", decoder.rejectReason(), decoder.invalidTagId()), isValid); }
<T extends Decoder> T readMessage(final T decoder) { final ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE); final MutableAsciiBuffer asciiBuffer = new MutableAsciiBuffer(buffer); try { final int read = socket.read(buffer); DebugLogger.log(FIX_TEST, "< [" + asciiBuffer.getAscii(OFFSET, read) + "] for attempted: " + decoder.getClass()); decoder.decode(asciiBuffer, OFFSET, read); if (!decoder.validate()) { fail("Failed: " + RejectReason.decode(decoder.rejectReason()) + " for " + decoder.invalidTagId()); } } catch (final IOException ex) { LangUtil.rethrowUnchecked(ex); } return decoder; }
@Test public void shouldResetTheInvalidAccessors() throws Exception { final Decoder decoder = decodeHeartbeat(TAG_APPEARS_MORE_THAN_ONCE_MESSAGE); decoder.validate(); decoder.reset(); assertEquals("Failed to reset tag id", NO_ERROR, decoder.invalidTagId()); assertEquals("Failed to reset reject reason", NO_ERROR, decoder.rejectReason()); }
@Test public void shouldLeaveDecoderInUsableIfValidationFailsDuringRepeatingGroup() throws Exception { //Given final Decoder decoder = decodeHeartbeat(FIELD_DEFINED_TWICE_IN_MESSAGE); //When assertFalse("Failed validation when it should have passed", decoder.validate()); assertEquals("Wrong reject reason", TAG_APPEARS_MORE_THAN_ONCE, decoder.rejectReason()); //Then decoder.reset(); decode(NO_MISSING_REQUIRED_FIELDS_IN_REPEATING_GROUP_MESSAGE, decoder); assertTrue("Failed validation when it should have passed", decoder.validate()); }
decoder.invalidTagId(), header.msgType(), msgTypeLength, decoder.rejectReason());
@Test public void shouldValidateIfNoRequiredFieldsMissingInRepeatingGroup() throws Exception { final Decoder decoder = decodeHeartbeat(NO_MISSING_REQUIRED_FIELDS_IN_REPEATING_GROUP_MESSAGE); assertTrue("Failed validation when it should have passed", decoder.validate()); }
@Test public void shouldResetDifferentFieldTypes() throws Exception { final Decoder decoder = (Decoder)fieldsMessage.getConstructor().newInstance(); decode(EG_FIELDS_MESSAGE, decoder); decoder.reset(); assertRequiredFieldsMessageFieldsAsStringDecoded(decoder, "", "", ""); assertOptionalDifferentFieldsNotDecoded(decoder); decode(EG_NO_OPTIONAL_FIELDS_MESSAGE, decoder); assertRequiredFieldsMessageFieldsDecoded(decoder, "USD", "N", "US"); assertOptionalDifferentFieldsNotDecoded(decoder); assertValid(decoder); }
private void decode(final String example, final Decoder decoder) { buffer.putAscii(1, example); decoder.decode(buffer, 1, example.length()); }
@Test public void shouldValidateIfNoRepeatingGroup() throws Exception { final Decoder decoder = decodeHeartbeat(NO_REPEATING_GROUP_IN_REPEATING_GROUP_MESSAGE); assertTrue("Failed validation when it should have passed", decoder.validate()); }
@Test public void shouldDecodeRepeatingGroupsAfterReset() throws Exception { final Decoder decoder = decodeHeartbeat(REPEATING_GROUP_MESSAGE); assertValidRepeatingGroupDecoded(decoder); decoder.reset(); decode(REPEATING_GROUP_MESSAGE, decoder); assertValidRepeatingGroupDecoded(decoder); }
private void assertInvalid(final Decoder decoder) { final boolean isValid = decoder.validate(); assertFalse(String.format( "Decoder fails validation due to: %s for tag: %d", decoder.rejectReason(), decoder.invalidTagId()), isValid); }
@Test public void shouldLeaveDecoderInUsableIfUnknownFieldForRepeatingGroupReachedAndRejectingOn() throws Exception { //Given final Decoder decoder = decodeHeartbeatWithRejectingUnknownFields(REPEATING_GROUP_WITH_UNKNOWN_FIELD); //When assertFalse("Passed validation with missing fields", decoder.validate()); assertEquals("Wrong tag id", 1000, decoder.invalidTagId()); assertEquals("Wrong reject reason", INVALID_TAG_NUMBER, decoder.rejectReason()); //Then decoder.reset(); decode(NO_MISSING_REQUIRED_FIELDS_IN_REPEATING_GROUP_MESSAGE, decoder); assertTrue("Failed validation when it should have passed", decoder.validate()); }
@Test public void shouldSkipUnknownFieldForMessageAndPassValidation() throws Exception { final Decoder decoder = decodeHeartbeat(INVALID_TAG_NUMBER_MESSAGE); assertTrue("Failed validation with invalid tag number", decoder.validate()); assertEquals(2, getIntField(decoder)); }
@Test public void shouldDecodeShorterRepeatingGroupsAfterReset() throws Exception { final Decoder decoder = decodeHeartbeat(REPEATING_GROUP_MESSAGE); assertValidRepeatingGroupDecoded(decoder); decoder.reset(); decode(SINGLE_REPEATING_GROUP_MESSAGE, decoder); assertSingleRepeatingGroupDecoded(decoder); }
@Test public void shouldValidateTagsAppearingMoreThanOnce() throws Exception { final Decoder decoder = decodeHeartbeat(TAG_APPEARS_MORE_THAN_ONCE_MESSAGE); assertFalse("Passed validation with incorrect value", decoder.validate()); assertEquals("Wrong tag id", 116, decoder.invalidTagId()); assertEquals("Wrong reject reason", TAG_APPEARS_MORE_THAN_ONCE, decoder.rejectReason()); }
@Test public void shouldSkipUnknownFieldInRepeatingGroupAndPassValidation() throws Exception { final Decoder decoder = decodeHeartbeat(REPEATING_GROUP_WITH_UNKNOWN_FIELD); assertTrue("Failed validation with missing fields", decoder.validate()); Object group = get(decoder, "secondEgGroupGroup"); assertEquals("TOM", get(group, "secondGroupFieldAsString")); assertEquals(180, get(group, "thirdGroupField")); group = next(group); assertEquals("Barbara", get(group, "secondGroupFieldAsString")); assertEquals(123, get(group, "thirdGroupField")); }
@Test public void shouldDecodeRepeatingGroupsAfterResetWithoutValidation() throws Exception { final Decoder decoder = decodeHeartbeatWithoutValidation(REPEATING_GROUP_MESSAGE); assertValidRepeatingGroupDecoded(decoder); decoder.reset(); decode(REPEATING_GROUP_MESSAGE, decoder); assertValidRepeatingGroupDecoded(decoder); }
@Test public void shouldValidateFirstThreeFieldsAreInOrder() throws Exception { final Decoder decoder = decodeHeartbeat(TAG_SPECIFIED_OUT_OF_REQUIRED_ORDER_MESSAGE); assertFalse("Passed validation with incorrect value", decoder.validate()); assertEquals("Wrong tag id", 9, decoder.invalidTagId()); assertEquals("Wrong reject reason", TAG_SPECIFIED_OUT_OF_REQUIRED_ORDER, decoder.rejectReason()); }
@Test public void shouldSkipFieldUnknownToMessageButDefinedInFIXSpec() throws Exception { final Decoder decoder = decodeHeartbeat(REPEATING_GROUP_WITH_FIELD_UNKNOWN_TO_MESSAGE_BUT_IN_SPEC); assertTrue("Failed validation with missing fields", decoder.validate()); Object group = get(decoder, "secondEgGroupGroup"); assertEquals("TOM", get(group, "secondGroupFieldAsString")); assertEquals(180, get(group, "thirdGroupField")); group = next(group); assertEquals("Barbara", get(group, "secondGroupFieldAsString")); assertEquals(123, get(group, "thirdGroupField")); }
@Test public void shouldNotRetainStringEnumFromPreviousMessagesForRequiredFieldsWhenReset() throws Throwable { final Decoder decoder = createRequiredFieldMessageDecoder(); decode(RF_ALL_FIELDS, decoder); assertEquals("one", getMethod(decoder, STRING_ENUM_RF + "AsString")); assertEquals("ONE", getMethod(decoder, STRING_ENUM_RF + "AsEnum").toString()); decoder.reset(); decode(RF_NO_FIELDS, decoder); assertEquals("", getMethod(decoder, STRING_ENUM_RF + "AsString")); assertEquals(UNKNOWN_NAME, getMethod(decoder, STRING_ENUM_RF + "AsEnum").toString()); }