### Question:
FixedFieldPackager extends  AFSDFieldPackager { @Override public byte[] pack(Map<String, String> fields) throws ISOException { if (value== null) { value = fields.get(getName()); } else { fields.put(getName(), value); } if (value==null){ throw new ISOException(String.format("Field [%s]: Unable to pack as field is not set",getName())); } if (value.length() <= size) { if (padLeft){ ISOUtil.padleft(getValue(), size,(char) padCharacter.byteValue() ); } else { ISOUtil.padright(getValue(), size,(char) padCharacter.byteValue() ); } } else { throw new ISOException(String.format("Field [%s]:Cannot pack as data has size %d and size needs to be %d",getName(), value.length(),size)); } byte[] packedbyte = new byte[interpretter.getPackedLength(size)]; interpretter.interpret(getValue(), packedbyte, 0); return packedbyte; } FixedFieldPackager(String name,int size,Interpreter interpretter);  FixedFieldPackager(String name,int size,Interpreter interpretter, APCICompliance compliance);  FixedFieldPackager(String name,String value, Interpreter interpretter); @Override int unpack(byte[] inStream, int offset, Map<String, String> fields); @Override byte[] pack(Map<String, String> fields); @Override String getValue(); @Override void setValue(String value); void setPad(boolean padLeft); Byte getPadCharacter(); void setPadCharacter(Byte padCharacter); @Override String dump(String prefix,Map<String, String> setfields); @Override byte[] hexDump(String prefix,Map<String, String> setfields); @Override String getParserTree(String prefix);  }### Answer:
@Test public void packTest01() throws ISOException { Throwable exception = assertThrows(ISOException.class, () -> { FixedFieldPackager p = new FixedFieldPackager("F1", 5, AsciiInterpreter.INSTANCE); String s = "ABC1234"; FSDMsgX msg = new FSDMsgX("Test"); ; msg.add("F1", p); msg.set("F1", s); byte[] b = msg.pack(); assertEquals(null, b); }); assertEquals("Field [F1]:Cannot pack as data has size 7 and size needs to be 5", exception.getMessage()); }
@Test public void packTest02() throws ISOException { FixedFieldPackager p = new FixedFieldPackager("F1", 5, AsciiInterpreter.INSTANCE); String s = "ABC12"; FSDMsgX msg = new FSDMsgX("Test"); ; msg.add("F1", p); msg.set("F1", s); byte[] stream = msg.pack(); assertArrayEquals("ABC12".getBytes(), stream); }
@Test public void packTest03() throws ISOException { int size = 5; FixedFieldPackager p = new FixedFieldPackager("F1", size, BCDInterpreter.RIGHT_PADDED); FSDMsgX msg = new FSDMsgX("Test"); ; msg.add("F1", p); msg.set("F1", "12345"); byte[] b = { 0x12, 0x34, 0x50 }; byte[] stream = msg.pack(); assertArrayEquals(b, stream); }