### Question:
PacketBuilder { public byte[] build() { packet = new ArrayList<>(); packet.add(OFFSET_SOF, SOF); packet.add(OFFSET_LENGTH, getLength()); packet.add(OFFSET_NULL, NULL); packet.add(OFFSET_CMD, (byte) command.getValue()); if (this.payload != null) { this.packet.addAll(OFFSET_PAYLOAD, this.payload); } byte[] crc16 = CRC16.calculate(toBytes(), 0, this.packet.size()); this.packet.add(crc16[0]); this.packet.add(crc16[1]); return this.toBytes(); } PacketBuilder(Command command);  PacketBuilder(Command command, ArrayList<Byte> payload); byte[] build(); static final int MAX_PAYLOAD; static final int MIN_LEN; static final int MAX_LEN; static final byte SOF; static final int OFFSET_SOF; static final int OFFSET_LENGTH; static final int OFFSET_NULL; static final byte NULL; static final int OFFSET_CMD; static final int OFFSET_PAYLOAD; static final int CRC_LEN; static final int HEADER_LEN; }### Answer:
@Test public void testPingCommand() { byte[] pingCommand = new byte[]{0x01, 0x06, 0x00, 0x0A, 0x5E, 0x65}; PacketBuilder builder = new PacketBuilder(Command.PING); assertThat(builder.build(), is(pingCommand)); }
@Test public void testReadDatabasePageRangeCommand() { byte[] readDatabasePageRangeCommand = new byte[]{0x01, 0x07, 0x00, 0x10, 0x04, (byte) 0x8B, (byte) 0xB8}; PacketBuilder builder = new PacketBuilder(Command.READ_DATABASE_PAGE_RANGE, Lists.newArrayList((byte) 0x04)); assertThat(builder.build(), is(readDatabasePageRangeCommand)); }
@Test public void testReadDatabasePagesCommand() { byte[] readDatabasePages = new byte[]{0x01, 0x0C, 0x00, 0x11, 0x05, 0x26, 0x00, 0x00, 0x00, 0x01, 0x5E, (byte) 0xC3}; ArrayList<Byte> payload = Lists.newArrayList((byte) 0x05, (byte) 0x26, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01); PacketBuilder builder = new PacketBuilder(Command.READ_DATABASE_PAGES, payload); assertThat(builder.build(), is(readDatabasePages)); }
@Test public void testReadDisplayTimeOffsetCommand() { byte[] readDatabasePages = new byte[]{0x01, 0x06, 0x00, 0x1D, (byte) 0x88, 0x07}; PacketBuilder builder = new PacketBuilder(Command.READ_DISPLAY_TIME_OFFSET); assertThat(builder.build(), is(readDatabasePages)); }
@Test public void testReadBatteryLevelCommand() { byte[] readBatteryLevel = new byte[]{0x01, 0x06, 0x00, 0x21, 0x57, (byte) 0xF0}; PacketBuilder builder = new PacketBuilder(Command.READ_BATTERY_LEVEL); assertThat(builder.build(), is(readBatteryLevel)); }
@Test public void testReadSystemTimeCommand() { byte[] readSystemTime = new byte[]{0x01, 0x06, 0x00, 0x22, 0x34, (byte) 0xC0}; PacketBuilder builder = new PacketBuilder(Command.READ_SYSTEM_TIME); assertThat(builder.build(), is(readSystemTime)); }