FixedLengthRecordMapper extends AbstractRecordMapper<P> implements RecordMapper<String, P> { protected List<Field> parseRecord(final Record<String> record) throws Exception { String payload = record.getPayload(); int recordLength = payload.length(); if (recordLength != recordExpectedLength) { throw new Exception("record length " + recordLength + " not equal to expected length of " + recordExpectedLength); } List<Field> fields = new ArrayList<>(); for (int i = 0; i < fieldsLength.length; i++) { String token = payload.substring(fieldsOffsets[i], fieldsOffsets[i + 1]); token = trimWhitespaces(token); Field field = new Field(i, token); fields.add(field); } return fields; } FixedLengthRecordMapper(Class<P> recordClass, int[] fieldsLength, String[] fieldNames); @Override Record<P> processRecord(final Record<String> record); void setTrimWhitespaces(final boolean trimWhitespaces); static final boolean DEFAULT_WHITESPACE_TRIMMING; }
@Test(expected = Exception.class) public void testIllFormedRecord() throws Exception { when(record.getPayload()).thenReturn("aaaabbcccd"); fixedLengthRecordMapper.parseRecord(record); }
@Test public void testRecordParsing() throws Exception { List<Field> fields = fixedLengthRecordMapper.parseRecord(record); assertThat(fields).hasSize(3); assertThat(fields).extracting("rawContent") .containsExactly("aaaa", "bb", "ccc"); }