### Question:
XBeeAPIPacket extends XBeePacket { @Override public byte[] getPacketData() { ByteArrayOutputStream data = new ByteArrayOutputStream(); data.write(frameTypeValue); byte[] apiData = getAPIData(); if (apiData == null) apiData = new byte[0]; if (apiData != null && apiData.length > 0) { try { data.write(apiData); } catch (IOException e) { logger.error(e.getMessage(), e); } } return data.toByteArray(); } protected  XBeeAPIPacket(APIFrameType frameType); protected  XBeeAPIPacket(int frameTypeValue); APIFrameType getFrameType(); int getFrameTypeValue(); @Override byte[] getPacketData(); byte[] getAPIData(); abstract boolean needsAPIFrameID(); int getFrameID(); void setFrameID(int frameID); abstract boolean isBroadcast(); boolean checkFrameID(int id); final static int NO_FRAME_ID; }### Answer:
@Test public final void testGetPacketDataGetAPIDataNull() { APIFrameType frameType = APIFrameType.GENERIC; TestXBeeAPIPacket packet = new TestXBeeAPIPacket(frameType) { @Override public byte[] getAPIData() { return null; } }; byte[] expectedData = new byte[]{(byte)frameType.getValue()}; byte[] data = packet.getPacketData(); assertThat("Returned packet data is not the expected one", data, is(equalTo(expectedData))); }
@Test public final void testGetPacketDataWithoutDataNoFrameID() { APIFrameType frameType = APIFrameType.GENERIC; TestXBeeAPIPacket packet = new TestXBeeAPIPacket(frameType); byte[] expectedData = new byte[]{(byte)frameType.getValue()}; byte[] data = packet.getPacketData(); assertThat("Returned packet data is not the expected one", data, is(equalTo(expectedData))); }
@Test public final void testGetPacketDataWithoutDataWithFrameIDNotConfigured() { APIFrameType frameType = APIFrameType.GENERIC; TestXBeeAPIPacket packet = new TestXBeeAPIPacket(frameType) { @Override public boolean needsAPIFrameID() { return true; } }; byte[] expectedData = new byte[]{(byte)frameType.getValue(), (byte)XBeeAPIPacket.NO_FRAME_ID}; byte[] data = packet.getPacketData(); assertThat("Returned packet data is not the expected one", data, is(equalTo(expectedData))); }
@Test public final void testGetPacketDataWithDataNoFrameID() { APIFrameType frameType = APIFrameType.GENERIC; final byte[] packetData = new byte[] {0x00, 0x01, 0x02}; TestXBeeAPIPacket packet = new TestXBeeAPIPacket(frameType) { @Override public byte[] getAPIPacketSpecificData() { return packetData; } }; byte[] expectedData = new byte[1 + packetData.length]; expectedData[0] = (byte)frameType.getValue(); System.arraycopy(packetData, 0, expectedData, 1, packetData.length); byte[] data = packet.getPacketData(); assertThat("Returned packet data is not the expected one", data, is(equalTo(expectedData))); }